Administrator
2023-04-21 195945efc5db921a4c9eb8cf9421c172273293f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * Fuel UX Wizard
 * https://github.com/ExactTarget/fuelux
 *
 * Copyright (c) 2014 ExactTarget
 * Licensed under the BSD New license.
 */
 
// -- BEGIN UMD WRAPPER PREFACE --
 
// For more information on UMD visit:
// https://github.com/umdjs/umd/blob/master/jqueryPlugin.js
 
(function (factory) {
    if (typeof define === 'function' && define.amd) {
        // if AMD loader is available, register as an anonymous module.
        define(['jquery'], factory);
    } else if (typeof exports === 'object') {
        // Node/CommonJS
        module.exports = factory(require('jquery'));
    } else {
        // OR use browser globals if AMD is not present
        factory(jQuery);
    }
}(function ($) {
    // -- END UMD WRAPPER PREFACE --
 
    // -- BEGIN MODULE CODE HERE --
 
    var old = $.fn.wizard;
 
    // WIZARD CONSTRUCTOR AND PROTOTYPE
 
    var Wizard = function (element, options) {
        var kids;
 
        this.$element = $(element);
        this.options = $.extend({}, $.fn.wizard.defaults, options);
        this.options.disablePreviousStep = (this.$element.attr('data-restrict') === 'previous') ? true : this.options.disablePreviousStep;
        this.currentStep = this.options.selectedItem.step;
        this.numSteps = this.$element.find('.steps li').length;
        this.$prevBtn = this.$element.find('button.btn-prev');
        this.$nextBtn = this.$element.find('button.btn-next');
 
        // maintains backwards compatibility with < 3.8, will be removed in the future
        if (this.$element.children('.steps-container').length === 0) {
            this.$element.addClass('no-steps-container');
            if (window && window.console && window.console.warn) {
                window.console.warn('please update your wizard markup to include ".steps-container" as seen in http://getfuelux.com/javascript.html#wizard-usage-markup');
            }
        }
 
        kids = this.$nextBtn.children().detach();
        this.nextText = $.trim(this.$nextBtn.text());
        this.$nextBtn.append(kids);
 
        // handle events
        this.$prevBtn.on('click.fu.wizard', $.proxy(this.previous, this));
        this.$nextBtn.on('click.fu.wizard', $.proxy(this.next, this));
        this.$element.on('click.fu.wizard', 'li.complete', $.proxy(this.stepclicked, this));
 
        this.selectedItem(this.options.selectedItem);
 
        if (this.options.disablePreviousStep) {
            this.$prevBtn.attr('disabled', true);
            this.$element.find('.steps').addClass('previous-disabled');
        }
    };
 
    Wizard.prototype = {
 
        constructor: Wizard,
 
        destroy: function () {
            this.$element.remove();
            // any external bindings [none]
            // empty elements to return to original markup [none]
            // returns string of markup
            return this.$element[0].outerHTML;
        },
 
        //index is 1 based
        //second parameter can be array of objects [{ ... }, { ... }] or you can pass n additional objects as args
        //object structure is as follows (all params are optional): { badge: '', label: '', pane: '' }
        addSteps: function (index) {
            var items = [].slice.call(arguments).slice(1);
            var $steps = this.$element.find('.steps');
            var $stepContent = this.$element.find('.step-content');
            var i, l, $pane, $startPane, $startStep, $step;
 
            index = (index === -1 || (index > (this.numSteps + 1))) ? this.numSteps + 1 : index;
            if (items[0] instanceof Array) {
                items = items[0];
            }
 
            $startStep = $steps.find('li:nth-child(' + index + ')');
            $startPane = $stepContent.find('.step-pane:nth-child(' + index + ')');
            if ($startStep.length < 1) {
                $startStep = null;
            }
 
            for (i = 0, l = items.length; i < l; i++) {
                $step = $('<li data-step="' + index + '"><span class="badge badge-info"></span></li>');
                $step.append(items[i].label || '').append('<span class="chevron"></span>');
                $step.find('.badge').append(items[i].badge || index);
 
                $pane = $('<div class="step-pane" data-step="' + index + '"></div>');
                $pane.append(items[i].pane || '');
 
                if (!$startStep) {
                    $steps.append($step);
                    $stepContent.append($pane);
                } else {
                    $startStep.before($step);
                    $startPane.before($pane);
                }
 
                index++;
            }
 
            this.syncSteps();
            this.numSteps = $steps.find('li').length;
            this.setState();
        },
 
        //index is 1 based, howMany is number to remove
        removeSteps: function (index, howMany) {
            var action = 'nextAll';
            var i = 0;
            var $steps = this.$element.find('.steps');
            var $stepContent = this.$element.find('.step-content');
            var $start;
 
            howMany = (howMany !== undefined) ? howMany : 1;
 
            if (index > $steps.find('li').length) {
                $start = $steps.find('li:last');
            } else {
                $start = $steps.find('li:nth-child(' + index + ')').prev();
                if ($start.length < 1) {
                    action = 'children';
                    $start = $steps;
                }
 
            }
 
            $start[action]().each(function () {
                var item = $(this);
                var step = item.attr('data-step');
                if (i < howMany) {
                    item.remove();
                    $stepContent.find('.step-pane[data-step="' + step + '"]:first').remove();
                } else {
                    return false;
                }
 
                i++;
            });
 
            this.syncSteps();
            this.numSteps = $steps.find('li').length;
            this.setState();
        },
 
        setState: function () {
            var canMovePrev = (this.currentStep > 1);//remember, steps index is 1 based...
            var isFirstStep = (this.currentStep === 1);
            var isLastStep = (this.currentStep === this.numSteps);
 
            // disable buttons based on current step
            if (!this.options.disablePreviousStep) {
                this.$prevBtn.attr('disabled', (isFirstStep === true || canMovePrev === false));
            }
 
            // change button text of last step, if specified
            var last = this.$nextBtn.attr('data-last');
            if (last) {
                this.lastText = last;
                // replace text
                var text = this.nextText;
                if (isLastStep === true) {
                    text = this.lastText;
                    // add status class to wizard
                    this.$element.addClass('complete');
                } else {
                    this.$element.removeClass('complete');
                }
 
                var kids = this.$nextBtn.children().detach();
                this.$nextBtn.text(text).append(kids);
            }
 
            // reset classes for all steps
            var $steps = this.$element.find('.steps li');
            $steps.removeClass('active').removeClass('complete');
            $steps.find('span.badge').removeClass('badge-info').removeClass('badge-success');
 
            // set class for all previous steps
            var prevSelector = '.steps li:lt(' + (this.currentStep - 1) + ')';
            var $prevSteps = this.$element.find(prevSelector);
            $prevSteps.addClass('complete');
            $prevSteps.find('span.badge').addClass('badge-success');
 
            // set class for current step
            var currentSelector = '.steps li:eq(' + (this.currentStep - 1) + ')';
            var $currentStep = this.$element.find(currentSelector);
            $currentStep.addClass('active');
            $currentStep.find('span.badge').addClass('badge-info');
 
            // set display of target element
            var $stepContent = this.$element.find('.step-content');
            var target = $currentStep.attr('data-step');
            $stepContent.find('.step-pane').removeClass('active');
            $stepContent.find('.step-pane[data-step="' + target + '"]:first').addClass('active');
 
            //ACE
            /**
            // reset the wizard position to the left
            this.$element.find('.steps').first().attr('style', 'margin-left: 0');
 
            // check if the steps are wider than the container div
            var totalWidth = 0;
            this.$element.find('.steps > li').each(function () {
                totalWidth += $(this).outerWidth();
            });
            var containerWidth = 0;
            if (this.$element.find('.actions').length) {
                containerWidth = this.$element.width() - this.$element.find('.actions').first().outerWidth();
            } else {
                containerWidth = this.$element.width();
            }
 
            if (totalWidth > containerWidth) {
                // set the position so that the last step is on the right
                var newMargin = totalWidth - containerWidth;
                this.$element.find('.steps').first().attr('style', 'margin-left: -' + newMargin + 'px');
 
                // set the position so that the active step is in a good
                // position if it has been moved out of view
                if (this.$element.find('li.active').first().position().left < 200) {
                    newMargin += this.$element.find('li.active').first().position().left - 200;
                    if (newMargin < 1) {
                        this.$element.find('.steps').first().attr('style', 'margin-left: 0');
                    } else {
                        this.$element.find('.steps').first().attr('style', 'margin-left: -' + newMargin + 'px');
                    }
 
                }
 
            }
            */
 
            // only fire changed event after initializing
            if (typeof (this.initialized) !== 'undefined') {
                var e = $.Event('changed.fu.wizard');
                this.$element.trigger(e, {
                    step: this.currentStep
                });
            }
 
            this.initialized = true;
        },
 
        stepclicked: function (e) {
            var li = $(e.currentTarget);
            var index = this.$element.find('.steps li').index(li);
 
            if (index < this.currentStep && this.options.disablePreviousStep) {//enforce restrictions
                return;
            } else {
                var evt = $.Event('stepclicked.fu.wizard');
                this.$element.trigger(evt, {
                    step: index + 1
                });
                if (evt.isDefaultPrevented()) {
                    return;
                }
 
                this.currentStep = (index + 1);
                this.setState();
            }
        },
 
        syncSteps: function () {
            var i = 1;
            var $steps = this.$element.find('.steps');
            var $stepContent = this.$element.find('.step-content');
 
            $steps.children().each(function () {
                var item = $(this);
                var badge = item.find('.badge');
                var step = item.attr('data-step');
 
                if (!isNaN(parseInt(badge.html(), 10))) {
                    badge.html(i);
                }
 
                item.attr('data-step', i);
                $stepContent.find('.step-pane[data-step="' + step + '"]:last').attr('data-step', i);
                i++;
            });
        },
 
        previous: function () {
            if (this.options.disablePreviousStep || this.currentStep === 1) {
                return;
            }
 
            var e = $.Event('actionclicked.fu.wizard');
            this.$element.trigger(e, {
                step: this.currentStep,
                direction: 'previous'
            });
            if (e.isDefaultPrevented()) {
                return;
            }// don't increment ...what? Why?
 
            this.currentStep -= 1;
            this.setState();
 
            // only set focus if focus is still on the $nextBtn (avoid stomping on a focus set programmatically in actionclicked callback)
            if (this.$prevBtn.is(':focus')) {
                var firstFormField = this.$element.find('.active').find('input, select, textarea')[0];
 
                if (typeof firstFormField !== 'undefined') {
                    // allow user to start typing immediately instead of having to click on the form field.
                    $(firstFormField).focus();
                } else if (this.$element.find('.active input:first').length === 0 && this.$prevBtn.is(':disabled')) {
                    //only set focus on a button as the last resort if no form fields exist and the just clicked button is now disabled
                    this.$nextBtn.focus();
                }
 
            }
        },
 
        next: function () {
            var e = $.Event('actionclicked.fu.wizard');
            this.$element.trigger(e, {
                step: this.currentStep,
                direction: 'next'
            });
            if (e.isDefaultPrevented()) {
                return;
            }// respect preventDefault in case dev has attached validation to step and wants to stop propagation based on it.
 
            if (this.currentStep < this.numSteps) {
                this.currentStep += 1;
                this.setState();
            } else {//is last step
                this.$element.trigger('finished.fu.wizard');
            }
 
            // only set focus if focus is still on the $nextBtn (avoid stomping on a focus set programmatically in actionclicked callback)
            if (this.$nextBtn.is(':focus')) {
                var firstFormField = this.$element.find('.active').find('input, select, textarea')[0];
 
                if (typeof firstFormField !== 'undefined') {
                    // allow user to start typing immediately instead of having to click on the form field.
                    $(firstFormField).focus();
                } else if (this.$element.find('.active input:first').length === 0 && this.$nextBtn.is(':disabled')) {
                    //only set focus on a button as the last resort if no form fields exist and the just clicked button is now disabled
                    this.$prevBtn.focus();
                }
 
            }
        },
 
        selectedItem: function (selectedItem) {
            var retVal, step;
 
            if (selectedItem) {
                step = selectedItem.step || -1;
                //allow selection of step by data-name
                step = Number(this.$element.find('.steps li[data-name="' + step + '"]').first().attr('data-step')) || Number(step);
 
                if (1 <= step && step <= this.numSteps) {
                    this.currentStep = step;
                    this.setState();
                } else {
                    step = this.$element.find('.steps li.active:first').attr('data-step');
                    if (!isNaN(step)) {
                        this.currentStep = parseInt(step, 10);
                        this.setState();
                    }
 
                }
 
                retVal = this;
            } else {
                retVal = {
                    step: this.currentStep
                };
                if (this.$element.find('.steps li.active:first[data-name]').length) {
                    retVal.stepname = this.$element.find('.steps li.active:first').attr('data-name');
                }
 
            }
 
            return retVal;
        }
    };
 
 
    // WIZARD PLUGIN DEFINITION
 
    $.fn.wizard = function (option) {
        var args = Array.prototype.slice.call(arguments, 1);
        var methodReturn;
 
        var $set = this.each(function () {
            var $this = $(this);
            var data = $this.data('fu.wizard');
            var options = typeof option === 'object' && option;
 
            if (!data) {
                $this.data('fu.wizard', (data = new Wizard(this, options)));
            }
 
            if (typeof option === 'string') {
                methodReturn = data[option].apply(data, args);
            }
        });
 
        return (methodReturn === undefined) ? $set : methodReturn;
    };
 
    $.fn.wizard.defaults = {
        disablePreviousStep: false,
        selectedItem: {
            step: -1
        }//-1 means it will attempt to look for "active" class in order to set the step
    };
 
    $.fn.wizard.Constructor = Wizard;
 
    $.fn.wizard.noConflict = function () {
        $.fn.wizard = old;
        return this;
    };
 
 
    // DATA-API
 
    $(document).on('mouseover.fu.wizard.data-api', '[data-initialize=wizard]', function (e) {
        var $control = $(e.target).closest('.wizard');
        if (!$control.data('fu.wizard')) {
            $control.wizard($control.data());
        }
    });
 
    // Must be domReady for AMD compatibility
    $(function () {
        $('[data-initialize=wizard]').each(function () {
            var $this = $(this);
            if ($this.data('fu.wizard')) return;
            $this.wizard($this.data());
        });
    });
 
    // -- BEGIN UMD WRAPPER AFTERWORD --
}));
// -- END UMD WRAPPER AFTERWORD --