Devices: NodesLearnt table integration with FuelUx Datagrid.
[controller.git] / opendaylight / web / root / src / main / resources / js / fuelux / wizard.js
1 /*
2  * Fuel UX Wizard
3  * https://github.com/ExactTarget/fuelux
4  *
5  * Copyright (c) 2012 ExactTarget
6  * Licensed under the MIT license.
7  */
8
9 define(['require','jquery'],function (require) {
10
11         var $ = require('jquery');
12
13
14         // WIZARD CONSTRUCTOR AND PROTOTYPE
15
16         var Wizard = function (element, options) {
17                 var kids;
18
19                 this.$element = $(element);
20                 this.options = $.extend({}, $.fn.wizard.defaults, options);
21                 this.currentStep = 1;
22                 this.numSteps = this.$element.find('li').length;
23                 this.$prevBtn = this.$element.find('button.btn-prev');
24                 this.$nextBtn = this.$element.find('button.btn-next');
25
26                 kids = this.$nextBtn.children().detach();
27                 this.nextText = $.trim(this.$nextBtn.text());
28                 this.$nextBtn.append(kids);
29
30                 // handle events
31                 this.$prevBtn.on('click', $.proxy(this.previous, this));
32                 this.$nextBtn.on('click', $.proxy(this.next, this));
33                 this.$element.on('click', 'li.complete', $.proxy(this.stepclicked, this));
34         };
35
36         Wizard.prototype = {
37
38                 constructor: Wizard,
39
40                 setState: function () {
41                         var canMovePrev = (this.currentStep > 1);
42                         var firstStep = (this.currentStep === 1);
43                         var lastStep = (this.currentStep === this.numSteps);
44
45                         // disable buttons based on current step
46                         this.$prevBtn.attr('disabled', (firstStep === true || canMovePrev === false));
47
48                         // change button text of last step, if specified
49                         var data = this.$nextBtn.data();
50                         if (data && data.last) {
51                                 this.lastText = data.last;
52                                 if (typeof this.lastText !== 'undefined') {
53                                         // replace text
54                                         var text = (lastStep !== true) ? this.nextText : this.lastText;
55                                         var kids = this.$nextBtn.children().detach();
56                                         this.$nextBtn.text(text).append(kids);
57                                 }
58                         }
59
60                         // reset classes for all steps
61                         var $steps = this.$element.find('li');
62                         $steps.removeClass('active').removeClass('complete');
63                         $steps.find('span.badge').removeClass('badge-info').removeClass('badge-success');
64
65                         // set class for all previous steps
66                         var prevSelector = 'li:lt(' + (this.currentStep - 1) + ')';
67                         var $prevSteps = this.$element.find(prevSelector);
68                         $prevSteps.addClass('complete');
69                         $prevSteps.find('span.badge').addClass('badge-success');
70
71                         // set class for current step
72                         var currentSelector = 'li:eq(' + (this.currentStep - 1) + ')';
73                         var $currentStep = this.$element.find(currentSelector);
74                         $currentStep.addClass('active');
75                         $currentStep.find('span.badge').addClass('badge-info');
76
77                         // set display of target element
78                         var target = $currentStep.data().target;
79                         $('.step-pane').removeClass('active');
80                         $(target).addClass('active');
81
82                         this.$element.trigger('changed');
83                 },
84
85                 stepclicked: function (e) {
86                         var li = $(e.currentTarget);
87
88                         var index = $('.steps li').index(li);
89
90                         var evt = $.Event('stepclick');
91                         this.$element.trigger(evt, {step: index + 1});
92                         if (evt.isDefaultPrevented()) return;
93
94                         this.currentStep = (index + 1);
95                         this.setState();
96                 },
97
98                 previous: function () {
99                         var canMovePrev = (this.currentStep > 1);
100                         if (canMovePrev) {
101                                 var e = $.Event('change');
102                                 this.$element.trigger(e, {step: this.currentStep, direction: 'previous'});
103                                 if (e.isDefaultPrevented()) return;
104
105                                 this.currentStep -= 1;
106                                 this.setState();
107                         }
108                 },
109
110                 next: function () {
111                         var canMoveNext = (this.currentStep + 1 <= this.numSteps);
112                         var lastStep = (this.currentStep === this.numSteps);
113
114                         if (canMoveNext) {
115                                 var e = $.Event('change');
116                                 this.$element.trigger(e, {step: this.currentStep, direction: 'next'});
117
118                                 if (e.isDefaultPrevented()) return;
119
120                                 this.currentStep += 1;
121                                 this.setState();
122                         }
123                         else if (lastStep) {
124                                 this.$element.trigger('finished');
125                         }
126                 },
127
128                 selectedItem: function (val) {
129                         return {
130                                 step: this.currentStep
131                         };
132                 }
133         };
134
135
136         // WIZARD PLUGIN DEFINITION
137
138         $.fn.wizard = function (option, value) {
139                 var methodReturn;
140
141                 var $set = this.each(function () {
142                         var $this = $(this);
143                         var data = $this.data('wizard');
144                         var options = typeof option === 'object' && option;
145
146                         if (!data) $this.data('wizard', (data = new Wizard(this, options)));
147                         if (typeof option === 'string') methodReturn = data[option](value);
148                 });
149
150                 return (methodReturn === undefined) ? $set : methodReturn;
151         };
152
153         $.fn.wizard.defaults = {};
154
155         $.fn.wizard.Constructor = Wizard;
156
157
158         // WIZARD DATA-API
159
160         $(function () {
161                 $('body').on('mousedown.wizard.data-api', '.wizard', function () {
162                         var $this = $(this);
163                         if ($this.data('wizard')) return;
164                         $this.wizard($this.data());
165                 });
166         });
167
168 });