-
Notifications
You must be signed in to change notification settings - Fork 2
/
simpla-admin.html
1 lines (1 loc) · 8.2 KB
/
simpla-admin.html
1
<link rel="import" href="../polymer/polymer.html"> <dom-module id="simpla-admin"> <template> <style>:host{position:absolute}:host([hidden]),[hidden]{display:none!important}</style> <simpla-admin-controls editable="[[_editable]]" authenticated="[[_authenticated]]" busy="{{busy}}" on-login="login" on-save="save" hidden$="[[_loginOpen]]"> </simpla-admin-controls> <simpla-notify></simpla-notify> </template> <script>!function(){"use strict";function e(e){var t=Simpla.getState("buffer"),n=void 0;if(n=t&&Object.keys(t).some(function(e){return t[e].modified}))return e.returnValue=S,S}function t(e){e.keyCode===E.edit&&e[w]&&(e.preventDefault(),Simpla.editable(!Simpla.getState("editable")))}function n(e){e.keyCode===E.save&&e[w]&&(e.preventDefault(),document.querySelector("simpla-admin").save())}function i(e){var t=function t(){return!!document.body&&(document.body.appendChild(e),document.removeEventListener("readystatechange",t),!0)};t()||document.addEventListener("readystatechange",t)}function o(e){var t=document.querySelector("simpla-admin"),n=void 0;e&&!t&&(n=document.createElement("simpla-admin"),i(n))}function a(){var e=window.location.hash===P;Simpla.editable(e)}function r(e){window.location.hash=e?P:""}function s(e){e.path[0].localName&&"input"===e.path[0].localName||e.preventDefault(),e.stopPropagation()}function u(e){var t=function(e){return 0===e.indexOf("simpla-")};return e&&t(e)}function c(e){return!!u(e.localName)||e.getAttribute&&u(e.getAttribute("is"))}function l(e){var t=function(e){return j.includes(e)},n=function(e){return u(e)&&!t(e)};return!!n(e.localName)||n(e.getAttribute&&e.getAttribute("is"))}function d(e){return Array.prototype.filter.call(e,l)}function f(e){return Array.prototype.filter.call(e,c)}function v(e){return Array.prototype.filter.call(e,function(e){return q.has(e)})}function p(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";e.style.pointerEvents=t}function m(e){p(e,"auto")}function h(e){p(e,"none")}function b(e){p(e,"")}function y(e){q.set(e,0),m(e),H.forEach(function(t){e.addEventListener(t,s)})}function g(e){q.delete(e),b(e),H.forEach(function(t){e.removeEventListener(t,s)})}var _={properties:{loginPrompt:{type:Boolean,observer:"_toggleLoginPromptObserver"}},_toggleLoginPromptObserver:function(e){var t=this,n=this._simplaObservers,i=function(e){e&&!t._authenticated&&Simpla.login()};return e?(i(Simpla.getState("editable")),void(n.login=Simpla.observeState("editable",i))):void(n.login&&n.login.unobserve())}},S="You have unsaved changes, are you sure you want to leave?",k={attached:function(){window.addEventListener("beforeunload",e)},detached:function(){window.removeEventListener("beforeunload",e)}},w="metaKey",E={edit:69,save:83},O={properties:{hotkeys:Boolean},observers:["_updateEditableListener(_authenticated, hotkeys)","_updateSaveListener(_editable, hotkeys)"],_updateEditableListener:function(e,n){e&&n?document.addEventListener("keydown",t):document.removeEventListener("keydown",t)},_updateSaveListener:function(e,t){e&&t?document.addEventListener("keydown",n):document.removeEventListener("keydown",n)},detached:function(){document.removeEventListener("keydown",t),document.removeEventListener("keydown",n)}},L=function(e,t){return new Promise(function(n,i){t="number"==typeof t?t:20;var o=function o(){Promise.resolve().then(e).then(function(e){if("boolean"!=typeof e)throw new TypeError("Expected condition to return a boolean");e===!0?n():setTimeout(o,t)}).catch(i)};o()})},A={_simplaObservers:[],observe:function(){var e=this;L(function(){return!!window.Simpla},1).then(function(){var t=Simpla.getState(),n=t.editable,i=t.authenticated,a=n||i;o(a),e._simplaObservers=[Simpla.observeState("authenticated",o),Simpla.observeState("editable",o)]})},unobserve:function(){this._simplaObservers.forEach(function(e){return e.unobserve()}),this._simplaObservers=[]}},P="#edit",B={_simplaObserver:null,_tracking:!1,track:function(){var e=this;L(function(){return!!window.Simpla},1).then(function(){a(),e._tracking||(window.addEventListener("hashchange",a),r(Simpla.getState("editable")),e._simplaObserver=Simpla.observeState("editable",r),e._tracking=!0)})},untrack:function(){this._tracking&&(window.removeEventListener("hashchange",a),this._simplaObserver&&this._simplaObserver.unobserve(),this._simplaObserver=null,this._tracking=!1)}},T="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},D=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},x=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),N=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},j=["simpla-admin","simpla-login","simpla-admin-controls","simpla-notify"],H=["click","ontouchend"],C=void 0,q=void 0;q=function(){var e=void 0;return void 0!==T(window.Map)?new Map:(e=[],{has:function(t){return e.indexOf(t)!==-1},set:function(t,n){var i=e.indexOf(t);i===-1&&e.push(t)},delete:function(t){var n=e.indexOf(t);n!==-1&&e.splice(n,1)},forEach:function(t){e.forEach(function(e,n,i){return t.call(i,0,e,n)})}})}(),C=new MutationObserver(function(e){e.forEach(function(e){d(e.addedNodes).forEach(y),f(e.addedNodes).forEach(m),v(e.removedNodes).forEach(g)})});var M={enable:function(){q.forEach(function(e,t){return g(t)}),C.disconnect(),b(document.body)},disable:function(){var e=document.querySelectorAll("*");d(e).forEach(y),f(e).forEach(m),h(document.body),C.observe(document.body,{childList:!0,subtree:!0})}},F=["../simpla-notify/simpla-notify.html","simpla-admin-controls.html"],I={saved:"changes saved",saveFailed:"something went wrong, try again?"},K={hashTracking:!0,loginPrompt:!0,hotkeys:!0,protectEditing:!0};window.SimplaAdmin=N({},K,window.SimplaAdmin),A.observe(),window.SimplaAdmin.hashTracking&&B.track();var R=function(){function e(){D(this,e)}return x(e,[{key:"beforeRegister",value:function(){this.is="simpla-admin",this.properties={hashTracking:{type:Boolean,observer:"_updateHashTracking"},protectEditing:Boolean,hidden:{type:Boolean,reflectToAttribute:!0},busy:{type:Boolean,value:!1},_editable:Boolean,_authenticated:Boolean,_dependenciesImported:Boolean,_loginOpen:Boolean,_simplaObservers:{type:Object,value:{}}},this.observers=["_toggleHidden(_editable, _authenticated)","_disableEditable(busy)","_updatePointerEvents(protectEditing, _editable)"]}},{key:"ready",value:function(){N(this,window.SimplaAdmin),window.SimplaAdmin=this}},{key:"attached",value:function(){this._importDependencies(),this._syncEditableAndAuthenticated()}},{key:"detached",value:function(){var e=this;Object.keys(this._simplaObservers).forEach(function(t){e._simplaObservers[t].unobserve()}),this._simplaObservers={},B.untrack(),A.unobserve()}},{key:"_disableEditable",value:function(e){e&&Simpla.getState("editable")?(B.untrack(),Simpla.editable(!1),this.__busyDisabled=!0):!e&&this.__busyDisabled&&(Simpla.editable(!0),B.track(),this.__busyDisabled=!1)}},{key:"login",value:function(){3===parseInt(Simpla.version)?Simpla.login():this.$.login.open()}},{key:"save",value:function(){var e=this,t=function(){e.busy=!1,e.fire("simpla-notification",{text:I.saved})},n=function(){e.busy=!1,e.fire("simpla-notification",{text:I.saveFailed})};this.busy=!0,Simpla.save().then(t).catch(n)}},{key:"_toggleHidden",value:function(e,t){this.hidden=!e&&!t}},{key:"_updateHashTracking",value:function(e){e?B.track():B.untrack()}},{key:"_updatePointerEvents",value:function(e,t){e&&t?M.disable():M.enable()}},{key:"_syncEditableAndAuthenticated",value:function(){var e=this,t=Simpla.getState(),n=t.editable,i=t.authenticated,o=function(t){return e._editable=t},a=function(t){return e._authenticated=t};o(n),a(i),this._simplaObservers.editable=Simpla.observeState("editable",o),this._simplaObservers.authenticated=Simpla.observeState("authenticated",a)}},{key:"_importDependencies",value:function(){var e=this;F.forEach(function(t){e.importHref(e.resolveUrl(t))})}},{key:"behaviors",get:function(){return[_,O,k]}}]),e}();Polymer(R)}(); </script> </dom-module>