diff --git a/django_unicorn/static/js/unicorn.min.js b/django_unicorn/static/js/unicorn.min.js index 4ad1878c..09a3c174 100644 --- a/django_unicorn/static/js/unicorn.min.js +++ b/django_unicorn/static/js/unicorn.min.js @@ -1 +1 @@ -var Unicorn=function(e){"use strict";function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(e,t){for(var n=0;n-1}function u(e,t){for(var n=document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT,null,!1);n.nextNode();)t(n.currentNode)}function c(e,t,n){var i,r=this;return void 0===n&&(n=!0),function(){for(var o=arguments.length,a=new Array(o),s=0;s1)||n[1],e.eventType&&(e.eventType=e.eventType.replace(".".concat(t),""))}))}}}]),e}(),f=function(){function e(n){t(this,e),this.el=n,this.init()}return i(e,[{key:"init",value:function(){var t=this;if(this.id=this.el.id,this.isUnicorn=!1,this.attributes=[],this.value=this.getValue(),this.parent=null,this.el.parentElement&&(this.parent=new e(this.el.parentElement)),this.model={},this.poll={},this.actions=[],this.db={},this.field={},this.key=null,this.errors=[],this.el.attributes){for(var n=0;n0&&(this.poll.timing=parseInt(a[0],10)||2e3)}else i.eventType&&function(){var e={};e.name=i.value,e.eventType=i.eventType,e.isPrevent=!1,e.isStop=!1,i.modifiers&&Object.keys(i.modifiers).forEach((function(t){"prevent"===t?e.isPrevent=!0:"stop"===t?e.isStop=!0:e.key=t})),t.actions.push(e)}();if(i.isKey&&(this.key=i.value),i.isError){var s=i.name.replace("unicorn:error:","");this.errors.push({code:s,message:i.value})}}if(this.isUnicorn&&!r(this.field)){var l=this;for(["pk","name"].forEach((function(e){for(l=t;r(t.db[e])&&!l.el.getAttribute("unicorn:checksum");)l.isUnicorn&&o(l.db[e])&&(t.db[e]=l.db[e]),l=l.parent})),l=this;r(this.model.name)&&!l.el.getAttribute("unicorn:checksum");)l.isUnicorn&&o(l.model.name)&&(this.model.name=l.model.name),l=l.parent}}}},{key:"focus",value:function(){this.el.focus()}},{key:"dbKey",value:function(){return s(this)}},{key:"isSame",value:function(e){return this.el.isSameNode(e.el)}},{key:"getValue",value:function(){var e=this.el.value;if(this.el.type)if("checkbox"===this.el.type.toLowerCase())e=this.el.checked;else if("select-multiple"===this.el.type.toLowerCase()){e=[];for(var t=0;t=97?r===o.toUpperCase():i<=90&&n>=97&&o===r.toUpperCase())}function g(e,t,n){e[n]!==t[n]&&(e[n]=t[n],e[n]?e.setAttribute(n,""):e.removeAttribute(n))}var E={OPTION:function(e,t){var n=e.parentNode;if(n){var i=n.nodeName.toUpperCase();"OPTGROUP"===i&&(i=(n=n.parentNode)&&n.nodeName.toUpperCase()),"SELECT"!==i||n.hasAttribute("multiple")||(e.hasAttribute("selected")&&!t.selected&&(e.setAttribute("selected","selected"),e.removeAttribute("selected")),n.selectedIndex=-1)}g(e,t,"selected")},INPUT:function(e,t){g(e,t,"checked"),g(e,t,"disabled"),e.value!==t.value&&(e.value=t.value),t.hasAttribute("value")||e.removeAttribute("value")},TEXTAREA:function(e,t){var n=t.value;e.value!==n&&(e.value=n);var i=e.firstChild;if(i){var r=i.nodeValue;if(r==n||!n&&r==e.placeholder)return;i.nodeValue=n}},SELECT:function(e,t){if(!t.hasAttribute("multiple")){for(var n,i,r=-1,o=0,a=e.firstChild;a;)if("OPTGROUP"===(i=a.nodeName&&a.nodeName.toUpperCase()))a=(n=a).firstChild;else{if("OPTION"===i){if(a.hasAttribute("selected")){r=o;break}o++}!(a=a.nextSibling)&&n&&(a=n.nextSibling,n=null)}e.selectedIndex=r}}};function k(){}function T(e){if(e)return e.getAttribute&&e.getAttribute("id")||e.id}var N=function(e){return function(t,n,i){if(i||(i={}),"string"==typeof n)if("#document"===t.nodeName||"HTML"===t.nodeName||"BODY"===t.nodeName){var r=n;(n=m.createElement("html")).innerHTML=r}else n=y(n);var o=i.getNodeKey||T,a=i.onBeforeNodeAdded||k,s=i.onNodeAdded||k,l=i.onBeforeElUpdated||k,u=i.onElUpdated||k,c=i.onBeforeNodeDiscarded||k,d=i.onNodeDiscarded||k,h=i.onBeforeElChildrenUpdated||k,f=!0===i.childrenOnly,v=Object.create(null),p=[];function g(e){p.push(e)}function N(e,t){if(1===e.nodeType)for(var n=e.firstChild;n;){var i=void 0;t&&(i=o(n))?g(i):(d(n),n.firstChild&&N(n,t)),n=n.nextSibling}}function A(e,t,n){!1!==c(e)&&(t&&t.removeChild(e),d(e),N(e,n))}function S(e){s(e);for(var t=e.firstChild;t;){var n=t.nextSibling,i=o(t);if(i){var r=v[i];r&&b(t,r)?(t.parentNode.replaceChild(r,t),C(r,t)):S(t)}else S(t);t=n}}function C(t,n,i){var r=o(n);if(r&&delete v[r],!i){if(!1===l(t,n))return;if(e(t,n),u(t),!1===h(t,n))return}"TEXTAREA"!==t.nodeName?function(e,t){var n,i,r,s,l,u=t.firstChild,c=e.firstChild;e:for(;u;){for(s=u.nextSibling,n=o(u);c;){if(r=c.nextSibling,u.isSameNode&&u.isSameNode(c)){u=s,c=r;continue e}i=o(c);var d=c.nodeType,h=void 0;if(d===u.nodeType&&(1===d?(n?n!==i&&((l=v[n])?r===l?h=!1:(e.insertBefore(l,c),i?g(i):A(c,e,!0),c=l):h=!1):i&&(h=!1),(h=!1!==h&&b(c,u))&&C(c,u)):3!==d&&8!=d||(h=!0,c.nodeValue!==u.nodeValue&&(c.nodeValue=u.nodeValue))),h){u=s,c=r;continue e}i?g(i):A(c,e,!0),c=r}if(n&&(l=v[n])&&b(l,u))e.appendChild(l),C(l,u);else{var f=a(u);!1!==f&&(f&&(u=f),u.actualize&&(u=u.actualize(e.ownerDocument||m)),e.appendChild(u),S(u))}u=s,c=r}!function(e,t,n){for(;t;){var i=t.nextSibling;(n=o(t))?g(n):A(t,e,!0),t=i}}(e,c,i);var p=E[e.nodeName];p&&p(e,t)}(t,n):t.innerHTML!=n.innerHTML&&E.TEXTAREA(t,n)}!function e(t){if(1===t.nodeType||11===t.nodeType)for(var n=t.firstChild;n;){var i=o(n);i&&(v[i]=n),e(n),n=n.nextSibling}}(t);var w,M,U=t,V=U.nodeType,L=n.nodeType;if(!f)if(1===V)1===L?b(t,n)||(d(t),U=function(e,t){for(var n=e.firstChild;n;){var i=n.nextSibling;t.appendChild(n),n=i}return t}(t,(w=n.nodeName,(M=n.namespaceURI)&&"http://www.w3.org/1999/xhtml"!==M?m.createElementNS(M,w):m.createElement(w)))):U=n;else if(3===V||8===V){if(L===V)return U.nodeValue!==n.nodeValue&&(U.nodeValue=n.nodeValue),U;U=n}if(U===n)d(t);else{if(n.isSameNode&&n.isSameNode(U))return;if(C(U,n,f),p)for(var O=0,P=p.length;O=0;s--)i=(n=a[s]).name,r=n.namespaceURI,o=n.value,r?(i=n.localName||i,e.getAttributeNS(r,i)!==o&&("xmlns"===n.prefix&&(i=n.name),e.setAttributeNS(r,i,o))):e.getAttribute(i)!==o&&e.setAttribute(i,o);for(var l=e.attributes,u=l.length-1;u>=0;u--)i=(n=l[u]).name,(r=n.namespaceURI)?(i=n.localName||i,t.hasAttributeNS(r,i)||e.removeAttributeNS(r,i)):t.hasAttribute(i)||e.removeAttribute(i)}})),A={childrenOnly:!1,getNodeKey:function(e){if(e.attributes){var t=e.getAttribute("unicorn:key")||e.id;if(t)return t}},onBeforeElUpdated:function(e,t){if(e.isEqualNode(t))return!1}};function S(e,t){if(0!==e.actionQueue.length&&e.currentActionQueue!==e.actionQueue){e.currentActionQueue=e.actionQueue,e.actionQueue=[];var n={id:e.id,data:e.data,checksum:e.checksum,actionQueue:e.currentActionQueue},i={Accept:"application/json","X-Requested-With":"XMLHttpRequest"};i[e.csrfTokenHeaderName]=function(){var e="csrftoken=",t=document.cookie.split(";").filter((function(t){return t.trim().startsWith(e)}));if(t.length>0)return t[0].replace(e,"");var n=document.getElementsByName("csrfmiddlewaretoken");if(n&&n.length>0)return n[0].getAttribute("value");throw Error("CSRF token is missing. Do you need to add {% csrf_token %}?")}(),fetch(e.syncUrl,{method:"POST",headers:i,body:JSON.stringify(n)}).then((function(e){if(e.ok)return e.json();throw Error("Error when getting response: ".concat(e.statusText," (").concat(e.status,")"))})).then((function(n){if(n){if(n.error)throw Error(n.error);e.modelEls.forEach((function(e){e.init(),e.removeErrors()})),e.data=n.data||{},e.errors=n.errors||{};var i=n.dom;N(e.root,i,A),e.refreshChecksum(),e.refreshEventListeners(),e.modelEls.forEach((function(t){Object.keys(e.errors).forEach((function(n){if(t.model.name===n){var i=e.errors[n][0];t.addError(i)}}))}));var r=e.lastTriggeringElements;e.lastTriggeringElements=[],e.currentActionQueue=null,a(t)&&t(r,null)}})).catch((function(n){e.actionQueue=[],e.currentActionQueue=null,e.lastTriggeringElements=[],a(t)&&t(null,null,n)}))}}var C=function(){function e(n){if(t(this,e),this.id=n.id,this.name=n.name,this.messageUrl=n.messageUrl,this.csrfTokenHeaderName=n.csrfTokenHeaderName,l(this.name,".")){var i=this.name.split(".");this.name=i[i.length-2]}this.data=n.data,this.syncUrl="".concat(this.messageUrl,"/").concat(this.name),this.document=n.document||document,this.walker=n.walker||u,this.root=void 0,this.modelEls=[],this.dbEls=[],this.errors={},this.poll={},this.actionQueue=[],this.currentActionQueue=null,this.lastTriggeringElements=[],this.actionEvents={},this.attachedEventTypes=[],this.attachedModelEvents=[],this.attachedDbEvents=[],this.init(),this.refreshEventListeners(),this.initPolling()}return i(e,[{key:"init",value:function(){var e,t;if(this.root=(e='[unicorn\\:id="'.concat(this.id,'"]'),void 0===(t=this.document)&&(t=document),t.querySelector(e)),!this.root)throw Error("No id found");this.refreshChecksum()}},{key:"refreshEventListeners",value:function(){var e=this;this.actionEvents={},this.modelEls=[],this.dbEls=[],this.walker(this.root,(function(t){if(!t.isSameNode(e.root)){var n=new f(t);n.isUnicorn&&(o(n.field)&&(o(n.db)||o(n.model))?(e.attachedDbEvents.some((function(e){return e.isSame(n)}))||(e.attachedDbEvents.push(n),function(e,t,n){t.addEventListener(n,(function(t){var n=new f(t.target);if(!(r(n.db.name)&&r(n.model.name)||r(n.db.pk))){e.lastTriggeringElements.some((function(e){return e.isSame(n)}))||e.lastTriggeringElements.push(n);var i={type:"dbInput",payload:{model:n.model.name,db:n.db,fields:{}}};if(i.payload.fields[n.field.name]=n.getValue(),n.field.isDefer){var o=!1;return e.actionQueue.forEach((function(e){s(e.payload)===n.dbKey()&&(e.payload.fields[n.field.name]=n.getValue(),o=!0)})),void(o||e.actionQueue.push(i))}e.actionQueue.push(i),e.queueMessage(n.model.debounceTime,(function(t,n){n?console.error(n):e.setDbModelValues()}))}}))}(e,n.el,n.field.eventType)),e.dbEls.some((function(e){return e.isSame(n)}))||e.dbEls.push(n)):o(n.model)&&r(n.db)&&r(n.field)&&(e.attachedModelEvents.some((function(e){return e.isSame(n)}))||(e.attachedModelEvents.push(n),function(e,t,n){t.addEventListener(n,(function(t){var n=new f(t.target),i={type:"syncInput",payload:{name:n.model.name,value:n.getValue()}};if(e.lastTriggeringElements.some((function(e){return e.isSame(n)}))||e.lastTriggeringElements.push(n),n.model.isDefer){var r=!1;return e.actionQueue.forEach((function(e){e.payload.name===n.model.name&&(e.payload.value=n.getValue(),r=!0)})),void(r||e.actionQueue.push(i))}e.actionQueue.push(i),e.queueMessage(n.model.debounceTime,(function(t,n){n?console.error(n):(e.setModelValues(t),e.setDbModelValues())}))}))}(e,n.el,n.model.eventType)),e.modelEls.some((function(e){return e.isSame(n)}))||e.modelEls.push(n)),n.actions.forEach((function(t){var i,o;e.actionEvents[t.eventType]?e.actionEvents[t.eventType].push({action:t,element:n}):(e.actionEvents[t.eventType]=[{action:t,element:n}],e.attachedEventTypes.some((function(e){return e===t.eventType}))||(e.attachedEventTypes.push(t.eventType),i=e,o=t.eventType,i.document.addEventListener(o,(function(e){var t=new f(e.target);t&&t.isUnicorn&&t.actions.length>0&&i.actionEvents[o].forEach((function(n){var o,a=n.action,s=n.element;t.isSame(s)&&(u(s.el,(function(e){i.modelEls.filter((function(t){return t.el.isSameNode(e)})).forEach((function(e){if(!r(e.model)&&e.model.isLazy){var t={type:"syncInput",payload:{name:e.model.name,value:e.getValue()}};i.actionQueue.push(t)}})),i.dbEls.filter((function(t){return t.el.isSameNode(e)})).forEach((function(e){if(!r(e.model)&&e.model.isLazy){var t={type:"dbInput",payload:{model:e.model.name,db:e.db.name,pk:e.db.pk,fields:{}}};t.payload.fields[e.field.name]=e.getValue(),i.actionQueue.push(t)}}))})),a.isPrevent&&e.preventDefault(),a.isStop&&e.stopPropagation(),a.key?a.key===((o=e.key)?o.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g).map((function(e){return e.toLowerCase()})).join("-"):"")&&i.callMethod(a.name):i.callMethod(a.name))}))}))))})))}}))}},{key:"callMethod",value:function(e,t){var n=this,i={type:"callMethod",payload:{name:e,params:[]}};this.actionQueue.push(i),this.queueMessage(-1,(function(e,i){i&&a(t)?t(i):i?console.error(i):(n.setModelValues(e),n.setDbModelValues())}))}},{key:"initPolling",value:function(){var e=this,t=new f(this.root);t.isUnicorn&&o(t.poll)&&(this.poll=t.poll,this.poll.timer=null,this.document.addEventListener("visibilitychange",(function(){e.document.hidden?e.poll.timer&&clearInterval(e.poll.timer):e.startPolling()}),!1),this.startPolling())}},{key:"startPolling",value:function(){this.poll.timer=null;var e=this.poll.timer;function t(t){t&&console.error(t),e&&clearInterval(e)}this.callMethod(this.poll.method,t),this.poll.timer=setInterval(this.callMethod.bind(this),this.poll.timing,this.poll.method,t)}},{key:"refreshChecksum",value:function(){this.checksum=this.root.getAttribute("unicorn:checksum")}},{key:"setValue",value:function(e){if(!r(e.model))for(var t=e.model.name.split("."),n=this.data,i=0;i0){var n=!1,i=e.slice(-1)[0];o(i)&&o(i.model)&&!i.model.isLazy&&["id","key"].forEach((function(e){t.modelEls.forEach((function(t){n||i[e]&&i[e]===t[e]&&(t.focus(),n=!0)}))}))}this.modelEls.forEach((function(n){var i=!1;e.forEach((function(e){n.isSame(e)||(i=!0)})),(i||0===e.length)&&t.setValue(n)}))}},{key:"queueMessage",value:function(e,t){-1===e?c(S,250,!1)(this,t):c(S,e,!1)(this,t)}}]),e}(),w="",M={};return e.call=function(e,t){var n;if(Object.keys(M).forEach((function(t){if(r(n)){var i=M[t];i.name===e&&(n=i)}})),!n)throw Error("No component found for: ",e);n.callMethod(t,(function(e){console.error(e)}))},e.componentInit=function(e){e.messageUrl=w,e.csrfTokenHeaderName="X-CSRFToken";var t=new C(e);t.init(),M[t.id]=t,t.setModelValues(),t.setDbModelValues()},e.init=function(e){w=e},e}({}); +var Unicorn=function(e){"use strict";function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var i=0;i-1}function u(e,t){return void 0===t&&(t=document),t.querySelector(e)}function d(e,t){for(var i=document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT,null,!1);i.nextNode();)t(i.currentNode)}function c(e,t,i){var n,o=this;return void 0===i&&(i=!0),function(){for(var r=arguments.length,s=new Array(r),a=0;a1)||i[1],e.eventType&&(e.eventType=e.eventType.replace(".".concat(t),""))}))}}}]),e}(),m=function(){function e(i){t(this,e),this.el=i,this.init()}return n(e,[{key:"init",value:function(){var t=this;if(this.id=this.el.id,this.isUnicorn=!1,this.attributes=[],this.value=this.getValue(),this.parent=null,this.el.parentElement&&(this.parent=new e(this.el.parentElement)),this.model={},this.poll={},this.loading={},this.actions=[],this.db={},this.field={},this.target=null,this.key=null,this.errors=[],this.el.attributes){for(var i=0;i0&&(this.poll.timing=parseInt(s[0],10)||2e3)}else n.isLoading?n.modifiers.attr?this.loading.attr=n.value:n.modifiers.class&&n.modifiers.remove?this.loading.removeClass=n.value:n.modifiers.class?this.loading.class=n.value:n.modifiers.remove?this.loading.hide=!0:this.loading.show=!0:n.isTarget?this.target=n.value:n.eventType&&function(){var e={};e.name=n.value,e.eventType=n.eventType,e.isPrevent=!1,e.isStop=!1,n.modifiers&&Object.keys(n.modifiers).forEach((function(t){"prevent"===t?e.isPrevent=!0:"stop"===t?e.isStop=!0:e.key=t})),t.actions.push(e)}();if(n.isKey&&(this.key=n.value),n.isError){var a=n.name.replace("unicorn:error:","");this.errors.push({code:a,message:n.value})}}if(this.isUnicorn&&r(this.field)){var l=this;for(["pk","name"].forEach((function(e){for(l=t;o(t.db[e])&&!l.el.getAttribute("unicorn:checksum");)l.isUnicorn&&r(l.db[e])&&(t.db[e]=l.db[e]),l=l.parent})),l=this;o(this.model.name)&&!l.el.getAttribute("unicorn:checksum");)l.isUnicorn&&r(l.model.name)&&(r(this.field)&&o(this.db)?(this.model=this.field,this.model.name="".concat(l.model.name,".").concat(this.field.name),this.field={}):this.model.name=l.model.name),l=l.parent}}}},{key:"focus",value:function(){this.el.focus()}},{key:"hide",value:function(){this.el.hidden="hidden"}},{key:"show",value:function(){this.el.hidden=null}},{key:"dbKey",value:function(){return a(this)}},{key:"isSame",value:function(e){return this.el.isSameNode(e.el)}},{key:"getValue",value:function(){var e=this.el.value;if(this.el.type)if("checkbox"===this.el.type.toLowerCase())e=this.el.checked;else if("select-multiple"===this.el.type.toLowerCase()){e=[];for(var t=0;t=97?o===r.toUpperCase():n<=90&&i>=97&&r===o.toUpperCase())}function E(e,t,i){e[i]!==t[i]&&(e[i]=t[i],e[i]?e.setAttribute(i,""):e.removeAttribute(i))}var k={OPTION:function(e,t){var i=e.parentNode;if(i){var n=i.nodeName.toUpperCase();"OPTGROUP"===n&&(n=(i=i.parentNode)&&i.nodeName.toUpperCase()),"SELECT"!==n||i.hasAttribute("multiple")||(e.hasAttribute("selected")&&!t.selected&&(e.setAttribute("selected","selected"),e.removeAttribute("selected")),i.selectedIndex=-1)}E(e,t,"selected")},INPUT:function(e,t){E(e,t,"checked"),E(e,t,"disabled"),e.value!==t.value&&(e.value=t.value),t.hasAttribute("value")||e.removeAttribute("value")},TEXTAREA:function(e,t){var i=t.value;e.value!==i&&(e.value=i);var n=e.firstChild;if(n){var o=n.nodeValue;if(o==i||!i&&o==e.placeholder)return;n.nodeValue=i}},SELECT:function(e,t){if(!t.hasAttribute("multiple")){for(var i,n,o=-1,r=0,s=e.firstChild;s;)if("OPTGROUP"===(n=s.nodeName&&s.nodeName.toUpperCase()))s=(i=s).firstChild;else{if("OPTION"===n){if(s.hasAttribute("selected")){o=r;break}r++}!(s=s.nextSibling)&&i&&(s=i.nextSibling,i=null)}e.selectedIndex=o}}};function T(){}function N(e){if(e)return e.getAttribute&&e.getAttribute("id")||e.id}var A=function(e){return function(t,i,n){if(n||(n={}),"string"==typeof i)if("#document"===t.nodeName||"HTML"===t.nodeName||"BODY"===t.nodeName){var o=i;(i=v.createElement("html")).innerHTML=o}else i=y(i);var r=n.getNodeKey||N,s=n.onBeforeNodeAdded||T,a=n.onNodeAdded||T,l=n.onBeforeElUpdated||T,u=n.onElUpdated||T,d=n.onBeforeNodeDiscarded||T,c=n.onNodeDiscarded||T,h=n.onBeforeElChildrenUpdated||T,f=!0===n.childrenOnly,m=Object.create(null),p=[];function g(e){p.push(e)}function E(e,t){if(1===e.nodeType)for(var i=e.firstChild;i;){var n=void 0;t&&(n=r(i))?g(n):(c(i),i.firstChild&&E(i,t)),i=i.nextSibling}}function A(e,t,i){!1!==d(e)&&(t&&t.removeChild(e),c(e),E(e,i))}function S(e){a(e);for(var t=e.firstChild;t;){var i=t.nextSibling,n=r(t);if(n){var o=m[n];o&&b(t,o)?(t.parentNode.replaceChild(o,t),w(o,t)):S(t)}else S(t);t=i}}function w(t,i,n){var o=r(i);if(o&&delete m[o],!n){if(!1===l(t,i))return;if(e(t,i),u(t),!1===h(t,i))return}"TEXTAREA"!==t.nodeName?function(e,t){var i,n,o,a,l,u=t.firstChild,d=e.firstChild;e:for(;u;){for(a=u.nextSibling,i=r(u);d;){if(o=d.nextSibling,u.isSameNode&&u.isSameNode(d)){u=a,d=o;continue e}n=r(d);var c=d.nodeType,h=void 0;if(c===u.nodeType&&(1===c?(i?i!==n&&((l=m[i])?o===l?h=!1:(e.insertBefore(l,d),n?g(n):A(d,e,!0),d=l):h=!1):n&&(h=!1),(h=!1!==h&&b(d,u))&&w(d,u)):3!==c&&8!=c||(h=!0,d.nodeValue!==u.nodeValue&&(d.nodeValue=u.nodeValue))),h){u=a,d=o;continue e}n?g(n):A(d,e,!0),d=o}if(i&&(l=m[i])&&b(l,u))e.appendChild(l),w(l,u);else{var f=s(u);!1!==f&&(f&&(u=f),u.actualize&&(u=u.actualize(e.ownerDocument||v)),e.appendChild(u),S(u))}u=a,d=o}!function(e,t,i){for(;t;){var n=t.nextSibling;(i=r(t))?g(i):A(t,e,!0),t=n}}(e,d,n);var p=k[e.nodeName];p&&p(e,t)}(t,i):t.innerHTML!=i.innerHTML&&k.TEXTAREA(t,i)}!function e(t){if(1===t.nodeType||11===t.nodeType)for(var i=t.firstChild;i;){var n=r(i);n&&(m[n]=i),e(i),i=i.nextSibling}}(t);var C,V,M=t,U=M.nodeType,L=i.nodeType;if(!f)if(1===U)1===L?b(t,i)||(c(t),M=function(e,t){for(var i=e.firstChild;i;){var n=i.nextSibling;t.appendChild(i),i=n}return t}(t,(C=i.nodeName,(V=i.namespaceURI)&&"http://www.w3.org/1999/xhtml"!==V?v.createElementNS(V,C):v.createElement(C)))):M=i;else if(3===U||8===U){if(L===U)return M.nodeValue!==i.nodeValue&&(M.nodeValue=i.nodeValue),M;M=i}if(M===i)c(t);else{if(i.isSameNode&&i.isSameNode(M))return;if(w(M,i,f),p)for(var O=0,P=p.length;O=0;a--)n=(i=s[a]).name,o=i.namespaceURI,r=i.value,o?(n=i.localName||n,e.getAttributeNS(o,n)!==r&&("xmlns"===i.prefix&&(n=i.name),e.setAttributeNS(o,n,r))):e.getAttribute(n)!==r&&e.setAttribute(n,r);for(var l=e.attributes,u=l.length-1;u>=0;u--)n=(i=l[u]).name,(o=i.namespaceURI)?(n=i.localName||n,t.hasAttributeNS(o,n)||e.removeAttributeNS(o,n)):t.hasAttribute(n)||e.removeAttribute(n)}})),S={childrenOnly:!1,getNodeKey:function(e){if(e.attributes){var t=e.getAttribute("unicorn:key")||e.getAttribute("u:key")||e.id;if(t)return t}},onBeforeElUpdated:function(e,t){if(e.isEqualNode(t))return!1}};function w(e,t){if(0!==e.actionQueue.length&&e.currentActionQueue!==e.actionQueue){e.currentActionQueue=e.actionQueue,e.actionQueue=[];var i={id:e.id,data:e.data,checksum:e.checksum,actionQueue:e.currentActionQueue},n={Accept:"application/json","X-Requested-With":"XMLHttpRequest"};n[e.csrfTokenHeaderName]=function(){var e="csrftoken=",t=document.cookie.split(";").filter((function(t){return t.trim().startsWith(e)}));if(t.length>0)return t[0].replace(e,"");var i=document.getElementsByName("csrfmiddlewaretoken");if(i&&i.length>0)return i[0].getAttribute("value");throw Error("CSRF token is missing. Do you need to add {% csrf_token %}?")}(),fetch(e.syncUrl,{method:"POST",headers:n,body:JSON.stringify(i)}).then((function(e){if(e.ok)return e.json();throw Error("Error when getting response: ".concat(e.statusText," (").concat(e.status,")"))})).then((function(i){if(i){if(i.error)throw Error(i.error);e.modelEls.forEach((function(e){e.init(),e.removeErrors()})),e.data=i.data||{},e.errors=i.errors||{};var n=i.dom;A(e.root,n,S),e.refreshChecksum(),e.refreshEventListeners(),e.modelEls.forEach((function(t){Object.keys(e.errors).forEach((function(i){if(t.model.name===i){var n=e.errors[i][0];t.addError(n)}}))}));var o=e.lastTriggeringElements;e.lastTriggeringElements=[],e.currentActionQueue=null,s(t)&&t(o,null)}})).catch((function(i){e.actionQueue=[],e.currentActionQueue=null,e.lastTriggeringElements=[],s(t)&&t(null,null,i)}))}}function C(e,t){e.document.addEventListener(t,(function(i){var n=new m(i.target);n&&n.isUnicorn&&n.actions.length>0&&e.actionEvents[t].forEach((function(t){var o,s=t.action,a=t.element;n.isSame(a)&&(e.walker(a.el,(function(t){e.modelEls.filter((function(e){return e.el.isSameNode(t)})).forEach((function(t){if(r(t.model)&&t.model.isLazy){var i={type:"syncInput",payload:{name:t.model.name,value:t.getValue()}};e.actionQueue.push(i)}})),e.dbEls.filter((function(e){return e.el.isSameNode(t)})).forEach((function(t){if(r(t.model)&&t.model.isLazy){var i={type:"dbInput",payload:{model:t.model.name,db:t.db.name,pk:t.db.pk,fields:{}}};i.payload.fields[t.field.name]=t.getValue(),e.actionQueue.push(i)}}))})),s.isPrevent&&i.preventDefault(),s.isStop&&i.stopPropagation(),function(e){if(!l(e=e.trim(),"(")||!e.endsWith(")"))return[];e=e.slice(e.indexOf("(")+1,e.length-1);for(var t=[],i="",n=!1,o=!1,r=0,s=0,a=0;a0){var i=!1,n=e.slice(-1)[0];r(n)&&r(n.model)&&!n.model.isLazy&&["id","key"].forEach((function(e){t.modelEls.forEach((function(t){i||n[e]&&n[e]===t[e]&&(t.focus(),i=!0)}))}))}this.modelEls.forEach((function(e){t.setValue(e)}))}},{key:"queueMessage",value:function(e,t){-1===e?c(w,250,!1)(this,t):c(w,e,!1)(this,t)}}]),e}(),M="",U={};return e.call=function(e,t){var i;if(Object.keys(U).forEach((function(t){if(o(i)){var n=U[t];n.name===e&&(i=n)}})),!i)throw Error("No component found for: ",e);i.callMethod(t,(function(e){console.error(e)}))},e.componentInit=function(e){e.messageUrl=M,e.csrfTokenHeaderName="X-CSRFToken";var t=new V(e);t.init(),U[t.id]=t,t.setModelValues(),t.setDbModelValues()},e.init=function(e){M=e},e}({}); diff --git a/pyproject.toml b/pyproject.toml index bd007612..4f1fc4fa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "django-unicorn" -version = "0.9.0" +version = "0.9.1" description = "A magical full-stack framework for Django." authors = ["Adam Hill "] license = "MIT"