1; 2(function($, window, document, undefined) { 3 'use strict'; 4 // undefined is used here as the undefined global variable in ECMAScript 3 is 5 // mutable (ie. it can be hasMoved by someone else). undefined isn't really being 6 // passed in so we can ensure the value of it is truly undefined. In ES5, undefined 7 // can no longer be modified. 8 9 // window and document are passed through as local variable rather than global 10 // as this (slightly) quickens the resolution process and can be more efficiently 11 // minified (especially when both are regularly referenced in your plugin). 12 13 // Create the defaults once 14 var pluginName = 'newsTicker', 15 defaults = { 16 row_height: 20, 17 max_rows: 3, 18 speed: 400, 19 direction: 'up', 20 time: 2500, 21 autostart: 1, 22 stopOnHover: 1, 23 nextButton: null, 24 prevButton: null, 25 startButton: null, 26 stopButton: null, 27 hasMoved: function() {}, 28 movingUp: function() {}, 29 movingDown: function() {}, 30 start: function() {}, 31 stop: function() {}, 32 pause: function() {}, 33 unpause: function() {} 34 }; 35 36 // The actual plugin constructor 37 function Plugin(element, options) { 38 this.element = element; 39 this.$el = $(element); 40 this.options = $.extend({}, defaults, options); 41 this._defaults = defaults; 42 this._name = pluginName; 43 this.moveInterval; 44 this.state = 0; 45 this.paused = 0; 46 this.moving = 0; 47 if (this.$el.is('ul')) { 48 this.init(); 49 } 50 } 51 52 Plugin.prototype = { 53 init: function() { 54 this.$el.height(this.options.row_height * this.options.max_rows) 55 .css({overflow : 'hidden'}); 56 57 this.checkSpeed(); 58 59 if(this.options.nextButton && typeof(this.options.nextButton[0]) !== 'undefined') 60 this.options.nextButton.click(function(e) { 61 this.moveNext(); 62 this.resetInterval(); 63 }.bind(this)); 64 if(this.options.prevButton && typeof(this.options.prevButton[0]) !== 'undefined') 65 this.options.prevButton.click(function(e) { 66 this.movePrev(); 67 this.resetInterval(); 68 }.bind(this)); 69 if(this.options.stopButton && typeof(this.options.stopButton[0]) !== 'undefined') 70 this.options.stopButton.click(function(e) { 71 this.stop() 72 }.bind(this)); 73 if(this.options.startButton && typeof(this.options.startButton[0]) !== 'undefined') 74 this.options.startButton.click(function(e) { 75 this.start() 76 }.bind(this)); 77 78 if(this.options.stopOnHover) { 79 this.$el.hover(function() { 80 if (this.state) 81 this.pause(); 82 }.bind(this), function() { 83 if (this.state) 84 this.unpause(); 85 }.bind(this)); 86 } 87 88 if(this.options.autostart) 89 this.start(); 90 }, 91 92 start: function() { 93 if (!this.state) { 94 this.state = 1; 95 this.resetInterval(); 96 this.options.start(); 97 } 98 }, 99 100 stop: function() { 101 if (this.state) { 102 clearInterval(this.moveInterval); 103 this.state = 0; 104 this.options.stop(); 105 } 106 }, 107 108 resetInterval: function() { 109 if (this.state) { 110 clearInterval(this.moveInterval); 111 this.moveInterval = setInterval(function() {this.move()}.bind(this), this.options.time); 112 } 113 }, 114 115 move: function() { 116 if (!this.paused) this.moveNext(); 117 }, 118 119 moveNext: function() { 120 if (this.options.direction === 'down') 121 this.moveDown(); 122 else if (this.options.direction === 'up') 123 this.moveUp(); 124 }, 125 126 movePrev: function() { 127 if (this.options.direction === 'down') 128 this.moveUp(); 129 else if (this.options.direction === 'up') 130 this.moveDown(); 131 }, 132 133 pause: function() { 134 if (!this.paused) this.paused = 1; 135 this.options.pause(); 136 }, 137 138 unpause: function() { 139 if (this.paused) this.paused = 0; 140 this.options.unpause(); 141 }, 142 143 moveDown: function() { 144 if (!this.moving) { 145 this.moving = 1; 146 this.options.movingDown(); 147 this.$el.children('li:last').detach().prependTo(this.$el).css('marginTop', '-' + this.options.row_height + 'px') 148 .animate({marginTop: '0px'}, this.options.speed, function(){ 149 this.moving = 0; 150 this.options.hasMoved(); 151 }.bind(this)); 152 } 153 }, 154 155 moveUp: function() { 156 if (!this.moving) { 157 this.moving = 1; 158 this.options.movingUp(); 159 var element = this.$el.children('li:first'); 160 element.animate({marginTop: '-' + this.options.row_height + 'px'}, this.options.speed, 161 function(){ 162 element.detach().css('marginTop', '0').appendTo(this.$el); 163 this.moving = 0; 164 this.options.hasMoved(); 165 }.bind(this)); 166 } 167 }, 168 169 updateOption: function(option, value) { 170 if (typeof(this.options[option]) !== 'undefined') 171 this.options[option] = value; 172 //TODO: checkSpeed if speed/time 173 }, 174 175 getState: function() { 176 if (paused) return 2 177 else return this.state;//0 = stopped, 1 = started 178 }, 179 180 checkSpeed: function() { 181 if (this.options.time < (this.options.speed + 25)) 182 this.options.speed = this.options.time - 25; 183 }, 184 185 destroy: function() { 186 this._destroy(); //or this.delete; depends on jQuery version 187 } 188 }; 189 190 // A really lightweight plugin wrapper around the constructor, 191 // preventing against multiple instantiations 192 $.fn[pluginName] = function(option) { 193 var args = arguments; 194 195 return this.each(function() { 196 var $this = $(this), 197 data = $.data(this, 'plugin_' + pluginName), 198 options = typeof option === 'object' && option; 199 if (!data) { 200 $this.data('plugin_' + pluginName, (data = new Plugin(this, options))); 201 } 202 // if first argument is a string, call silimarly named function 203 // this gives flexibility to call functions of the plugin e.g. 204 // - $('.dial').plugin('destroy'); 205 // - $('.dial').plugin('render', $('.new-child')); 206 if (typeof option === 'string') { 207 data[option].apply(data, Array.prototype.slice.call(args, 1)); 208 } 209 }); 210 }; 211})(jQuery, window, document);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.