forked from signalpoint/jDrupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdrupal.min.js
1 lines (1 loc) · 14.2 KB
/
jdrupal.min.js
1
var jDrupal={};jDrupal.init=function(){jDrupal.csrf_token=!1,jDrupal.sessid=null,jDrupal.modules={},jDrupal.connected=!1,jDrupal.settings={sitePath:null,basePath:"/"}},jDrupal.init(),jDrupal.config=function(a){var b=void 0!==arguments[1]?arguments[1]:null;return b?void(jDrupal.settings[a]=b):jDrupal.settings[a]},jDrupal.sitePath=function(){return jDrupal.settings.sitePath},jDrupal.basePath=function(){return jDrupal.settings.basePath},jDrupal.restPath=function(){return this.sitePath()+this.basePath()},jDrupal.path=function(){return this.restPath().substr(this.restPath().indexOf("://")+3).replace("localhost","")},jDrupal.isReady=function(){try{var a=!jDrupal.isEmpty(jDrupal.sitePath());return a||console.log("sitePath not set in jdrupal.settings.js"),a}catch(b){console.log("jDrupal.isReady - "+b)}},jDrupal.isEmpty=function(a){return null!==a&&"object"==typeof a?0===Object.keys(a).length:void 0===a||null===a||""==a},jDrupal.functionExists=function(name){try{return"function"==eval("typeof "+name)}catch(error){alert("jDrupal.functionExists - "+error)}},jDrupal.inArray=function(a,b){try{if(void 0===b)return!1;if("string"==typeof a)return b.indexOf(a)>-1;for(var c=!1,d=0;d<b.length;d++)if(b[d]==a){c=!0;break}return c}catch(e){console.log("jDrupal.inArray - "+e)}},jDrupal.isArray=function(a){return"[object Array]"===Object.prototype.toString.call(a)},jDrupal.isInt=function(a){return"string"==typeof a&&(a=parseInt(a)),"number"==typeof a&&a%1==0},jDrupal.isPromise=function(a){return Promise.resolve(a)==a},jDrupal.shuffle=function(a){for(var b=a.length-1;b>0;b--){var c=Math.floor(Math.random()*(b+1)),d=a[b];a[b]=a[c],a[c]=d}return a},jDrupal.time=function(){var a=new Date;return Math.floor(a/1e3)},jDrupal.lcfirst=function(a){return a+="",a.charAt(0).toLowerCase()+a.substr(1)},jDrupal.ucfirst=function(a){return a+="",a.charAt(0).toUpperCase()+a.substr(1)},jDrupal.Module=function(){this.name=null},jDrupal.moduleExists=function(a){try{return void 0!==jDrupal.modules[a]}catch(b){console.log("jDrupal.moduleExists - "+b)}},jDrupal.moduleImplements=function(a){try{var b=[];if(a)for(var c in jDrupal.modules)jDrupal.modules.hasOwnProperty(c)&&jDrupal.functionExists(c+"_"+a)&&b.push(c);return 0!=b.length&&b}catch(d){console.log("jDrupal.moduleImplements - "+d)}},jDrupal.moduleInvoke=function(a,b){if(jDrupal.moduleLoad(a)){var c=a+"_"+b;if(jDrupal.functionExists(c)){var d=window[c],e=Array.prototype.slice.call(arguments);return e.splice(0,2),0==Object.getOwnPropertyNames(e).length?d():d.apply(null,e)}}},jDrupal.moduleInvokeAll=function(a){var b=[],c=Array.prototype.slice.call(arguments);c.splice(0,1);var d=[];for(var e in jDrupal.modules)jDrupal.modules.hasOwnProperty(e)&&jDrupal.functionExists(e+"_"+a)&&d.push(e);if(jDrupal.isEmpty(d))return Promise.resolve();for(var f=0;f<d.length;f++){0==c.length?b.push(jDrupal.moduleInvoke(d[f],a)):(c.unshift(d[f],a),b.push(jDrupal.moduleInvoke.apply(null,c)),c.splice(0,2))}return Promise.all(b)},jDrupal.moduleLoad=function(a){try{return!!jDrupal.modules[a]&&jDrupal.modules[a]}catch(b){console.log("jDrupal.moduleLoad - "+b)}},jDrupal.modulesLoad=function(){return jDrupal.modules},function(a){XMLHttpRequest.prototype.send=function(b){var c=this,d=jDrupal.moduleInvokeAll("rest_pre_process",this,b);d?d.then(function(){a.call(c,b)}):a.call(this,b)}}(XMLHttpRequest.prototype.send),jDrupal.token=function(){return new Promise(function(a,b){var c=new XMLHttpRequest;c.dg={service:"system",resource:"token"},c.open("GET",jDrupal.restPath()+"rest/session/token"),c.onload=function(){if(200==c.status){var d=jDrupal.moduleInvokeAll("rest_post_process",c);d?d.then(a(c.response)):a(c.response)}else b(c)},c.onerror=function(){b(Error("Network Error"))},c.send()})},jDrupal.connect=function(){return new Promise(function(a,b){var c=new XMLHttpRequest;c.dg={service:"system",resource:"connect"},c.open("GET",jDrupal.restPath()+"jdrupal/connect?_format=json");var d=function(){jDrupal.connected=!0;var b=JSON.parse(void 0!==c.responseText?c.responseText:c.response);0==b.uid?(jDrupal.setCurrentUser(jDrupal.userDefaults()),a(b)):jDrupal.userLoad(b.uid).then(function(c){jDrupal.setCurrentUser(c),a(b)})};c.onload=function(){if(200!=c.status)return void b(c);var a=jDrupal.moduleInvokeAll("rest_post_process",c);a?a.then(d):d()},c.onerror=function(){b(Error("Network Error"))},c.send()})},jDrupal.userLogin=function(a,b){return new Promise(function(c,d){var e=new XMLHttpRequest;e.dg={service:"user",resource:"login"},e.open("POST",jDrupal.restPath()+"user/login?_format=json"),e.setRequestHeader("Content-type","application/json");var f=function(){jDrupal.connect().then(c)};e.onload=function(){if(200==e.status){var a=jDrupal.moduleInvokeAll("rest_post_process",e);a?a.then(f):f()}else d(e)},e.onerror=function(){d(Error("Network Error"))},e.send(JSON.stringify({name:a,pass:b}))})},jDrupal.userLogout=function(){return new Promise(function(a,b){var c=new XMLHttpRequest;c.dg={service:"user",resource:"logout"},c.open("GET",jDrupal.restPath()+"user/logout"),c.setRequestHeader("Content-type","application/x-www-form-urlencoded");var d=function(){jDrupal.setCurrentUser(jDrupal.userDefaults()),jDrupal.connect().then(a)};c.onload=function(){if(200==c.status||303==c.status){var a=jDrupal.moduleInvokeAll("rest_post_process",c);a?a.then(d):d()}else b(c)},c.onerror=function(){b(Error("Network Error"))},c.send()})},jDrupal.entityLoad=function(a,b){return new(this[this.ucfirst(a)])(b).load()},jDrupal.commentLoad=function(a){return this.entityLoad("comment",a)},jDrupal.nodeLoad=function(a){return this.entityLoad("node",a)},jDrupal.userLoad=function(a){return this.entityLoad("user",a)},jDrupal.userRegister=function(a,b,c){return new Promise(function(d,e){var f=new XMLHttpRequest;f.dg={service:"user",resource:"register"},f.open("POST",jDrupal.restPath()+"user/register?_format=json"),f.setRequestHeader("Content-type","application/json");var g=function(){jDrupal.connect().then(d)};f.onload=function(){if(200==f.status){var a=jDrupal.moduleInvokeAll("rest_post_process",f);a?a.then(g):g()}else e(f)},f.onerror=function(){e(Error("Network Error"))},f.send(JSON.stringify({name:{value:a},pass:{value:b},mail:{value:c}}))})},jDrupal.Views=function(a){this.path=a,this.results=null},jDrupal.Views.prototype.getPath=function(){return this.path},jDrupal.Views.prototype.getResults=function(){return this.results},jDrupal.Views.prototype.getView=function(){var a=this;return new Promise(function(b,c){var d=new XMLHttpRequest;d.dg={service:"views",resource:null},d.open("GET",jDrupal.restPath()+a.getPath());var e=function(){a.results=JSON.parse(d.response),b()};d.onload=function(){if(200==d.status){var a=jDrupal.moduleInvokeAll("rest_post_process",d);a?a.then(e):e()}else c(d)},d.onerror=function(){c(Error("Network Error"))},d.send()})},jDrupal.viewsLoad=function(a){return new Promise(function(b,c){var d=new jDrupal.Views(a);d.getView().then(function(){b(d)})})},jDrupal.Entity=function(a,b,c){this.entity=null,this.bundle=b,this.entityID=c,this.entityKeys={}},jDrupal.Entity.prototype.get=function(a,b){return this.entity&&void 0!==this.entity[a]?void 0!==b?this.entity[a][b]:this.entity[a]:null},jDrupal.Entity.prototype.set=function(a,b,c){this.entity&&(void 0!==b&&void 0!==this.entity[a]?this.entity[a][b]=c:this.entity[a]=c)},jDrupal.Entity.prototype.getEntityKey=function(a){return void 0!==this.entityKeys[a]?this.entityKeys[a]:null},jDrupal.Entity.prototype.getEntityType=function(){return this.entityKeys.type},jDrupal.Entity.prototype.getBundle=function(){var a=this.getEntityKey("bundle");return void 0!==this.entity[a]?this.entity[a][0].target_id:null},jDrupal.Entity.prototype.id=function(){var a=this.getEntityKey("id");return void 0!==this.entity[a]?this.entity[a][0].value:null},jDrupal.Entity.prototype.language=function(){return this.entity.langcode[0].value},jDrupal.Entity.prototype.isNew=function(){return!this.id()},jDrupal.Entity.prototype.label=function(){var a=this.getEntityKey("label");return void 0!==this.entity[a]?this.entity[a][0].value:null},jDrupal.Entity.prototype.stringify=function(){return JSON.stringify(this.entity)},jDrupal.Entity.prototype.preLoad=function(a){return new Promise(function(a,b){a()})},jDrupal.Entity.prototype.load=function(){try{var a=this,b=a.getEntityType();return new Promise(function(c,d){a.preLoad().then(function(){var e=jDrupal.restPath()+b+"/"+a.id()+"?_format=json",f=new XMLHttpRequest;f.dg={service:b,resource:"retrieve"},f.open("GET",e);var g=function(){a.entity=JSON.parse(f.response),a.postLoad(f).then(function(){c(a)})};f.onload=function(){if(200==f.status){var a=jDrupal.moduleInvokeAll("rest_post_process",f);a?a.then(g):g()}else d(f)},f.onerror=function(){d(Error("Network Error"))},f.send()})})}catch(c){console.log("jDrupal.Entity.load - "+c)}},jDrupal.Entity.prototype.postLoad=function(a){return new Promise(function(a,b){a()})},jDrupal.Entity.prototype.preSave=function(a){return new Promise(function(a,b){a()})},jDrupal.Entity.prototype.save=function(){var a=this;return new Promise(function(b,c){a.preSave().then(function(){jDrupal.token().then(function(d){var e=a.getEntityType(),f=null,g=null,h=null;a.isNew()?(f="POST",g="create",h="entity/"+e):(f="PATCH",g="update",h=e+"/"+a.id());var i=new XMLHttpRequest;i.dg={service:e,resource:g},i.open(f,jDrupal.restPath()+h),i.setRequestHeader("Content-type","application/json"),i.setRequestHeader("X-CSRF-Token",d),i.onload=function(){a.postSave(i).then(function(){if("POST"==f&&201==i.status||"PATCH"==f&&204==i.status||200==i.status){var a=jDrupal.moduleInvokeAll("rest_post_process",i);a?a.then(b(i)):b(i)}else c(i)})},i.onerror=function(){c(Error("Network Error"))},i.send(a.stringify())})})})},jDrupal.Entity.prototype.postSave=function(a){var b=this;return new Promise(function(c,d){if(b.isNew()&&a.getResponseHeader("Location")){var e=a.getResponseHeader("Location").split("/");b.entity[b.getEntityKey("id")]=[{value:e[e.length-1]}]}c()})},jDrupal.Entity.prototype.preDelete=function(a){return new Promise(function(a,b){a()})},jDrupal.Entity.prototype.delete=function(a){var b=this;return new Promise(function(a,c){b.preDelete().then(function(){jDrupal.token().then(function(d){var e=b.getEntityType(),f=jDrupal.restPath()+e+"/"+b.id(),g={};g[b.getEntityKey("bundle")]=[{target_id:b.getBundle()}];var h=new XMLHttpRequest;h.dg={service:e,resource:"delete"},h.open("DELETE",f),h.setRequestHeader("Content-type","application/json"),h.setRequestHeader("X-CSRF-Token",d),h.onload=function(){b.postDelete(h).then(function(){if(204==h.status){var b=jDrupal.moduleInvokeAll("rest_post_process",h);b?b.then(a(h)):a(h)}else c(h)})},h.onerror=function(){c(Error("Network Error"))},h.send(JSON.stringify(g))})})})},jDrupal.Entity.prototype.postDelete=function(a){var b=this;return new Promise(function(a,c){b.entity=null,a()})},jDrupal.entityConstructorPrep=function(a,b){if(b)if("object"==typeof b)a.entity=b;else{var c=a.getEntityKey("id"),d={};d[c]=[{value:b}],a.entity=d}else;},jDrupal.Comment=function(a){this.entityKeys.type="comment",this.entityKeys.bundle="comment_type",this.entityKeys.id="cid",this.entityKeys.label="subject",jDrupal.entityConstructorPrep(this,a)},jDrupal.Comment.prototype=new jDrupal.Entity,jDrupal.Comment.prototype.constructor=jDrupal.Comment,jDrupal.Comment.prototype.getSubject=function(){return this.entity.subject[0].value},jDrupal.Comment.prototype.setSubject=function(a){try{this.entity.subject[0].value=a}catch(b){console.log("jDrupal.Comment.setSubject - "+b)}},jDrupal.Comment.prototype.preSave=function(a){return new Promise(function(a,b){a()})},jDrupal.Comment.prototype.stringify=function(){try{if(!this.isNew()){for(var a=JSON.parse(JSON.stringify(this.entity)),b=["hostname","changed","cid","thread","uuid","entity_id","entity_type","pid","field_name","created","name","mail","homepage"],c=0;c<b.length;c++)void 0!==a[b[c]]&&delete a[b[c]];return JSON.stringify(a)}return JSON.stringify(this.entity)}catch(d){console.log("jDrupal.Comment.stringify - "+d)}},jDrupal.Node=function(a){this.entityKeys.type="node",this.entityKeys.bundle="type",this.entityKeys.id="nid",this.entityKeys.label="title",jDrupal.entityConstructorPrep(this,a),this.entity&&(this.entity.title||(this.entity.title=[{value:""}]))},jDrupal.Node.prototype=new jDrupal.Entity,jDrupal.Node.prototype.constructor=jDrupal.Node,jDrupal.Node.prototype.getTitle=function(){return this.label()},jDrupal.Node.prototype.setTitle=function(a){try{this.entity.title[0].value=a}catch(b){console.log("jDrupal.Node.setTitle - "+b)}},jDrupal.Node.prototype.getType=function(){return this.getBundle()},jDrupal.Node.prototype.getCreatedTime=function(){return this.entity.created[0].value},jDrupal.Node.prototype.isPromoted=function(){return this.entity.promote[0].value},jDrupal.Node.prototype.isPublished=function(){return this.entity.status[0].value},jDrupal.Node.prototype.isSticky=function(){return this.entity.sticky[0].value},jDrupal.Node.prototype.preSave=function(a){var b=this;return new Promise(function(a,c){for(var d=["changed","revision_timestamp","revision_uid"],e=0;e<d.length;e++)delete b.entity[d[e]];a()})},jDrupal.User=function(a){this.entityKeys.type="user",this.entityKeys.bundle="user",this.entityKeys.id="uid",this.entityKeys.label="name",jDrupal.entityConstructorPrep(this,a)},jDrupal.User.prototype=new jDrupal.Entity,jDrupal.User.prototype.constructor=jDrupal.User,jDrupal.User.prototype.getAccountName=function(){return this.label()},jDrupal.User.prototype.getRoles=function(){for(var a=(this.entity.roles,[]),b=0;b<this.entity.roles.length;b++)a.push(this.entity.roles[b].target_id);return a},jDrupal.User.prototype.hasRole=function(a){return jDrupal.inArray(a,this.getRoles())},jDrupal.User.prototype.isAnonymous=function(){return 0==this.id()},jDrupal.User.prototype.isAuthenticated=function(){return!this.isAnonymous()},jDrupal.currentUser=function(){return jDrupal._currentUser},jDrupal.User.prototype.postLoad=function(a){var b=this;return new Promise(function(a,c){b.entity.roles||(b.entity.roles=[{target_id:"authenticated"}]),a()})},jDrupal.userDefaults=function(){return new jDrupal.User({uid:[{value:0}],roles:[{target_id:"anonymous"}]})},jDrupal.setCurrentUser=function(a){a.isAuthenticated()&&!jDrupal.inArray("authenticated",a.getRoles())&&a.entity.roles.push({target_id:"authenticated"}),jDrupal._currentUser=a},jDrupal.userPassword=function(){var a=10;arguments[0]&&(a=arguments[0]);for(var b="",c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz23456789",d=0;d<a;d++)b+=c.charAt(Math.floor(Math.random()*c.length));return b};