"use strict"; $(function () { $.ajaxSetup({ cache: false, xhrFields: { withCredentials: true } }); if (!ModernJs.isDefined(ModernJs.ListCode)) { ModernJs.ListCode = {}; ModernJs.Templater = ModernJs.ListCode; ModernJs.ConditionCode = ModernJs.ListCode; ModernJs.ListCode.loadMain = function () {}; ModernJs.ListCode.codeWait = ""; ModernJs.ListCode.codeDone = function () {}; ModernJs.ListCode.codeParser = function (code) { return code; }; ModernJs.ListCode.codeClear = true; ModernJs.ListCode.config = function (node) { var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (arguments.length < 2) { config = node; if (ModernJs.isDefined(config.wait)) { ModernJs.ListCode.codeWait = config.wait; } if (ModernJs.isDefined(config.clear)) { ModernJs.ListCode.codeClear = config.clear; } if (ModernJs.isDefined(config.done)) { ModernJs.ListCode.codeDone = config.done; } if (ModernJs.isDefined(config.parser)) { ModernJs.ListCode.codeParser = config.parser; } } else { if (ModernJs.isDefined(config.wait)) { node.data("ListCodeWait", config.wait); } if (ModernJs.isDefined(config.clear)) { node.data("ListCodeClear", config.clear); } if (ModernJs.isDefined(config.done)) { node.data("ListCodeDone", config.done); } if (ModernJs.isDefined(config.parser)) { node.data("ListCodeParser", config.parser); } } }; ModernJs.ListCode.ajax = function (node, options) { var wait = ModernJs.chooseDefined(options.wait, node.data("ListCodeWait"), ModernJs.ListCode.codeWait); var clear = ModernJs.chooseDefined(options.clear, node.data("ListCodeClear"), ModernJs.ListCode.codeClear); var done = options.done || node.data("ListCodeDone") || ModernJs.ListCode.codeDone; var parser = options.parser || node.data("ListCodeParser") || ModernJs.ListCode.codeParser; if (clear) node.html(""); if (ModernJs.isDefined(wait)) { node.append(wait); } $.ajax({ method: options.method || "GET", url: options.url, data: options.data || {}, dataType: "text" }).done(function (code) { var parsed = parser.call(node[0], code); if (parsed != null) { options = ModernJs.copy(options); options.code = parsed; ModernJs.ListCode.list(node, options); done.call(node[0], parsed, code); } }); }; ModernJs.ListCode.list = function (node, options) { if (options.condition === false) { return ""; } var code = options.code || node.data("ListCode") || node.html(); node.data("ListCode", code); var length = options.length || 1; node.html(""); for (var i = 0; i < length; i++) { var itemCode = code; for (var key in options.data) { itemCode = itemCode.replace(new RegExp("@".concat(key, "@")), options.data[key]); } var item = ""; try { item = $(itemCode); if (item.length != 1) { throw "... creating parent for element's ..."; } } catch (e) { if (e.stack) console.log(e.stack); var parent = $("
"); parent.html(itemCode); item = parent; } node.append(item); } }; ModernJs.ListCode.load = ModernJs.ListCode.list; ModernJs.ListCode.check = ModernJs.ListCode.list; $(ModernJs.ListCode.loadMain); } });