forked from thin-js/thin.js
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy paththin.min.js
More file actions
1 lines (1 loc) · 8.86 KB
/
Copy paththin.min.js
File metadata and controls
1 lines (1 loc) · 8.86 KB
1
function poplayer(t){var e=document.createElement("popmask");e.style.zIndex=1e3;var a,n=document.createElement("popdialog");switch(typeof t.width){case"string":n.style.width=t.width;break;case"number":n.style.width=t.width+"px"}if(void 0!==t.width&&(n.style.width=t.width),e.appendChild(n),void 0!==t.header){a=document.createElement("popheader"),$(a).html(t.header);var i=document.createElement("closeicon");i.innerText="✕",i.onclick=function(){void 0!==t.onclose&&t.onclose(),$(e).remove()},i.onmouseover=function(){this.style.filter="alpha(opacity=80)",this.style.opacity=".8"},i.onmouseout=function(){this.style.filter="alpha(opacity=50)",this.style.opacity=".5"},a.appendChild(i),n.appendChild(a)}var l=document.createElement("popbody");switch(typeof t.height){case"string":l.style.height=t.height,l.style.overflowY="scroll";break;case"number":l.style.height=t.height+"px",l.style.overflowY="scroll";break;default:l.style.maxHeight="600px",l.style.overflowY="scroll"}n.appendChild(l),document.body.appendChild(e),void 0!==t.render?t.render({container:l}):$(l).render({data:t.data,template:t.template}),popDrag(a,n)}$(function(){$(document).on("click","tab-nav",function(){$("tab-nav",this.parentElement).removeClass("active"),$(this).addClass("active");var t=this.getAttribute("function");null===t||"function"==typeof window[t]&&window[t]();var e=this.getAttribute("view");if(null!==e){var a=$(this).parents("multiview:first");$("view",a).removeClass("active"),$("view#"+e,a).addClass("active")}})}),$.fn.extend({render:function(t){function e(t){if("array"==typeof t.data)for(var e=0;e<t.data.length;e++)a({container:t.container,template:t.template,data:t.data[e]});else a({container:t.container,template:t.template,data:t.data})}function a(t){if("array"==typeof t.template)for(var e=0;e<t.template.length;e++)n({template:t.template[e],container:t.container,data:t.data});else n({template:t.template,container:t.container,data:t.data})}function n(t){if("string"==typeof t.template){var e=l({template:t.template,container:t.container}),a=document.createDocumentFragment();$(a).append(e),t.container.appendChild(a)}else if("object"==typeof t.template)i({container:t.container,template:t.template,data:t.data});else if("function"==typeof t.template){var n=o(t.container);t.template({container:t.container,data:n?n.data_of_thin:void 0})}else console.log(Object.prototype.toString.call(t.template))}function i(t){if(void 0!==t.template.datapath&&void 0===t.data){var n=r({container:t.container,path:t.template.datapath});null!==n&&e({container:t.container,data:n,template:t.template})}else{if(void 0!==t.template.if)switch(typeof t.template.if){case"function":t.template.if({container:t.container,data:t.template.data?t.template.data:t.data})?a({container:t.container,template:t.template.then}):t.template.else&&a({container:t.container,template:t.template.else});break;default:t.template.if?a({container:t.container,template:t.template.then}):t.template.else&&a({container:t.container,template:t.template.else})}else if(void 0!==t.template.switch){let e="function"==typeof t.template.switch?t.template.switch({container:t.container,data:t.template.data?t.template.data:t.data}):l({template:t.template.switch,container:t.container});void 0===t.template.case||(void 0!==t.template.case[e]?a({container:t.container,template:t.template.case[e]}):void 0!==t.template.case.default&&a({container:t.container,template:t.template.case.default}))}else i();function i(){var e=document.createElement(t.template.e?t.template.e:"div");if($(t.container).append(e),t.template.data?e.data_of_thin=t.template.data:void 0!==t.data&&(e.data_of_thin=t.data),void 0!==t.template.name&&e.setAttribute("name",t.template.name),void 0!==t.template.id&&e.setAttribute("id",t.template.id),void 0!==t.template.class&&e.setAttribute("class",t.template.class),t.template.width&&e.style.setProperty("width","number"==typeof t.template.width?t.template.width+"px":t.template.width),t.template.height&&e.style.setProperty("height","number"==typeof t.template.height?t.template.height+"px":t.template.height),void 0!==t.template.options)if("[object Array]"===Object.prototype.toString.call(t.template.options))for(var n=0;n<t.template.options.length;n++)e.options.add(new Option(t.template.options[n]));else if("[object Object]"===Object.prototype.toString.call(t.template.options));else if("[object String]"===Object.prototype.toString.call(t.template.options))for(var i=t.template.options.split(","),r=0;r<i.length;r++)e.options.add(new Option(i[r]));if(void 0!==t.template.selected){var p=l({template:t.template.selected,container:e});$(e).val(p)}if(void 0!==t.template.value){let a=l({template:t.template.value,container:e});$(e).val(a)}if(t.template.bind){var c=o(e);if(c){var s=c.data_of_thin;let a=t.template.bind.split("/");for(var d=0;d<a.length-1;d++)s=s[a[d]];$(e).val(s[a[a.length-1]]),$(e).on("change",t=>{console.log(t);for(var n=c.data_of_thin,i=0;i<a.length-1;i++)n[a[i]]||(n[a[i]]={}),n=n[a[i]];n[a[a.length-1]]=$(e).val(),console.log(c.data_of_thin)})}}switch(void 0!==t.template.click&&(e.onclick=function(){var e=o(this),a={};null!==e&&($("input",e).each(function(t,e){if(void 0!==this.attributes.name){var n=this.attributes.name.value;a[n]=$(this).val()}}),$("select",e).each(function(t,e){if(void 0!==this.attributes.name){var n=this.attributes.name.value;a[n]=$(this).val()}}),$("textarea",e).each(function(t,e){if(void 0!==this.attributes.name){var n=this.attributes.name.value;a[n]=$(this).val()}})),t.template.click({sender:this,org_data:e.data_of_thin,new_data:a})}),void 0!==t.template.event&&Object.keys(t.template.event).forEach(function(a){$(e).on(a,function(e){var a=o(this),n=new Object;$("input",a).each(function(t,e){if(void 0!==this.attributes.name){var a=this.attributes.name.value;n[a]=$(this).val()}}),$("select",a).each(function(t,e){if(void 0!==this.attributes.name){var a=this.attributes.name.value;n[a]=$(this).val()}}),$("textarea",a).each(function(t,e){if(void 0!==this.attributes.name){var a=this.attributes.name.value;n[a]=$(this).val()}});var i=e.type;t.template.event[i]({sender:this,type:i,event:e,org_data:a.data_of_thin,new_data:n})})}),t.template.e){case"fieldset":if(t.template.title){let a=document.createElement("legend");a.innerText=t.template.title,e.appendChild(a)}break;case"field":case"f1":case"f2":case"f3":if(void 0!==t.template.title){let a=document.createElement("label");a.innerText=t.template.title,e.appendChild(a)}}void 0!==t.template.t&&a({container:e,template:t.template.t}),void 0!==t.template.a&&Object.keys(t.template.a).forEach(function(a){if("function"==typeof t.template.a[a]){var n=o(e);e.setAttribute(a,t.template.a[a]({container:e,data:n.data_of_thin}))}else e.setAttribute(a,l({template:t.template.a[a],container:e}))}),void 0!==t.template.style&&Object.keys(t.template.style).forEach(function(a){if("function"==typeof t.template.style[a]){var n=o(e);e.style.setProperty(a,t.template.style[a]({container:e,data:n.data_of_thin}))}else e.style.setProperty(a,l({template:t.template.style[a],container:e}))})}}}function l(t){var e=t.template,a=/\[\[[a-zA-Z0-9\-\.\/_]*\]\]/gi,n="string"!=typeof e?e:e.replace(a,function(e){for(var a=e.replace("[[","").replace("]]",""),n=a.split("/"),i=o(t.container),l=0;l<n.length;l++)if(".."===n[l]){if(!p(i))return e;i=o(i.parentNode)}else{if(p(i)){if(void 0===i.data_of_thin)return e;i=i.data_of_thin}if(null===i)return null;i=i[n[l]]}return null==i?"":i});return n}function o(t){for(;!t.hasOwnProperty("data_of_thin");){if(null===t.parentNode)return null;t=t.parentNode}return t}function r(t){for(var e=t.path.split("/"),a=o(t.container),n=0;n<e.length;n++)if(".."===e[n]){if(!p(a))return null;a=o(a.parentNode)}else{if(p(a)){if(void 0===a.data_of_thin)return null;a=a.data_of_thin}if(null===a)return null;if(!(e[n]in a))return null;a=a[e[n]]}return a}function p(t){return!(!t||"undefined"==typeof window||t!==window&&!t.nodeType)}void 0===t.data&&(t.data={}),"array"==typeof t.template?e({container:this[0],template:t.template,data:t.data}):"array"==typeof t.data?e({container:this[0],template:t.template,data:t.data}):"object"==typeof t.template?e({container:this[0],template:t.template,data:t.data}):(this[0].data_of_thin=t.data,e({container:this[0],template:t.template}))}}),poplayer.close=function(t){console.log({pos:"poplayer.close",element:t})};var popDrag=function(t,e,a){var n={left:0,top:0,currentX:0,currentY:0,flag:!1,resetPosition:function(){var t=$(e).css("left"),a=$(e).css("top");"auto"!==t&&(this.left=t),"auto"!==a&&(this.top=a)}};n.resetPosition(),t.onmousedown=function(e){n.flag=!0,e||(e=window.event,t.onselectstart=function(){return!1});var a=e;n.currentX=a.clientX,n.currentY=a.clientY},document.onmouseup=function(){n.flag=!1,n.resetPosition()},document.onmousemove=function(t){var i=t||window.event;if(n.flag){var l=i.clientX,o=i.clientY,r=l-n.currentX,p=o-n.currentY;return e.style.left=parseInt(n.left)+r+"px",e.style.top=parseInt(n.top)+p+"px","function"==typeof a&&a((parseInt(n.left)||0)+r,(parseInt(n.top)||0)+p),t.preventDefault&&t.preventDefault(),!1}}};