plexpy/data/interfaces/default/js/bootstrap-wizard.min.js
Tim 4f00ecc070 A new first run setup wizard (WIP)
Move all user related links to use user_id instead of username.
Remove excess debug loggin.
Catch more exceptions on PW importer.
2015-07-17 00:31:46 +02:00

2 lines
18 KiB
JavaScript
Executable File

(function(e){e.fn.wizard=function(e){return new Wizard(this,e)};e.fn.wizard.logging=false;var t=function(e,t,n,r,i){this.wizard=e;this.index=n;this.prev=r;this.next=i;this.el=t;this.title=t.find("h3").first().text();this.name=t.data("cardname")||this.title;this.nav=this._createNavElement(this.title,n);this._disabled=false;this._loaded=false;this._events={}};t.prototype={select:function(){this.log("selecting");if(!this.isSelected()){this.nav.addClass("active");this.el.show();if(!this._loaded){this.trigger("loaded");this.reload()}this.trigger("selected")}var e=this.wizard;e.backButton.toggleClass("disabled",this.index==0);if(this.index>=e._cards.length-1){this.log("on last card, changing next button to submit");e.changeNextButton(e.args.buttons.submitText,"btn-success");e._readyToSubmit=true;e.trigger("readySubmit")}else{e._readyToSubmit=false;e.changeNextButton(e.args.buttons.nextText,"btn-primary")}return this},_createNavElement:function(t,n){var r=e('<li class="wizard-nav-item"></li>');var i=e('<a class="wizard-nav-link"></a>');i.data("navindex",n);r.append(i);i.append('<span class="glyphicon glyphicon-chevron-right"></span> ');i.append(t);return r},markVisited:function(){this.log("marking as visited");this.nav.addClass("already-visited");this.trigger("markVisited");return this},unmarkVisited:function(){this.log("unmarking as visited");this.nav.removeClass("already-visited");this.trigger("unmarkVisited");return this},deselect:function(){this.nav.removeClass("active");this.el.hide();this.trigger("deselect");return this},enable:function(){this.log("enabling");this.nav.addClass("active");this._disabled=false;this.trigger("enabled");return this},disable:function(e){this.log("disabling");this._disabled=true;this.nav.removeClass("active already-visited");if(e){this.el.hide()}this.trigger("disabled");return this},isDisabled:function(){return this._disabled},alreadyVisited:function(){return this.nav.hasClass("already-visited")},isSelected:function(){return this.nav.hasClass("active")},reload:function(){this._loaded=true;this.trigger("reload");return this},on:function(){return this.wizard.on.apply(this,arguments)},trigger:function(){this.callListener("on"+arguments[0]);return this.wizard.trigger.apply(this,arguments)},toggleAlert:function(t,n){this.log("toggling alert to: "+n);n=typeof n=="undefined"?true:n;if(n){this.trigger("showAlert")}else{this.trigger("hideAlert")}var r;var i=this.el.children("h3").first().next("div.alert");if(i.length==0){if(!n){return this}this.log("couldn't find existing alert div, creating one");r=e("<div />");r.addClass("alert");r.addClass("hide");r.insertAfter(this.el.find("h3").first())}else{this.log("found existing alert div");r=i.first()}if(n){if(t!=null){this.log("setting alert msg to",t);r.html(t)}r.show()}else{r.hide()}return this},callListener:function(e){e=e.toLowerCase();this.log("looking for listener "+e);var t=window[this.el.data(e)];if(t){this.log("calling listener "+e);var n=this.wizard;try{var r=t(this)}catch(i){this.log("exception calling listener "+e+": ",i)}}else{this.log("didn't find listener "+e)}},problem:function(e){this.nav.find("a").toggleClass("wizard-step-error",e)},validate:function(){var t=false;var n=this;this.el.find("[data-validate]").each(function(r,i){n.log("validating individiual inputs");i=e(i);var s=i.data("validate");if(!s){return}var o={status:true,title:"Error",msg:""};var u=window[s](i);e.extend(o,u);if(e("#btn-"+i.attr("id")).length===1){i=e("#btn-"+i.attr("id"))}if(!o.status){t=true;i.parents("div.form-group").toggleClass("has-error",true);if(e("#btn-"+i.attr("id")).length===1){i=e("#btn-"+i.attr("id"))}n.wizard.errorPopover(i,o.msg)}else{i.parents("div.form-group").toggleClass("has-error",false);if(e("#btn-"+i.attr("id")).length===1){i=e("#btn-"+i.attr("id"))}try{i.popover("destroy")}catch(a){i.popover("hide")}}});this.log("after validating inputs, failures is",t);var r=window[this.el.data("validate")];if(r){this.log("running html-embedded card validator");var i=r(this);if(typeof i=="undefined"||i==null){i=true}if(!i)t=true;this.log("after running html-embedded card validator, failures is",t)}this.log("running listener validator");var s=this.trigger("validate");if(typeof s=="undefined"||s==null){s=true}if(!s)t=true;this.log("after running listener validator, failures is",t);var o=!t;if(o){this.log("validated, calling listeners");this.trigger("validated")}else{this.log("invalid");this.trigger("invalid")}return o},log:function(){if(!window.console||!e.fn.wizard.logging){return}var t="card '"+this.name+"': ";var n=[t];n.push.apply(n,arguments);console.log.apply(console,n)},isActive:function(){return this.nav.hasClass("active")}};Wizard=function(t,n){this.wizard_template=['<div class="modal fade wizard">','<div class="modal-dialog wizard-dialog">','<div class="modal-content wizard-content">','<div class="modal-header wizard-header">','<button type="button" class="close wizard-close" aria-hidden="true">&times;</button>','<h3 class="modal-title wizard-title"></h3>','<span class="wizard-subtitle"></span>',"</div>",'<div class="modal-body wizard-body">','<div class="pull-left wizard-steps">','<div class="wizard-nav-container">','<ul class="nav wizard-nav-list">',"</ul>","</div>",'<div class="wizard-progress-container">','<div class="progress progress-striped">','<div class="progress-bar" style="width: 0%;"></div>',"</div>","</div>","</div>","<form>",'<div class="wizard-cards">','<div class="wizard-card-container">',"</div>",'<div class="wizard-footer">','<div class="wizard-buttons-container">','<button class="btn wizard-cancel wizard-close" type="button">Cancel</button>','<div class="btn-group-single pull-right">','<button class="btn wizard-back" type="button">Back</button>','<button class="btn btn-primary wizard-next" type="button">Next</button>',"</div>","</div>","</div>","</div>","</form>","</div>","</div>","</div>","</div>"];this.args={keyboard:true,backdrop:true,show:false,submitUrl:"",showCancel:false,showClose:true,progressBarCurrent:false,increaseHeight:0,contentHeight:300,contentWidth:580,buttons:{cancelText:"Cancel",nextText:"Next",backText:"Back",submitText:"Submit",submittingText:"Submitting..."},formClass:"form-horizontal"};e.extend(this.args,n||{});this._create(t)};Wizard.prototype={log:function(){if(!window.console||!e.fn.wizard.logging){return}var t="wizard "+this.el.id+": ";var n=[t];n.push.apply(n,arguments);console.log.apply(console,n)},_create:function(t){this.markup=e(t);this.title=this.markup.data("title");this.submitCards=this.markup.find(".wizard-error,.wizard-failure,.wizard-success,.wizard-loading");this.el=e(this.wizard_template.join("\n"));e("body").append(this.el);this.modal=this.el.modal({keyboard:this.args.keyboard,show:this.args.show,backdrop:this.args.backdrop});this.dimensions={contentHeight:this.args.contentHeight,contentWidth:this.args.contentWidth};this.dialog=this.modal.find(".wizard-dialog");this.content=this.modal.find(".wizard-content");this.header=this.modal.find(".wizard-header");this.body=this.modal.find(".wizard-body");this.wizardSteps=this.modal.find(".wizard-steps");this.wizardCards=this.modal.find(".wizard-cards");this.wizardCardContainer=this.modal.find(".wizard-card-container");this.wizardCardContainer.append(this.markup.find(".wizard-card")).append(this.submitCards);this.navContainer=this.modal.find(".wizard-nav-container");this.navList=this.modal.find(".wizard-nav-list");this.progressContainer=this.modal.find(".wizard-progress-container");this.progress=this.progressContainer.find(".progress-bar");this.closeButton=this.modal.find("button.wizard-close.close");this.cardsContainer=this.modal.find("wizard-cards-container");this.form=this.modal.find("form");this.footer=this.modal.find(".wizard-footer");this.cancelButton=this.footer.find(".wizard-cancel");this.backButton=this.footer.find(".wizard-back");this.nextButton=this.footer.find(".wizard-next");this._cards=[];this.cards={};this._readyToSubmit=false;this.percentComplete=0;this._submitting=false;this._events={};this._firstShow=true;this._createCards();this.nextButton.click(this,this._handleNextClick);this.backButton.click(this,this._handleBackClick);this.cancelButton.text(this.args.buttons.cancelText);this.backButton.text(this.args.buttons.backText);this.nextButton.text(this.args.buttons.nextText);this.form.addClass(this.args.formClass);this.popovers=[];var n=this;var r=function(){n.reset();n.close();n.trigger("closed")};this.closeButton.click(r);this.cancelButton.click(r);this.wizardSteps.on("click","li.already-visited a.wizard-nav-link",this,function(t){var n=parseInt(e(t.target).data("navindex"));t.data.setCard(n)});if(this.title.length!=0){this.setTitle(this.title)}this.on("submit",this._defaultSubmit);this.autoDimensions()},autoDimensions:function(){this.modal.css("display","block");this.dimensions.header=this.header.outerHeight(true);this.dimensions.navigation=this.wizardSteps.outerHeight(true);if(this.dimensions.navigation<this.dimensions.contentHeight){this.dimensions.navigation=this.dimensions.contentHeight;this.navContainer.height(this.dimensions.contentHeight-30-this.progressContainer.outerHeight(true))}this.dimensions.body=this.dimensions.navigation;this.wizardSteps.height(this.dimensions.body);this.dimensions.modal=this.dimensions.header+this.dimensions.navigation;this.content.height(this.dimensions.modal+"px");this.dialog.width(this.dimensions.contentWidth);this.body.height(this.dimensions.body+"px");this.wizardCards.height(this.dimensions.body+"px");this.dimensions.footer=this.footer.outerHeight(true);this.dimensions.cardContainer=this.dimensions.body-this.dimensions.footer;this.wizardCardContainer.height(this.dimensions.cardContainer);this.dimensions.offset=(e(window).height()-this.dialog.height())/2;this.dialog.css({"margin-top":this.dimensions.offset+"px","padding-top":0});this.modal.css("display","")},setTitle:function(e){this.log("setting title to",e);this.modal.find(".wizard-title").first().text(e);return this},setSubtitle:function(e){this.log("setting subtitle to",e);this.modal.find(".wizard-subtitle").first().text(e);return this},errorPopover:function(e,t,n){this.log("launching popover on",e);n=typeof n!=="undefined"?n:false;var r=e.popover({content:t,trigger:"manual",html:n,container:e.parents(".form-group")}).addClass("error-popover").popover("show").next(".popover");e.parents(".form-group").find(".popover").addClass("error-popover");this.popovers.push(e);return r},destroyPopover:function(t){t=e(t);try{t.popover("destroy")}catch(n){t.popover("hide")}},hidePopovers:function(t){this.log("hiding all popovers");var n=this;e.each(this.popovers,function(e,t){n.destroyPopover(t)});this.modal.find(".has-error").removeClass("has-error");this.popovers=[]},eachCard:function(t){e.each(this._cards,t);return this},getActiveCard:function(){this.log("getting active card");var t=null;e.each(this._cards,function(e,n){if(n.isActive()){t=n;return false}});if(t){this.log("found active card",t)}else{this.log("couldn't find an active card")}return t},changeNextButton:function(e,t){this.log("changing next button, text: "+e,"class: "+t);if(typeof t!="undefined"){this.nextButton.removeClass("btn-success btn-primary")}if(t){this.nextButton.addClass(t)}this.nextButton.text(e);return this},hide:function(){this.log("hiding");this.modal.modal("hide");return this},close:function(){this.log("closing");this.modal.modal("hide");return this},show:function(e){this.log("showing");if(this._firstShow){this.setCard(0);this._firstShow=false}if(this.args.showCancel){this.cancelButton.show()}else{this.cancelButton.hide()}if(this.args.showClose){this.closeButton.show()}this.modal.modal("show");return this},on:function(e,t){this.log("adding listener to event "+e);this._events[e]=t;return this},trigger:function(){var e=arguments[0];var t=Array.prototype.slice.call(arguments);t.shift();t.unshift(this);this.log("firing event "+e);var n=this._events[e];if(n===undefined&&this.wizard!==undefined){n=this.wizard._events[e]}var r=null;if(typeof n=="function"){this.log("found event handler, calling "+e);try{r=n.apply(this,t)}catch(i){this.log("event handler "+e+" had an exception")}}else{this.log("couldn't find an event handler for "+e)}return r},reset:function(){this.log("resetting");this.updateProgressBar(0);this.hideSubmitCards();this.setCard(0);this.lockCards();this.enableNextButton();this.showButtons();this.hidePopovers();this.trigger("reset");return this},_abstractIncrementStep:function(e,t){var n=this.getActiveCard();var r;if(n){this.log("searching for valid next card");while(true){r=t(n);if(r){this.log("looking at card",r.index);if(r.isDisabled()){this.log("card "+r.index+" is disabled/locked, continuing");n=r;continue}else{return this.setCard(n.index+e)}}else{this.log("next card is not defined, breaking");break}}}else{this.log("current card is undefined")}},incrementCard:function(){this.log("incrementing card");var e=this._abstractIncrementStep(1,function(e){return e.next});this.trigger("incrementCard");return e},decrementCard:function(){this.log("decrementing card");var e=this._abstractIncrementStep(-1,function(e){return e.prev});this.trigger("decrementCard");return e},setCard:function(e){this.log("setting card to "+e);this.hideSubmitCards();var t=this.getActiveCard();if(this._submitting){this.log("we're submitting the wizard already, can't change cards");return t}var n=this._cards[e];if(n){if(n.isDisabled()){this.log("new card is currently disabled, returning");return t}if(t){if(e>t.index){var r=t;var i=false;while(r.index!=n.index){if(r.index!=t.index){r.prev.deselect();r.prev.markVisited();r.select()}i=r.validate();if(!i){return r}r=r.next}r.prev.deselect();r.prev.markVisited()}t.deselect();t.markVisited()}n.select();if(this.args.progressBarCurrent){this.percentComplete=e*100/this._cards.length;this.updateProgressBar(this.percentComplete)}else{var s=this.percentComplete;this.percentComplete=e*100/this._cards.length;this.percentComplete=Math.max(s,this.percentComplete);this.updateProgressBar(this.percentComplete)}return n}else{this.log("couldn't find card "+e)}},updateProgressBar:function(e){this.log("updating progress to "+e+"%");this.progress.css({width:e+"%"});this.percentComplete=e;this.trigger("progressBar",e);if(e==100){this.log("progress is 100, animating progress bar");this.progressContainer.find(".progress").addClass("active")}else if(e==0){this.log("progress is 0, disabling animation");this.progressContainer.find(".progress").removeClass("active")}},getNextCard:function(){var e=this.getActiveCard();if(e)return e.next},lockCards:function(){this.log("locking nav cards");this.eachCard(function(e,t){t.unmarkVisited()});return this},disableCards:function(){this.log("disabling all nav cards");this.eachCard(function(e,t){t.disable()});return this},enableCards:function(){this.log("enabling all nav cards");this.eachCard(function(e,t){t.enable()});return this},hideCards:function(){this.log("hiding cards");this.eachCard(function(e,t){t.deselect()});this.hideSubmitCards();return this},hideButtons:function(){this.log("hiding buttons");this.cancelButton.hide();this.closeButton.hide();this.nextButton.hide();this.backButton.hide();return this},showButtons:function(){this.log("showing buttons");if(this.args.showCancel){this.cancelButton.show()}else{this.cancelButton.hide()}if(this.args.showClose){this.closeButton.show()}this.nextButton.show();this.backButton.show();return this},getCard:function(t){var n=e(t).parents(".wizard-card").first()[0];if(n){var r=null;this.eachCard(function(e,t){if(n==t.el[0]){r=t;return false}return true});return r}else{return null}},_createCards:function(){var n=null;var r=null;var i=null;var s=this;var o=this;o.log("Creating Cards");var u=this.modal.find(".wizard-cards .wizard-card");e.each(u,function(u,a){a=e(a);n=i;i=new t(s,a,u,n,r);o._cards.push(i);if(i.name){o.cards[i.name]=i}if(n){n.next=i}o.modal.find(".wizard-steps .wizard-nav-list").append(i.nav)})},showSubmitCard:function(e){this.log("showing "+e+" submit card");var t=this.el.find(".wizard-"+e);if(t.length){this.hideCards();this.el.find(".wizard-"+e).show()}else{this.log("couldn't find submit card "+e)}},hideSubmitCard:function(e){this.log("hiding "+e+" submit card");this.el.find(".wizard-"+e).hide()},hideSubmitCards:function(){var t=this;e.each(["success","error","failure","loading"],function(e,n){t.hideSubmitCard(n)})},enableNextButton:function(){this.log("enabling next button");this.nextButton.removeAttr("disabled");return this},disableNextButton:function(){this.log("disabling next button");this.nextButton.attr("disabled","disabled");return this},serializeArray:function(){var t=this.form.serializeArray();this.form.find('input[disabled][data-serialize="1"]').each(function(){formObj={name:e(this).attr("name"),value:e(this).val()};t.push(formObj)});return t},serialize:function(){var t=this.form.serialize();this.form.find('input[disabled][data-serialize="1"]').each(function(){t=t+"&"+e(this).attr("name")+"="+e(this).val()});return t},find:function(e){return this.modal.find(e)},submitSuccess:function(){this.log("submit success");this._submitting=false;this.showSubmitCard("success");this.trigger("submitSuccess")},submitFailure:function(){this.log("submit failure");this._submitting=false;this.showSubmitCard("failure");this.trigger("submitFailure")},submitError:function(){this.log("submit error");this._submitting=false;this.showSubmitCard("error");this.trigger("submitError")},_submit:function(){this.log("submitting wizard");this._submitting=true;this.lockCards();this.cancelButton.hide();this.closeButton.hide();this.backButton.hide();this.showSubmitCard("loading");this.updateProgressBar(100);this.changeNextButton(this.args.buttons.submittingText,false);this.disableNextButton();var e=this.trigger("submit");this.trigger("loading")},_onNextClick:function(){this.log("handling 'next' button click");var e=this.getActiveCard();if(this._readyToSubmit&&e.validate()){this._submit()}else{e=this.incrementCard()}},_onBackClick:function(){this.log("handling 'back' button click");var e=this.decrementCard()},_handleNextClick:function(e){var t=e.data;t._onNextClick.call(t)},_handleBackClick:function(e){var t=e.data;t._onBackClick.call(t)},_defaultSubmit:function(t){e.ajax({type:"POST",url:t.args.submitUrl,data:t.serialize(),dataType:"json"}).done(function(e){t.submitSuccess();t.hideButtons();t.updateProgressBar(0)}).fail(function(){t.submitFailure();t.hideButtons()})}}})(window.jQuery)