1/* ============================================================================== 2 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 * use this file except in compliance with the License. You may obtain a copy of 4 * the License at 5 * 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * 8 * Unless required by applicable law or agreed to in writing, software 9 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 10 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 11 * License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14;(function ($) { 15 $.fn.spinner = function (opts) { 16 return this.each(function () { 17 var defaults = {value:"1", min:1} 18 var options = $.extend(defaults, opts) 19 var keyCodes = {up:38, down:40} 20 var container = $('<div></div>') 21 container.addClass('spinner') 22 var textField = $(this).addClass('value').attr('maxlength', '2').val(options.value) 23 .bind('keyup paste change', function (e) { 24 var field = $(this) 25 if (e.keyCode == keyCodes.up) changeValue(1) 26 else if (e.keyCode == keyCodes.down) changeValue(-1) 27 else if (getValue(field) != container.data('lastValidValue')) validateAndTrigger(field) 28 }) 29 textField.wrap(container) 30 31 var increaseButton = $('<span class="increase">+</span>').click(function () { changeValue(1) }) 32 var decreaseButton = $('<span class="decrease">-</span>').click(function () { changeValue(-1) }) 33 34 validate(textField) 35 container.data('lastValidValue', options.value) 36 textField.before(decreaseButton) 37 textField.after(increaseButton) 38 39 function changeValue(delta) { 40 textField.val(getValue() + delta) 41 validateAndTrigger(textField) 42 } 43 44 function validateAndTrigger(field) { 45 clearTimeout(container.data('timeout')) 46 var value = validate(field) 47 if (!isInvalid(value)) { 48 textField.trigger('update', [field, value]) 49 } 50 } 51 52 function validate(field) { 53 var value = getValue() 54 if (value <= options.min) decreaseButton.attr('disabled', 'disabled') 55 else decreaseButton.removeAttr('disabled') 56 field.toggleClass('invalid', isInvalid(value)).toggleClass('passive', value === 0) 57 58 if (isInvalid(value)) { 59 var timeout = setTimeout(function () { 60 textField.val(container.data('lastValidValue')) 61 validate(field) 62 }, 500) 63 container.data('timeout', timeout) 64 } else { 65 container.data('lastValidValue', value) 66 } 67 return value 68 } 69 70 function isInvalid(value) { return isNaN(+value) || value < options.min; } 71 72 function getValue(field) { 73 field = field || textField; 74 return parseInt(field.val() || 0, 10) 75 } 76 }) 77 } 78})(jQuery)
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.