-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathjdrupal.min.js
1 lines (1 loc) · 14.4 KB
/
jdrupal.min.js
1
var jDrupal="undefined"==typeof 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="undefined"!=typeof arguments[1]?arguments[1]:null;return e?void(jDrupal.settings[t]=e):jDrupal.settings[t]},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:"undefined"==typeof t||null===t||""==t},jDrupal.functionExists=function(name){return"function"==eval("typeof "+name)},jDrupal.inArray=function(t,e){try{if("undefined"==typeof e)return!1;if("string"==typeof t)return e.indexOf(t)>-1;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"string"==typeof t&&(t=parseInt(t)),"number"==typeof t&&t%1==0},jDrupal.isPromise=function(t){return Promise.resolve(t)==t},jDrupal.shuffle=function(t){for(var e=t.length-1;e>0;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){t+="";var e=t.charAt(0).toLowerCase();return e+t.substr(1)},jDrupal.ucfirst=function(t){t+="";var e=t.charAt(0).toUpperCase();return e+t.substr(1)},jDrupal.Module=function(){this.name=null},jDrupal.moduleExists=function(t){return"undefined"!=typeof jDrupal.modules[t]},jDrupal.moduleImplements=function(t){var e=[];for(var r in jDrupal.modules)jDrupal.modules.hasOwnProperty(r)&&jDrupal.functionExists(r+"_"+t)&&e.push(r);return!!e.length&&e},jDrupal.moduleInvoke=function(t,e){if(jDrupal.moduleLoad(t)){var r=t+"_"+e;if(jDrupal.functionExists(r)){var n=window[r],o=Array.prototype.slice.call(arguments);return o.splice(0,2),0==Object.getOwnPropertyNames(o).length?n():n.apply(null,o)}}},jDrupal.moduleInvokeAll=function(t){var e=[],r=Array.prototype.slice.call(arguments);r.splice(0,1);var n=[];for(var o in jDrupal.modules)jDrupal.modules.hasOwnProperty(o)&&jDrupal.functionExists(o+"_"+t)&&n.push(o);if(jDrupal.isEmpty(n))return Promise.resolve();for(var u=0;u<n.length;u++)0==r.length?e.push(jDrupal.moduleInvoke(n[u],t)):(r.unshift(n[u],t),e.push(jDrupal.moduleInvoke.apply(null,r)),r.splice(0,2));return Promise.all(e)},jDrupal.moduleLoad=function(t){return!!jDrupal.modules[t]&&jDrupal.modules[t]},jDrupal.modulesLoad=function(){return jDrupal.modules},function(t){XMLHttpRequest.prototype.send=function(e){var r=this,n=jDrupal.moduleInvokeAll("rest_pre_process",this,e);n?n.then(function(){t.call(r,e)}):t.call(this,e)}}(XMLHttpRequest.prototype.send),jDrupal.getResultFromRequest=function(t){return JSON.parse("undefined"!=typeof t.responseText?t.responseText:t.response)},jDrupal.token=function(){return new Promise(function(t,e){var r=new XMLHttpRequest;r.dg={service:"system",resource:"token"},r.open("GET",jDrupal.restPath()+"session/token"),r.onload=function(){if(200==r.status){var n=jDrupal.moduleInvokeAll("rest_post_process",r);n?n.then(t(r.response)):t(r.response)}else e(r)},r.onerror=function(){e(Error("Network Error"))},r.send()})},jDrupal.connect=function(){return new Promise(function(t,e){var r=new XMLHttpRequest;r.dg={service:"system",resource:"connect"},r.open("GET",jDrupal.restPath()+"jdrupal/connect?_format=json");var n=function(){jDrupal.connected=!0;var e=JSON.parse("undefined"!=typeof r.responseText?r.responseText:r.response);0==e.uid?(jDrupal.setCurrentUser(jDrupal.userDefaults()),t(e)):jDrupal.userLoad(e.uid).then(function(r){jDrupal.setCurrentUser(r),t(e)})};r.onload=function(){if(200!=r.status)return void e(r);var t=jDrupal.moduleInvokeAll("rest_post_process",r);t?t.then(n):n()},r.onerror=function(){e(Error("Network Error"))},r.send()})},jDrupal.userLogin=function(t,e){return new Promise(function(r,n){var o=new XMLHttpRequest;o.dg={service:"user",resource:"login"},o.open("POST",jDrupal.restPath()+"user/login?_format=json"),o.setRequestHeader("Content-type","application/json");var u=function(){jDrupal.connect().then(r)};o.onload=function(){if(200==o.status){var t=jDrupal.moduleInvokeAll("rest_post_process",o);t?t.then(u):u()}else n(o)},o.onerror=function(){n(Error("Network Error"))},o.send(JSON.stringify({name:t,pass:e}))})},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");var n=function(){jDrupal.setCurrentUser(jDrupal.userDefaults()),jDrupal.connect().then(t)};r.onload=function(){if(200==r.status||303==r.status){var t=jDrupal.moduleInvokeAll("rest_post_process",r);t?t.then(n):n()}else e(r)},r.onerror=function(){e(Error("Network Error"))},r.send()})},jDrupal.entityLoad=function(t,e){var r=new(this[this.ucfirst(t)])(e);return r.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(t,e,r){return new Promise(function(n,o){var u=new XMLHttpRequest;u.dg={service:"user",resource:"register"},u.open("POST",jDrupal.restPath()+"user/register?_format=json"),u.setRequestHeader("Content-type","application/json");var i=function(){jDrupal.connect().then(n)};u.onload=function(){if(200==u.status){var t=jDrupal.moduleInvokeAll("rest_post_process",u);t?t.then(i):i()}else o(u)},u.onerror=function(){o(Error("Network Error"))},u.send(JSON.stringify({name:{value:t},pass:{value:e},mail:{value:r}}))})},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 t=this;return new Promise(function(e,r){var n=new XMLHttpRequest;n.dg={service:"views",resource:null},n.open("GET",jDrupal.restPath()+t.getPath());var o=function(){t.results=JSON.parse(n.response),e()};n.onload=function(){if(200==n.status){var t=jDrupal.moduleInvokeAll("rest_post_process",n);t?t.then(o):o()}else r(n)},n.onerror=function(){r(Error("Network Error"))},n.send()})},jDrupal.viewsLoad=function(t){return new Promise(function(e,r){var n=new jDrupal.Views(t);n.getView().then(function(){e(n)})})},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&&"undefined"!=typeof this.entity[t]?"undefined"!=typeof e?this.entity[t][e]:this.entity[t]:null},jDrupal.Entity.prototype.set=function(t,e,r){this.entity&&("undefined"!=typeof e&&"undefined"!=typeof this.entity[t]?this.entity[t][e]=r:this.entity[t]=r)},jDrupal.Entity.prototype.getEntityKey=function(t){return"undefined"!=typeof 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"undefined"!=typeof this.entity[t]?this.entity[t][0].target_id:null},jDrupal.Entity.prototype.id=function(){var t=this.getEntityKey("id");return"undefined"!=typeof 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"undefined"!=typeof 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 t=this,e=t.getEntityType();return new Promise(function(r,n){t.preLoad().then(function(){var o=jDrupal.restPath()+e+"/"+t.id()+"?_format=json",u=new XMLHttpRequest;u.dg={service:e,resource:"retrieve"},u.open("GET",o);var i=function(){t.entity=JSON.parse(u.response),t.postLoad(u).then(function(){r(t)})};u.onload=function(){if(200==u.status){var t=jDrupal.moduleInvokeAll("rest_post_process",u);t?t.then(i):i()}else n(u)},u.onerror=function(){n(Error("Network Error"))},u.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 t=this;return new Promise(function(e,r){t.preSave().then(function(){jDrupal.token().then(function(n){var o=t.getEntityType(),u=null,i=null,s=null,a=t.isNew();a?(u="POST",i="create",s=o):(u="PATCH",i="update",s=o+"/"+t.id()),s+="?_format=json";var l=new XMLHttpRequest;l.dg={service:o,resource:i},l.open(u,jDrupal.restPath()+s),l.setRequestHeader("Content-type","application/json"),l.setRequestHeader("X-CSRF-Token",n),l.onload=function(){t.postSave(l).then(function(){if("POST"==u&&201==l.status||"PATCH"==u&&204==l.status||200==l.status){var t=jDrupal.moduleInvokeAll("rest_post_process",l);t?t.then(e(l)):e(l)}else r(l)})},l.onerror=function(){r(Error("Network Error"))},l.send(t.stringify())})})})},jDrupal.Entity.prototype.postSave=function(t){var e=this;return new Promise(function(r,n){if(e.isNew()&&t.getResponseHeader("Location")){var o=t.getResponseHeader("Location").split("/");e.entity[e.getEntityKey("id")]=[{value:o[o.length-1]}]}r()})},jDrupal.Entity.prototype.preDelete=function(t){return new Promise(function(t,e){t()})},jDrupal.Entity.prototype.delete=function(t){var e=this;return new Promise(function(t,r){e.preDelete().then(function(){jDrupal.token().then(function(n){var o=e.getEntityType(),u=jDrupal.restPath()+o+"/"+e.id(),i={};i[e.getEntityKey("bundle")]=[{target_id:e.getBundle()}];var s=new XMLHttpRequest;s.dg={service:o,resource:"delete"},s.open("DELETE",u),s.setRequestHeader("Content-type","application/json"),s.setRequestHeader("X-CSRF-Token",n),s.onload=function(){e.postDelete(s).then(function(){if(204==s.status){var e=jDrupal.moduleInvokeAll("rest_post_process",s);e?e.then(t(s)):t(s)}else r(s)})},s.onerror=function(){r(Error("Network Error"))},s.send(JSON.stringify(i))})})})},jDrupal.Entity.prototype.postDelete=function(t){var e=this;return new Promise(function(t,r){e.entity=null,t()})},jDrupal.entityConstructorPrep=function(t,e){if(e)if("object"==typeof e)t.entity=e;else{var r=t.getEntityKey("id"),n={};n[r]=[{value:e}],t.entity=n}else;},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()){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++)"undefined"!=typeof t[e[r]]&&delete t[e[r]];return JSON.stringify(t)}return JSON.stringify(this.entity)}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 e=this;return new Promise(function(t,r){for(var n=["changed","revision_timestamp","revision_uid"],o=0;o<n.length;o++)delete e.entity[n[o]];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(){for(var t=(this.entity.roles,[]),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 e=this;return new Promise(function(t,r){e.entity.roles||(e.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};