UNPKG

bootstrap-help-manager

Version:

Plugin and management console to create and manage help icons and content across an entire site.

5 lines 5.75 kB
/** * VertebrateJS v 0.3.0 * https://github.com/psalmody/vertebratejs */ var Vertebrate=function($){if("undefined"==typeof $)return console.log("%cError:","color:red;font-weight:bold","Vertebrate.js requires jQuery 1.11+"),!1;var Vertebrate={settings:{},get:function(setting){return"undefined"==typeof setting?this.settings:this.settings[setting]},set:function(setting,value){return this.settings[setting]=value},shared:{fn:function(){this.attributes={},this.changedattrs=[],this.set=function(attr,value){return this.attributes[attr]=value,this.changedattrs.push(attr),$([this]).trigger("vertebrate:changeattr",[this,this.attributes,this.changedattrs]),$(document).trigger("vertebrate:changeattr",[this,this.attributes,this.changedattrs]),!0},this.get=function(attr){return"undefined"==typeof attr?this.attributes:this.attributes[attr]},this.has=function(attr){return"undefined"==typeof this.attributes[attr]?!1:!0},this.hasChanged=function(attr){var attr="undefined"==typeof attr?!1:attr;return attr?this.changedattrs.indexOf(attr)>=0?!0:!1:this.changedattrs.length>0?!0:!1}}}};return Vertebrate.Model=function(options){var self=this;Vertebrate.shared.fn.call(this);var options="undefined"==typeof options?!1:options;options&&$.each(options,function(k,v){"object"==typeof v?self[k]=$.extend(self[k],v):self[k]=v}),this.save=function(callback){var promise=$.ajax({url:"undefined"==typeof this.url?Vertebrate.get("url"):this.url,method:"POST",data:{model:this.get()},success:function(data,status,xhr){self.changedattrs=[],"function"==typeof callback&&callback.call(self,data,status,xhr)}});return promise},this.fetch=function(callback){var promise=$.ajax({url:"undefined"==typeof this.url?Vertebrate.get("url"):this.url,method:"GET",data:{model:this.get()},success:function(data,status,xhr){self.changedattrs=[],self.attributes=$.extend(self.attributes,data[0]),"function"==typeof callback&&callback.call(self,data,status,xhr)}});return promise},this["delete"]=function(callback){var promise=$.ajax({url:"undefined"==typeof this.url?Vertebrate.get("url"):this.url,method:"DELETE",data:{model:this.get()},success:function(data,status,xhr){$([self]).trigger("vertebrate:deleted",[self,self.attributes,data,status,xhr]),$(document).trigger("vertebrate:deleted",[self,self.attributes,data,status,xhr]),"function"==typeof callback&&callback.call(self,data,status,xhr)}});return promise}},Vertebrate.Model.prototype=Object.create(Vertebrate.shared.fn),Vertebrate.Model.prototype.constructor=Vertebrate.Model,Vertebrate.Model.Extend=function(options){var model=function(opts){Vertebrate.Model.call(this,options);var self=this,opts="undefined"==typeof opts?!1:opts;return opts&&(self.attributes=$.extend(self.attributes,opts)),this};return model.prototype=Object.create(Vertebrate.Model.prototype),model.prototype.constructor=model,model},Vertebrate.Collection=function(options){Vertebrate.shared.fn.call(this,options),this.model=!1,this.models=[],this.added=[],this.removed=[];var self=this;$.each(options,function(k,v){"object"==typeof v?$.extend(self[k],v):self[k]=v}),this.save=function(callback){var postdata={collection:{attributes:this.get(),models:JSON.stringify(this.models)}},promise=$.ajax({url:"undefined"==typeof this.url?Vertebrate.get("url"):this.url,method:"POST",data:postdata,success:function(data,status,xhr){self.removed=[],"function"==typeof callback&&callback.call(self,data,status,xhr)}});return promise},this.fetch=function(callback){if(!self.model)return console.log("No model defined. Models must be defined before fetch call."),!1;var promise=$.ajax({url:"undefined"==typeof this.url?Vertebrate.get("url"):this.url,method:"GET",dataType:"JSON",data:{collection:this.get()},success:function(data,status,xhr){self.models=[],$.each(data,function(i){self.models.push(new self.model(data[i]))}),self.removed=[],$([self]).trigger("vertebrate:fetched",[self.models]),$(document).trigger("vertebrate:fetched",[self,self.models]),"function"==typeof callback&&callback.call(self,data,status,xhr)}});return promise},this.find=function(term,attr){var attr="undefined"==typeof attr?!1:attr,found=!1;return attr?($.each(self.models,function(k,v){return v.attributes[attr]==term?(found=this,!1):void 0}),found?found:!1):self.models[term]},this.findAll=function(term,attr){var found=!1,founds=[];return $.each(self.models,function(k,v){v.attributes[attr]==term&&(found=!0,founds.push(this))}),found?founds:!1},this.count=function(){return self.models.length},this.add=function(model){return self.added.push(model),self.models.push(model),$([self]).trigger("vertebrate:added",[model,self.added]),$(document).trigger("vertebrate:added",[self,model,self.added]),self.models.length},this.remove=function(model){var newmodels=self.models.filter(function(m){return m!=model});return self.models=newmodels,self.removed.push(model),$([self]).trigger("vertebrate:removed",[model,self.removed]),$(document).trigger("vertebrate:removed",[self,model,self.removed]),self.models.length},this.max=function(attr){if("undefined"==typeof attr)return!1;var greatest=0;return $.each(self.models,function(k,v){Number(v.attributes[attr])>greatest&&(greatest=v.attributes[attr])}),greatest},this.next=function(attr){var m=self.max(attr);return Number(m)+1}},Vertebrate.Collection.prototype=Object.create(Vertebrate.shared.fn),Vertebrate.Collection.prototype.constructor=Vertebrate.Collection,Vertebrate.Collection.Extend=function(options){function collection(opts){Vertebrate.Collection.call(this,options);var self=this;"undefined"!=typeof opts&&$.each(opts,function(k,v){"object"==typeof v?$.extend(self[k],v):self[k]=v})}return collection.prototype=Object.create(Vertebrate.Collection.prototype),collection.prototype.constructor=collection,collection},Vertebrate}($);