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