UNPKG

bootstrap-help-manager

Version:

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

5 lines 11 kB
/** * Bootstrap-Help-Manager v 0.5.2 * https://github.com/psalmody/Bootstrap-Help-Manager */ !function($){$.fn.JSONTable=function(options){function formatData(data,status,xhr){if(data.length<1)return"function"==typeof settings.nodata&&settings.nodata(table,status,xhr),self;if("TABLE"!=self.prop("tagName")?(div=self,div.empty(),table=$('<table class="table '+settings.tableClasses+'"><thead></thead><tbody></tbody></table>'),div.append(table),settings.responsive&&div.addClass("table-responsive"),table.hide()):(table=self,div=self.closest("div"),settings.tableClasses&&table.addClass(settings.tableClasses),settings.responsive&&div.addClass("table-responsive")),table.find("thead").length>0?thead=table.find("thead"):(thead=$("<thead></thead>"),table.append(thead)),table.find("tbody").length>0?(tbody=table.find("tbody"),tbody.empty()):(tbody=$("<tbody></tbody>"),table.append(tbody)),thead.find("tr").length<1){var tr=$("<tr></tr>");thead.append(tr),settings.columns.length>0?$.each(settings.columns,function(i){var th=$("<th></th>");tr.append(th),th.html(settings.columns[i])}):$.each(data[0],function(k,v){var th=$("<th></th>");tr.append(th),th.html(k)})}return $(data).each(function(){if(settings.template)return void tbody.append(BHM.tmpl(settings.template.html(),$.extend(this,settings.templateParams)));var tr=$("<tr></tr>");tbody.append(tr),$.each(this,function(k,v){var td=$("<td></td>");(settings.noWraps.indexOf(k)>=0||settings.noWraps.indexOf("allrows")>=0)&&td.addClass("noWrap"),tr.append(td),td.html(v)})}),table.show(),"function"==typeof settings.success&&settings.success(table,status,xhr),self}var settings=$.extend({},{url:!1,data:!1,method:"GET",tableClasses:"table-condensed table-striped",responsive:!1,dataType:"JSON",appendTo:!1,noWraps:[],success:!1,nodata:!1,columns:[],options:{},template:!1,ajaxstatus:!1,templateParams:{}},options);if(!settings.url&&!settings.data)return void console.log("url or data must be specified for JSONTable plugin.");var table,thead,tbody,div,self=this;settings.data?formatData(settings.data,"local, no ajax",!1):$.ajax({method:settings.method,url:settings.url,data:settings.options,dataType:settings.dataType}).done(function(data,status,xhr){formatData(data,status,xhr)}).fail(function(xhr,status,error){"function"==typeof settings.fail&&settings.fail(xhr,status,error)})}}(jQuery);var BHM=function(my){var cache={};return my.tmpl=function tmpl(str,data){var fn=/\W/.test(str)?new Function("obj","var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('"+str.replace(/[\r\t\n]/g," ").split("<%").join(" ").replace(/((^|%>)[^\t]*)'/g,"$1\r").replace(/\t=(.*?)%>/g,"',$1,'").split(" ").join("');").split("%>").join("p.push('").split("\r").join("\\'")+"');}return p.join('');"):cache[str]=cache[str]||tmpl(document.getElementById(str).innerHTML);return data?fn(data):fn},my}(BHM||{}),BHM=function(Vertebrate,$,my){return my.helper=Vertebrate.Model.Extend({attributes:{id:-1,field_selecter:"",title:"",large:!1,html:"",page_ids:""}}),my.page=Vertebrate.Model.Extend({attributes:{id:-1,url:""}}),my.helpers=Vertebrate.Collection.Extend({model:my.helper}),my.pages=Vertebrate.Collection.Extend({model:my.page}),my.cp=new my.pages,my.ch=new my.helpers,my}(Vertebrate,jQuery,BHM||{}),BHM=function(Vertebrate,$,my){var clean=function(what){return"string"!=typeof what?what:what.replace(/([.])|([/])|([ ])/g,"")},renderPage=function(model){var $el=BHM.mc.$el;cleanfilename=clean(model.get("url")),$el.find(".panel-group").append(BHM.tmpl($("#templatePanel").html(),$.extend({},{clean:cleanfilename},model.attributes))),$el.find(".panel-collapse:first").addClass("in")},renderHelps=function(model){var panel=(BHM.mc.$el,$("#bhmpanel"+model.get("id")+" .panel-body")),cols=BHM.mc.settings.columns.slice(0);cols.push(BHM.mc.settings.addButton);var jsondata=[],helps=[],pageid=model.get("id");$.each(BHM.ch.models,function(k,v){this.get("page_ids").indexOf(pageid)>-1&&helps.push(this)}),$.each(helps,function(){jsondata.push(this.attributes)}),panel.JSONTable({data:jsondata,template:$("#templateHelperRow"),columns:cols,success:function(){$(".helpHTML").hide()}})},renderHelp=function(model){BHM.mc.$el;if(!$("#bhmpanel"+model.get("page_ids")+" .panel-body tbody").length){var page=BHM.cp.find(model.get("page_ids"),"id");return renderHelps(page),!0}var page_ids=model.get("page_ids").split(",");$.each(page_ids,function(k,v){var prependto=!1;$("#bhmpanel"+v+" .panel-body tbody tr").each(function(){var help=BHM.ch.find($(this).data("help-id"),"id");return model.get("field_selecter")>help.get("field_selecter")?!0:(prependto=$(this),!1)});var newrow=BHM.tmpl($("#templateHelperRow").html(),model.get());prependto?prependto.before(newrow):$("#bhmpanel"+v+" .panel-body tbody").append(newrow)}),page_ids.length>1&&$(".help"+model.get("id")).addClass("info")};return my.cp=BHM.cp||{},my.cp.render=function(){var models=this.models,$el=BHM.mc.$el;$el.find("ul").empty(),$.each(models,function(){renderPage(this)}),$el.prepend($("#templateOpeningText").html())},my.ch=BHM.ch||{},my.ch.render=function(){var pages=BHM.cp.models;BHM.mc.$el;$.each(pages,function(){renderHelps(this)})},my.renderHelp=function(model){return renderHelp(model)},my.renderPage=function(model){return renderPage(model)},my.clean=function(filename){return clean(filename)},my.mc={settings:{addButton:'<button class="btn btn-sm btn-default btn-block addHelper">Add</button>',columns:["Field Selecter","Modal Title","Size","Content","Save"],ajaxFail:!1,templateurl:"",helpersurl:"",pagesurl:""},$el:"",render:function(){var self=this;BHM.ch.url=this.settings.helpersurl,BHM.cp.url=this.settings.pagesurl,BHM.helper.prototype.url=this.settings.helpersurl,BHM.page.prototype.url=this.settings.pagesurl;var dfd=$.get(self.settings.templateurl);$.when(dfd).then(function(data){$("body").append(data),CKEDITOR.replace("bhmTextareaEditor"),self.$el.append(BHM.tmpl($("#templatePanelGroup").html(),{}))}).then(function(){return BHM.cp.fetch()}).then(function(){return BHM.ch.fetch()})}},my}(Vertebrate,jQuery,BHM||{});!function(CKEDITOR,$){var cssfiles=$(document).find('link[rel="stylesheet"]'),arrcss=["body{padding:5px;}"];cssfiles.each(function(){arrcss.push($(this).attr("href"))}),CKEDITOR.config.contentsCss=arrcss,CKEDITOR.config.height=500,CKEDITOR.config.htmlEncodeOutput=!1,CKEDITOR.config.entities=!1}(CKEDITOR,jQuery),function($){$.fn.BHMConsole=function(opts){var mc=BHM.mc;mc.settings=$.extend({},mc.settings,opts),mc.$el=this,mc.render();var self=this,getPageFor=function($obj){return BHM.cp.find($obj.closest(".panel-default").data("pageid"),"id")},getHelpFor=function($obj){return BHM.ch.find($obj.closest("tr").data("help-id"),"id")},getElForHelp=function(model){return $(".help"+model.get("id"))};$(document).on("vertebrate:fetched",function(e,c,ms){c.render()}).on("vertebrate:changeattr",function(e,m,mattr,mchanged){return m.has("url")?!1:void getElForHelp(m).find(".saveHelp").addClass("btn-warning")}),$(this).on("change",'input[type="text"]',function(){var model=getHelpFor($(this)),attr=$(this).data("attr"),val=$(this).val();return val==model.get(attr)?!1:void model.set($(this).data("attr"),$(this).val())}).on("click",'input[type="checkbox"]',function(){var val=$(this).is(":checked")?1:0,model=getHelpFor($(this)),old=model.get($(this).data("attr"));return val==old?!1:void model.set($(this).data("attr"),val)}).on("click",".saveHelp",function(){var help=getHelpFor($(this));help.save();var el=getElForHelp(help);el.find(".saveHelp").removeClass("btn-warning")}).on("click",".deleteHelp",function(){var sure=confirm("Are you sure you want to delete this row?");if(!sure)return!1;var model=getHelpFor($(this)),page=getPageFor($(this)),modelel=getElForHelp(model),panel=modelel.closest(".panel");BHM.ch.remove(model),$.when(model["delete"]()).done(function(){var help_pages=model.get("page_ids").split(",");modelel.fadeOut(300,function(){modelel.remove(),$.each(help_pages,function(){panel.find("tbody tr").length||(page["delete"](),panel.fadeOut(300,function(){$(this).remove(),self.find(".panel-title").children("a").click()}))})})})}).on("click",".addHelper",function(){var page=getPageFor($(this)),model=new BHM.helper({id:BHM.ch.next("id").toString(),page_ids:page.get("id")});BHM.ch.add(model),BHM.renderHelp(model),getElForHelp(model).find(".saveHelp").addClass("btn-warning")}).on("click",".editHelp",function(){var model=getHelpFor($(this)),modal=$("#bhmEditHtmlModal");$("#bhmEditHtmlModalFieldselecter").text(model.get("field_selecter")),CKEDITOR.instances.bhmTextareaEditor.setData(model.get("html")),modal.data("helpId",model.get("id")),modal.modal()}).on("click","#BHMaddPage",function(){var url=prompt("Enter the relative url of the page to add to:");if(url.length<1)return!1;var id=BHM.cp.next("id").toString(),page=new BHM.page({id:id,url:url}),help=new BHM.helper({id:BHM.ch.next("id"),page_ids:id});BHM.cp.add(page),BHM.ch.add(help),BHM.renderPage(page),BHM.renderHelp(help),page.save(),help.save(),$("#bhmpanelheader"+page.get("id")+" a:first").click()}).on("click",".bhm-change-url",function(){page=getPageFor($(this));var newurl=prompt("Enter the new url:",page.get("url"));return newurl?(page.set("url",newurl),page.save(),void $(this).closest(".panel-title").children("a").text(newurl)):!1}).on("click",".addToPages",function(){help=getHelpFor($(this));var modal=$("#bhmSelectMultipleModal");modal.data("helpid",help.get("id"));var pages=BHM.cp.models,rows=[];$.each(pages,function(){var checkbox='<input type="checkbox" value="'+this.get("id")+'">',row={checkbox:checkbox,url:this.get("url")};rows.push(row)}),modal.find(".modal-body").JSONTable({data:rows,columns:["Appears On:","Page:"]}),modal.find('.modal-body tbody input[type="checkbox"]').each(function(){help.get("page_ids").indexOf($(this).val())>-1&&$(this).attr("checked",!0)}),modal.modal()}),$("body").on("click","#bhmEditHtmlModal .btn-save-html",function(){var modal=$("#bhmEditHtmlModal"),model=BHM.ch.find(modal.data("helpId"),"id");model.set("html",CKEDITOR.instances.bhmTextareaEditor.getData()),CKEDITOR.instances.bhmTextareaEditor.setData(""),$("#bhmEditHtmlModal").modal("hide")}),$("body").on("click","#bhmSelectMultipleModal .btn-save-page-ids",function(){var modal=$("#bhmSelectMultipleModal"),model=BHM.ch.find(modal.data("helpid"),"id"),newpageids=[];if(!modal.find('input[type="checkbox"]:checked').length)return alert("At least one checkbox must be selected."),!1;modal.find('input[type="checkbox"]:checked').each(function(){newpageids.push($(this).val())});var el=getElForHelp(model);newpageids.length>1?el.addClass("info"):el.removeClass("info"),newpageids=newpageids.join(","),model.set("page_ids",newpageids),modal.modal("hide"),el.remove(),BHM.renderHelp(model);var newel=getElForHelp(model);newel.find(".saveHelp").addClass("btn-warning"),newel.first().is(":visible")||newel.first().closest(".panel").find(".panel-title").children("a").click()})}}(jQuery),$(document).on("focusin",function(e){$(e.target).closest(".cke_dialog_body").length&&e.stopImmediatePropagation()});