Source: controls/Spinner.js

/***
 * @guicreatable
 * @requirefiles {controls/Control.js,controls/Textbox.js}
 */

/**
 * Creates an instance of a new Spinner control.
 * @class Spinner
 * @classdesc The Spinner class provides a GUI representation of a Spinner.
 * @augments Control
 * @mixes Editor
 * @param settings
 * @param {JQuery} settings.Parent Element within which to inject the textbox.
 * @param {Control} [settings.ForFocus=null] Control which should receive focus in place of this one.
 * @param {Control} [settings.ParentControl=null] The containing control.
 * @param {number} [settings.TabIndex=0] The tab index of the control determines which order controls may shift focus via the tab key.
 * @param {number[]} [settings.BlockedKeys=[]] Key codes which should be blocked from input.
 * @param {string} [settings.Class=""] CSS class(es) to apply to the injected textbox, space separated.
 * @param {Object} [settings.DataBinding=null] The data binding to bind the textbox value to.
 * @param {Control} [settings.KeyBlocker=settings.ParentControl] Control which should receive input events from blocked keys.
 * @param {Control} [settings.ParentControl=null] The control which contains the textbox, if applicable.
 * @param {boolean} [settings.ReadOnly=false] Sets whether or not the control is read-only.
 * @param {Object.<string, string>} [settings.Style={}] CSS style to apply to the injected textbox.
 * @param {Validator} [settings.Validator=Validator] Validator to use in conjunction with the textbox control.
 * @param {string} [settings.Value=""] Value to set in the textbox upon creation.
 * @constructor
 */
