/*** * Contains basic SlickGrid editors. * @module Editors * @namespace Slick */ (function ($) { // register namespace $.extend(true, window, { "Slick": { "Editors": { "Text": TextEditor, "Integer": IntegerEditor, "Float": FloatEditor, "Date": DateEditor, "YesNoSelect": YesNoSelectEditor, "Checkbox": CheckboxEditor, "PercentComplete": PercentCompleteEditor, "LongText": LongTextEditor } } }); function TextEditor(args) { var $input; var defaultValue; var scope = this; this.init = function () { var navOnLR = args.grid.getOptions().editorCellNavOnLRKeys; $input = $("") .appendTo(args.container) .on("keydown.nav", navOnLR ? handleKeydownLRNav : handleKeydownLRNoNav) .focus() .select(); }; this.destroy = function () { $input.remove(); }; this.focus = function () { $input.focus(); }; this.getValue = function () { return $input.val(); }; this.setValue = function (val) { $input.val(val); }; this.loadValue = function (item) { defaultValue = item[args.column.field] || ""; $input.val(defaultValue); $input[0].defaultValue = defaultValue; $input.select(); }; this.serializeValue = function () { return $input.val(); }; this.applyValue = function (item, state) { item[args.column.field] = state; }; this.isValueChanged = function () { return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue); }; this.validate = function () { if (args.column.validator) { var validationResults = args.column.validator($input.val()); if (!validationResults.valid) { return validationResults; } } return { valid: true, msg: null }; }; this.init(); } function IntegerEditor(args) { var $input; var defaultValue; var scope = this; this.init = function () { var navOnLR = args.grid.getOptions().editorCellNavOnLRKeys; $input = $("") .appendTo(args.container) .on("keydown.nav", navOnLR ? handleKeydownLRNav : handleKeydownLRNoNav) .focus() .select(); }; this.destroy = function () { $input.remove(); }; this.focus = function () { $input.focus(); }; this.loadValue = function (item) { defaultValue = item[args.column.field]; $input.val(defaultValue); $input[0].defaultValue = defaultValue; $input.select(); }; this.serializeValue = function () { return parseInt($input.val(), 10) || 0; }; this.applyValue = function (item, state) { item[args.column.field] = state; }; this.isValueChanged = function () { return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue); }; this.validate = function () { if (isNaN($input.val())) { return { valid: false, msg: "Please enter a valid integer" }; } if (args.column.validator) { var validationResults = args.column.validator($input.val()); if (!validationResults.valid) { return validationResults; } } return { valid: true, msg: null }; }; this.init(); } function FloatEditor(args) { var $input; var defaultValue; var scope = this; this.init = function () { var navOnLR = args.grid.getOptions().editorCellNavOnLRKeys; $input = $("") .appendTo(args.container) .on("keydown.nav", navOnLR ? handleKeydownLRNav : handleKeydownLRNoNav) .focus() .select(); }; this.destroy = function () { $input.remove(); }; this.focus = function () { $input.focus(); }; function getDecimalPlaces() { // returns the number of fixed decimal places or null var rtn = args.column.editorFixedDecimalPlaces; if (typeof rtn == 'undefined') { rtn = FloatEditor.DefaultDecimalPlaces; } return (!rtn && rtn!==0 ? null : rtn); } this.loadValue = function (item) { defaultValue = item[args.column.field]; var decPlaces = getDecimalPlaces(); if (decPlaces !== null && (defaultValue || defaultValue===0) && defaultValue.toFixed) { defaultValue = defaultValue.toFixed(decPlaces); } $input.val(defaultValue); $input[0].defaultValue = defaultValue; $input.select(); }; this.serializeValue = function () { var rtn = parseFloat($input.val()); if (FloatEditor.AllowEmptyValue) { if (!rtn && rtn !==0) { rtn = ''; } } else { rtn = rtn || 0; } var decPlaces = getDecimalPlaces(); if (decPlaces !== null && (rtn || rtn===0) && rtn.toFixed) { rtn = parseFloat(rtn.toFixed(decPlaces)); } return rtn; }; this.applyValue = function (item, state) { item[args.column.field] = state; }; this.isValueChanged = function () { return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue); }; this.validate = function () { if (isNaN($input.val())) { return { valid: false, msg: "Please enter a valid number" }; } if (args.column.validator) { var validationResults = args.column.validator($input.val()); if (!validationResults.valid) { return validationResults; } } return { valid: true, msg: null }; }; this.init(); } FloatEditor.DefaultDecimalPlaces = null; FloatEditor.AllowEmptyValue = false; function DateEditor(args) { var $input; var defaultValue; var scope = this; var calendarOpen = false; this.init = function () { $input = $(""); $input.appendTo(args.container); $input.focus().select(); $input.datepicker({ showOn: "button", buttonImageOnly: true, beforeShow: function () { calendarOpen = true }, onClose: function () { calendarOpen = false } }); $input.width($input.width() - 18); }; this.destroy = function () { $.datepicker.dpDiv.stop(true, true); $input.datepicker("hide"); $input.datepicker("destroy"); $input.remove(); }; this.show = function () { if (calendarOpen) { $.datepicker.dpDiv.stop(true, true).show(); } }; this.hide = function () { if (calendarOpen) { $.datepicker.dpDiv.stop(true, true).hide(); } }; this.position = function (position) { if (!calendarOpen) { return; } $.datepicker.dpDiv .css("top", position.top + 30) .css("left", position.left); }; this.focus = function () { $input.focus(); }; this.loadValue = function (item) { defaultValue = item[args.column.field]; $input.val(defaultValue); $input[0].defaultValue = defaultValue; $input.select(); }; this.serializeValue = function () { return $input.val(); }; this.applyValue = function (item, state) { item[args.column.field] = state; }; this.isValueChanged = function () { return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue); }; this.validate = function () { if (args.column.validator) { var validationResults = args.column.validator($input.val()); if (!validationResults.valid) { return validationResults; } } return { valid: true, msg: null }; }; this.init(); } function YesNoSelectEditor(args) { var $select; var defaultValue; var scope = this; this.init = function () { $select = $(""); $select.appendTo(args.container); $select.focus(); }; this.destroy = function () { $select.remove(); }; this.focus = function () { $select.focus(); }; this.loadValue = function (item) { $select.val((defaultValue = item[args.column.field]) ? "yes" : "no"); $select.select(); }; this.serializeValue = function () { return ($select.val() == "yes"); }; this.applyValue = function (item, state) { item[args.column.field] = state; }; this.isValueChanged = function () { return ($select.val() != defaultValue); }; this.validate = function () { return { valid: true, msg: null }; }; this.init(); } function CheckboxEditor(args) { var $select; var defaultValue; var scope = this; this.init = function () { $select = $(""); $select.appendTo(args.container); $select.focus(); }; this.destroy = function () { $select.remove(); }; this.focus = function () { $select.focus(); }; this.loadValue = function (item) { defaultValue = !!item[args.column.field]; if (defaultValue) { $select.prop('checked', true); } else { $select.prop('checked', false); } }; this.preClick = function () { $select.prop('checked', !$select.prop('checked')); }; this.serializeValue = function () { return $select.prop('checked'); }; this.applyValue = function (item, state) { item[args.column.field] = state; }; this.isValueChanged = function () { return (this.serializeValue() !== defaultValue); }; this.validate = function () { return { valid: true, msg: null }; }; this.init(); } function PercentCompleteEditor(args) { var $input, $picker; var defaultValue; var scope = this; this.init = function () { $input = $(""); $input.width($(args.container).innerWidth() - 25); $input.appendTo(args.container); $picker = $("
").appendTo(args.container); $picker.append("