sdc_client 0.56.4 → 0.56.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- var e={d:(t,n)=>{for(var r in n)e.o(n,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:n[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};let n;e.d(t,{n$:()=>re,du:()=>F,yA:()=>ue,g3:()=>d,u2:()=>m,n9:()=>k,on:()=>E,yy:()=>N,R6:()=>p,$n:()=>q,hA:()=>de,VB:()=>fe,hZ:()=>R});const r=/([^\s,]+)/g,s=/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;function o(){return n||(n=$("body")),n}function i(){return new Promise((function(e){e()}))}function l(e){return(e=(e=e.replace(/[A-Z]/g,(e=>`-${e.toLowerCase()}`))).replace(/[0-9]+/g,(e=>`-${e}`))).replace(/^[-]/g,"")}function a(e){return e.replace(/-./g,(e=>`${e[1].toUpperCase()}`))}const c=(e,t)=>{let n=t,r=Object.getOwnPropertyNames(e.prototype).concat(Object.getOwnPropertySymbols(e.prototype));for(;""!==n.name;)Object.getOwnPropertyNames(n.prototype).concat(Object.getOwnPropertySymbols(n.prototype)).forEach((t=>{t.match(/^(?:constructor|prototype|arguments|caller|name|bind|call|apply|toString|length)$/)||r.includes(t)||(r.push(t),Object.defineProperty(e.prototype,t,Object.getOwnPropertyDescriptor(n.prototype,t)))})),n=Object.getPrototypeOf(n)};function h(e,t,n){return $.ajax({url:t,type:n||"POST",xhr:function(){var e=$.ajaxSettings.xhr();return e.upload&&e.upload.addEventListener("progress",u,!1),e},data:e,cache:!1,contentType:!1,processData:!1,beforeSend:function(e,t){(function(e){return/^(GET|HEAD|OPTIONS|TRACE)$/.test(e)})(t.type)||this.crossDomain||e.setRequestHeader("X-CSRFToken",window.CSRF_TOKEN)}})}function u(e){if(e.lengthComputable){var t=Math.round(e.loaded/e.total*100),n=$(".progress-container");100===t?n.hide():n.show(),t+="%",n.find(".progress-bar").css({width:t}).text(t)}}function d(e,t=null){return"string"!=typeof e?e:t&&void 0!==t[e]?"function"==typeof t[e]?t[e].bind(t):t[e]:e.match(/^(true|false)$/)?"true"===e:"undefined"!==e?"none"===e.toLowerCase()?null:e.match(/^-?\d+$/)?parseInt(e):e.match(/^-?\d+\.?\d+$/)?parseFloat(e):e.match(/^(['][^']*['])|(["][^"]*["])$/)?e.substr(1,e.length-2):e:void 0}function f(){return([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,(e=>(e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>e/4).toString(16)))}function m(e){e.find(".has-error").removeClass("has-error").find(".alert-danger").remove(),e.find(".non-field-errors").remove()}function p(e,t){t=$("<div>").append(t),e.find(".has-error").removeClass("has-error").find(".alert-danger").safeRemove(),e.find(".non-field-errors").safeRemove();let n=t.find("input[type=file]").parent();e.find("input[type=file]").parent().each((function(e){$(this).replaceWith(n[e])}));let r=0===t.find(".non-field-errors").insertAfter(e.find(".hidden-form-fields")).length;return t.find(".has-error").each((function(){r=!1;let t=$(this),n=t.data("auto-id"),s=e.find(".form-group."+n);s.addClass("has-error"),s.find(".form-input-container").append(t.find(".alert-danger"))})),r}function g(e,t,n=null){let r=function(e,t){let n;e||(e=[]);let r=t.data(),s={};for(let t in r)r.hasOwnProperty(t)&&t!==j&&!e.includes(t)&&(s[t]=r[t]);n=[];for(let t=0;t<e.length;t++){let s=e[t];r.hasOwnProperty(s)?n.push(r[s]):n.push("undefined")}return n.push(s),n}(e,t);return function(e,t=null){let n=[];for(let r=0;r<e.length;r++){let s=d(e[r],t);n.push(s)}return n}(r,n)}function y(e,t,n){if(!e)return!1;if("function"!=typeof e.onInit)return!1;let o;var i;"function"==typeof e._on_init_params?o=e._on_init_params():o=(i=e.onInit.toString().replace(s,"")).slice(i.indexOf("(")+1,i.indexOf(")")).match(r)||[];let l=g(o,t,n._parentController);if(e.onInit.apply(n,l),n===e)for(let r in e._mixins)y(e._mixins[r],t,n)}let v={},w={};function b(){return Object.keys(w)}function C(e,t){if(e){let n=a(t._tagName);e._childController[n]||(e._childController[n]=[]),e._childController[n].push(t)}return t._parentController=e}function O(e,t,n,r){let s=[];r=function(e,t){e=(e=e.concat(w[t][1])).filter(((e,t,n)=>n.indexOf(e)===t));let n=!0;for(;n;){n=!1;for(let t of e)for(let r of w[t][1])e.includes(r)||(e.push(r),n=!0)}return e}(r,n);for(let e of r)s.push(w[e][0]);let o=new(function(e,...t){let n=class{constructor(...n){let r={};t.forEach((e=>{let t;Object.assign(this,t=new e),t._tagName=e.prototype._tagName,t._isMixin=!0,r[e.name]=t})),Object.assign(this,new e),this._mixins=r}get mixins(){return this._mixins}};return c(n,e),t.forEach((e=>{c(n,e)})),n}(w[n][0],...s));return o._tagName=n,C(e,o),o.$container=t,function(e,t){y(t,e,t)}(t,o),o}function k(e,t,n,r){let s=a(n);if(v[s]){let n=v[s];return C(e,n),n.$container=t,n}return O(e,t,n,r)}function N(e){const t=function(e){return function(e){let t={args:{}};return e.contentUrl&&(t=A(e,e.contentUrl),e.contentUrl=t.url),Promise.all([I(e.contentUrl,t.args,e._tagName,e.contentReload)]).then((function(e){let t=e[0];if(t)try{return $(t)}catch{return $("<div></div>").append(t)}return null}))}(e).then((t=>!e.onLoad||e._onLoadDone?t:(e._onLoadDone=!0,(e.onLoad(t)||i()).then((()=>t)))))}(e).then((function(t){return function(e,t){return J(e,t).then((function(t){if(t=t||!0,e.willShow){let n=e.willShow();if(n instanceof Promise)return n.then((function(){return t}))}return t}))}(e,t)})).then((()=>function(e){return e.refresh&&e.refresh()}(e))).catch((function(t){return J(e,t)}));return e.load_async?Promise.resolve():t}let P={},S={};function E(e,t){return q(e),S.hasOwnProperty(e)?t[S[e]]?void P[e].push(t):console.log("No event handler: "+e,t):console.log("No event: "+e,t)}function q(e,t){t||(t=e),S[e]||(S[e]=t,P[e]=[])}function F(e){for(let t in P)if(P.hasOwnProperty(t))for(let n=P[t].length;n>=0;n--)e===P[t][n]&&P[t].splice(n,1)}function R(e){let t=Array.apply(null,arguments);if(e=t.shift(),!P.hasOwnProperty(e)||!S.hasOwnProperty(e))return i();let n=P[e],r=S[e],s=[];for(let e=0;e<n.length;e++){let o=n[e][r].apply(n[e],t);void 0!==o&&s.push(o)}return Promise.all(s)}let x={};const j="_controller_",T="_sdc_controller_";function M(e,t,n){if(!e)return[];let r=e.children(),s=[];return r.each((function(e,r){let o=$(r),i=o.prop("tagName").toLowerCase().split("_");$.inArray(i[0],t)>=0?s.push({tag:i[0],super:i.splice(1)||[],dom:o}):i[0].startsWith("this.")?o.addClass(`_bind_to_update_handler sdc_uuid_${n._uuid}`):s=s.concat(M(o,t,n))})),s}function I(e,t,n,r){return e?x[n]?Promise.resolve(x[n]):(t.VERSION=ue.VERSION,t._method="content",$.get(e,t).then((function(e){return r||(x[n]=e),e})).catch((function(e){throw 301===e.status&&R("_RedirectOnView",e.responseJSON["url-link"]),R("navLoaded",{controller_name:()=>e.status}),`<sdc-error data-code="${e.status}">${e.responseText}</sdc-error>`}))):Promise.resolve(!1)}function L(e,t){return t=t||e.data(j),V(ue.tagNames,e,t)}function A(e){let t=e.contentUrl;if(e&&0===e._urlParams.length){let n,r=/%\(([^)]+)\)\w/gm;for(e._urlParams=[];n=r.exec(t);)e._urlParams.push(n[1]),e.contentReload=!0}let n=function(e,t){return g(e._urlParams,t)}(e,e.$container);return e._urlParams.length&&(t=function(e,t,n){for(let r in e._urlParams)if(e._urlParams.hasOwnProperty(r)){let s=e._urlParams[r],o=RegExp("%\\("+s+"\\)\\w","gm");t=t.replace(o,""+n.shift())}return t}(e,t,n)),e.parsedContentUrl=t,{url:t,args:n[n.length-1]}}function U(e){return e.hasClass(T)?e.data(`${j}`):e.closest(`.${T}`).data(`${j}`)}function D(e,t,n,r){let s=e.data(j);return s?L(e,s):(s=k(r,e,t,n),e.data(j,s),e.addClass(T),N(s))}function J(e,t){if(t&&t.length>0){e.$container.empty(),e.$container.attr(e._tagName,"");for(let t in e._mixins)e.$container.attr(e._mixins[t]._tagName,"");e.$container.append(t)}return L(e.$container,e)}function V(e,t,n){return new Promise((r=>{let s=M(t,e,n),o=s.length;if(0===o)return r();for(let e=0;e<s.length;e++)D(s[e].dom,s[e].tag,s[e].super,n).then((()=>{if(o--,0===o)return r()}))}))}function G(e){return B(e,e.$container)}function B(e,t){const n=[];return t.find(`._bind_to_update_handler.sdc_uuid_${e._uuid}`).each((function(){const t=$(this);let r;if(t.hasClass("_with_handler"))r=t.data("handler");else{let t=this.tagName.toLowerCase().replace(/^this./,"");e[t]&&(r=e[t])}void 0!==r&&("function"==typeof r&&(r=r.bind(e)(t.data())),n.push(Promise.resolve(r).then((n=>{const r=$("<div></div>");return r.append(n),V(b(),r,e).then((()=>B(e,r).then((()=>(t.safeEmpty().text("").append(n),!0)))))}))))})),Promise.all(n)}let W=!1,K=!1,Z=null;const z=25e3;let H={};class X{constructor(e,t){this.pk=e,this._model=t}set model(e){this._model=e}get model(){return this._model}load(e){if(!this._model)throw new TypeError("Model is not set!!");return e.newModel(this._model,{pk:this.pk})}}const Q={get(e,t){const n=e[t]??void 0;if(n instanceof X){if(!n.pk&&0!==n.pk)return null;const e=new Number(n.pk);return e.load=n.load.bind(n),e}return n},set(e,t,n){if(t in e){const r=e[t];r instanceof X?n.hasOwnProperty("pk")?r.pk=n.pk:r.pk=n:e[t]=n}else e[t]=n;return!0}};function Y(){return K=!0,new Promise((e=>{Z="https:"===window.location.protocol?new WebSocket(`wss://${window.location.host}/sdc_ws/ws/`):new WebSocket(`ws://${window.location.host}/sdc_ws/ws/`),Z.onmessage=function(e){let t=JSON.parse(e.data);if(t.is_error)(t.msg||t.header)&&R("pushErrorMsg",t.header||"",t.msg||""),H[t.id]&&(H[t.id][1](t.data||null),delete H[t.id]);else if((t.msg||t.header)&&R("pushMsg",t.header||"",t.msg||""),t.type&&"sdc_recall"===t.type)H[t.id]&&(H[t.id][0](t.data),delete H[t.id]);else if(t.type&&"sdc_event"===t.type){let e=t.event;e&&R(e,t.payload)}else t.type&&"sdc_redirect"===t.type&&R("onNavLink",t.link)},Z.onclose=function(){W&&console.error("SDC Socket closed unexpectedly"),W=!1;for(const[e,t]of Object.entries(H))t[1]({}),delete H[e];setTimeout((()=>{Y()}),1e3)},Z.onerror=function(e){if(console.error("Socket encountered error: ",e.message,"Closing socket"),W)try{Z.close()}catch(e){}},Z.onopen=function(){W=!0,K=!1,e()}}))}function ee(){if(W){W=!1;try{Z.close()}catch(e){}}}function te(){return new Promise((e=>W?e():K?void setTimeout((()=>{te().then((()=>{e()}))}),200):e(Y())))}class ne{constructor(e,t={}){this.values_list=[],this.values={},this.model_name=e,this.model_query=t,this._is_connected=!1,this._is_conneting_process=!1,this._auto_reconnect=!0,this.socket=null,this.open_request={},this.on_update=()=>{},this.on_create=()=>{},this.form_id=f()}[Symbol.iterator](){let e=-1;return{next:()=>(++e,e<this.values_list.length?{value:this.values_list[e],done:!1}:{value:null,done:!0})}}length(){return this.values_list.length}byPk(e){if(null!==e){e=parseInt(e),isNaN(e)&&(e=-1);let t=this.values_list.find((t=>t.pk===e));return t||(t=new Proxy({pk:e},Q),this.values_list.push(t)),t}return{pk:e}}filter(e){return this.model_query=Object.assign({},this.model_query,e),this}load(){return this.isConnected().then((()=>{const e=f();return new Promise(((t,n)=>{this.socket.send(JSON.stringify({event:"model",event_type:"load",event_id:e,args:{model_name:this.model_name,model_query:this.model_query}})),this.open_request[e]=[t,n]}))}))}listView(e={},t=null,n=null){let r=$('<div class="container-fluid">');return this.isConnected().then((()=>{const s=f();this.socket.send(JSON.stringify({event:"model",event_type:"list_view",event_id:s,args:{model_name:this.model_name,model_query:this.model_query,filter:e}})),this.open_request[s]=[e=>{r.append(e.html),ue.refresh(r),t&&t(e)},e=>{n&&n(e)}]})),r}detailView(e=-1,t=null,n=null){e=parseInt(e),isNaN(e)&&(e=-1);let r,s=$('<div class="container-fluid">');return r=0!==this.values_list.length?this.isConnected():this.load(),r.then((()=>{-1===e&&(e=this.values_list[0].pk);const r=f();this.socket.send(JSON.stringify({event:"model",event_type:"detail_view",event_id:r,args:{model_name:this.model_name,model_query:this.model_query,pk:e}})),this.open_request[r]=[e=>{s.append(e.html),ue.refresh(s),t&&t(e)},e=>{n&&n(e)}]})),s}syncFormToModel(e){return this.syncForm(e)}syncModelToForm(e){e&&e.hasClass(this.form_id)||(e=$(`.${this.form_id}`));let t=this;e.each((function(){if(!this.hasAttribute("data-model_pk"))return;let e=$(this).data("model_pk"),n=t.byPk(e);for(let e of this.elements){let t=e.name;if(t&&""!==t)if("checkbox"===e.type)e.checked=n[t];else if("file"===e.type&&n[t]instanceof File){let r=new DataTransfer;r.items.add(n[t]),e.files=r}else $(e).val(n[t])}}))}syncForm(e){e&&e.hasClass(this.form_id)||(e=$(`.${this.form_id}`));const t=this;let n=[];return e.each((function(){let e=$(this).data("model_pk"),r=t.byPk(e);for(let e of this.elements){let t=e.name;t&&""!==t&&("hidden"===e.type?r[t]=(s=$(e).val()).toLowerCase().match(/^(true|false)$/)?"true"===s.toLowerCase():"undefined"!==s?"none"===s.toLowerCase()?null:s.match(/^-?\d+$/)?parseInt(s):s.match(/^-?\d+\.?\d+$/)?parseFloat(s):s.match(/^(['][^']*['])|(["][^"]*["])$/)?s.substring(1,s.length-1):s:void 0:"checkbox"===e.type?r[t]=e.checked:"file"===e.type?r[t]=e.files[0]:r[t]=$(e).val())}var s;return n.push(r),r})),this.values_list.length<=1&&n.length>0&&(this.values=n.at(-1)),n}createForm(e=null,t=null){let n=$('<div class="container-fluid">');return this.isConnected().then((()=>{this._getForm(null,"create_form",null,n,e,t)})),n}editForm(e=-1,t=null,n=null){let r;e=parseInt(e),isNaN(e)&&(e=-1),r=0!==this.values_list.length?this.isConnected():this.load();let s=$('<div class="container-fluid">');return r.then((()=>{e<=-1&&(e=this.values_list.at(e).pk),this._getForm(e,"edit_form",null,s,t,n)})),s}namedForm(e=-1,t,n=null,r=null){let s;e=parseInt(e),isNaN(e)&&(e=-1),s=0!==this.values_list.length?this.isConnected():this.load();let o=$('<div class="container-fluid">');return s.then((()=>{e<=-1&&(e=this.values_list.at(e).pk),this._getForm(e,"named_form",t,o,n,r)})),o}_getForm(e,t,n,r,s,o){e=parseInt(e),isNaN(e)&&(e=-1);const i=f();this.socket.send(JSON.stringify({event:"model",event_type:t,event_id:i,args:{model_name:this.model_name,model_query:this.model_query,pk:e,form_name:n}}));const l=null===e||-1===e?"create":"edit";this.open_request[i]=[t=>{r.append(t.html);let n=r.closest("form").addClass(`sdc-model-${l}-form sdc-model-form ${this.form_id}`).data("model",this).data("model_pk",e);n.length>0&&!n[0].hasAttribute("sdc_submit")&&n.attr("sdc_submit","submitModelFormDistributor"),ue.refresh(r),s&&s(t)},e=>{o&&o(e)}]}new(){return new Promise(((e,t)=>{const n=$("<form>").append(this.createForm((()=>{this.syncFormToModel(n),e()}),t))}))}save(e=-1){return e=parseInt(e),isNaN(e)&&(e=-1),this.isConnected().then((()=>{let t;t=e>-1?[this.byPk(e)]:this.values_list;let n=[];return t.forEach((e=>{const t=f();n.push(new Promise(((n,r)=>{this._readFiles(e).then((s=>{this.socket.send(JSON.stringify({event:"model",event_type:"save",event_id:t,args:{model_name:this.model_name,model_query:this.model_query,data:e,files:s}})),this.open_request[t]=[e=>{let t="string"==typeof e.data.instance?JSON.parse(e.data.instance):e.data.instance;e.data.instance=this._parseServerRes(t),n(e)},r]}))})))})),Promise.all(n)}))}create(e=this.values){const t=f();return this.isConnected().then((()=>new Promise(((n,r)=>{this._readFiles(e).then((s=>{this.socket.send(JSON.stringify({event:"model",event_type:"create",event_id:t,args:{model_name:this.model_name,model_query:this.model_query,data:e,files:s}})),this.open_request[t]=[e=>{let t="string"==typeof e.data.instance?JSON.parse(e.data.instance):e.data.instance;e.data.instance=this._parseServerRes(t)[0],n(e)},r]}))}))))}delete(e=-1){e=parseInt(e),isNaN(e)&&(e=-1),-1===e&&(e=this.values?.pk);const t=f();return this.isConnected().then((()=>new Promise(((n,r)=>{this.socket.send(JSON.stringify({event:"model",event_type:"delete",event_id:t,args:{model_name:this.model_name,model_query:this.model_query,pk:e}})),this.open_request[t]=[n,r]}))))}isConnected(){return new Promise(((e,t)=>{if(this._is_connected)e();else if(this._is_conneting_process){const[n,r]=this.open_request._connecting_process;this.open_request._connecting_process=[()=>{n(),e()},()=>{r(),t()}]}else this._is_conneting_process=!0,this.open_request._connecting_process=[()=>{},()=>{}],this._connectToServer().then((()=>{e(this._checkConnection())}))}))}close(){this.socket&&(this._auto_reconnect=!1,this.socket.onclose=()=>{},this.socket.close(),delete this.socket)}clean(){return this.values_list=[],this.values={},this}_readFiles(e){let t=[],n={};for(const[r,s]of Object.entries(e))s instanceof File&&t.push(new Promise(((e,t)=>{((r,s)=>{let o=new FileReader;o.onload=o=>{const i=f();this.open_request[i]=[e,t];let l=o.target.result,a=parseInt(Math.ceil(l.length/z));n[r]={id:i,file_name:s.name,field_name:r,content_length:s.size};for(let e=0;e<a;++e)this.socket.send(JSON.stringify({event:"model",event_type:"upload",event_id:i,args:{chunk:l.slice(z*e,z*(e+1)),idx:e,number_of_chunks:a,file_name:s.name,field_name:r,content_length:s.size,content_type:s.type,model_name:this.model_name,model_query:this.model_query}}))},o.onerror=()=>{t()},o.readAsBinaryString(s)})(r,s)})));return Promise.all(t).then((()=>n))}_onMessage(e){let t=JSON.parse(e.data);if(t.is_error)this.open_request.hasOwnProperty(t.event_id)&&(this.open_request[t.event_id][1](t),delete this.open_request[t.event_id]),(t.msg||t.header)&&R("pushErrorMsg",t.header||"",t.msg||""),"connect"===t.type&&(this.open_request._connecting_process[1](t),delete this.open_request._connecting_process,this._auto_reconnect=!1,this.socket.close());else{if((t.msg||t.header)&&R("pushMsg",t.header||"",t.msg||""),"connect"===t.type)this._is_connected=!0,this._is_conneting_process=!1,this.open_request._connecting_process[0](t),delete this.open_request._connecting_process;else if("load"===t.type){const e=JSON.parse(t.args.data);this.values_list=[],t.args.data=this._parseServerRes(e)}else if("on_update"===t.type||"on_create"===t.type){const e=JSON.parse(t.args.data);let n,r=this._parseServerRes(e);n="on_create"===t.type?this.on_create:this.on_update,n(r),t.args.data=r}let e=t.data?.instance;e&&(t.data.instance=JSON.parse(t.data.instance)),this.open_request.hasOwnProperty(t.event_id)&&(this.open_request[t.event_id][0](t),delete this.open_request[t.event_id])}}_connectToServer(){return new Promise((e=>{const t=`${this.model_name}`+(this.model_id>0?`/${this.model_id}`:"");"https:"===window.location.protocol?this.socket=new WebSocket(`wss://${window.location.host}/sdc_ws/model/${t}`):this.socket=new WebSocket(`ws://${window.location.host}/sdc_ws/model/${t}`),this.socket.onmessage=this._onMessage.bind(this),this.socket.onclose=e=>{console.error(`SDC Model (${this.model_name}, ${this.model_id}) Socket closed unexpectedly`),this._is_connected=!1;for(const[t,n]of Object.entries(this.open_request))n[1](e);this.open_request={},setTimeout((()=>{this._auto_reconnect&&this._connectToServer().then((()=>{}))}),1e3)},this.socket.onerror=e=>{if(console.error(`Model Socket encountered error: ${e} Closing socket`),this._is_connected)try{this.socket.close()}catch(e){}},this.socket.onopen=()=>{e()}}))}_checkConnection(){const e=f();return new Promise(((t,n)=>{this.socket.send(JSON.stringify({event:"model",event_type:"connect",event_id:e,args:{model_name:this.model_name,model_query:this.model_query}})),this.open_request[e]=[t,n]}))}_parseServerRes(e){let t=[];for(let n of e){const e=n.pk,r=this.byPk(e);for(const[e,t]of Object.entries(n.fields))t&&"object"==typeof t&&t.__is_sdc_model__?r[e]=new X(t.pk,t.model):r[e]=t;t.push(r)}return 1===this.values_list.length?this.values=this.values_list.at(-1):this.values={},t}}class re{constructor(){this._uuid=f(),this.contentUrl="",this.contentReload=!1,this.events=[],this.load_async=!1,this._isEventsSet=!1,this._allEvents=null,this._urlParams=[],this._models=[],this._cssUrls=[],this.afterShow=()=>{console.warn("afterShow is deprecated!!")},this._mixins={},this._tagName="",this._childController={},this._parentController=null,this._onLoadDone=!1,this.$container=null,this._isMixin=!1}_runLifecycle(e,t){ue.DEBUG&&console.debug(e,this._tagName);let n=[];if(!this._isMixin){this._isMixin=!0;for(let r in this._mixins){let s=this._mixins[r];"function"==typeof s[e]&&n.push(s[e].apply(this,t))}return Promise.all(n).then((()=>{this._isMixin=!1}))}}onInit(){ue.DEBUG&&console.DEBUG(Array.apply(null,arguments),this._tagName)}get parentController(){return this._parentController}get childController(){return this._childController}onLoad(){return this._runLifecycle("onLoad",arguments)}willShow(){return this._runLifecycle("willShow",arguments)}onRefresh(){return this._runLifecycle("onRefresh",arguments)}onRemove(){return this._runLifecycle("onRemove",arguments),!0}remove(){for(const e of this._models)e.close();let e=this._childController;for(let t in e)if(e.hasOwnProperty(t))for(let n of e[t])if(!n.remove())return!1;if(!this.onRemove||this.onRemove()){F(this);const e=a(this._tagName);if(this._parentController._childController[e]){let t=this._parentController._childController[e];for(let e=0;e<t.length;e++)t[e]===this&&t.splice(e,1)}return this.$container.remove(),!0}return!1}controller_name(){return this._tagName.replace(/-./g,(e=>` ${e[1].toUpperCase()}`)).replace(/^./g,(e=>`${e.toUpperCase()}`))}addEvent(e,t,n){this.getEvents(),this._allEvents[e]=this._allEvents[e]||{},this._allEvents[e][t]=n}getEvents(){if(this._allEvents)return this._allEvents;let e=[];e=e.concat(this.events);for(let t in this._mixins){let n=this._mixins[t];Array.isArray(n.events)&&(e=e.concat(n.events))}return this._allEvents=_.merge(...e)}post(e,t){return ue.post(this,e,t)}get(e,t){return ue.get(this,e,t)}submitForm(e,t,n){return ue.submitFormAndUpdateView(this,e,t,n)}serverCall(e,t){let n=this.contentUrl.match(/sdc_view\/([^/]+)/i);if(n&&!(n.length<2))return function(e,t,n,r){let s=f();return te().then((()=>{Z.send(JSON.stringify({event:"sdc_call",id:s,controller:t,app:e,function:n,args:r}))})),new Promise(((e,t)=>{H[s]=[e,t]}))}(n[1],this._tagName,e,t);console.error("To use the serverCall function the contentUrl must be set: "+this.name)}newModel(e,t={}){if(e instanceof Number&&e.hasOwnProperty("load"))return e.load(this);const n=new ne(e,t);return this._models.push(n),n}updateModel(e,t={},n){let r=new ne(e,t);return r.load().then((()=>{r.values|=n,r.save().then((()=>(r.close(),r.values)))}))}find(e){return this.$container.find(e)}refresh(){return ue.refresh(this.$container,this)}reload(){return ue.reloadController(this)}submitModelFormDistributor(e,t){return"function"==typeof this._submitModelForm?this._submitModelForm(e,t):"function"==typeof this.submitModelForm?this.submitModelForm(e,t):this.defaultSubmitModelForm(e,t)}iterateAllChildren(){let e=this._childController,t=[];for(let n in e)if(e.hasOwnProperty(n))for(let r of e[n])t.push(r),t.push(...r.iterateAllChildren());return t}defaultSubmitModelForm(e,t){let n=[];if(!this._isMixin){t.stopPropagation(),t.preventDefault();let r=e.data("model");const s=r.syncForm(e);for(let t of s)n.push(new Promise(((n,s)=>{let o;o=null!==t.pk&&t.pk>=0?r.save(t.pk):r.create(t),o.then((t=>{m(e),this.submit_model_form_success&&this.submit_model_form_success(t[0]);for(const e of this.iterateAllChildren())e.submit_model_form_success&&e.submit_model_form_success(t[0]);n(t)})).catch((t=>{p(e,$(t.html)),this.submit_model_form_error&&this.submit_model_form_error(t);for(const e of this.iterateAllChildren())e.submit_model_form_error&&e.submit_model_form_error(t);s(t)}))})))}return Promise.all(n).then((e=>Object.assign({},...e.flat())))}}const se=["onbeforeunload"],oe=Object.keys(window).filter((e=>/^on/.test(e)&&!se.includes(e))).map((e=>e.slice(2)));function ie(e){let t=e.type;e.hasOwnProperty("namespace")&&e.namespace&&e.namespace.length&&(t+=`.${e.namespace}`);let n=$(e.target),r=null,s=!1,o=!1;for(e.stopImmediatePropagation=()=>o=!0,e.stopPropagation=()=>o=s=!0;n.length;){let i=n.attr(`sdc_${t}`);if(i){if(!r&&(r=U(n),!r))return;for(;r;){if(i.split(" ").forEach((o=>{if(s)return;let i=null;if("function"==typeof o)i=o;else if("function"==typeof r[o])i=r[o];else if("string"==typeof o&&o.startsWith("this.event_")){if(i=r.getEvents()[t],!i)return;if(i=i[o.slice(11)],!i)return}i&&i.call(r,n,e)})),o)return;r=r._parentController}}if(s)return;n=n.parent()}return{res:!0}}function le(e){if(e._isEventsSet)return;const t=e.getEvents();for(let n in t)if(t.hasOwnProperty(n)){let r=t[n];for(let t in r)r.hasOwnProperty(t)&&e.find(t).each((function(){let e=$(this),r=e.attr(`sdc_${n}`)||null;r=r?r.split(" "):[];const s=`this.event_${t}`;-1===r.indexOf(s)&&(r.push(s),e.attr(`sdc_${n}`,r.join(" ")))}))}}const ae={classname:"class"};window.sdcDom=function(e,t,...n){if(!e)return"";const r=function(e,t){let n,r=!1;if("string"==typeof e)n=$(document.createElement(e));else{const t=`this.${e.name}`;n=$(document.createElement(t)),n.data("handler",e),r=!0}return t&&Object.entries(t).forEach((([e,t])=>{e.startsWith("on")?n[0].addEventListener(e.substring(2).toLowerCase(),t):(ae[e.toLowerCase()]&&(e=ae[e.toLowerCase()]),n[0].setAttribute(e,t))})),r&&n.addClass("_bind_to_update_handler _with_handler"),n}(e,t);for(const e of n)r.append(e);return r};let ce,he,ue={CSRF_TOKEN:window.CSRF_TOKEN||"",LANGUAGE_CODE:window.LANGUAGE_CODE||"en",DEBUG:window.DEBUG||!1,VERSION:window.VERSION||"0.0",tagNames:[],Global:v,rootController:null,_isInit:!1,_origin_trigger:null,init_sdc:()=>{ue._isInit||(ue._isInit=!0,ue._origin_trigger?ee():(ue._origin_trigger=$.fn.trigger,$.fn.trigger=function(e){const t={}.hasOwnProperty.call(e,"type")?e.type:e;return oe.includes(t)||(oe.push(t),$(window).on(t,ie)),ue._origin_trigger.call(this,e)},ue.updateJquery()),te(),function(){const e=$(window);oe.forEach((t=>{e.off(t).on(t,ie)}))}(),ue.rootController=ue.rootController||new re),ue.tagNames=b();for(let[e,t]of Object.entries(ue.Global))t.$container||(v[e].$container=o());return V(ue.tagNames,o(),ue.rootController)},updateJquery:()=>{$.fn.safeReplace=function(e){return ue.safeReplace($(this),e)},$.fn.safeEmpty=function(){return ue.safeEmpty($(this))},$.fn.safeRemove=function(){return ue.safeRemove($(this))}},controllerToTag:e=>l(e.name).replace(/-controller$/,""),registerGlobal:e=>{let t=ue.controllerToTag(e),n=new e;w[t]=[n,[]],n._tagName=t,window[a(t)]=v[a(t)]=n},cleanCache:()=>{x={}},register:e=>{let t=ue.controllerToTag(e);return w[t]=[e,[]],e.prototype._tagName=t,{addMixin:(...e)=>{for(let n of e){let e;"string"==typeof n?e=l(n):n&&(e=ue.controllerToTag(n)),w[t][1].push(e)}}}},post:(e,t,n)=>(n||(n={}),n.CSRF_TOKEN=ue.CSRF_TOKEN,ue.ajax(e,t,params,$.post)),get:(e,t,n)=>ue.ajax(e,t,n,$.get),ajax:(e,t,n,r)=>{n||(n={}),n.VERSION=ue.VERSION,n._method=n._method||"api";const s=new Promise(((o,i)=>r(t,n).then(((t,n,r)=>{o(t,n,r),"redirect"===t.status?R("onNavLink",t["url-link"]):s.then((()=>{ue.refresh(e.$container)}))})).catch(i)));return s},submitFormAndUpdateView:(e,t,n,r)=>{let s=new FormData(t);const o=e=>{e["url-link"]?R("onNavLink",e["url-link"]):window.location.href=e.url},i=new Promise(((l,a)=>{h(s,n||t.action,r||t.method).then(((t,n,r)=>{l(t,n,r),"redirect"===t.status?o(t):i.then((()=>{ue.refresh(e.$container)}))})).catch(((e,t,n)=>{301===e.status?(e=e.responseJSON,o(e),l(e,t,n)):a(e,t,n)}))}));return i},submitForm:(e,t,n)=>{let r=new FormData(e);return new Promise(((s,o)=>{h(r,t||e.action,n||e.method).then(s).catch(o)}))},getController:e=>U(e),safeEmpty:e=>(e.children().each((function(e,t){let n=$(t);ue.safeRemove(n)})),e),safeReplace:(e,t)=>(t.insertBefore(e),ue.safeRemove(e)),safeRemove:e=>(e.each((function(){let e=$(this);e.data(`${j}`)&&e.data(`${j}`).remove()})),e.find(`.${T}`).each((function(){const e=$(this).data(`${j}`);e&&e.remove()})),e.remove()),reloadController:e=>function(e){if(e.contentUrl){let t=A(e,e.contentUrl);return e.contentUrl=t.url,I(e.contentUrl,t.args,e._tagName,e.contentReload)}return new Promise((e=>{e($())}))}(e).then((t=>{let n=$(t);e._childController={},V(ue.tagNames,n,e).then((()=>{ue.safeEmpty(e.$container),e.$container.append(n),ue.refresh(e.$container,e)}))})),refresh:(e,t)=>{if(t||(t=ue.getController(e)),!t)return i();let n=t,r=[];for(;n;)n._isEventsSet=!1,r.unshift(n),n=n._parentController;return V(ue.tagNames,t.$container,t).then((()=>{G(t).then((()=>{for(let e of r)le(e);t.onRefresh(e)}))}))}};const de=ee,fe={get_controller:async function(e,t={},n=""){if(!jest)throw new Error("JEST is not defined");ce||(he=$.ajax.bind($),ce=jest.spyOn($,"ajax"),ce.mockImplementation((function(e){return he(e).then((e=>e)).catch((e=>e))})));const r=$("body");ue.updateJquery(),r.safeEmpty();const s=$(`<${e}>${n}</${e}>`);for(const[e,n]of Object.entries(t))s.data(e,n);const o=$("<div></div>").append(s);return r.append(o),ue._isInit=!1,ue.cleanCache(),await ue.init_sdc(),ue.getController(s)}};var _e=t.n$,me=t.du,pe=t.yA,ge=t.g3,ye=t.u2,ve=t.n9,we=t.on,$e=t.yy,be=t.R6,Ce=t.$n,Oe=t.hA,ke=t.VB,Ne=t.hZ;export{_e as AbstractSDC,me as allOff,pe as app,ge as checkIfParamNumberBoolOrString,ye as clearErrorsInForm,ve as controllerFactory,we as on,$e as runControlFlowFunctions,be as setErrorsInForm,Ce as setEvent,Oe as socketReconnect,ke as test_utils,Ne as trigger};
1
+ var e={d:(t,n)=>{for(var r in n)e.o(n,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:n[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};let n;e.d(t,{n$:()=>re,du:()=>q,yA:()=>he,g3:()=>d,u2:()=>m,n9:()=>O,on:()=>E,yy:()=>S,R6:()=>p,$n:()=>R,hA:()=>de,VB:()=>_e,hZ:()=>F});const r=/([^\s,]+)/g,o=/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;function s(){return n||(n=$("body")),n}function i(){return new Promise((function(e){e()}))}function l(e){return(e=(e=e.replace(/[A-Z]/g,(e=>`-${e.toLowerCase()}`))).replace(/[0-9]+/g,(e=>`-${e}`))).replace(/^[-]/g,"")}function a(e){return e.replace(/-./g,(e=>`${e[1].toUpperCase()}`))}const c=(e,t)=>{let n=t,r=Object.getOwnPropertyNames(e.prototype).concat(Object.getOwnPropertySymbols(e.prototype));for(;""!==n.name;)Object.getOwnPropertyNames(n.prototype).concat(Object.getOwnPropertySymbols(n.prototype)).forEach((t=>{t.match(/^(?:constructor|prototype|arguments|caller|name|bind|call|apply|toString|length)$/)||r.includes(t)||(r.push(t),Object.defineProperty(e.prototype,t,Object.getOwnPropertyDescriptor(n.prototype,t)))})),n=Object.getPrototypeOf(n)};function u(e,t,n){return $.ajax({url:t,type:n||"POST",xhr:function(){var e=$.ajaxSettings.xhr();return e.upload&&e.upload.addEventListener("progress",h,!1),e},data:e,cache:!1,contentType:!1,processData:!1,beforeSend:function(e,t){(function(e){return/^(GET|HEAD|OPTIONS|TRACE)$/.test(e)})(t.type)||this.crossDomain||e.setRequestHeader("X-CSRFToken",window.CSRF_TOKEN)}})}function h(e){if(e.lengthComputable){var t=Math.round(e.loaded/e.total*100),n=$(".progress-container");100===t?n.hide():n.show(),t+="%",n.find(".progress-bar").css({width:t}).text(t)}}function d(e,t=null){return"string"!=typeof e?e:t&&void 0!==t[e]?"function"==typeof t[e]?t[e].bind(t):t[e]:e.match(/^(true|false)$/)?"true"===e:"undefined"!==e?"none"===e.toLowerCase()?null:e.match(/^-?\d+$/)?parseInt(e):e.match(/^-?\d+\.?\d+$/)?parseFloat(e):e.match(/^(['][^']*['])|(["][^"]*["])$/)?e.substr(1,e.length-2):e:void 0}function f(){return([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,(e=>(e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>e/4).toString(16)))}function m(e){e.find(".has-error").removeClass("has-error").find(".alert-danger").remove(),e.find(".non-field-errors").remove()}function p(e,t){t=$("<div>").append(t),e.find(".has-error").removeClass("has-error").find(".alert-danger").safeRemove(),e.find(".non-field-errors").safeRemove();let n=t.find("input[type=file]").parent();e.find("input[type=file]").parent().each((function(e){$(this).replaceWith(n[e])}));let r=0===t.find(".non-field-errors").insertAfter(e.find(".hidden-form-fields")).length;return t.find(".has-error").each((function(){r=!1;let t=$(this),n=t.data("auto-id"),o=e.find(".form-group."+n);o.addClass("has-error"),o.find(".form-input-container").append(t.find(".alert-danger"))})),r}function g(e,t,n=null){let r=function(e,t){let n;e||(e=[]);let r=t.data(),o={};for(let t in r)r.hasOwnProperty(t)&&t!==T&&!e.includes(t)&&(o[t]=r[t]);n=[];for(let t=0;t<e.length;t++){let o=e[t];r.hasOwnProperty(o)?n.push(r[o]):n.push("undefined")}return n.push(o),n}(e,t);return function(e,t=null){let n=[];for(let r=0;r<e.length;r++){let o=d(e[r],t);n.push(o)}return n}(r,n)}function y(e,t,n){if(!e)return!1;if("function"!=typeof e.onInit)return!1;let s;var i;"function"==typeof e._on_init_params?s=e._on_init_params():s=(i=e.onInit.toString().replace(o,"")).slice(i.indexOf("(")+1,i.indexOf(")")).match(r)||[];let l=g(s,t,n._parentController);if(e.onInit.apply(n,l),n===e)for(let r in e._mixins)y(e._mixins[r],t,n)}let v={},w={};function C(){return Object.keys(w)}function b(e,t){if(e){let n=a(t._tagName);e._childController[n]||(e._childController[n]=[]),e._childController[n].push(t)}return t._parentController=e}function k(e,t,n,r){let o=[];r=function(e,t){e=(e=e.concat(w[t][1])).filter(((e,t,n)=>n.indexOf(e)===t));let n=!0;for(;n;){n=!1;for(let t of e)for(let r of w[t][1])e.includes(r)||(e.push(r),n=!0)}return e}(r,n);for(let e of r)o.push(w[e][0]);let s=new(function(e,...t){let n=class{constructor(...n){let r={};t.forEach((e=>{let t;Object.assign(this,t=new e),t._tagName=e.prototype._tagName,t._isMixin=!0,r[e.name]=t})),Object.assign(this,new e),this._mixins=r}get mixins(){return this._mixins}};return c(n,e),t.forEach((e=>{c(n,e)})),n}(w[n][0],...o));return s._tagName=n,b(e,s),s.$container=t,function(e,t){y(t,e,t)}(t,s),s}function O(e,t,n,r){let o=a(n);if(v[o]){let n=v[o];return b(e,n),n.$container=t,n}return k(e,t,n,r)}function S(e){const t=function(e){return function(e){let t={args:{}};return e.contentUrl&&(t=M(e,e.contentUrl),e.contentUrl=t.url),Promise.all([j(e.contentUrl,t.args,e._tagName,e.contentReload)]).then((function(e){let t=e[0];if(t)try{return $(t)}catch{return $("<div></div>").append(t)}return null}))}(e).then((t=>!e.onLoad||e._onLoadDone?t:(e._onLoadDone=!0,(e.onLoad(t)||i()).then((()=>t)))))}(e).then((function(t){return function(e,t){return J(e,t).then((function(t){if(t=t||!0,e.willShow){let n=e.willShow();if(n instanceof Promise)return n.then((function(){return t}))}return t}))}(e,t)})).then((()=>function(e){return e.refresh&&e.refresh()}(e))).catch((function(t){return J(e,t)}));return e.load_async?Promise.resolve():t}let N={},P={};function E(e,t){return R(e),P.hasOwnProperty(e)?t[P[e]]?void N[e].push(t):console.log("No event handler: "+e,t):console.log("No event: "+e,t)}function R(e,t){t||(t=e),P[e]||(P[e]=t,N[e]=[])}function q(e){for(let t in N)if(N.hasOwnProperty(t))for(let n=N[t].length;n>=0;n--)e===N[t][n]&&N[t].splice(n,1)}function F(e){let t=Array.apply(null,arguments);if(e=t.shift(),!N.hasOwnProperty(e)||!P.hasOwnProperty(e))return i();let n=N[e],r=P[e],o=[];for(let e=0;e<n.length;e++){let s=n[e][r].apply(n[e],t);void 0!==s&&o.push(s)}return Promise.all(o)}let x={};const T="_controller_",A="_sdc_controller_";function L(e,t,n){if(!e)return[];let r=e.children(),o=[];return r.each((function(e,r){let s=$(r),i=s.prop("tagName").toLowerCase().split("_");$.inArray(i[0],t)>=0?o.push({tag:i[0],super:i.splice(1)||[],dom:s}):i[0].startsWith("this.")?s.addClass(`_bind_to_update_handler sdc_uuid_${n._uuid}`):o=o.concat(L(s,t,n))})),o}function j(e,t,n,r){return e?x[n]?Promise.resolve(x[n]):(t.VERSION=he.VERSION,t._method="content",$.get(e,t).then((function(e){return r||(x[n]=e),e})).catch((function(e){throw 301===e.status&&F("_RedirectOnView",e.responseJSON["url-link"]),F("navLoaded",{controller_name:()=>e.status}),`<sdc-error data-code="${e.status}">${e.responseText}</sdc-error>`}))):Promise.resolve(!1)}function I(e,t){return t=t||e.data(T),V(he.tagNames,e,t)}function M(e){let t=e.contentUrl;if(e&&0===e._urlParams.length){let n,r=/%\(([^)]+)\)\w/gm;for(e._urlParams=[];n=r.exec(t);)e._urlParams.push(n[1]),e.contentReload=!0}let n=function(e,t){return g(e._urlParams,t)}(e,e.$container);return e._urlParams.length&&(t=function(e,t,n){for(let r in e._urlParams)if(e._urlParams.hasOwnProperty(r)){let o=e._urlParams[r],s=RegExp("%\\("+o+"\\)\\w","gm");t=t.replace(s,""+n.shift())}return t}(e,t,n)),e.parsedContentUrl=t,{url:t,args:n[n.length-1]}}function U(e){return e.hasClass(A)?e.data(`${T}`):e.closest(`.${A}`).data(`${T}`)}function D(e,t,n,r){let o=e.data(T);return o?I(e,o):(o=O(r,e,t,n),e.data(T,o),e.addClass(A),S(o))}function J(e,t){if(t&&t.length>0){e.$container.empty(),e.$container.attr(e._tagName,"");for(let t in e._mixins)e.$container.attr(e._mixins[t]._tagName,"");e.$container.append(t)}return I(e.$container,e)}function V(e,t,n){return new Promise((r=>{let o=L(t,e,n),s=o.length;if(0===s)return r();for(let e=0;e<o.length;e++)D(o[e].dom,o[e].tag,o[e].super,n).then((()=>{if(s--,0===s)return r()}))}))}function B(e){return G(e,e.$container)}function G(e,t){const n=[];return t.find(`._bind_to_update_handler.sdc_uuid_${e._uuid}`).each((function(){const t=$(this);let r;if(t.hasClass("_with_handler"))r=t.data("handler");else{let t=this.tagName.toLowerCase().replace(/^this./,"");e[t]&&(r=e[t])}void 0!==r&&("function"==typeof r&&(r=r.bind(e)(t.data())),n.push(Promise.resolve(r).then((n=>{const r=$("<div></div>");return r.append(n),V(C(),r,e).then((()=>G(e,r).then((()=>(t.safeEmpty().text("").append(n),!0)))))}))))})),Promise.all(n)}const W=25e3;class K{constructor(e,t){this.pk=e,this._model=t}set model(e){this._model=e}get model(){return this._model}load(e){if(!this._model)throw new TypeError("Model is not set!!");return e.newModel(this._model,{pk:this.pk})}}const H={get(e,t){const n=e[t]??void 0;if(n instanceof K){if(!n.pk&&0!==n.pk)return null;const e=new Number(n.pk);return e.load=n.load.bind(n),e}return n},set(e,t,n){if(t in e){const r=e[t];r instanceof K?n.hasOwnProperty("pk")?r.pk=n.pk:r.pk=n:e[t]=n}else e[t]=n;return!0}};class Z{constructor(e,t={}){this.values_list=[],this.values={},this.model_name=e,this.model_query=t,this._is_connected=!1,this._is_conneting_process=!1,this._auto_reconnect=!0,this.socket=null,this.open_request={},this.on_update=()=>{},this.on_create=()=>{},this.form_id=f()}[Symbol.iterator](){let e=-1;return{next:()=>(++e,e<this.values_list.length?{value:this.values_list[e],done:!1}:{value:null,done:!0})}}length(){return this.values_list.length}byPk(e){if(null!==e){e=parseInt(e),isNaN(e)&&(e=-1);let t=this.values_list.find((t=>t.pk===e));return t||(t=new Proxy({pk:e},H),this.values_list.push(t)),t}return{pk:e}}filter(e){return this.model_query=Object.assign({},this.model_query,e),this}load(){return this.isConnected().then((()=>{const e=f();return new Promise(((t,n)=>{this.socket.send(JSON.stringify({event:"model",event_type:"load",event_id:e,args:{model_name:this.model_name,model_query:this.model_query}})),this.open_request[e]=[t,n]}))}))}listView(e={},t=null,n=null){let r=$('<div class="container-fluid">');return this.isConnected().then((()=>{const o=f();this.socket.send(JSON.stringify({event:"model",event_type:"list_view",event_id:o,args:{model_name:this.model_name,model_query:this.model_query,filter:e}})),this.open_request[o]=[e=>{r.append(e.html),he.refresh(r),t&&t(e)},e=>{n&&n(e)}]})),r}detailView(e=-1,t=null,n=null){e=parseInt(e),isNaN(e)&&(e=-1);let r,o=$('<div class="container-fluid">');return r=0!==this.values_list.length?this.isConnected():this.load(),r.then((()=>{-1===e&&(e=this.values_list[0].pk);const r=f();this.socket.send(JSON.stringify({event:"model",event_type:"detail_view",event_id:r,args:{model_name:this.model_name,model_query:this.model_query,pk:e}})),this.open_request[r]=[e=>{o.append(e.html),he.refresh(o),t&&t(e)},e=>{n&&n(e)}]})),o}syncFormToModel(e){return this.syncForm(e)}syncModelToForm(e){e&&e.hasClass(this.form_id)||(e=$(`.${this.form_id}`));let t=this;e.each((function(){if(!this.hasAttribute("data-model_pk"))return;let e=$(this).data("model_pk"),n=t.byPk(e);for(let e of this.elements){let t=e.name;if(t&&""!==t)if("checkbox"===e.type)e.checked=n[t];else if("file"===e.type&&n[t]instanceof File){let r=new DataTransfer;r.items.add(n[t]),e.files=r}else $(e).val(n[t])}}))}syncForm(e){e&&e.hasClass(this.form_id)||(e=$(`.${this.form_id}`));const t=this;let n=[];return e.each((function(){let e=$(this).data("model_pk"),r=t.byPk(e);for(let e of this.elements){let t=e.name;t&&""!==t&&("hidden"===e.type?r[t]=(o=$(e).val()).toLowerCase().match(/^(true|false)$/)?"true"===o.toLowerCase():"undefined"!==o?"none"===o.toLowerCase()?null:o.match(/^-?\d+$/)?parseInt(o):o.match(/^-?\d+\.?\d+$/)?parseFloat(o):o.match(/^(['][^']*['])|(["][^"]*["])$/)?o.substring(1,o.length-1):o:void 0:"checkbox"===e.type?r[t]=e.checked:"file"===e.type?r[t]=e.files[0]:r[t]=$(e).val())}var o;return n.push(r),r})),this.values_list.length<=1&&n.length>0&&(this.values=n.at(-1)),n}createForm(e=null,t=null){let n=$('<div class="container-fluid">');return this.isConnected().then((()=>{this._getForm(null,"create_form",null,n,e,t)})),n}editForm(e=-1,t=null,n=null){let r;e=parseInt(e),isNaN(e)&&(e=-1),r=0!==this.values_list.length?this.isConnected():this.load();let o=$('<div class="container-fluid">');return r.then((()=>{e<=-1&&(e=this.values_list.at(e).pk),this._getForm(e,"edit_form",null,o,t,n)})),o}namedForm(e=-1,t,n=null,r=null){let o;e=parseInt(e),isNaN(e)&&(e=-1),o=0!==this.values_list.length?this.isConnected():this.load();let s=$('<div class="container-fluid">');return o.then((()=>{e<=-1&&(e=this.values_list.at(e).pk),this._getForm(e,"named_form",t,s,n,r)})),s}_getForm(e,t,n,r,o,s){e=parseInt(e),isNaN(e)&&(e=-1);const i=f();this.socket.send(JSON.stringify({event:"model",event_type:t,event_id:i,args:{model_name:this.model_name,model_query:this.model_query,pk:e,form_name:n}}));const l=null===e||-1===e?"create":"edit";this.open_request[i]=[t=>{r.append(t.html);let n=r.closest("form").addClass(`sdc-model-${l}-form sdc-model-form ${this.form_id}`).data("model",this).data("model_pk",e);n.length>0&&!n[0].hasAttribute("sdc_submit")&&n.attr("sdc_submit","submitModelFormDistributor"),he.refresh(r),o&&o(t)},e=>{s&&s(e)}]}new(){return new Promise(((e,t)=>{const n=$("<form>").append(this.createForm((()=>{this.syncFormToModel(n),e()}),t))}))}save(e=-1){return e=parseInt(e),isNaN(e)&&(e=-1),this.isConnected().then((()=>{let t;t=e>-1?[this.byPk(e)]:this.values_list;let n=[];return t.forEach((e=>{const t=f();n.push(new Promise(((n,r)=>{this._readFiles(e).then((o=>{this.socket.send(JSON.stringify({event:"model",event_type:"save",event_id:t,args:{model_name:this.model_name,model_query:this.model_query,data:e,files:o}})),this.open_request[t]=[e=>{let t="string"==typeof e.data.instance?JSON.parse(e.data.instance):e.data.instance;e.data.instance=this._parseServerRes(t),n(e)},r]}))})))})),Promise.all(n)}))}create(e=this.values){const t=f();return this.isConnected().then((()=>new Promise(((n,r)=>{this._readFiles(e).then((o=>{this.socket.send(JSON.stringify({event:"model",event_type:"create",event_id:t,args:{model_name:this.model_name,model_query:this.model_query,data:e,files:o}})),this.open_request[t]=[e=>{let t="string"==typeof e.data.instance?JSON.parse(e.data.instance):e.data.instance;e.data.instance=this._parseServerRes(t)[0],n(e)},r]}))}))))}delete(e=-1){e=parseInt(e),isNaN(e)&&(e=-1),-1===e&&(e=this.values?.pk);const t=f();return this.isConnected().then((()=>new Promise(((n,r)=>{this.socket.send(JSON.stringify({event:"model",event_type:"delete",event_id:t,args:{model_name:this.model_name,model_query:this.model_query,pk:e}})),this.open_request[t]=[n,r]}))))}isConnected(){return new Promise(((e,t)=>{if(this._is_connected)e();else if(this._is_conneting_process){const[n,r]=this.open_request._connecting_process;this.open_request._connecting_process=[()=>{n(),e()},()=>{r(),t()}]}else this._is_conneting_process=!0,this.open_request._connecting_process=[()=>{},()=>{}],this._connectToServer().then((()=>{e(this._checkConnection())}))}))}close(){this.socket&&(this._auto_reconnect=!1,this.socket.onclose=()=>{},this.socket.close(),delete this.socket)}clean(){return this.values_list=[],this.values={},this}_readFiles(e){let t=[],n={};for(const[r,o]of Object.entries(e))o instanceof File&&t.push(new Promise(((e,t)=>{((r,o)=>{let s=new FileReader;s.onload=s=>{const i=f();this.open_request[i]=[e,t];let l=s.target.result,a=parseInt(Math.ceil(l.length/W));n[r]={id:i,file_name:o.name,field_name:r,content_length:o.size};for(let e=0;e<a;++e)this.socket.send(JSON.stringify({event:"model",event_type:"upload",event_id:i,args:{chunk:l.slice(W*e,W*(e+1)),idx:e,number_of_chunks:a,file_name:o.name,field_name:r,content_length:o.size,content_type:o.type,model_name:this.model_name,model_query:this.model_query}}))},s.onerror=()=>{t()},s.readAsBinaryString(o)})(r,o)})));return Promise.all(t).then((()=>n))}_onMessage(e){let t=JSON.parse(e.data);if(t.is_error)this.open_request.hasOwnProperty(t.event_id)&&(this.open_request[t.event_id][1](t),delete this.open_request[t.event_id]),(t.msg||t.header)&&F("pushErrorMsg",t.header||"",t.msg||""),"connect"===t.type&&(this.open_request._connecting_process[1](t),delete this.open_request._connecting_process,this._auto_reconnect=!1,this.socket.close());else{if((t.msg||t.header)&&F("pushMsg",t.header||"",t.msg||""),"connect"===t.type)this._is_connected=!0,this._is_conneting_process=!1,this.open_request._connecting_process[0](t),delete this.open_request._connecting_process;else if("load"===t.type){const e=JSON.parse(t.args.data);this.values_list=[],t.args.data=this._parseServerRes(e)}else if("on_update"===t.type||"on_create"===t.type){const e=JSON.parse(t.args.data);let n,r=this._parseServerRes(e);n="on_create"===t.type?this.on_create:this.on_update,n(r),t.args.data=r}let e=t.data?.instance;e&&(t.data.instance=JSON.parse(t.data.instance)),this.open_request.hasOwnProperty(t.event_id)&&(this.open_request[t.event_id][0](t),delete this.open_request[t.event_id])}}_connectToServer(){return new Promise((e=>{const t=`${this.model_name}`+(this.model_id>0?`/${this.model_id}`:"");"https:"===window.location.protocol?this.socket=new WebSocket(`wss://${window.location.host}/sdc_ws/model/${t}`):this.socket=new WebSocket(`ws://${window.location.host}/sdc_ws/model/${t}`),this.socket.onmessage=this._onMessage.bind(this),this.socket.onclose=e=>{console.error(`SDC Model (${this.model_name}, ${this.model_id}) Socket closed unexpectedly`),this._is_connected=!1;for(const[t,n]of Object.entries(this.open_request))n[1](e);this.open_request={},setTimeout((()=>{this._auto_reconnect&&this._connectToServer().then((()=>{}))}),1e3)},this.socket.onerror=e=>{if(console.error(`Model Socket encountered error: ${e} Closing socket`),this._is_connected)try{this.socket.close()}catch(e){}},this.socket.onopen=()=>{e()}}))}_checkConnection(){const e=f();return new Promise(((t,n)=>{this.socket.send(JSON.stringify({event:"model",event_type:"connect",event_id:e,args:{model_name:this.model_name,model_query:this.model_query}})),this.open_request[e]=[t,n]}))}_parseServerRes(e){let t=[];for(let n of e){const e=n.pk,r=this.byPk(e);for(const[e,t]of Object.entries(n.fields))t&&"object"==typeof t&&t.__is_sdc_model__?r[e]=new K(t.pk,t.model):r[e]=t;t.push(r)}return 1===this.values_list.length?this.values=this.values_list.at(-1):this.values={},t}}let z=!1,X=!1,Q=null,Y={};function ee(){return window.SERVER_CALL_VIA_WEB_SOCKET?new Promise((e=>z?e():X?void setTimeout((()=>{ee().then((()=>{e()}))}),200):e(ne()))):Promise.resolve(!0)}function te(){window.SERVER_CALL_VIA_WEB_SOCKET&&function(){if(z){z=!1;try{Q.close()}catch(e){}}}()}function ne(){return X=!0,new Promise((e=>{Q="https:"===window.location.protocol?new WebSocket(`wss://${window.location.host}/sdc_ws/ws/`):new WebSocket(`ws://${window.location.host}/sdc_ws/ws/`),Q.onmessage=function(e){let t=JSON.parse(e.data);if(t.is_error)(t.msg||t.header)&&F("pushErrorMsg",t.header||"",t.msg||""),Y[t.id]&&(Y[t.id][1](t.data||null),delete Y[t.id]);else if((t.msg||t.header)&&F("pushMsg",t.header||"",t.msg||""),t.type&&"sdc_recall"===t.type)Y[t.id]&&(Y[t.id][0](t.data),delete Y[t.id]);else if(t.type&&"sdc_event"===t.type){let e=t.event;e&&F(e,t.payload)}else t.type&&"sdc_redirect"===t.type&&F("onNavLink",t.link)},Q.onclose=function(){z&&console.error("SDC Socket closed unexpectedly"),z=!1;for(const[e,t]of Object.entries(Y))t[1]({}),delete Y[e];setTimeout((()=>{ne()}),1e3)},Q.onerror=function(e){if(console.error("Socket encountered error: ",e.message,"Closing socket"),z)try{Q.close()}catch(e){}},Q.onopen=function(){z=!0,X=!1,e()}}))}class re{constructor(){this._uuid=f(),this.contentUrl="",this.contentReload=!1,this.events=[],this.load_async=!1,this._isEventsSet=!1,this._allEvents=null,this._urlParams=[],this._models=[],this._cssUrls=[],this.afterShow=()=>{console.warn("afterShow is deprecated!!")},this._mixins={},this._tagName="",this._childController={},this._parentController=null,this._onLoadDone=!1,this.$container=null,this._isMixin=!1}_runLifecycle(e,t){he.DEBUG&&console.debug(e,this._tagName);let n=[];if(!this._isMixin){this._isMixin=!0;for(let r in this._mixins){let o=this._mixins[r];"function"==typeof o[e]&&n.push(o[e].apply(this,t))}return Promise.all(n).then((()=>{this._isMixin=!1}))}}onInit(){he.DEBUG&&console.DEBUG(Array.apply(null,arguments),this._tagName)}get parentController(){return this._parentController}get childController(){return this._childController}onLoad(){return this._runLifecycle("onLoad",arguments)}willShow(){return this._runLifecycle("willShow",arguments)}onRefresh(){return this._runLifecycle("onRefresh",arguments)}onRemove(){return this._runLifecycle("onRemove",arguments),!0}remove(){for(const e of this._models)e.close();let e=this._childController;for(let t in e)if(e.hasOwnProperty(t))for(let n of e[t])if(!n.remove())return!1;if(!this.onRemove||this.onRemove()){q(this);const e=a(this._tagName);if(this._parentController._childController[e]){let t=this._parentController._childController[e];for(let e=0;e<t.length;e++)t[e]===this&&t.splice(e,1)}return this.$container.remove(),!0}return!1}controller_name(){return this._tagName.replace(/-./g,(e=>` ${e[1].toUpperCase()}`)).replace(/^./g,(e=>`${e.toUpperCase()}`))}addEvent(e,t,n){this.getEvents(),this._allEvents[e]=this._allEvents[e]||{},this._allEvents[e][t]=n}getEvents(){if(this._allEvents)return this._allEvents;let e=[];e=e.concat(this.events);for(let t in this._mixins){let n=this._mixins[t];Array.isArray(n.events)&&(e=e.concat(n.events))}return this._allEvents=_.merge(...e)}post(e,t){return he.post(this,e,t)}get(e,t){return he.get(this,e,t)}submitForm(e,t,n){return he.submitFormAndUpdateView(this,e,t,n)}serverCall(e,t){let n=this.contentUrl.match(/sdc_view\/([^/]+)/i);if(n&&!(n.length<2))return function(e,t,n,r,o){return window.SERVER_CALL_VIA_WEB_SOCKET?function(e,t,n,r){let o=f();return ee().then((()=>{Q.send(JSON.stringify({event:"sdc_call",id:o,controller:t,app:e,function:n,args:r}))})),new Promise(((e,t)=>{Y[o]=[e,t]}))}(e,t,r,o):function(e,t,n){return"object"!=typeof n&&Array.isArray(n)&&null===n&&(n={arg0:n}),n={data:JSON.stringify(n),_sdc_func_name:t,_method:"sdc_server_call"},$.post({url:e,data:n,beforeSend:function(e,t){e.setRequestHeader("X-CSRFToken",window.CSRF_TOKEN)}}).then((e=>e._return_data))}(n,r,o)}(n[1],this._tagName,this.parsedContentUrl,e,t);console.error("To use the serverCall function the contentUrl must be set: "+this.name)}newModel(e,t={}){if(e instanceof Number&&e.hasOwnProperty("load"))return e.load(this);const n=new Z(e,t);return this._models.push(n),n}updateModel(e,t={},n){let r=new Z(e,t);return r.load().then((()=>{r.values|=n,r.save().then((()=>(r.close(),r.values)))}))}find(e){return this.$container.find(e)}refresh(){return he.refresh(this.$container,this)}reload(){return he.reloadController(this)}submitModelFormDistributor(e,t){return"function"==typeof this._submitModelForm?this._submitModelForm(e,t):"function"==typeof this.submitModelForm?this.submitModelForm(e,t):this.defaultSubmitModelForm(e,t)}iterateAllChildren(){let e=this._childController,t=[];for(let n in e)if(e.hasOwnProperty(n))for(let r of e[n])t.push(r),t.push(...r.iterateAllChildren());return t}defaultSubmitModelForm(e,t){let n=[];if(!this._isMixin){t.stopPropagation(),t.preventDefault();let r=e.data("model");const o=r.syncForm(e);for(let t of o)n.push(new Promise(((n,o)=>{let s;s=null!==t.pk&&t.pk>=0?r.save(t.pk):r.create(t),s.then((t=>{m(e),this.submit_model_form_success&&this.submit_model_form_success(t[0]);for(const e of this.iterateAllChildren())e.submit_model_form_success&&e.submit_model_form_success(t[0]);n(t)})).catch((t=>{p(e,$(t.html)),this.submit_model_form_error&&this.submit_model_form_error(t);for(const e of this.iterateAllChildren())e.submit_model_form_error&&e.submit_model_form_error(t);o(t)}))})))}return Promise.all(n).then((e=>Object.assign({},...e.flat())))}}const oe=["onbeforeunload"],se=Object.keys(window).filter((e=>/^on/.test(e)&&!oe.includes(e))).map((e=>e.slice(2)));function ie(e){let t=e.type;e.hasOwnProperty("namespace")&&e.namespace&&e.namespace.length&&(t+=`.${e.namespace}`);let n=$(e.target),r=null,o=!1,s=!1;for(e.stopImmediatePropagation=()=>s=!0,e.stopPropagation=()=>s=o=!0;n.length;){let i=n.attr(`sdc_${t}`);if(i){if(!r&&(r=U(n),!r))return;for(;r;){if(i.split(" ").forEach((s=>{if(o)return;let i=null;if("function"==typeof s)i=s;else if("function"==typeof r[s])i=r[s];else if("string"==typeof s&&s.startsWith("this.event_")){if(i=r.getEvents()[t],!i)return;if(i=i[s.slice(11)],!i)return}i&&i.call(r,n,e)})),s)return;r=r._parentController}}if(o)return;n=n.parent()}return{res:!0}}function le(e){if(e._isEventsSet)return;const t=e.getEvents();for(let n in t)if(t.hasOwnProperty(n)){let r=t[n];for(let t in r)r.hasOwnProperty(t)&&e.find(t).each((function(){let e=$(this),r=e.attr(`sdc_${n}`)||null;r=r?r.split(" "):[];const o=`this.event_${t}`;-1===r.indexOf(o)&&(r.push(o),e.attr(`sdc_${n}`,r.join(" ")))}))}}const ae={classname:"class"};window.sdcDom=function(e,t,...n){if(!e)return"";const r=function(e,t){let n,r=!1;if("string"==typeof e)n=$(document.createElement(e));else{const t=`this.${e.name}`;n=$(document.createElement(t)),n.data("handler",e),r=!0}return t&&Object.entries(t).forEach((([e,t])=>{e.startsWith("on")?n[0].addEventListener(e.substring(2).toLowerCase(),t):(ae[e.toLowerCase()]&&(e=ae[e.toLowerCase()]),n[0].setAttribute(e,t))})),r&&n.addClass("_bind_to_update_handler _with_handler"),n}(e,t);for(const e of n)r.append(e);return r};let ce,ue,he={CSRF_TOKEN:window.CSRF_TOKEN||"",LANGUAGE_CODE:window.LANGUAGE_CODE||"en",DEBUG:window.DEBUG||!1,VERSION:window.VERSION||"0.0",tagNames:[],Global:v,rootController:null,_isInit:!1,_origin_trigger:null,init_sdc:()=>{he._isInit||(he._isInit=!0,he._origin_trigger?te():(he._origin_trigger=$.fn.trigger,$.fn.trigger=function(e){const t={}.hasOwnProperty.call(e,"type")?e.type:e;return se.includes(t)||(se.push(t),$(window).on(t,ie)),he._origin_trigger.call(this,e)},he.updateJquery()),ee(),function(){const e=$(window);se.forEach((t=>{e.off(t).on(t,ie)}))}(),he.rootController=he.rootController||new re),he.tagNames=C();for(let[e,t]of Object.entries(he.Global))t.$container||(v[e].$container=s());return V(he.tagNames,s(),he.rootController)},updateJquery:()=>{$.fn.safeReplace=function(e){return he.safeReplace($(this),e)},$.fn.safeEmpty=function(){return he.safeEmpty($(this))},$.fn.safeRemove=function(){return he.safeRemove($(this))}},controllerToTag:e=>l(e.name).replace(/-controller$/,""),registerGlobal:e=>{let t=he.controllerToTag(e),n=new e;w[t]=[n,[]],n._tagName=t,window[a(t)]=v[a(t)]=n},cleanCache:()=>{x={}},register:e=>{let t=he.controllerToTag(e);return w[t]=[e,[]],e.prototype._tagName=t,{addMixin:(...e)=>{for(let n of e){let e;"string"==typeof n?e=l(n):n&&(e=he.controllerToTag(n)),w[t][1].push(e)}}}},post:(e,t,n)=>(n||(n={}),n.CSRF_TOKEN=he.CSRF_TOKEN,he.ajax(e,t,params,$.post)),get:(e,t,n)=>he.ajax(e,t,n,$.get),ajax:(e,t,n,r)=>{n||(n={}),n.VERSION=he.VERSION,n._method=n._method||"api";const o=new Promise(((s,i)=>r(t,n).then(((t,n,r)=>{s(t,n,r),"redirect"===t.status?F("onNavLink",t["url-link"]):o.then((()=>{he.refresh(e.$container)}))})).catch(i)));return o},submitFormAndUpdateView:(e,t,n,r)=>{let o=new FormData(t);const s=e=>{e["url-link"]?F("onNavLink",e["url-link"]):window.location.href=e.url},i=new Promise(((l,a)=>{u(o,n||t.action,r||t.method).then(((t,n,r)=>{l(t,n,r),"redirect"===t.status?s(t):i.then((()=>{he.refresh(e.$container)}))})).catch(((e,t,n)=>{301===e.status?(e=e.responseJSON,s(e),l(e,t,n)):a(e,t,n)}))}));return i},submitForm:(e,t,n)=>{let r=new FormData(e);return new Promise(((o,s)=>{u(r,t||e.action,n||e.method).then(o).catch(s)}))},getController:e=>U(e),safeEmpty:e=>(e.children().each((function(e,t){let n=$(t);he.safeRemove(n)})),e),safeReplace:(e,t)=>(t.insertBefore(e),he.safeRemove(e)),safeRemove:e=>(e.each((function(){let e=$(this);e.data(`${T}`)&&e.data(`${T}`).remove()})),e.find(`.${A}`).each((function(){const e=$(this).data(`${T}`);e&&e.remove()})),e.remove()),reloadController:e=>function(e){if(e.contentUrl){let t=M(e,e.contentUrl);return e.contentUrl=t.url,j(e.contentUrl,t.args,e._tagName,e.contentReload)}return new Promise((e=>{e($())}))}(e).then((t=>{let n=$(t);e._childController={},V(he.tagNames,n,e).then((()=>{he.safeEmpty(e.$container),e.$container.append(n),he.refresh(e.$container,e)}))})),refresh:(e,t)=>{if(t||(t=he.getController(e)),!t)return i();let n=t,r=[];for(;n;)n._isEventsSet=!1,r.unshift(n),n=n._parentController;return V(he.tagNames,t.$container,t).then((()=>{B(t).then((()=>{for(let e of r)le(e);t.onRefresh(e)}))}))}};const de=te,_e={get_controller:async function(e,t={},n=""){if(!jest)throw new Error("JEST is not defined");ce||(ue=$.ajax.bind($),ce=jest.spyOn($,"ajax"),ce.mockImplementation((function(e){return ue(e).then((e=>e)).catch((e=>e))})));const r=$("body");he.updateJquery(),r.safeEmpty();const o=$(`<${e}>${n}</${e}>`);for(const[e,n]of Object.entries(t))o.data(e,n);const s=$("<div></div>").append(o);return r.append(s),he._isInit=!1,he.cleanCache(),await he.init_sdc(),he.getController(o)},getCsrfToken:function(){return function(e){if(document.cookie&&""!==document.cookie){const t=document.cookie.split(";");for(let n=0;n<t.length;n++){const r=t[n].trim();if(r.substring(0,10)===e+"=")return decodeURIComponent(r.substring(10))}}return""}("csrftoken")}};var fe=t.n$,me=t.du,pe=t.yA,ge=t.g3,ye=t.u2,ve=t.n9,we=t.on,$e=t.yy,Ce=t.R6,be=t.$n,ke=t.hA,Oe=t.VB,Se=t.hZ;export{fe as AbstractSDC,me as allOff,pe as app,ge as checkIfParamNumberBoolOrString,ye as clearErrorsInForm,ve as controllerFactory,we as on,$e as runControlFlowFunctions,Ce as setErrorsInForm,be as setEvent,ke as socketReconnect,Oe as test_utils,Se as trigger};
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "sdc_client",
3
- "version": "0.56.4",
3
+ "version": "0.56.5",
4
4
  "description": "Simple Dom Control",
5
- "main": "dist/ugly.index.js",
5
+ "main": "src/index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "build": "gulp development & gulp build",
package/src/index.js CHANGED
@@ -3,11 +3,11 @@ import {AbstractSDC} from './simpleDomControl/AbstractSDC.js';
3
3
  import {on, trigger, allOff, setEvent} from './simpleDomControl/sdc_events.js';
4
4
  import {clearErrorsInForm, setErrorsInForm, checkIfParamNumberBoolOrString} from './simpleDomControl/sdc_utils.js';
5
5
  import {controllerFactory, runControlFlowFunctions} from './simpleDomControl/sdc_controller.js';
6
- import {close} from './simpleDomControl/sdc_socket.js'
7
- import {get_controller} from './simpleDomControl/sdc_test_utils.js'
6
+ import {close} from './simpleDomControl/sdc_server_call.js'
7
+ import {get_controller, getCsrfToken} from './simpleDomControl/sdc_test_utils.js'
8
8
 
9
9
  const socketReconnect = close
10
- const test_utils = {get_controller};
10
+ const test_utils = {get_controller, getCsrfToken};
11
11
 
12
12
  export {
13
13
  app, AbstractSDC, on, trigger, allOff, setEvent, clearErrorsInForm, setErrorsInForm, checkIfParamNumberBoolOrString,
@@ -1,6 +1,7 @@
1
1
  import {allOff} from "./sdc_events.js";
2
2
  import {app} from "./sdc_main.js";
3
- import {callServer, Model} from "./sdc_socket.js";
3
+ import {Model} from "./sdc_socket.js";
4
+ import {callServer} from "./sdc_server_call.js";
4
5
  import {uuidv4, setErrorsInForm, clearErrorsInForm, tagNameToCamelCase, tagNameToReadableName} from "./sdc_utils.js";
5
6
 
6
7
  export class AbstractSDC {
@@ -202,7 +203,7 @@ export class AbstractSDC {
202
203
  return;
203
204
  }
204
205
 
205
- return callServer(app[1], this._tagName, methode, args);
206
+ return callServer(app[1], this._tagName, this.parsedContentUrl, methode, args);
206
207
  }
207
208
 
208
209
  /**
@@ -15,7 +15,7 @@ import {AbstractSDC} from "./AbstractSDC.js";
15
15
  import {Global, controllerList, tagList} from "./sdc_controller.js";
16
16
  import {initEvents, setControllerEvents, STD_EVENT_LIST, windowEventHandler} from "./sdc_dom_events.js";
17
17
  import {trigger} from "./sdc_events.js";
18
- import {isConnected, close} from "./sdc_socket.js";
18
+ import {isConnected, close} from "./sdc_server_call.js";
19
19
 
20
20
  const PROPERTIES_UPDATE = {'classname': 'class'}
21
21
 
@@ -0,0 +1,171 @@
1
+ import {uuidv4} from "./sdc_utils.js";
2
+ import {trigger} from "./sdc_events.js";
3
+
4
+ let IS_CONNECTED = false;
5
+ let IS_CONNECTING = false;
6
+ let SDC_SOCKET = null
7
+ let OPEN_REQUESTS = {};
8
+
9
+ export function callServer(app, controller, parsedContentUrl, funcName, args) {
10
+ if (window.SERVER_CALL_VIA_WEB_SOCKET) {
11
+ return socketCallServer(app, controller, funcName, args);
12
+ } else {
13
+ return postCallServer(parsedContentUrl, funcName, args);
14
+ }
15
+
16
+ }
17
+
18
+ export function isConnected() {
19
+ if (window.SERVER_CALL_VIA_WEB_SOCKET) {
20
+ return socketIsConnected();
21
+ } else {
22
+ return Promise.resolve(true);
23
+ }
24
+ }
25
+
26
+ export function close() {
27
+ if (window.SERVER_CALL_VIA_WEB_SOCKET) {
28
+ socketClose();
29
+ }
30
+ }
31
+
32
+ function postCallServer(parsedContentUrl, funcName, args) {
33
+ if(typeof args !== 'object' && Array.isArray(args) && args === null) {
34
+ args = {'arg0': args}
35
+ }
36
+ args = {
37
+ 'data': JSON.stringify(args),
38
+ '_sdc_func_name': funcName, '_method': 'sdc_server_call'
39
+ }
40
+ return $.post({
41
+ url: parsedContentUrl,
42
+ data: args,
43
+ beforeSend: function (xhr, settings) {
44
+ xhr.setRequestHeader("X-CSRFToken", window.CSRF_TOKEN);
45
+ }
46
+ }).then((res) => res['_return_data']);
47
+ }
48
+
49
+ function socketCallServer(app, controller, funcName, args) {
50
+
51
+ let id = uuidv4();
52
+ isConnected().then(() => {
53
+ SDC_SOCKET.send(JSON.stringify({
54
+ event: 'sdc_call',
55
+ id: id,
56
+ controller: controller,
57
+ app: app,
58
+ function: funcName,
59
+ args: args
60
+ }));
61
+ });
62
+
63
+ return new Promise((resolve, reject) => {
64
+ OPEN_REQUESTS[id] = [resolve, reject];
65
+ });
66
+ }
67
+
68
+ function _connect() {
69
+ IS_CONNECTING = true;
70
+ return new Promise((resolve) => {
71
+ if (window.location.protocol === "https:") {
72
+ SDC_SOCKET = new WebSocket(`wss://${window.location.host}/sdc_ws/ws/`);
73
+ } else {
74
+ SDC_SOCKET = new WebSocket(`ws://${window.location.host}/sdc_ws/ws/`);
75
+ }
76
+
77
+
78
+ SDC_SOCKET.onmessage = function (e) {
79
+ let data = JSON.parse(e.data);
80
+ if (data.is_error) {
81
+ if (data.msg || data.header) {
82
+ trigger('pushErrorMsg', data.header || '', data.msg || '');
83
+ }
84
+ if (OPEN_REQUESTS[data.id]) {
85
+ OPEN_REQUESTS[data.id][1](data.data || null);
86
+ delete OPEN_REQUESTS[data.id];
87
+ }
88
+ } else {
89
+ if (data.msg || data.header) {
90
+ trigger('pushMsg', data.header || '', data.msg || '');
91
+ }
92
+
93
+ if (data.type && data.type === 'sdc_recall') {
94
+ if (OPEN_REQUESTS[data.id]) {
95
+ OPEN_REQUESTS[data.id][0](data.data);
96
+ delete OPEN_REQUESTS[data.id];
97
+ }
98
+ } else if (data.type && data.type === 'sdc_event') {
99
+ let event = data.event;
100
+ if (event) {
101
+ trigger(event, data.payload);
102
+ }
103
+
104
+ } else if (data.type && data.type === 'sdc_redirect') {
105
+ trigger('onNavLink', data.link);
106
+ }
107
+ }
108
+ };
109
+
110
+ SDC_SOCKET.onclose = function () {
111
+ if (IS_CONNECTED) {
112
+ console.error('SDC Socket closed unexpectedly');
113
+ }
114
+ IS_CONNECTED = false;
115
+ for (const [key, value] of Object.entries(OPEN_REQUESTS)) {
116
+ value[1]({});
117
+ delete OPEN_REQUESTS[key];
118
+ }
119
+
120
+ setTimeout(() => {
121
+ _connect();
122
+ }, 1000);
123
+ };
124
+
125
+ SDC_SOCKET.onerror = function (err) {
126
+ console.error('Socket encountered error: ', err.message, 'Closing socket');
127
+ if (IS_CONNECTED) {
128
+ try {
129
+ SDC_SOCKET.close();
130
+ } catch (e) {
131
+
132
+ }
133
+ }
134
+ };
135
+
136
+ SDC_SOCKET.onopen = function () {
137
+ IS_CONNECTED = true;
138
+ IS_CONNECTING = false;
139
+ resolve();
140
+ }
141
+ })
142
+ }
143
+
144
+ function socketClose() {
145
+ if (IS_CONNECTED) {
146
+ IS_CONNECTED = false;
147
+ try {
148
+ SDC_SOCKET.close();
149
+ } catch (e) {
150
+
151
+ }
152
+
153
+ }
154
+ }
155
+
156
+ function socketIsConnected() {
157
+
158
+ return new Promise((resolve) => {
159
+ if (IS_CONNECTED) {
160
+ return resolve();
161
+ } else if (IS_CONNECTING) {
162
+ setTimeout(() => {
163
+ isConnected().then(() => {
164
+ resolve();
165
+ });
166
+ }, 200);
167
+ } else {
168
+ return resolve(_connect());
169
+ }
170
+ });
171
+ }
@@ -2,11 +2,7 @@ import {app} from './sdc_main.js';
2
2
  import {trigger} from "./sdc_events.js";
3
3
  import {uuidv4} from "./sdc_utils.js";
4
4
 
5
- let IS_CONNECTED = false;
6
- let IS_CONNECTING = false;
7
- let SDC_SOCKET = null
8
5
  const MAX_FILE_UPLOAD = 25000;
9
- let OPEN_REQUESTS = {};
10
6
 
11
7
 
12
8
  class SubModel {
@@ -74,113 +70,8 @@ const ModelProxyHandler = {
74
70
  }
75
71
  }
76
72
 
77
- export function callServer(app, controller, funcName, args) {
78
-
79
- let id = uuidv4();
80
- isConnected().then(() => {
81
- SDC_SOCKET.send(JSON.stringify({
82
- event: 'sdc_call',
83
- id: id,
84
- controller: controller,
85
- app: app,
86
- function: funcName,
87
- args: args
88
- }));
89
- });
90
-
91
- return new Promise((resolve, reject) => {
92
- OPEN_REQUESTS[id] = [resolve, reject];
93
- });
94
- }
95
-
96
- function _connect() {
97
- IS_CONNECTING = true;
98
- return new Promise((resolve) => {
99
- if (window.location.protocol === "https:") {
100
- SDC_SOCKET = new WebSocket(`wss://${window.location.host}/sdc_ws/ws/`);
101
- } else {
102
- SDC_SOCKET = new WebSocket(`ws://${window.location.host}/sdc_ws/ws/`);
103
- }
104
73
 
105
74
 
106
- SDC_SOCKET.onmessage = function (e) {
107
- let data = JSON.parse(e.data);
108
- if (data.is_error) {
109
- if (data.msg || data.header) {
110
- trigger('pushErrorMsg', data.header || '', data.msg || '');
111
- }
112
- if (OPEN_REQUESTS[data.id]) {
113
- OPEN_REQUESTS[data.id][1](data.data || null);
114
- delete OPEN_REQUESTS[data.id];
115
- }
116
- } else {
117
- if (data.msg || data.header) {
118
- trigger('pushMsg', data.header || '', data.msg || '');
119
- }
120
-
121
- if (data.type && data.type === 'sdc_recall') {
122
- if (OPEN_REQUESTS[data.id]) {
123
- OPEN_REQUESTS[data.id][0](data.data);
124
- delete OPEN_REQUESTS[data.id];
125
- }
126
- } else if (data.type && data.type === 'sdc_event') {
127
- let event = data.event;
128
- if (event) {
129
- trigger(event, data.payload);
130
- }
131
-
132
- } else if (data.type && data.type === 'sdc_redirect') {
133
- trigger('onNavLink', data.link);
134
- }
135
- }
136
- };
137
-
138
- SDC_SOCKET.onclose = function () {
139
- if (IS_CONNECTED) {
140
- console.error('SDC Socket closed unexpectedly');
141
- }
142
- IS_CONNECTED = false;
143
- for (const [key, value] of Object.entries(OPEN_REQUESTS)) {
144
- value[1]({});
145
- delete OPEN_REQUESTS[key];
146
- }
147
-
148
- setTimeout(() => {
149
- _connect();
150
- }, 1000);
151
- };
152
-
153
- SDC_SOCKET.onerror = function (err) {
154
- console.error('Socket encountered error: ', err.message, 'Closing socket');
155
- if (IS_CONNECTED) {
156
- try {
157
- SDC_SOCKET.close();
158
- } catch (e) {
159
-
160
- }
161
- }
162
- };
163
-
164
- SDC_SOCKET.onopen = function () {
165
- IS_CONNECTED = true;
166
- IS_CONNECTING = false;
167
- resolve();
168
- }
169
- })
170
- }
171
-
172
- export function close() {
173
- if (IS_CONNECTED) {
174
- IS_CONNECTED = false;
175
- try {
176
- SDC_SOCKET.close();
177
- } catch (e) {
178
-
179
- }
180
-
181
- }
182
- }
183
-
184
75
  function parse_hidden_inputs(value) {
185
76
 
186
77
  let isFloatReg = /^-?\d+\.?\d+$/;
@@ -205,22 +96,7 @@ function parse_hidden_inputs(value) {
205
96
  return value;
206
97
  }
207
98
 
208
- export function isConnected() {
209
99
 
210
- return new Promise((resolve) => {
211
- if (IS_CONNECTED) {
212
- return resolve();
213
- } else if (IS_CONNECTING) {
214
- setTimeout(() => {
215
- isConnected().then(() => {
216
- resolve();
217
- });
218
- }, 200);
219
- } else {
220
- return resolve(_connect());
221
- }
222
- });
223
- }
224
100
 
225
101
  export class Model {
226
102
  /**
@@ -5,6 +5,28 @@
5
5
  import {app} from './sdc_main.js';
6
6
  let spy, _originAjax;
7
7
 
8
+ function getCookie(name) {
9
+ let cookieValue = null;
10
+ if (document.cookie && document.cookie !== '') {
11
+ const cookies = document.cookie.split(';');
12
+ for (let i = 0; i < cookies.length; i++) {
13
+ const cookie = cookies[i].trim();
14
+ // Does this cookie string begin with the name we want?
15
+ if (cookie.substring(0, name.length + 1) === (name + '=')) {
16
+ return decodeURIComponent(cookie.substring(name.length + 1));
17
+ }
18
+ }
19
+ }
20
+ return '';
21
+ }
22
+
23
+ /**
24
+ * Returns the CSRF token
25
+ */
26
+ export function getCsrfToken() {
27
+ return getCookie('csrftoken');
28
+ }
29
+
8
30
  /**
9
31
  * Returns a controller. This controller has been created by the using the normal SDC life cycle.
10
32
  *