/*! * jQuery List v0.2.0 * https://github.com/nathggns/jq-list * Copyright (c) 2013 Nathaniel Higgins; Licensed MIT * Built on 2013-04-06 */ (function(t,e,n,i,a){"use strict";var r={},o={},s={rows:[],element:null,template:function(){},template_html:"",children:[],events:{},count:1,templates:{},original:{html:"",attrs:{},data:{}}},f={},h=function(t){var e=[a];return function(){for(var n;e.indexOf(n)>-1;n=Math.floor(Math.random()*Math.pow(10,t||10)));return n}}(),l=function(t){var e={add:function(t,e){this.add(e-1),this.render()},"delete":function(t,e){this["delete"](e-1),this.render()}},l={init:function(e,i){if(i||(i={}),!t.data("ran")){var a={html:t.html(),attrs:n.extend(!0,{},this.attrs(t.get(0))),data:n.extend(!0,{},t.data())};i.events&&n.each(i.events,function(t,e){"function"==typeof e&&(i.events[t]=[e])});var r=n.extend(!0,{},s,{element:t,original:a},i||{});if(this.saveInformation(r),r.children){var o=[];n.each(r.children,function(t,e){"string"==typeof e&&(e={selector:e}),e.name||(e.name=t),e.options||(e.options={}),e.element=n(e.selector),o.push(e)}),r.children=o}for(n.each(r.templates,function(t,e){f[t]=d.compileTemplate(e)}),this.setTemplate(n(e)),{var h=0;r.count};r.count>h;h++)this.add(h)}this.render(),t.data("ran",!0),this.events("init")},compileTemplate:function(t){return t=this.giveAttrs(t),function(e){t=d.runOnString(t,function(t){var n=t.find("[data-content]");if(n.length>0){var i=e.html();n.after(i),n.remove()}}),e.after(t),e.remove()}},runOnString:function(t,e){"string"!=typeof t&&(t=t.html());var i=n("
").html(t);return e(i),i.html()},giveAttrs:function(t){var e=!1,i=this.getKey();"string"==typeof t&&(t=n("
").html(t),e=!0);var a=t.find("[value], input, button, textarea, select").each(function(t){n(this).attr("data-identifier","{{ item.identifier }}-"+t)});return a=a.add(t.find("[data-role]")).each(function(){n(this).attr("data-parent",i)}),a.add(t.find("[data-sublist]")).each(function(){n(this).attr("data-index","{{ loop.index }}"),n(this).attr("data-parent-identifier","{{ item.identifier }}")}),e&&(t=t.html()),t},add:function(t){this.events("before_add");var e=this.getInformation(),i=n.extend(!0,{},o);i.identifier=this.getKey()+"-"+h(),e.rows.splice(t+1,0,i),this.saveInformation(e),this.events("add",t)},"delete":function(t){this.events("before_delete",t);var e=this.getInformation();e.rows.splice(t,1),this.saveInformation(e),this.events("delete",t)},render:function(i){this.events("before_render");var a=this.getInformation(),r=a.children,o={},s=a.rows,f=n.map(s,function(t){return t.identifier});n.each(r,function(e,i){var a=t.find('[data-sublist="'+i.name+'"]');a.each(function(){var t=n(this);t.data("ran")||t.list(i.element,i.options);var e=f.indexOf(t.data("parent-identifier"));-1!==e&&(s[e].child={rows:t.list("getInformation").rows,string:'[data-sublist="'+i.name+'"][data-parent-identifier="'+t.data("parent-identifier")+'"]',info:i})})}),t.find("[data-identifier]").each(function(){o[n(this).data("identifier")]=this}),t.html(a.template(n.extend(!0,{},{list:s,length:s.length,showdelete:s.length>1},i||{},a.render_options||{}))),this.events("render_before_fix"),t.find("[data-role]").on("click",function(t){var i=n(this),a=i.data("role");i.data("has-assigned")||(i.data("has-assigned",!0),e[a]&&(t.preventDefault(),e[a].call(d,t,i.data("index"))))}),n.each(r,function(e,i){var a=t.find('[data-sublist="'+i.name+'"]').filter(function(){return!n(this).data("ran")});a.each(function(){var t=n(this);t.list(i.element,i.options);var e=t.list("getInformation");e.render_options={owner:{index:t.data("index")}},t.list("saveInformation",e).list("render")})}),n.each(s,function(e,n){if(n.child){var i=t.find(n.child.string);if(i.data("ran")){var a=i.list("getInformation");a.rows=n.child.rows,i.list("saveInformation",a).list("render")}}}),n.each(o,function(e,i){var a=t.find('[data-identifier="'+e+'"]'),r=n(i);a.length>0&&(n.each(d.attrs(a[0]),function(t,e){"type"!==t&&r.attr(t,e)}),r.insertAfter(a),a.remove())}),a.rows=s,this.saveInformation(a),this.events("render")},getInformation:function(){return r[this.getKey()]},getKey:function(){return t.data("info-key")||t.data("info-key",h()),t.data("info-key")},saveInformation:function(t){this.events("save_info",t),r[this.getKey()]=t},attrs:function(e){e=e||t;var i={};return n.each(e.attributes,function(){i[this.nodeName]=this.nodeValue}),i},events:function(){if(arguments.length>0){var e=arguments[0],i=this.getInformation(),a=Array.prototype.slice.call(arguments,1);i&&i.events[e]&&n.each(i.events[e],function(){this.apply(t,a)}),i&&(n.each(i.events[e]||[],function(){this.apply(t,a)}),t.trigger("list-"+e,a))}},on:function(t,e){var n=this.getInformation();n.events[t]||(n.events[t]=[]),n.events[t].push(e),this.saveInformation(n)},off:function(t,e){var n=this.getInformation();if(n.events[t]){var i=n.events[t].indexOf(e);i>-1&&n.events[t].splice(i,1)}this.saveInformation(n)},methodRunner:function(){return d},reset:function(){var e=this.getKey(),i=this.getInformation();delete r[e],n.each(t.data(),function(e){t.removeData(e)}),n.each(i.original.data,function(e,n){t.data(e,n)}),n.each(this.attrs(t[0]),function(e){"type"!==e&&t.removeAttr(e)}),n.each(i.original.attrs,function(e,n){"type"!==e&&t.attr(e,n)}),t.html(i.original.html)},getTemplate:function(t){var e=this.getInformation();return t===a?e.template_html:e.template},setTemplate:function(t){"string"!=typeof t&&(t=t.html()),t=this.giveAttrs(t);for(var e,a=n("
").html(t),r=function(t){t.each(function(){var t=n(this),e=t.data("template"),i=f[e];i(t)})};(e=a.find("[data-template]")).length>0;)r(e);t=a.html();var o=this.getInformation();o.template_html=t,o.template=i.compile(t),this.saveInformation(o)}},d=function(){var t=Array.prototype.shift.call(arguments);return l[t].apply(d,arguments)};return n.each(l,function(t){d[t]=function(){Array.prototype.unshift.call(arguments,t);var e=arguments;return d.apply(d,e)}}),d},d=function(){1>arguments.length&&Array.prototype.push.call(arguments,"init");var t=Array.prototype.splice.call(arguments,0,1)[0];return[t,arguments]};n.list=l(!1),n.fn.list=function(){var t=d.apply(this,arguments),e=t[0],n=t[1];if(!this.data("ran")){var i;"init"!==e&&(Array.prototype.unshift.call(n,e),e="init"),1>n.length?i='[type="text/swig"]':(i=n[0],Array.prototype.splice.call(n,0,1)),Array.prototype.unshift.call(n,i)}var a=l(this)[e].apply(this,n);return a?a:this}})(window,document,jQuery,swig);