// // $.ui.cycling // // Cycling widget // // depends on: // - jquery.js // - jquery.ui.core.js // - jquery.ui.widget.js // // Copyright: © 2012 Is Cool Entertainment // Author: Antoine BERNIER (abernier) // // Usage: $(el).cycling({ // items: Collection's items (jQuery|selector|el) Default: '> *', eg: children // value: Current value (Number) Default: 0, // min: Min value (Number) Default: 0 // max: Max value (Number) Default: $(items).length - 1 // loop: Loop? (Boolean) Default: true // step: Step (Number >= 0) Default: 1 // }) // (function ($) { $.widget("ui.cycling", { // // Defaults options // // /!\ Order below matters // options: { items: '> *', // The collection of elements to cycle around min: -Infinity, // The min index below stop cycling max: Infinity, // The max index below stop cycling value: 0, // The index of the current element loop: true, // Whether to loop step: 1, // step used for prev/next actions }, _create: function () { $.Widget.prototype._create.apply(this, arguments); // Force setting options in order to normalize them this._setOptions(this.options); this.element.bind(this.widgetName + 'setoption.' + this.widgetName, $.proxy(function (event, data) { // // Options dependencies computation // // TODO: // // value = f(min, max, loop) // min = f(max) // max = f(items, min) // // So, for example, updating 'min' => updating 'max' then 'value' // //console.info("option '%s' has just set to '%s'(original: '%s')", data.option, data.current, data.original); switch (data.option) { case 'min': this._setOption('max', this.options.max, true); this._setOption('value', this.options.value); break; case 'max': this._setOption('min', this.options.min, true); this._setOption('value', this.options.value); break; case 'items': this._setOption('max', this.options.max, true); break; case 'loop': this._setOption('value', this.options.value); break; case 'value': // // Trigger the 'cyclingchange' event // this._trigger('change', null, { value: data.current, $activeItem: this.options.items.eq(data.current) }); break; } }, this)); // this.go(this.options.value); return this; }, go: function (value) { this._setOption('value', value); return this; }, jump: function (increment) { this.go(this.options.value + increment); return this; }, next: function (step) { if (step) { this._setOption('step', step); } this.jump(this.options.step); return this; }, prev: function (step) { if (step) { this._setOption('step', step); } this.jump(-this.options.step); return this; }, _setOption: function (k, v, silently) { var oldValue = this.options[k]; switch (k) { case 'items': v = $(v, this.element); break; case 'min': if (v < 0) { v = 0; } if (v > this.options.max) { v = this.options.max; } break; case 'max': if (v > this.options.items.length - 1) { v = this.options.items.length - 1; } if (v < this.options.min) { v = this.options.min; } break; case 'value': if (v > this.options.max) { v = (this.options.loop && this.options.value === this.options.max) ? this.options.min : this.options.max; } if (v < this.options.min) { v = (this.options.loop && this.options.value == this.options.min) ? this.options.max : this.options.min; } break; case 'step': if (v < 0) { v = Math.abs(v); } break; } $.Widget.prototype._setOption.apply(this, arguments); // // Trigger an event when setting a option // !silently && this._trigger("setoption", null, { option: k, original: oldValue, current: v }); return this; } }); }(jQuery));