vpn.email
Version:
vpn.email client
166 lines (130 loc) • 4.4 kB
JavaScript
$.widget("metro.stepper", {
version: "3.0.0",
options: {
steps: 3,
start: 1,
type: 'default',
clickable: true,
onStep: function(index, step){},
onStepClick: function(index, step){}
},
_create: function(){
var element = this.element, o = this.options, element_id = element.attr('id');
$.each(element.data(), function(key, value){
if (key in o) {
try {
o[key] = $.parseJSON(value);
} catch (e) {
o[key] = value;
}
}
});
if (!element.hasClass('stepper')) {element.addClass('stepper');}
if (element_id === undefined) {
element_id = window.uniqueId(this.widgetName+'_');
element.attr('id', element_id);
}
this._createStepper();
if (o.clickable) {this._createEvents();}
this._positioningSteps();
this._stepTo(o.start);
element.data('stepper', this);
},
_createEvents: function(){
var that = this, element = this.element, o= this.options;
element.on('click', 'li', function(e){
var step = $(this).data('step');
if (typeof o.onStepClick === 'function') {
o.onStepClick(step - 1, step);
} else {
if (typeof window[o.onStepClick] === 'function') {
window[o.onStepClick](step - 1, step);
} else {
var result = eval("(function(){"+o.onStepClick+"})");
result.call(step - 1, step);
}
}
element.trigger("stepclick", step);
});
},
_createStepper: function(){
var element = this.element, o= this.options;
var i, ul, li;
ul = $("<ul/>");
switch(o.type) {
case 'diamond': element.addClass('diamond'); break;
case 'cycle': element.addClass('cycle'); break;
}
for(i=0;i< o.steps;i++) {
li = $("<li/>").data('step', i + 1).appendTo(ul);
}
ul.appendTo(element);
},
_positioningSteps: function(){
var that = this, element = this.element, o = this.options,
steps = element.find("li"),
element_width = element.width(),
steps_length = steps.length-1,
step_width = $(steps[0]).width();
$.each(steps, function(i, step){
var left = i === 0 ? 0 : (element_width - step_width)/steps_length * i;
console.log(element_width);
$(step).animate({
left: left
});
});
},
_stepTo: function(step){
var element = this.element, o = this.options;
var steps = element.find("li");
steps.removeClass('current').removeClass('complete');
$.each(steps, function(i, s){
if (i < step - 1) {$(s).addClass('complete');}
if (i === step - 1) {
$(s).addClass('current') ;
if (typeof o.onStep === 'function') {
o.onStep(i+1, s);
} else {
if (typeof window[o.onStep] === 'function') {
window[o.onStep](i+1, s);
} else {
var result = eval("(function(){"+o.onStep+"})");
result.call(i+1, s);
}
}
}
});
},
stepTo: function(step){
this._stepTo(step);
},
first: function(){
var o = this.options;
o.start = 1;
this._stepTo(o.start);
},
last: function(){
var element = this.element, o = this.options;
var steps = element.find("li");
o.start = steps.length;
this._stepTo(o.start);
},
next: function(){
var element = this.element, o = this.options;
var steps = element.find("li");
if (o.start + 1 > steps.length) {return;}
o.start++;
this._stepTo(o.start);
},
prior: function(){
var o = this.options;
if (o.start - 1 === 0) {return;}
o.start--;
this._stepTo(o.start);
},
_destroy: function(){
},
_setOption: function(key, value){
this._super('_setOption', key, value);
}
});