// vim: et:ts=4:sw=4:sts=4:ft=javascript 'use strict'; /*@ Ox.Input Input Element () -> Input Element (options) -> Input Element (options, self) -> Input Element options Options object arrows if true, and type is 'float' or 'int', display arrows arrowStep step when clicking arrows autocomplete array of possible values, or function(key, value, callback), returns one or more values autocompleteReplace if true, value is replaced autocompleteReplaceCorrect if true, only valid values can be entered autocompleteSelect if true, menu is displayed autocompleteSelectHighlight if true, value in menu is highlighted autocompleteSelectMaxWidth Maximum width of autocomplete menu, or 0 autocompleteSelectSubmit if true, submit input on menu selection autocorrect ('email', 'float', 'int', 'phone', 'url'), or regexp(value), or function(key, value, blur, callback), returns value autovalidate --remote validation-- clear if true, has clear button changeOnKeypress if true, fire change event while typing disabled if true, is disabled height px (for type='textarea' and type='range' with orientation='horizontal') id element id key to be passed to autocomplete and autovalidate functions label Label labelWidth Label width max max value if type is 'int' or 'float' min min value if type is 'int' or 'float' name will be displayed by autovalidate function ('invalid ' + name) overlap '', 'left' or 'right', will cause padding and negative margin picker picker object rangeOptions range options arrows boolean, if true, display arrows //arrowStep number, step when clicking arrows //arrowSymbols array of two strings max number, maximum value min number, minimum value orientation 'horizontal' or 'vertical' step number, step thumbValue boolean, if true, value is displayed on thumb, or array of strings per value, or function(value), returns string thumbSize integer, px trackGradient string, css gradient for track trackImage string, image url, or array of image urls //trackStep number, 0 for 'scroll here', positive for step trackValues boolean serialize function used to serialize value in submit style 'rounded' or 'square' textAlign 'left', 'center' or 'right' type 'float', 'int', 'password', 'text', 'textarea' value string validate remote validation width px change input changed event submit input submit event @*/ Ox.Input = function(options, self) { self = self || {}; var that = Ox.Element({}, self) .defaults({ arrows: false, arrowStep: 1, autocomplete: null, autocompleteReplace: false, autocompleteReplaceCorrect: false, autocompleteSelect: false, autocompleteSelectHighlight: false, autocompleteSelectMax: 0, autocompleteSelectMaxWidth: 0, autocompleteSelectSubmit: false, autovalidate: null, changeOnKeypress: false, clear: false, decimals: 0, disabled: false, height: 16, key: '', min: -Infinity, max: Infinity, label: '', labelWidth: 64, overlap: 'none', placeholder: '', serialize: null, style: 'rounded', textAlign: 'left', type: 'text', validate: null, value: '', width: 128 }) .options(options || {}) .addClass( 'OxInput OxMedium Ox' + Ox.toTitleCase(self.options.style) /*+ ( self.options.overlap != 'none' ? ' OxOverlap' + Ox.toTitleCase(self.options.overlap) : '' )*/ ) .css( Ox.extend({ width: self.options.width + 'px' }, self.options.type == 'textarea' ? { height: self.options.height + 'px' } : {}) ) .bindEvent(Ox.extend(self.options.type == 'textarea' ? { key_shift_enter: submit } : { key_enter: submit }, { key_control_v: paste, key_escape: cancel })); if ( Ox.isArray(self.options.autocomplete) && self.options.autocompleteReplace && self.options.autocompleteReplaceCorrect && self.options.value === '' ) { self.options.value = self.options.autocomplete[0] } // fixme: set to min, not 0 // fixme: validate self.options.value ! if (self.options.type == 'float') { self.decimals = Ox.repeat('0', self.options.decimals || 1) Ox.extend(self.options, { autovalidate: 'float', textAlign: 'right', value: self.options.value || '0.' + self.decimals }); } else if (self.options.type == 'int') { Ox.extend(self.options, { autovalidate: 'int', textAlign: 'right', value: self.options.value || '0' }); } if (self.options.label) { self.$label = Ox.Label({ overlap: 'right', textAlign: 'right', title: self.options.label, width: self.options.labelWidth }) .css({ float: 'left', // fixme: use css rule }) .click(function() { // fixme: ??? // that.focus(); }) .appendTo(that); } if (self.options.arrows) { self.arrows = []; self.arrows[0] = [ Ox.Button({ overlap: 'right', title: 'left', type: 'image' }) .css({ float: 'left' }) .click(function() { clickArrow(0); }) .appendTo(that), Ox.Button({ overlap: 'left', title: 'right', type: 'image' }) .css({ float: 'right' }) .click(function() { clickArrow(1); }) .appendTo(that) ] } self.bindKeyboard = self.options.autocomplete || self.options.autovalidate || self.options.changeOnKeypress; self.hasPasswordPlaceholder = self.options.type == 'password' && self.options.placeholder; self.inputWidth = getInputWidth(); if (self.options.clear) { self.$button = Ox.Button({ overlap: 'left', title: 'close', type: 'image' }) .css({ float: 'right' // fixme: use css rule }) .click(clear) .appendTo(that); } self.$input = $(self.options.type == 'textarea' ? '