diff --git a/django_unicorn/static/js/unicorn.min.js b/django_unicorn/static/js/unicorn.min.js index 9991efb6..4ad1878c 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 a(e,t){for(var n=document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT,null,!1);n.nextNode();)t(n.currentNode)}function s(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}(),c=function(){function e(n){t(this,e),this.el=n,this.init()}return i(e,[{key:"init",value:function(){var e=this;if(this.id=this.el.id,this.isUnicorn=!1,this.attributes=[],this.value=this.getValue(),this.model={},this.poll={},this.actions=[],this.key=void 0,this.errors=[],this.el.attributes)for(var t=0;t0&&(this.poll.timing=parseInt(i[0],10)||2e3)}else n.eventType&&function(){var t={};t.name=n.value,t.eventType=n.eventType,t.isPrevent=!1,t.isStop=!1,n.modifiers&&Object.keys(n.modifiers).forEach((function(e){"prevent"===e?t.isPrevent=!0:"stop"===e?t.isStop=!0:t.key=e})),e.actions.push(t)}();if(n.isKey&&(this.key=n.value),n.isError){var r=n.name.replace("unicorn:error:","");this.errors.push({code:r,message:n.value})}}}},{key:"focus",value:function(){this.el.focus()}},{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 p(e,t,n){e[n]!==t[n]&&(e[n]=t[n],e[n]?e.setAttribute(n,""):e.removeAttribute(n))}var y={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)}p(e,t,"selected")},INPUT:function(e,t){p(e,t,"checked"),p(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 E(){}function g(e){if(e)return e.getAttribute&&e.getAttribute("id")||e.id}var b=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=d.createElement("html")).innerHTML=r}else n=v(n);var o=i.getNodeKey||g,a=i.onBeforeNodeAdded||E,s=i.onNodeAdded||E,l=i.onBeforeElUpdated||E,u=i.onElUpdated||E,c=i.onBeforeNodeDiscarded||E,h=i.onNodeDiscarded||E,f=i.onBeforeElChildrenUpdated||E,p=!0===i.childrenOnly,b=Object.create(null),k=[];function T(e){k.push(e)}function N(e,t){if(1===e.nodeType)for(var n=e.firstChild;n;){var i=void 0;t&&(i=o(n))?T(i):(h(n),n.firstChild&&N(n,t)),n=n.nextSibling}}function A(e,t,n){!1!==c(e)&&(t&&t.removeChild(e),h(e),N(e,n))}function C(e){s(e);for(var t=e.firstChild;t;){var n=t.nextSibling,i=o(t);if(i){var r=b[i];r&&m(t,r)?(t.parentNode.replaceChild(r,t),S(r,t)):C(t)}else C(t);t=n}}function S(t,n,i){var r=o(n);if(r&&delete b[r],!i){if(!1===l(t,n))return;if(e(t,n),u(t),!1===f(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 h=c.nodeType,f=void 0;if(h===u.nodeType&&(1===h?(n?n!==i&&((l=b[n])?r===l?f=!1:(e.insertBefore(l,c),i?T(i):A(c,e,!0),c=l):f=!1):i&&(f=!1),(f=!1!==f&&m(c,u))&&S(c,u)):3!==h&&8!=h||(f=!0,c.nodeValue!==u.nodeValue&&(c.nodeValue=u.nodeValue))),f){u=s,c=r;continue e}i?T(i):A(c,e,!0),c=r}if(n&&(l=b[n])&&m(l,u))e.appendChild(l),S(l,u);else{var v=a(u);!1!==v&&(v&&(u=v),u.actualize&&(u=u.actualize(e.ownerDocument||d)),e.appendChild(u),C(u))}u=s,c=r}!function(e,t,n){for(;t;){var i=t.nextSibling;(n=o(t))?T(n):A(t,e,!0),t=i}}(e,c,i);var p=y[e.nodeName];p&&p(e,t)}(t,n):t.innerHTML!=n.innerHTML&&y.TEXTAREA(t,n)}!function e(t){if(1===t.nodeType||11===t.nodeType)for(var n=t.firstChild;n;){var i=o(n);i&&(b[i]=n),e(n),n=n.nextSibling}}(t);var w,U,M=t,L=M.nodeType,O=n.nodeType;if(!p)if(1===L)1===O?m(t,n)||(h(t),M=function(e,t){for(var n=e.firstChild;n;){var i=n.nextSibling;t.appendChild(n),n=i}return t}(t,(w=n.nodeName,(U=n.namespaceURI)&&"http://www.w3.org/1999/xhtml"!==U?d.createElementNS(U,w):d.createElement(w)))):M=n;else if(3===L||8===L){if(O===L)return M.nodeValue!==n.nodeValue&&(M.nodeValue=n.nodeValue),M;M=n}if(M===n)h(t);else{if(n.isSameNode&&n.isSameNode(M))return;if(S(M,n,p),k)for(var V=0,P=k.length;V=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)}})),k={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}},T=function(){function e(n){if(t(this,e),this.id=n.id,this.name=n.name,this.messageUrl=n.messageUrl,this.csrfTokenHeaderName=n.csrfTokenHeaderName,o(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||a,this.root=void 0,this.modelEls=[],this.errors={},this.poll={},this.actionQueue=[],this.currentActionQueue=null,this.actionEvents={},this.attachedEventTypes=[],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:"addActionEventListener",value:function(e){var t=this;this.document.addEventListener(e,(function(n){var i=new c(n.target);i&&i.isUnicorn&&i.actions.length>0&&t.actionEvents[e].forEach((function(e){var o,s=e.action,l=e.element;i.el.isSameNode(l.el)&&(a(l.el,(function(e){t.modelEls.filter((function(t){return t.el.isSameNode(e)})).forEach((function(e){if(!r(e.model)&&e.model.isLazy){var n={type:"syncInput",payload:{name:e.model.name,value:e.getValue()}};t.actionQueue.push(n)}}))})),s.isPrevent&&n.preventDefault(),s.isStop&&n.stopPropagation(),s.key?s.key===((o=n.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("-"):"")&&t.callMethod(s.name):t.callMethod(s.name))}))}))}},{key:"addModelEventListener",value:function(e,t){var n=this;e.el.addEventListener(t,(function(){var t={type:"syncInput",payload:{name:e.model.name,value:e.getValue()}};if(e.model.isDefer){var i=!1;return n.actionQueue.forEach((function(t){t.payload.name===e.model.name&&(t.payload.value=e.getValue(),i=!0)})),void(i||n.actionQueue.push(t))}n.actionQueue.push(t),n.sendMessage(e.model.debounceTime,(function(t,i){i?console.error(i):t?n.setModelValues(e):n.setModelValues()}))}))}},{key:"refreshEventListeners",value:function(){var e=this;this.actionEvents={},this.walker(this.root,(function(t){if(!t.isSameNode(e.root)){var n=new c(t);n.isUnicorn&&(r(n.model)||0===e.modelEls.filter((function(e){return e.el.isSameNode(n.el)})).length&&(e.modelEls.push(n),e.addModelEventListener(n,n.model.eventType)),n.actions.forEach((function(t){e.actionEvents[t.eventType]?e.actionEvents[t.eventType].push({action:t,element:n}):(e.actionEvents[t.eventType]=[{action:t,element:n}],0===e.attachedEventTypes.filter((function(e){return e===t.eventType})).length&&(e.attachedEventTypes.push(t.eventType),e.addActionEventListener(t.eventType)))})))}}))}},{key:"callMethod",value:function(e,t){var n=this,i={type:"callMethod",payload:{name:e,params:[]}};this.actionQueue.push(i),this.sendMessage(-1,(function(e,i){i&&"function"==typeof t?t(i):i?console.error(i):n.setModelValues()}))}},{key:"initPolling",value:function(){var e=this,t=new c(this.root);t.isUnicorn&&!r(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){for(var t=e.model.name.split("."),n=this.data,i=0;i0)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;b(e.root,i,k),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=!1;e.currentActionQueue.forEach((function(e){"callMethod"===e.type&&(r=!0)})),e.currentActionQueue=null,t&&"function"==typeof t&&t(!r,null)}})).catch((function(n){e.actionQueue=[],e.currentActionQueue=null,t&&"function"==typeof t&&t(null,n)}))}}-1===e?s(n,250,!1)(this):s(n,e,!1)(this)}}]),e}(),N="",A={};return e.call=function(e,t){var n;if(Object.keys(A).forEach((function(t){if(void 0===n){var i=A[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=N,e.csrfTokenHeaderName="X-CSRFToken";var t=new T(e);t.init(),A[t.id]=t,t.setModelValues()},e.init=function(e){N=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 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}({}); diff --git a/pyproject.toml b/pyproject.toml index 403c19df..7c646018 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "django-unicorn" -version = "0.7.1" +version = "0.8.0" description = "A magical full-stack framework for Django." authors = ["Adam Hill "] license = "MIT"