function Spinner(settings) {
    /**
     * Element within which the spinner has been injected.
     * @name Spinner#Parent
     * @type {JQuery}
     */
    this.Parent = settings.Parent;
    this._dataSource = null;
    Object.defineProperty(this, 'DataSource', {
        get: function () { return (this._dataSource); },
        set: function (value) {
            this._dataSource = value;
            if (this._dataSource instanceof Array) {
                this._dataSource.OnChange = this.OnDataSourceChanged;
                this._dataSource.pop = function () { var ret = Array.prototype.pop.apply(this, arguments); if (this.OnChange) { this.OnChange(); } return (ret); };
                this._dataSource.push = function () { var ret = Array.prototype.push.apply(this, arguments); if (this.OnChange) { this.OnChange(); } return (ret); };
                this._dataSource.reverse = function () { var ret = Array.prototype.reverse.apply(this, arguments); if (this.OnChange) { this.OnChange(); } return (ret); };
                this._dataSource.shift = function () { var ret = Array.prototype.shift.apply(this, arguments); if (this.OnChange) { this.OnChange(); } return (ret); };
                this._dataSource.slice = function () { var ret = Array.prototype.slice.apply(this, arguments); if (this.OnChange) { this.OnChange(); } return (ret); };
                this._dataSource.sort = function () { var ret = Array.prototype.sort.apply(this, arguments); if (this.OnChange) { this.OnChange(); } return (ret); };
                this._dataSource.splice = function () { var ret = Array.prototype.splice.apply(this, arguments); if (this.OnChange) { this.OnChange(); } return (ret); };
                this._dataSource.unshift = function () { var ret = Array.prototype.unshift.apply(this, arguments); if (this.OnChange) { this.OnChange(); } return (ret); };
            }
            this.OnDataSourceChanged();
        }
    });
    this.SelectedIndex = 0;
    if (settings.hasOwnProperty('DataSource')) { this.DataSource = settings.DataSource; }
    this.Parent.append('<div class="spinner"><div class="scroller"><table border="0" cellpadding="0" cellspacing="0"><tr><td class="up button" align="center" valign="middle"><div class="glyph"></div></td></tr><tr><td class="down button" align="center" valign="middle"><div class="glyph"></div></td></tr></table></div></div>');
    this.Container = $('div.spinner', this.Parent).last();
    this.CanGetFocus = true;
    this.CanTabFocus = true;
    Control.prototype.Initialize.call(this, arguments, [settings.ParentControl, [this.Container]]);
    this.Container.append('<div class="bg" style="right: 15px;"></div>');
    this.Text = new Textbox({
        Parent: this.Container,
        ParentControl: this,
        BlockedKeys: (settings.hasOwnProperty('BlockedKeys') ? settings.BlockedKeys : new Array()),
        KeyBlocker: this.ParentControl,
        Validator: Validator.String,
        ReadOnly: (settings.hasOwnProperty('AllowEntry') ? !settings.AllowEntry : false),
        Style: {
            'background-color': '',
            display: 'block',
            position: 'absolute',
            border: '0px',
            margin: '0px',
            padding: '0px',
            outline: 'none',
            'z-index': '3',
            left: '1px',
            top: '-1px',
            right: '15px',
            bottom: '0px',
            overflow: 'hidden'
        },
        Value: this.Default
    });
    if (settings.hasOwnProperty('Style')) {
        for (var style in settings.Style) {
            if (settings.Style.hasOwnProperty(style)) {
                this.Container.css(style, settings.Style[style]);
            }
        }
        this.Style = settings.Style;
    } else {
        this.Style = {};
    }
    Object.defineProperty(this, 'onKeyBlock', {
        get: function () { return (this.Text.onKeyBlock); },
        set: function (value) { this.Text.onKeyBlock = value; }
    });
    Object.defineProperty(this, 'onResize', {
        get: function () { return (this.Text.onResize); },
        set: function (value) { this.Text.onResize = value; }
    });
    Object.defineProperty(this, 'AllowEntry', {
        get: function () { return (!this.Text.ReadOnly); },
        set: function (value) { this.Text.ReadOnly = !value; }
    });
    Object.defineProperty(this, 'Text', {
        get: function () { return (this.Text.Value); },
        set: function (value) { this.Text.Value = value; }
    });
    Object.defineProperty(this, 'Value', {
        get: Spinner_Value_get,
        set: Spinner_Value_set
    });
    if (settings.hasOwnProperty('Value')) { this.Value = settings.Value; }
}
function Spinner_DataSourceChanged() {
    if (this._dataSource instanceof Array) {
        if (this._dataSource.length <= this.SelectedIndex) {
            this.SelectedIndex = this._dataSource.length - 1;
        } else if (this.SelectedIndex < 0) {
            this.SelectedIndex = 0;
        }
        if (this.SelectedIndex != -1) {
            this.Value = this._dataSource[this.SelectedIndex];
        } else {
            this.Value = { Value: -2, Text: '' };
        }
    } else {

    }
}
function Spinner_Dispose() {
    Control.prototype.Dispose.call(this);
}
function Spinner_Keydown(event) {
    if (this.Text.BlockedKeys.indexOf(event.which) >= 0) {
        if (this.Text.onKeyBlock != null) { this.Text.onKeyBlock.apply(this.ParentControl, [event]); }
        return (false);
    }
    event.stopPropagation();
}
function Spinner_Keypress(event) {
    event.stopPropagation();
}
function Spinner_Keyup(event) {
    event.stopPropagation();
}
function Spinner_Mousedown(event) {
    event.stopPropagation();
}
function Spinner_Mouseup(event) {
    event.stopPropagation();
    event.preventDefault();
}
function Spinner_Value_get() {
    if (this._dataSource != null) {
        if (this._dataSource instanceof Array) {
            if (this.SelectedIndex >= 0) {
                return ((typeof this._dataSource[this.SelectedIndex] == 'string' ? this._dataSource[this.SelectedIndex] : this._dataSource[this.SelectedIndex].Value));
            } else {
                return (this.Text.Value);
            }
        } else if (this._dataSource instanceof DBTable) {
            if (this.SelectedIndex >= 0) {
                return (parseInt(this._dataSource.Rows[this.SelectedIndex]));
            } else {
                return (this.Text.Value);
            }
        }
    } else {
        return (this.Text.Value);
    }
}
function Spinner_Value_set(value) {
    if (this._dataSource != null) {
        if (this._dataSource instanceof Array) {
            var sel = -1;
            for (var i = 0; i < this._dataSource.length; i++) {
                if (this._dataSource[i].Value == value) { sel = i; break; }
                if ((sel < 0) && (this._dataSource[i].Text == value)) { sel = i; }
            }
            if (sel >= 0) {
                this.SelectedIndex = sel;
                this.Text.Value = this._dataSource[sel].Text;
            }
        } else if (this._dataSource instanceof DBTable) {
            var sel = -1;
            for (var i = 0; i < this._dataSource.Rows.length; i++) {
                if (this._dataSource.Rows[i][".BIGINT"].slice(0, 2) == value) { sel = i; break; }
                if ((sel < 0) && (this._dataSource[i].ToString() == value)) { sel = i; }
            }
            if (sel >= 0) {
                this.SelectedIndex = sel;
                this.Text.Value = this._dataSource[sel].ToString();
            }
        }
    } else {
        this.ValueArray = value;
    }
    if (this.onResize != null) { this.onResize(); }
}
Spinner.prototype = new Control();
Spinner.prototype.constructor = Spinner;
Spinner.prototype.DataSourceChanged = Spinner_DataSourceChanged;
Spinner.prototype.Dispose = Spinner_Dispose;
Spinner.prototype.Keydown = Spinner_Keydown;
Spinner.prototype.Keypress = Spinner_Keypress;
Spinner.prototype.Keyup = Spinner_Keyup;
Spinner.prototype.Mousedown = Spinner_Mousedown;
Spinner.prototype.Mouseup = Spinner_Mouseup;
Spinner.prototype.Value_get = Spinner_Value_get;
Spinner.prototype.Value_set = Spinner_Value_set;