dhxmvp
Version:
A complete boilerplate for building online, offline and syncable MVP Single Page Applications using DHTMLX.
1 lines • 14.5 kB
JavaScript
!function(e){"use strict"}(window.$dhx=window.$dhx||{}),function(e){"use strict"}($dhx.ui=$dhx.ui||{}),function(e){"use strict";var t,i,n,r,o=[],s=[],d=[],a={},p=function(n){"undefined"==typeof n.appId&&(n.appId=""+Math.random()),this.appId=n.appId,this.container=n.container,this.root=n.root,this.icons_path=n.icons_path||n.root+"assets/icons/64/",this.deps_path=n.deps_path||n.root+"deps/",this.lib_path=n.lib_path||n.root+"lib/",this.stash=n,t=n.root,a=n,a.from="super";var r=e.triggerMethod("before:start",a);if(!r)throw" application will not initialize due a onBeforeStart event returning false";this.initialize(a),this.active_route=!1,this.last_active_route=!1,i=this},u=function(e){n=this},h=function(t){h=e.copyTo(h,t),this.appId=i.appId,this.container=i.container,this.root=i.root,this.icons_path=i.icons_path,this.dispatch=function(e){n.dispatch(e,!0),this.onDispatch&&this.onDispatch(e)},r=this},c=function(t){c=e.copyTo(c,t),this.appId=i.appId,this.container=i.container,this.root=i.root,this.icons_path=i.icons_path};p.prototype={initialize:function(e){},start:function(t){var n=(window.location.hash,[]),r=[],o=[],s=[];t=t||{},window.screen.availWidth<1024&&(e.ui.isMobile=!0),r.push(i.lib_path+"presenter/"+("test"!=$dhx.environment?"min.":"")+"presenter.js"),r.push(i.lib_path+"view/"+("test"!=$dhx.environment?"min.":"")+"view.js"),n.push(i.deps_path+"thirdparty/dhtmlx5.0/dhtmlx.css"),n.push(i.deps_path+"thirdparty/dhtmlx5.0/dhtmlx_.js"),n.push(i.deps_path+"thirdparty/signals.js"),n.push(i.deps_path+"thirdparty/hasher.js"),n.push(i.deps_path+"thirdparty/crossroads.js"),n.push(i.deps_path+"dhx/min.dhx.MQ.js"),n.push(i.deps_path+"dhx/dhx.ui.Mediator.js"),n.push(i.deps_path+"dhx/dhx.ui.router.js"),n.push(i.deps_path+"dhx/dhx.ui.session.js"),t.full?(n.push(i.deps_path+"thirdparty/jquery.min.js"),n.push("https://cdn.jsdelivr.net/pouchdb/5.4.5/pouchdb.min.js"),n.push(i.deps_path+"dhx/min.dhx.ui.i18n.js"),n.push(i.deps_path+"dhx/min.dhx.ui.i18n.en-us.js"),n.push(i.deps_path+"thirdparty/moment.min.js"),n.push(i.deps_path+"thirdparty/moment-timezone-with-data.min.js"),n.push(i.deps_path+"dhx/min.dhtmlx_grid_moment_type.js"),n.push(i.deps_path+"dhx/min.dhx.excells.js"),n.push(i.deps_path+"dhx/min.dhx.crypt.js"),n.push(i.deps_path+"thirdparty/ie10-viewport-bug-workaround.js"),n.push(i.deps_path+"thirdparty/min.underscore.js"),n.push(i.deps_path+"thirdparty/backbone-min.js"),n.push(i.deps_path+"thirdparty/min.backbone-indexeddb.js"),n.push(i.deps_path+"dhx/min.dhx.component.js"),n.push(i.deps_path+"dhx/min.dhx.dhtmlx.js"),n.push(i.deps_path+"dhx/min.dhx.ui.form.js")):(t.stripe&&n.push("https://js.stripe.com/v2/"),t.pouch&&n.push("https://cdn.jsdelivr.net/pouchdb/5.4.5/pouchdb.min.js"),t.backboneIDB&&(n.push("http://cdn.pubnub.com/pubnub.min.js"),n.push(i.deps_path+"thirdparty/min.underscore.js"),n.push(i.deps_path+"thirdparty/backbone-min.js"),n.push(i.deps_path+"thirdparty/min.backbone-indexeddb.js")),t.$dhx_crypt&&n.push(i.deps_path+"dhx/min.dhx.crypt.js"),(t.$dhx_grid||t.$dhx_form)&&(n.push(i.deps_path+"thirdparty/jquery.min.js"),n.push(i.deps_path+"dhx/min.dhx.ui.i18n.js"),n.push(i.deps_path+"dhx/min.dhx.ui.i18n.en-us.js"),n.push(i.deps_path+"thirdparty/moment.min.js"),n.push(i.deps_path+"thirdparty/moment-timezone-with-data.min.js"),n.push(i.deps_path+"dhx/min.dhtmlx_grid_moment_type.js"),n.push(i.deps_path+"dhx/min.dhx.component.js"),n.push(i.deps_path+"thirdparty/creditcard.min.js")),t.$dhx_grid&&n.push(i.deps_path+"dhx/min.dhx.excells.js"),t.$dhx_form&&(n.push(i.deps_path+"dhx/min.dhx.dhtmlx.js"),n.push(i.deps_path+"dhx/min.dhx.ui.form.js"))),i.stash.model.models&&i.stash.model.models.forEach(function(e){o.push(i.lib_path+"model/models/"+("test"!=$dhx.environment?"min.":"")+e+".js")}),i.stash.model.collections&&i.stash.model.collections.forEach(function(e){o.push(i.lib_path+"model/collections/"+("test"!=$dhx.environment?"min.":"")+e+".js")}),i.stash.model.engine?s=[i.lib_path+"model/engines/"+("test"!=$dhx.environment?"min.":"")+i.stash.model.engine+".js"]:(i.stash.model.engine="backboneIDB",s=[i.lib_path+"model/engines/"+("test"!=$dhx.environment?"min.":"")+i.stash.model.engine+".js"]),$dhx.onDemand.require(n,function(){$dhx.onDemand.require(o,function(){$dhx.onDemand.require(s,function(){$dhx.onDemand.require(r,function(){e.start_all()})})})})}},u.prototype={dispatch:function(t,n){this.router;e.destroy_active_route(),"#"!=t?($dhx.ui.router.routeTo(t),$dhx.ui.router.router.resetState()):(window.location.hash="/",i.active_route=t)},route:function(e){this.routes[e.url]=e}},h.prototype={initialize:function(e){}},c.prototype={initialize:function(e){}},e.router={extend:function(t){var i=function(){console.log("start from extend factory")};return e.extend({base:u,factory:t,onBeforeExtend:function(e){e=e||{},e.presenter=e.presenter||{start:i},e.presenter.start=e.presenter.start||i,e.routes=e.routes||{},e.routes.hasOwnProperty("#")||(e.routes["#"]={url:"#",method:"start"})}})}},e.application={extend:function(t){return e.extend({base:p,factory:t,onBeforeExtend:function(){}})}},e.main_view={extend:function(t){return e.extend({base:h,factory:t,onBeforeExtend:function(){}})}},e.child_view={extend:function(t){return e.extend({base:c,factory:t,onBeforeExtend:function(){}})}},e.start_all=function(){window.pubnub=PUBNUB.init({publish_key:"pub-c-d128f7d4-39ab-43e7-9547-4dc1bdea2300",subscribe_key:"sub-c-142379cc-668e-11e6-9eba-02ee2ddab7fe",error:function(e){console.log("Error:",e)}}),e.setup_routes(),e.setup_models(),e.setup_collections(),e.start_model()},e.setup_routes=function(){var e=function(e){};for(var t in n.routes)"#"!=t&&(console.info("Route setup: ",t),$dhx.ui.router.addRoute(t,e))},e.start_model=function(){var t=$dhx.ui.mvp.presenters.get("presenter"),r=$dhx.ui.mvp.views.get("view"),o=$dhx.ui.mvp.model.engine.get(i.stash.model.engine);o.start({onSuccess:function(){console.info("model started"),o.schema.start({onSuccess:function(){r.model=o,r.rendered=!1,r.initialize(),t._view=r,t.view=r,t.model=o,t._subscriber=t.subscriber||function(e,t){console.log("Presenter subescriber defined in $dhx.ui.mvp Received Message: ",t)},t.model._subscriber=t.model.subscriber||function(e,t){console.log("Model subescriber defined in $dhx.ui.mvp Received Message: ",t)},t._subscriber_token=$dhx.ui.Mediator.listen("users,pets,questions:changeModel",t._subscriber),t.model._subscriber_presenter_token=$dhx.ui.Mediator.listen("users,pets,questions:changeModel",t.model._subscriber),i.presenters=$dhx.ui.mvp.presenters,i.views=$dhx.ui.mvp.views,n.presenter=t,r._router=n,r.presenter=t,i.main_presenter=t,i.main_view=r,e.app_started||e.start_app()}})}})},e.setup_models=function(){i.stash.model.models.forEach(function(e){var t={};t[e]=function(e){"strict";return e}(window[e]),$dhx.ui.mvp.model.declare(t),window[e]=null})},e.setup_collections=function(){i.stash.model.collections.forEach(function(e){var t={};t[e]=function(e){"strict";return e.defaults=$dhx.ui.mvp.model.get(e.model),e.item=e.model,e}(window[e]),$dhx.ui.mvp.model.collection.declare(t),window[e]=null})},e.queryString=function(e,t){t||(t=window.location.href),e=e.replace(/[\[\]]/g,"\\$&");var i=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)"),n=i.exec(t);return n?n[2]?decodeURIComponent(n[2].replace(/\+/g," ")):"":null},e.app_started=!1,e.start_app=function(){var t=$dhx.ui.router.request_url||"";"#/"==t&&(t=""),""!==t&&(t=t.toString().replace(/#/,"")),n.dispatch("#"),$dhx.ui.mvp.presenters.get("presenter").start(),e.triggerMethod("start",a),$dhx.ui.i18n&&$dhx.ui.i18n.start(),$dhx.excells&&$dhx.excells.init(),""!==t?n.presenter._view.render({onSuccess:function(i){e.triggerMethod("render",a),$dhx.ui.mvp.views.get("view").rendered=!0,$dhx.ui.mvp.views.get("view").dispatch(t),i.onComplete&&i.onComplete(t)},ok:function(i){e.triggerMethod("render",a),$dhx.ui.mvp.views.get("view").rendered=!0,$dhx.ui.mvp.views.get("view").dispatch(t),i.onComplete&&i.onComplete(t)},onFail:function(){}}):n.presenter._view.render({onSuccess:function(){e.triggerMethod("render",a),$dhx.ui.mvp.views.get("view").rendered=!0},ok:function(){e.triggerMethod("render",a),$dhx.ui.mvp.views.get("view").rendered=!0},onFail:function(){}}),$dhx.MQ.publish(n.presenter.topic,{action:"start",target:null}),e.app_started=!0},e.extend=function(t){var i=t.base,n=t.factory,r=null;return t.onBeforeExtend&&t.onBeforeExtend(n),r=function(e){e=e||{},i.call(this,e)},r.prototype=Object.create(i.prototype),r.prototype.constructor=r,r=e.copyTo(r,n),r.on=function(e,t){d.push({pattern:e,fn:t,base:i})},r.start=function(e){a=e},r},e.copyTo=function(e,t){for(var i in t)t.hasOwnProperty(i)&&(e.prototype[i]=t[i]);return e},e.triggerMethod=function(){var e=arguments[0],t=arguments[1]||!1,i=[];return d.forEach(function(n){n.pattern===e&&(t?i.push(n.fn(t)):i.push(n.fn()))}),!(i.join(".").indexOf("false")>-1)},e.loadModule=function(e,t){var d="start",a=n,p=a.routes,u=null,h=!1,c=eval,l=c(t.route._matchRegexp);for(var m in a.routes)l.test(m)&&(h=!0);if(!h)throw"Undeclared "+e+" route";if(u=t.route._pattern,!(u in a.routes))throw"Undeclared "+u+" route";if(a.routes[u].method&&(d=a.routes[u].method),i.active_route=u,"#"!=u&&(p[u].presenter=p[u].presenter||u,p[u].view=p[u].view||u),p[u].presenter){var _=[];p[u].view&&_.push(i.lib_path+"view/"+("test"!=$dhx.environment?"min.":"")+p[u].view+".js"),_.push(i.lib_path+"presenter/"+("test"!=$dhx.environment?"min.":"")+p[u].presenter+".js"),p[u].append_views&&p[u].append_views.forEach(function(e){_.push(i.lib_path+"view/"+("test"!=$dhx.environment?"min.":"")+e[Object.keys(e)[0]]+".js")}),$dhx.onDemand.load(_,function(){s[u]=$dhx.ui.mvp.presenters.get(p[u].presenter),o[u]=$dhx.ui.mvp.views.get(p[u].view),p[u].append_views&&p[u].append_views.forEach(function(e){o[u][Object.keys(e)[0]]=window[e[Object.keys(e)[0]]]}),s[u].view=o[u],s[u].view._wrapper=r._wrapper,s[u].view.app={mainView:$dhx.ui.mvp.views.get("view"),_child_presenters:s,_child_views:o},s[u].model=n.presenter.model,s[u]._subscriber=s[u].subscriber||function(e,t){console.log("Child Presenter subescriber defined in $dhx.ui.mvp Received Message: ",t)},s[u]._subscriber_token=$dhx.ui.Mediator.listen("questions:changeModel",s[u]._subscriber),o[u].presenter=s[u],s[u].start(),o[u].initialize(),o[u].render(n.presenter._model,s[u]),$dhx.MQ.publish(s[u].topic,{action:"start",target:null})})}},e.destroy_active_route=function(){i.last_active_route=i.active_route,s[i.last_active_route]&&(s[i.last_active_route].destroy&&s[i.last_active_route].destroy(),o[i.last_active_route].destroy&&o[i.last_active_route].destroy(),$dhx.ui.Mediator.unlisten(s[i.last_active_route]._subscriber_token),o[i.last_active_route]=null,s[i.last_active_route]=null,delete o[i.last_active_route],delete s[i.last_active_route])},e.views={views:[],declare:function(t){var i=Object.keys(t)[0];i=i.toString().replace(/\//g,""),e.views.views[i]=t[i]},get:function(t){return t=t.toString().replace(/\//g,""),e.views.views[t]||!1}},e.presenters={presenters:[],declare:function(t){var i=Object.keys(t)[0];i=i.toString().replace(/\//g,""),e.presenters.presenters[i]=t[i]},get:function(t){return t=t.toString().replace(/\//g,""),e.presenters.presenters[t]||!1}},e.models={models:[],declare:function(t){var i=Object.keys(t)[0];i=i.toString().replace(/\//g,""),e.models.models[i]=t[i]},get:function(t){return t=t.toString().replace(/\//g,""),e.models.models[t]||!1}},e.ui={isMobile:!1,window_manager:null,window_manager_is_ready:!1,_window_manager:function(t){var i=e.ui;i.window_manager_is_ready||(i.window_manager=new dhtmlXWindows({}),i.window_manager_is_ready=!0)},window:function(t){var i=e.ui;return i.window_manager_is_ready||i._window_manager(),i.window_manager.createWindow(t)},askNotificationPermission:function(e){Notification.requestPermission(function(t){if("permission"in Notification||(Notification.permission=t),e.onSuccess&&e.onSuccess(t),"granted"===t){new Notification("Notification test",{body:"it is working!",icon:i.root+"assets/images/notification.png"})}})},askLocationPermission:function(e){},getQuota:function(e,t){var n=window.webkitStorageInfo||navigator.webkitTemporaryStorage||navigator.webkitPersistentStorage||!1;if(!n){var r=$dhx.Browser.name+" does not provide quota management.";return $dhx.notify("Quota information",r,i.root+"assets/images/notification.png"),void(t&&t(r))}n.queryUsageAndQuota(n.TEMPORARY,function(t,i){t=(t/1024/1024/1024).toFixed(5),i=(i/1024/1024/1024).toFixed(2),e&&e(t,i)},function(e){t&&t(e),console.log("Error",e)})}},e.model={_collections:{},collection:{declare:function(t){var i=Object.keys(t)[0];i=i.toString().replace(/\//g,""),e.model._collections[i]=t[i]},get:function(t){return t=t||!1,t?(t=t.toString().replace(/\//g,""),e.model._collections[t]||!1):e.model._collections}},_engines:{},engine:{declare:function(t){var i=Object.keys(t)[0];i=i.toString().replace(/\//g,""),e.model._engines[i]=t[i]},get:function(t){return t=t.toString().replace(/\//g,""),e.model._engines[t]||!1}},_models:{},declare:function(t){var i=Object.keys(t)[0];i=i.toString().replace(/\//g,""),e.model._models[i]=t[i]},get:function(t){return t?(t=t.toString().replace(/\//g,""),e.model._models[t]||!1):e.model._models},helpers:{schema:{record:function(t){var i=this,n={},r=["_id","id","__v"],o=e.model.get(t.model);t.record=JSON.parse(JSON.stringify(t.record));for(var s in t.record)r.contains(s)||s in o&&(n[s]=t.record[s]);for(var d in o)if(!r.contains(d))if("undefined"!=typeof n[d]){if(typeof n[d]!==o[d].type.toLowerCase())throw console.warn("error creating record object"),"Invalid type for "+d+". It should be "+o[d].type.toLowerCase()+", but you passed "+typeof n[d];if((o[d].validate.required||(o[d].validate.rules||"").indexOf("NotEmpty")>-1)&&(""===n[d]||null===n[d]))throw console.warn("error creating record object"),"You need to provide a value for "+d+". It should be "+o[d].type.toLowerCase()+"."}else console.info("setting default value ("+o[d]["default"]+") for "+d),n[d]=o[d]["default"];t.record.__v&&(n.__v=t.record.__v),t.record.id?n.id=t.record.id:n.id=$dhx.guid(),n._id=n.id;for(var a in n)i[a]=n[a]},defaults:{__v:{type:"number","default":0,unique:!1,validate:{required:!0,mask_to_use:"",rules:""},ui:{form:{label:"__v",type:"hidden"},grid:{header:"__v",align:"left",coltype:"ro",width:"0"}}},_id:{type:"string","default":0,unique:!0,validate:{required:!0,mask_to_use:"",rules:""},ui:{form:{label:"_id",type:"hidden"},grid:{header:"_id",align:"left",coltype:"ro",width:"0"}}}}}}}}($dhx.ui.mvp=$dhx.ui.mvp||{});