UNPKG

24.2 kBJavaScriptView Raw
1/***
2 * Contains basic SlickGrid editors.
3 * @module Editors
4 * @namespace Slick
5 */
6
7(function ($) {
8 function TextEditor(args) {
9 var $input;
10 var defaultValue;
11 var scope = this;
12 this.args = args;
13
14 this.init = function () {
15 var navOnLR = args.grid.getOptions().editorCellNavOnLRKeys;
16 $input = $("<INPUT type=text class='editor-text' />")
17 .appendTo(args.container)
18 .on("keydown.nav", navOnLR ? handleKeydownLRNav : handleKeydownLRNoNav)
19 .focus()
20 .select();
21
22 // don't show Save/Cancel when it's a Composite Editor and also trigger a onCompositeEditorChange event when input changes
23 if (args.compositeEditorOptions) {
24 $input.on("change", function () {
25 var activeCell = args.grid.getActiveCell();
26
27 // when valid, we'll also apply the new value to the dataContext item object
28 if (scope.validate().valid) {
29 scope.applyValue(scope.args.item, scope.serializeValue());
30 }
31 scope.applyValue(scope.args.compositeEditorOptions.formValues, scope.serializeValue());
32 args.grid.onCompositeEditorChange.notify({ row: activeCell.row, cell: activeCell.cell, item: scope.args.item, column: scope.args.column, formValues: scope.args.compositeEditorOptions.formValues });
33 });
34 }
35 };
36
37 this.destroy = function () {
38 $input.remove();
39 };
40
41 this.focus = function () {
42 $input.focus();
43 };
44
45 this.getValue = function () {
46 return $input.val();
47 };
48
49 this.setValue = function (val) {
50 $input.val(val);
51 };
52
53 this.loadValue = function (item) {
54 defaultValue = item[args.column.field] || "";
55 $input.val(defaultValue);
56 $input[0].defaultValue = defaultValue;
57 $input.select();
58 };
59
60 this.serializeValue = function () {
61 return $input.val();
62 };
63
64 this.applyValue = function (item, state) {
65 item[args.column.field] = state;
66 };
67
68 this.isValueChanged = function () {
69 return (!($input.val() === "" && defaultValue == null)) && ($input.val() != defaultValue);
70 };
71
72 this.validate = function () {
73 if (args.column.validator) {
74 var validationResults = args.column.validator($input.val(), args);
75 if (!validationResults.valid) {
76 return validationResults;
77 }
78 }
79
80 return {
81 valid: true,
82 msg: null
83 };
84 };
85
86 this.init();
87 }
88
89 function IntegerEditor(args) {
90 var $input;
91 var defaultValue;
92 var scope = this;
93 this.args = args;
94
95 this.init = function () {
96 var navOnLR = args.grid.getOptions().editorCellNavOnLRKeys;
97 $input = $("<INPUT type=text class='editor-text' />")
98 .appendTo(args.container)
99 .on("keydown.nav", navOnLR ? handleKeydownLRNav : handleKeydownLRNoNav)
100 .focus()
101 .select();
102
103 // trigger onCompositeEditorChange event when input changes and it's a Composite Editor
104 if (args.compositeEditorOptions) {
105 $input.on("change", function () {
106 var activeCell = args.grid.getActiveCell();
107
108 // when valid, we'll also apply the new value to the dataContext item object
109 if (scope.validate().valid) {
110 scope.applyValue(scope.args.item, scope.serializeValue());
111 }
112 scope.applyValue(scope.args.compositeEditorOptions.formValues, scope.serializeValue());
113 args.grid.onCompositeEditorChange.notify({ row: activeCell.row, cell: activeCell.cell, item: scope.args.item, column: scope.args.column, formValues: scope.args.compositeEditorOptions.formValues });
114 });
115 }
116 };
117
118 this.destroy = function () {
119 $input.remove();
120 };
121
122 this.focus = function () {
123 $input.focus();
124 };
125
126 this.loadValue = function (item) {
127 defaultValue = item[args.column.field];
128 $input.val(defaultValue);
129 $input[0].defaultValue = defaultValue;
130 $input.select();
131 };
132
133 this.serializeValue = function () {
134 return parseInt($input.val(), 10) || 0;
135 };
136
137 this.applyValue = function (item, state) {
138 item[args.column.field] = state;
139 };
140
141 this.isValueChanged = function () {
142 return (!($input.val() === "" && defaultValue == null)) && ($input.val() != defaultValue);
143 };
144
145 this.validate = function () {
146 if (isNaN($input.val())) {
147 return {
148 valid: false,
149 msg: "Please enter a valid integer"
150 };
151 }
152
153 if (args.column.validator) {
154 var validationResults = args.column.validator($input.val(), args);
155 if (!validationResults.valid) {
156 return validationResults;
157 }
158 }
159
160 return {
161 valid: true,
162 msg: null
163 };
164 };
165
166 this.init();
167 }
168
169 function FloatEditor(args) {
170 var $input;
171 var defaultValue;
172 var scope = this;
173 this.args = args;
174
175 this.init = function () {
176 var navOnLR = args.grid.getOptions().editorCellNavOnLRKeys;
177 $input = $("<INPUT type=text class='editor-text' />")
178 .appendTo(args.container)
179 .on("keydown.nav", navOnLR ? handleKeydownLRNav : handleKeydownLRNoNav)
180 .focus()
181 .select();
182
183 // trigger onCompositeEditorChange event when input changes and it's a Composite Editor
184 if (args.compositeEditorOptions) {
185 $input.on("change", function () {
186 var activeCell = args.grid.getActiveCell();
187
188 // when valid, we'll also apply the new value to the dataContext item object
189 if (scope.validate().valid) {
190 scope.applyValue(scope.args.item, scope.serializeValue());
191 }
192 scope.applyValue(scope.args.compositeEditorOptions.formValues, scope.serializeValue());
193 args.grid.onCompositeEditorChange.notify({ row: activeCell.row, cell: activeCell.cell, item: scope.args.item, column: scope.args.column, formValues: scope.args.compositeEditorOptions.formValues });
194 });
195 }
196 };
197
198 this.destroy = function () {
199 $input.remove();
200 };
201
202 this.focus = function () {
203 $input.focus();
204 };
205
206 function getDecimalPlaces() {
207 // returns the number of fixed decimal places or null
208 var rtn = args.column.editorFixedDecimalPlaces;
209 if (typeof rtn == 'undefined') {
210 rtn = FloatEditor.DefaultDecimalPlaces;
211 }
212 return (!rtn && rtn !== 0 ? null : rtn);
213 }
214
215 this.loadValue = function (item) {
216 defaultValue = item[args.column.field];
217
218 var decPlaces = getDecimalPlaces();
219 if (decPlaces !== null
220 && (defaultValue || defaultValue === 0)
221 && defaultValue.toFixed) {
222 defaultValue = defaultValue.toFixed(decPlaces);
223 }
224
225 $input.val(defaultValue);
226 $input[0].defaultValue = defaultValue;
227 $input.select();
228 };
229
230 this.serializeValue = function () {
231 var rtn = parseFloat($input.val());
232 if (FloatEditor.AllowEmptyValue) {
233 if (!rtn && rtn !== 0) { rtn = ''; }
234 } else {
235 rtn = rtn || 0;
236 }
237
238 var decPlaces = getDecimalPlaces();
239 if (decPlaces !== null
240 && (rtn || rtn === 0)
241 && rtn.toFixed) {
242 rtn = parseFloat(rtn.toFixed(decPlaces));
243 }
244
245 return rtn;
246 };
247
248 this.applyValue = function (item, state) {
249 item[args.column.field] = state;
250 };
251
252 this.isValueChanged = function () {
253 return (!($input.val() === "" && defaultValue == null)) && ($input.val() != defaultValue);
254 };
255
256 this.validate = function () {
257 if (isNaN($input.val())) {
258 return {
259 valid: false,
260 msg: "Please enter a valid number"
261 };
262 }
263
264 if (args.column.validator) {
265 var validationResults = args.column.validator($input.val(), args);
266 if (!validationResults.valid) {
267 return validationResults;
268 }
269 }
270
271 return {
272 valid: true,
273 msg: null
274 };
275 };
276
277 this.init();
278 }
279
280 FloatEditor.DefaultDecimalPlaces = null;
281 FloatEditor.AllowEmptyValue = false;
282
283 function DateEditor(args) {
284 var $input;
285 var defaultValue;
286 var scope = this;
287 var calendarOpen = false;
288 this.args = args;
289
290 this.init = function () {
291 $input = $("<INPUT type=text class='editor-text' />");
292 $input.appendTo(args.container);
293 $input.focus().select();
294 $input.datepicker({
295 showOn: "button",
296 buttonImageOnly: true,
297 beforeShow: function () {
298 calendarOpen = true;
299 },
300 onClose: function () {
301 calendarOpen = false;
302
303 // trigger onCompositeEditorChange event when input changes and it's a Composite Editor
304 if (args.compositeEditorOptions) {
305 var activeCell = args.grid.getActiveCell();
306
307 // when valid, we'll also apply the new value to the dataContext item object
308 if (scope.validate().valid) {
309 scope.applyValue(scope.args.item, scope.serializeValue());
310 }
311 scope.applyValue(scope.args.compositeEditorOptions.formValues, scope.serializeValue());
312 args.grid.onCompositeEditorChange.notify({ row: activeCell.row, cell: activeCell.cell, item: scope.args.item, column: scope.args.column, formValues: scope.args.compositeEditorOptions.formValues });
313 }
314 }
315 });
316
317 $input.width($input.width() - (!args.compositeEditorOptions ? 18 : 28));
318 };
319
320 this.destroy = function () {
321 $.datepicker.dpDiv.stop(true, true);
322 $input.datepicker("hide");
323 $input.datepicker("destroy");
324 $input.remove();
325 };
326
327 this.show = function () {
328 if (calendarOpen) {
329 $.datepicker.dpDiv.stop(true, true).show();
330 }
331 };
332
333 this.hide = function () {
334 if (calendarOpen) {
335 $.datepicker.dpDiv.stop(true, true).hide();
336 }
337 };
338
339 this.position = function (position) {
340 if (!calendarOpen) {
341 return;
342 }
343 $.datepicker.dpDiv
344 .css("top", position.top + 30)
345 .css("left", position.left);
346 };
347
348 this.focus = function () {
349 $input.focus();
350 };
351
352 this.loadValue = function (item) {
353 defaultValue = item[args.column.field];
354 $input.val(defaultValue);
355 $input[0].defaultValue = defaultValue;
356 $input.select();
357 };
358
359 this.serializeValue = function () {
360 return $input.val();
361 };
362
363 this.applyValue = function (item, state) {
364 item[args.column.field] = state;
365 };
366
367 this.isValueChanged = function () {
368 return (!($input.val() === "" && defaultValue == null)) && ($input.val() != defaultValue);
369 };
370
371 this.validate = function () {
372 if (args.column.validator) {
373 var validationResults = args.column.validator($input.val(), args);
374 if (!validationResults.valid) {
375 return validationResults;
376 }
377 }
378
379 return {
380 valid: true,
381 msg: null
382 };
383 };
384
385 this.init();
386 }
387
388 function YesNoSelectEditor(args) {
389 var $select;
390 var defaultValue;
391 var scope = this;
392 this.args = args;
393
394 this.init = function () {
395 $select = $("<SELECT tabIndex='0' class='editor-yesno'><OPTION value='yes'>Yes</OPTION><OPTION value='no'>No</OPTION></SELECT>");
396 $select.appendTo(args.container);
397 $select.focus();
398
399 // trigger onCompositeEditorChange event when input changes and it's a Composite Editor
400 if (args.compositeEditorOptions) {
401 $select.on("change", function () {
402 var activeCell = args.grid.getActiveCell();
403
404 // when valid, we'll also apply the new value to the dataContext item object
405 if (scope.validate().valid) {
406 scope.applyValue(scope.args.item, scope.serializeValue());
407 }
408 scope.applyValue(scope.args.compositeEditorOptions.formValues, scope.serializeValue());
409 args.grid.onCompositeEditorChange.notify({ row: activeCell.row, cell: activeCell.cell, item: scope.args.item, column: scope.args.column, formValues: scope.args.compositeEditorOptions.formValues });
410 });
411 }
412 };
413
414 this.destroy = function () {
415 $select.remove();
416 };
417
418 this.focus = function () {
419 $select.focus();
420 };
421
422 this.loadValue = function (item) {
423 $select.val((defaultValue = item[args.column.field]) ? "yes" : "no");
424 $select.select();
425 };
426
427 this.serializeValue = function () {
428 return ($select.val() == "yes");
429 };
430
431 this.applyValue = function (item, state) {
432 item[args.column.field] = state;
433 };
434
435 this.isValueChanged = function () {
436 return ($select.val() != defaultValue);
437 };
438
439 this.validate = function () {
440 return {
441 valid: true,
442 msg: null
443 };
444 };
445
446 this.init();
447 }
448
449 function CheckboxEditor(args) {
450 var $select;
451 var defaultValue;
452 var scope = this;
453 this.args = args;
454
455 this.init = function () {
456 $select = $("<INPUT type=checkbox value='true' class='editor-checkbox' hideFocus>");
457 $select.appendTo(args.container);
458 $select.focus();
459
460 // trigger onCompositeEditorChange event when input checkbox changes and it's a Composite Editor
461 if (args.compositeEditorOptions) {
462 $select.on("change", function () {
463 var activeCell = args.grid.getActiveCell();
464
465 // when valid, we'll also apply the new value to the dataContext item object
466 if (scope.validate().valid) {
467 scope.applyValue(scope.args.item, scope.serializeValue());
468 }
469 scope.applyValue(scope.args.compositeEditorOptions.formValues, scope.serializeValue());
470 args.grid.onCompositeEditorChange.notify({ row: activeCell.row, cell: activeCell.cell, item: scope.args.item, column: scope.args.column, formValues: scope.args.compositeEditorOptions.formValues });
471 });
472 }
473 };
474
475 this.destroy = function () {
476 $select.remove();
477 };
478
479 this.focus = function () {
480 $select.focus();
481 };
482
483 this.loadValue = function (item) {
484 defaultValue = !!item[args.column.field];
485 if (defaultValue) {
486 $select.prop('checked', true);
487 } else {
488 $select.prop('checked', false);
489 }
490 };
491
492 this.preClick = function () {
493 $select.prop('checked', !$select.prop('checked'));
494 };
495
496 this.serializeValue = function () {
497 return $select.prop('checked');
498 };
499
500 this.applyValue = function (item, state) {
501 item[args.column.field] = state;
502 };
503
504 this.isValueChanged = function () {
505 return (this.serializeValue() !== defaultValue);
506 };
507
508 this.validate = function () {
509 return {
510 valid: true,
511 msg: null
512 };
513 };
514
515 this.init();
516 }
517
518 function PercentCompleteEditor(args) {
519 var $input, $picker;
520 var defaultValue;
521 var scope = this;
522 this.args = args;
523
524 this.init = function () {
525 $input = $("<INPUT type=text class='editor-percentcomplete' />");
526 $input.width($(args.container).innerWidth() - 25);
527 $input.appendTo(args.container);
528
529 $picker = $("<div class='editor-percentcomplete-picker' />").appendTo(args.container);
530 $picker.append("<div class='editor-percentcomplete-helper'><div class='editor-percentcomplete-wrapper'><div class='editor-percentcomplete-slider' /><div class='editor-percentcomplete-buttons' /></div></div>");
531
532 $picker.find(".editor-percentcomplete-buttons").append("<button val=0>Not started</button><br/><button val=50>In Progress</button><br/><button val=100>Complete</button>");
533
534 $input.focus().select();
535
536 $picker.find(".editor-percentcomplete-slider").slider({
537 orientation: "vertical",
538 range: "min",
539 value: defaultValue,
540 slide: function (event, ui) {
541 $input.val(ui.value);
542 },
543 stop: function (event, ui) {
544 // trigger onCompositeEditorChange event when slider stops and it's a Composite Editor
545 if (args.compositeEditorOptions) {
546 var activeCell = args.grid.getActiveCell();
547
548 // when valid, we'll also apply the new value to the dataContext item object
549 if (scope.validate().valid) {
550 scope.applyValue(scope.args.item, scope.serializeValue());
551 }
552 scope.applyValue(scope.args.compositeEditorOptions.formValues, scope.serializeValue());
553 args.grid.onCompositeEditorChange.notify({ row: activeCell.row, cell: activeCell.cell, item: scope.args.item, column: scope.args.column, formValues: scope.args.compositeEditorOptions.formValues });
554 }
555 }
556 });
557
558 $picker.find(".editor-percentcomplete-buttons button").on("click", function (e) {
559 $input.val($(this).attr("val"));
560 $picker.find(".editor-percentcomplete-slider").slider("value", $(this).attr("val"));
561 });
562 };
563
564 this.destroy = function () {
565 $input.remove();
566 $picker.remove();
567 };
568
569 this.focus = function () {
570 $input.focus();
571 };
572
573 this.loadValue = function (item) {
574 $input.val(defaultValue = item[args.column.field]);
575 $input.select();
576 };
577
578 this.serializeValue = function () {
579 return parseInt($input.val(), 10) || 0;
580 };
581
582 this.applyValue = function (item, state) {
583 item[args.column.field] = state;
584 };
585
586 this.isValueChanged = function () {
587 return (!($input.val() === "" && defaultValue == null)) && ((parseInt($input.val(), 10) || 0) != defaultValue);
588 };
589
590 this.validate = function () {
591 if (isNaN(parseInt($input.val(), 10))) {
592 return {
593 valid: false,
594 msg: "Please enter a valid positive number"
595 };
596 }
597
598 return {
599 valid: true,
600 msg: null
601 };
602 };
603
604 this.init();
605 }
606
607 /*
608 * An example of a "detached" editor.
609 * The UI is added onto document BODY and .position(), .show() and .hide() are implemented.
610 * KeyDown events are also handled to provide handling for Tab, Shift-Tab, Esc and Ctrl-Enter.
611 */
612 function LongTextEditor(args) {
613 var $input, $wrapper;
614 var defaultValue;
615 var scope = this;
616 this.args = args;
617
618 this.init = function () {
619 var compositeEditorOptions = args.compositeEditorOptions;
620 var navOnLR = args.grid.getOptions().editorCellNavOnLRKeys;
621 var $container = compositeEditorOptions ? args.container : $('body');
622
623 $wrapper = $("<DIV class='slick-large-editor-text' style='z-index:10000;background:white;padding:5px;border:3px solid gray; border-radius:10px;'/>")
624 .appendTo($container);
625 if (compositeEditorOptions) {
626 $wrapper.css({ position: 'relative', padding: 0, border: 0 });
627 } else {
628 $wrapper.css({ position: 'absolute' });
629 }
630
631 $input = $("<TEXTAREA hidefocus rows=5 style='background:white;width:250px;height:80px;border:0;outline:0'>")
632 .appendTo($wrapper);
633
634 // trigger onCompositeEditorChange event when input changes and it's a Composite Editor
635 if (compositeEditorOptions) {
636 $input.on("change", function () {
637 var activeCell = args.grid.getActiveCell();
638
639 // when valid, we'll also apply the new value to the dataContext item object
640 if (scope.validate().valid) {
641 scope.applyValue(scope.args.item, scope.serializeValue());
642 }
643 scope.applyValue(scope.args.compositeEditorOptions.formValues, scope.serializeValue());
644 args.grid.onCompositeEditorChange.notify({ row: activeCell.row, cell: activeCell.cell, item: scope.args.item, column: scope.args.column, formValues: scope.args.compositeEditorOptions.formValues });
645 });
646 } else {
647 $("<DIV style='text-align:right'><BUTTON>Save</BUTTON><BUTTON>Cancel</BUTTON></DIV>")
648 .appendTo($wrapper);
649
650 $wrapper.find("button:first").on("click", this.save);
651 $wrapper.find("button:last").on("click", this.cancel);
652 $input.on("keydown", this.handleKeyDown);
653 scope.position(args.position);
654 }
655
656 $input.focus().select();
657 };
658
659 this.handleKeyDown = function (e) {
660 if (e.which == Slick.keyCode.ENTER && e.ctrlKey) {
661 scope.save();
662 } else if (e.which == Slick.keyCode.ESCAPE) {
663 e.preventDefault();
664 scope.cancel();
665 } else if (e.which == Slick.keyCode.TAB && e.shiftKey) {
666 e.preventDefault();
667 args.grid.navigatePrev();
668 } else if (e.which == Slick.keyCode.TAB) {
669 e.preventDefault();
670 args.grid.navigateNext();
671 } else if (e.which == Slick.keyCode.LEFT || e.which == Slick.keyCode.RIGHT) {
672 if (args.grid.getOptions().editorCellNavOnLRKeys) {
673 var cursorPosition = this.selectionStart;
674 var textLength = this.value.length;
675 if (e.keyCode === Slick.keyCode.LEFT && cursorPosition === 0) {
676 args.grid.navigatePrev();
677 }
678 if (e.keyCode === Slick.keyCode.RIGHT && cursorPosition >= textLength - 1) {
679 args.grid.navigateNext();
680 }
681 }
682 }
683 };
684
685 this.save = function () {
686 args.commitChanges();
687 };
688
689 this.cancel = function () {
690 $input.val(defaultValue);
691 args.cancelChanges();
692 };
693
694 this.hide = function () {
695 $wrapper.hide();
696 };
697
698 this.show = function () {
699 $wrapper.show();
700 };
701
702 this.position = function (position) {
703 $wrapper
704 .css("top", position.top - 5)
705 .css("left", position.left - 5);
706 };
707
708 this.destroy = function () {
709 $wrapper.remove();
710 };
711
712 this.focus = function () {
713 $input.focus();
714 };
715
716 this.loadValue = function (item) {
717 $input.val(defaultValue = item[args.column.field]);
718 $input.select();
719 };
720
721 this.serializeValue = function () {
722 return $input.val();
723 };
724
725 this.applyValue = function (item, state) {
726 item[args.column.field] = state;
727 };
728
729 this.isValueChanged = function () {
730 return (!($input.val() === "" && defaultValue == null)) && ($input.val() != defaultValue);
731 };
732
733 this.validate = function () {
734 if (args.column.validator) {
735 var validationResults = args.column.validator($input.val(), args);
736 if (!validationResults.valid) {
737 return validationResults;
738 }
739 }
740
741 return {
742 valid: true,
743 msg: null
744 };
745 };
746
747 this.init();
748 }
749
750 /*
751 * Depending on the value of Grid option 'editorCellNavOnLRKeys', us
752 * Navigate to the cell on the left if the cursor is at the beginning of the input string
753 * and to the right cell if it's at the end. Otherwise, move the cursor within the text
754 */
755 function handleKeydownLRNav(e) {
756 var cursorPosition = this.selectionStart;
757 var textLength = this.value.length;
758 if ((e.keyCode === Slick.keyCode.LEFT && cursorPosition > 0) ||
759 e.keyCode === Slick.keyCode.RIGHT && cursorPosition < textLength - 1) {
760 e.stopImmediatePropagation();
761 }
762 }
763
764 function handleKeydownLRNoNav(e) {
765 if (e.keyCode === Slick.keyCode.LEFT || e.keyCode === Slick.keyCode.RIGHT) {
766 e.stopImmediatePropagation();
767 }
768 }
769
770 // exports
771 $.extend(true, window, {
772 "Slick": {
773 "Editors": {
774 "Text": TextEditor,
775 "Integer": IntegerEditor,
776 "Float": FloatEditor,
777 "Date": DateEditor,
778 "YesNoSelect": YesNoSelectEditor,
779 "Checkbox": CheckboxEditor,
780 "PercentComplete": PercentCompleteEditor,
781 "LongText": LongTextEditor
782 }
783 }
784 });
785})(jQuery);