Debian Codegraph @

dijit/InlineEditBox.js

raw symbols SCIP
1 define([
2 "require",
3 "dojo/_base/array", // array.forEach
4 "dojo/aspect",
5 "dojo/_base/declare", // declare
6 "dojo/dom-attr", // domAttr.set domAttr.get
7 "dojo/dom-class", // domClass.add domClass.remove domClass.toggle
8 "dojo/dom-construct", // domConstruct.create domConstruct.destroy
9 "dojo/dom-style", // domStyle.getComputedStyle domStyle.set domStyle.get
10 "dojo/i18n", // i18n.getLocalization
11 "dojo/_base/kernel", // kernel.deprecated
12 "dojo/keys", // keys.ENTER keys.ESCAPE
13 "dojo/_base/lang", // lang.getObject
14 "dojo/on",
15 "dojo/sniff", // has("ie")
16 "dojo/when",
17 "./a11yclick",
18 "./focus",
19 "./_Widget",
20 "./_TemplatedMixin",
21 "./_WidgetsInTemplateMixin",
22 "./_Container",
23 "./form/Button",
24 "./form/_TextBoxMixin",
25 "./form/TextBox",
26 "dojo/text!./templates/InlineEditBox.html",
27 "dojo/i18n!./nls/common"
28 ], function(require, array, aspect, declare, domAttr, domClass, domConstruct, domStyle, i18n, kernel, keys, lang, on, has, when, a11yclick, fm, _Widget, _TemplatedMixin, _WidgetsInTemplateMixin, _Container, Button, _TextBoxMixin, TextBox, template){
29
30 // module:
31 // dijit/InlineEditBox
32
33 var InlineEditor = declare("dijit._InlineEditor", [_Widget, _TemplatedMixin, _WidgetsInTemplateMixin], {
34 // summary:
35 // Internal widget used by InlineEditBox, displayed when in editing mode
36 // to display the editor and maybe save/cancel buttons. Calling code should
37 // connect to save/cancel methods to detect when editing is finished
38 //
39 // Has mainly the same parameters as InlineEditBox, plus these values:
40 //
41 // style: Object
42 // Set of CSS attributes of display node, to replicate in editor
43 //
44 // value: String
45 // Value as an HTML string or plain text string, depending on renderAsHTML flag
46
47 templateString: template,
48
49 contextRequire: require,
50
51 postMixInProperties: function(){
52 this.inherited(arguments);
53 this.messages = i18n.getLocalization("dijit", "common", this.lang);
54 array.forEach(["buttonSave", "buttonCancel"], function(prop){
55 if(!this[prop]){
56 this[prop] = this.messages[prop];
57 }
58 }, this);
59 },
60
61 buildRendering: function(){
62 this.inherited(arguments);
63
64 // Create edit widget in place in the template
65 // TODO: remove getObject() for 2.0
66 var Cls = typeof this.editor == "string" ? (lang.getObject(this.editor) || require(this.editor)) : this.editor;
67
68 // Copy the style from the source
69 // Don't copy ALL properties though, just the necessary/applicable ones.
70 // wrapperStyle/destStyle code is to workaround IE bug where getComputedStyle().fontSize
71 // is a relative value like 200%, rather than an absolute value like 24px, and
72 // the 200% can refer *either* to a setting on the node or it's ancestor (see #11175)
73 var srcStyle = this.sourceStyle,
74 editStyle = "line-height:" + srcStyle.lineHeight + ";",
75 destStyle = domStyle.getComputedStyle(this.domNode);
76 array.forEach(["Weight", "Family", "Size", "Style"], function(prop){
77 var textStyle = srcStyle["font" + prop],
78 wrapperStyle = destStyle["font" + prop];
79 if(wrapperStyle != textStyle){
80 editStyle += "font-" + prop + ":" + srcStyle["font" + prop] + ";";
81 }
82 }, this);
83 array.forEach(["marginTop", "marginBottom", "marginLeft", "marginRight", "position", "left", "top", "right", "bottom", "float", "clear", "display"], function(prop){
84 this.domNode.style[prop] = srcStyle[prop];
85 }, this);
86 var width = this.inlineEditBox.width;
87 if(width == "100%"){
88 // block mode
89 editStyle += "width:100%;";
90 this.domNode.style.display = "block";
91 }else{
92 // inline-block mode
93 editStyle += "width:" + (width + (Number(width) == width ? "px" : "")) + ";";
94 }
95 var editorParams = lang.delegate(this.inlineEditBox.editorParams, {
96 style: editStyle,
97 dir: this.dir,
98 lang: this.lang,
99 textDir: this.textDir
100 });
101 // set the value in onLoadDeferred instead so the widget has time to finish initializing
102 //editorParams[("displayedValue" in Cls.prototype || "_setDisplayedValueAttr" in Cls.prototype) ? "displayedValue" : "value"] = this.value;
103 this.editWidget = new Cls(editorParams, this.editorPlaceholder);
104
105 if(this.inlineEditBox.autoSave){
106 // Remove the save/cancel buttons since saving is done by simply tabbing away or
107 // selecting a value from the drop down list
108 this.saveButton.destroy();
109 this.cancelButton.destroy();
110 this.saveButton = this.cancelButton = null;
111 domConstruct.destroy(this.buttonContainer);
112 }
113 },
114
115 postCreate: function(){
116 this.inherited(arguments);
117
118 var ew = this.editWidget;
119
120 if(this.inlineEditBox.autoSave){
121 this.own(
122 // Selecting a value from a drop down list causes an onChange event and then we save
123 aspect.after(ew, "onChange", lang.hitch(this, "_onChange"), true),
124
125 // ESC and TAB should cancel and save.
126 on(ew, "keydown", lang.hitch(this, "_onKeyDown"))
127 );
128 }else{
129 // If possible, enable/disable save button based on whether the user has changed the value
130 if("intermediateChanges" in ew){
131 ew.set("intermediateChanges", true);
132 this.own(aspect.after(ew, "onChange", lang.hitch(this, "_onIntermediateChange"), true));
133 this.saveButton.set("disabled", true);
134 }
135 }
136 },
137
138 startup: function(){
139 this.editWidget.startup();
140 this.inherited(arguments);
141 },
142
143 _onIntermediateChange: function(/*===== val =====*/){
144 // summary:
145 // Called for editor widgets that support the intermediateChanges=true flag as a way
146 // to detect when to enable/disabled the save button
147 this.saveButton.set("disabled", (this.getValue() == this._resetValue) || !this.enableSave());
148 },
149
150 destroy: function(){
151 this.editWidget.destroy(true); // let the parent wrapper widget clean up the DOM
152 this.inherited(arguments);
153 },
154
155 getValue: function(){
156 // summary:
157 // Return the [display] value of the edit widget
158 var ew = this.editWidget;
159 return String(ew.get(("displayedValue" in ew || "_getDisplayedValueAttr" in ew) ? "displayedValue" : "value"));
160 },
161
162 _onKeyDown: function(e){
163 // summary:
164 // Handler for keydown in the edit box in autoSave mode.
165 // description:
166 // For autoSave widgets, if Esc/Enter, call cancel/save.
167 // tags:
168 // private
169
170 if(this.inlineEditBox.autoSave && this.inlineEditBox.editing){
171 if(e.altKey || e.ctrlKey){
172 return;
173 }
174 // If Enter/Esc pressed, treat as save/cancel.
175 if(e.keyCode == keys.ESCAPE){
176 e.stopPropagation();
177 e.preventDefault();
178 this.cancel(true); // sets editing=false which short-circuits _onBlur processing
179 }else if(e.keyCode == keys.ENTER && e.target.tagName == "INPUT"){
180 e.stopPropagation();
181 e.preventDefault();
182 this._onChange(); // fire _onBlur and then save
183 }
184
185 // _onBlur will handle TAB automatically by allowing
186 // the TAB to change focus before we mess with the DOM: #6227
187 // Expounding by request:
188 // The current focus is on the edit widget input field.
189 // save() will hide and destroy this widget.
190 // We want the focus to jump from the currently hidden
191 // displayNode, but since it's hidden, it's impossible to
192 // unhide it, focus it, and then have the browser focus
193 // away from it to the next focusable element since each
194 // of these events is asynchronous and the focus-to-next-element
195 // is already queued.
196 // So we allow the browser time to unqueue the move-focus event
197 // before we do all the hide/show stuff.
198 }
199 },
200
201 _onBlur: function(){
202 // summary:
203 // Called when focus moves outside the editor
204 // tags:
205 // private
206
207 this.inherited(arguments);
208 if(this.inlineEditBox.autoSave && this.inlineEditBox.editing){
209 if(this.getValue() == this._resetValue){
210 this.cancel(false);
211 }else if(this.enableSave()){
212 this.save(false);
213 }
214 }
215 },
216
217 _onChange: function(){
218 // summary:
219 // Called when the underlying widget fires an onChange event,
220 // such as when the user selects a value from the drop down list of a ComboBox,
221 // which means that the user has finished entering the value and we should save.
222 // tags:
223 // private
224
225 if(this.inlineEditBox.autoSave && this.inlineEditBox.editing && this.enableSave()){
226 fm.focus(this.inlineEditBox.displayNode); // fires _onBlur which will save the formatted value
227 }
228 },
229
230 enableSave: function(){
231 // summary:
232 // User overridable function returning a Boolean to indicate
233 // if the Save button should be enabled or not - usually due to invalid conditions
234 // tags:
235 // extension
236 return this.editWidget.isValid ? this.editWidget.isValid() : true;
237 },
238
239 focus: function(){
240 // summary:
241 // Focus the edit widget.
242 // tags:
243 // protected
244
245 this.editWidget.focus();
246
247 if(this.editWidget.focusNode){
248 // IE can take 30ms to report the focus event, but focus manager needs to know before a 0ms timeout.
249 fm._onFocusNode(this.editWidget.focusNode);
250
251 if(this.editWidget.focusNode.tagName == "INPUT"){
252 this.defer(function(){
253 _TextBoxMixin.selectInputText(this.editWidget.focusNode);
254 });
255 }
256 }
257 }
258 });
259
260
261 var InlineEditBox = declare("dijit.InlineEditBox" + (has("dojo-bidi") ? "_NoBidi" : ""), _Widget, {
262 // summary:
263 // An element with in-line edit capabilities
264 //
265 // description:
266 // Behavior for an existing node (`<p>`, `<div>`, `<span>`, etc.) so that
267 // when you click it, an editor shows up in place of the original
268 // text. Optionally, Save and Cancel button are displayed below the edit widget.
269 // When Save is clicked, the text is pulled from the edit
270 // widget and redisplayed and the edit widget is again hidden.
271 // By default a plain Textarea widget is used as the editor (or for
272 // inline values a TextBox), but you can specify an editor such as
273 // dijit.Editor (for editing HTML) or a Slider (for adjusting a number).
274 // An edit widget must support the following API to be used:
275 //
276 // - displayedValue or value as initialization parameter,
277 // and available through set('displayedValue') / set('value')
278 // - void focus()
279 // - DOM-node focusNode = node containing editable text
280
281 // editing: [readonly] Boolean
282 // Is the node currently in edit mode?
283 editing: false,
284
285 // autoSave: [const] Boolean
286 // Changing the value automatically saves it; don't have to push save button
287 // (and save button isn't even displayed)
288 autoSave: true,
289
290 // buttonSave: String
291 // Save button label
292 buttonSave: "",
293
294 // buttonCancel: String
295 // Cancel button label
296 buttonCancel: "",
297
298 // renderAsHtml: Boolean
299 // Set this to true if the specified Editor's value should be interpreted as HTML
300 // rather than plain text (ex: `dijit.Editor`)
301 renderAsHtml: false,
302
303 // editor: String|Function
304 // MID (ex: "dijit/form/TextBox") or constructor for editor widget
305 editor: TextBox,
306
307 // editorWrapper: String|Function
308 // Class name (or reference to the Class) for widget that wraps the editor widget, displaying save/cancel
309 // buttons.
310 editorWrapper: InlineEditor,
311
312 // editorParams: Object
313 // Set of parameters for editor, like {required: true}
314 editorParams: {},
315
316 // disabled: Boolean
317 // If true, clicking the InlineEditBox to edit it will have no effect.
318 disabled: false,
319
320 onChange: function(/*===== value =====*/){
321 // summary:
322 // Set this handler to be notified of changes to value.
323 // tags:
324 // callback
325 },
326
327 onCancel: function(){
328 // summary:
329 // Set this handler to be notified when editing is cancelled.
330 // tags:
331 // callback
332 },
333
334 // width: String
335 // Width of editor. By default it's width=100% (ie, block mode).
336 width: "100%",
337
338 // value: String
339 // The display value of the widget in read-only mode
340 value: "",
341
342 // noValueIndicator: [const] String
343 // The text that gets displayed when there is no value (so that the user has a place to click to edit)
344 noValueIndicator: has("ie") <= 6 ? // font-family needed on IE6 but it messes up IE8
345 "<span style='font-family: wingdings; text-decoration: underline;'>&#160;&#160;&#160;&#160;&#x270d;&#160;&#160;&#160;&#160;</span>" :
346 "<span style='text-decoration: underline;'>&#160;&#160;&#160;&#160;&#x270d;&#160;&#160;&#160;&#160;</span>", // &#160; == &nbsp;
347
348 constructor: function(/*===== params, srcNodeRef =====*/){
349 // summary:
350 // Create the widget.
351 // params: Object|null
352 // Hash of initialization parameters for widget, including scalar values (like title, duration etc.)
353 // and functions, typically callbacks like onClick.
354 // The hash can contain any of the widget's properties, excluding read-only properties.
355 // srcNodeRef: DOMNode|String?
356 // If a srcNodeRef (DOM node) is specified:
357 //
358 // - use srcNodeRef.innerHTML as my value
359 // - replace srcNodeRef with my generated DOM tree
360
361 this.editorParams = {};
362 },
363
364 postMixInProperties: function(){
365 this.inherited(arguments);
366
367 // save pointer to original source node, since Widget nulls-out srcNodeRef
368 this.displayNode = this.srcNodeRef;
369
370 // connect handlers to the display node
371 this.own(
372 on(this.displayNode, a11yclick, lang.hitch(this, "_onClick")),
373 on(this.displayNode, "mouseover, focus", lang.hitch(this, "_onMouseOver")),
374 on(this.displayNode, "mouseout, blur", lang.hitch(this, "_onMouseOut"))
375 );
376
377 this.displayNode.setAttribute("role", "button");
378 if(!this.displayNode.getAttribute("tabIndex")){
379 this.displayNode.setAttribute("tabIndex", 0);
380 }
381
382 if(!this.value && !("value" in this.params)){ // "" is a good value if specified directly so check params){
383 this.value = lang.trim(this.renderAsHtml ? this.displayNode.innerHTML :
384 (this.displayNode.innerText || this.displayNode.textContent || ""));
385 }
386 if(!this.value){
387 this.displayNode.innerHTML = this.noValueIndicator;
388 }
389
390 domClass.add(this.displayNode, 'dijitInlineEditBoxDisplayMode');
391 },
392
393 setDisabled: function(/*Boolean*/ disabled){
394 // summary:
395 // Deprecated. Use set('disabled', ...) instead.
396 // tags:
397 // deprecated
398 kernel.deprecated("dijit.InlineEditBox.setDisabled() is deprecated. Use set('disabled', bool) instead.", "", "2.0");
399 this.set('disabled', disabled);
400 },
401
402 _setDisabledAttr: function(/*Boolean*/ disabled){
403 // summary:
404 // Hook to make set("disabled", ...) work.
405 // Set disabled state of widget.
406 this.domNode.setAttribute("aria-disabled", disabled ? "true" : "false");
407 if(disabled){
408 this.displayNode.removeAttribute("tabIndex");
409 }else{
410 this.displayNode.setAttribute("tabIndex", 0);
411 }
412 domClass.toggle(this.displayNode, "dijitInlineEditBoxDisplayModeDisabled", disabled);
413 this._set("disabled", disabled);
414 },
415
416 _onMouseOver: function(){
417 // summary:
418 // Handler for onmouseover and onfocus event.
419 // tags:
420 // private
421 if(!this.disabled){
422 domClass.add(this.displayNode, "dijitInlineEditBoxDisplayModeHover");
423 }
424 },
425
426 _onMouseOut: function(){
427 // summary:
428 // Handler for onmouseout and onblur event.
429 // tags:
430 // private
431 domClass.remove(this.displayNode, "dijitInlineEditBoxDisplayModeHover");
432 },
433
434 _onClick: function(/*Event*/ e){
435 // summary:
436 // Handler for onclick event.
437 // tags:
438 // private
439 if(this.disabled){
440 return;
441 }
442 if(e){
443 e.stopPropagation();
444 e.preventDefault();
445 }
446 this._onMouseOut();
447
448 // Since FF gets upset if you move a node while in an event handler for that node...
449 this.defer("edit");
450 },
451
452 edit: function(){
453 // summary:
454 // Display the editor widget in place of the original (read only) markup.
455 // tags:
456 // private
457
458 if(this.disabled || this.editing){
459 return;
460 }
461 this._set('editing', true);
462
463 // save some display node values that can be restored later
464 this._savedTabIndex = domAttr.get(this.displayNode, "tabIndex") || "0";
465
466 if(!this.wrapperWidget){
467 // Placeholder for edit widget
468 // Put place holder (and eventually editWidget) before the display node so that it's positioned correctly
469 // when Calendar dropdown appears, which happens automatically on focus.
470 var placeholder = domConstruct.create("span", null, this.domNode, "before");
471
472 // Create the editor wrapper (the thing that holds the editor widget and the save/cancel buttons)
473 var Ewc = typeof this.editorWrapper == "string" ? lang.getObject(this.editorWrapper) : this.editorWrapper;
474 this.wrapperWidget = new Ewc({
475 value: this.value,
476 buttonSave: this.buttonSave,
477 buttonCancel: this.buttonCancel,
478 dir: this.dir,
479 lang: this.lang,
480 tabIndex: this._savedTabIndex,
481 editor: this.editor,
482 inlineEditBox: this,
483 sourceStyle: domStyle.getComputedStyle(this.displayNode),
484 save: lang.hitch(this, "save"),
485 cancel: lang.hitch(this, "cancel"),
486 textDir: this.textDir
487 }, placeholder);
488 if(!this.wrapperWidget._started){
489 this.wrapperWidget.startup();
490 }
491 if(!this._started){
492 this.startup();
493 }
494 }
495 var ww = this.wrapperWidget;
496
497 // to avoid screen jitter, we first create the editor with position: absolute, visibility: hidden,
498 // and then when it's finished rendering, we switch from display mode to editor
499 // position: absolute releases screen space allocated to the display node
500 // opacity:0 is the same as visibility: hidden but is still focusable
501 // visibility: hidden removes focus outline
502
503 domClass.add(this.displayNode, "dijitOffScreen");
504 domClass.remove(ww.domNode, "dijitOffScreen");
505 domStyle.set(ww.domNode, { visibility: "visible" });
506 domAttr.set(this.displayNode, "tabIndex", "-1"); // needed by WebKit for TAB from editor to skip displayNode
507
508 // After edit widget has finished initializing (in particular need to wait for dijit.Editor),
509 // or immediately if there is no onLoadDeferred Deferred,
510 // replace the display widget with edit widget, leaving them both displayed for a brief time so that
511 // focus can be shifted without incident.
512 var ew = ww.editWidget;
513 var self = this;
514 when(ew.onLoadDeferred, lang.hitch(ww, function(){
515 // set value again in case the edit widget's value is just now valid
516 ew.set(("displayedValue" in ew || "_setDisplayedValueAttr" in ew) ? "displayedValue" : "value", self.value);
517 this.defer(function(){ // defer needed so that the change of focus doesn't happen on mousedown which also sets focus
518 // the saveButton should start out disabled in most cases but the above set could have fired onChange
519 if(ww.saveButton){
520 ww.saveButton.set("disabled", "intermediateChanges" in ew);
521 }
522 this.focus(); // both nodes are showing, so we can switch focus safely
523 this._resetValue = this.getValue();
524 });
525 }));
526 },
527
528 _onBlur: function(){
529 // summary:
530 // Called when focus moves outside the InlineEditBox.
531 // Performs garbage collection.
532 // tags:
533 // private
534
535 this.inherited(arguments);
536 if(!this.editing){
537 /* causes IE focus problems, see TooltipDialog_a11y.html...
538 this.defer(function(){
539 if(this.wrapperWidget){
540 this.wrapperWidget.destroy();
541 delete this.wrapperWidget;
542 }
543 });
544 */
545 }
546 },
547
548 destroy: function(){
549 if(this.wrapperWidget && !this.wrapperWidget._destroyed){
550 this.wrapperWidget.destroy();
551 delete this.wrapperWidget;
552 }
553 this.inherited(arguments);
554 },
555
556 _showText: function(/*Boolean*/ focus){
557 // summary:
558 // Revert to display mode, and optionally focus on display node
559 // tags:
560 // private
561
562 var ww = this.wrapperWidget;
563 domStyle.set(ww.domNode, { visibility: "hidden" }); // hide the editor from mouse/keyboard events
564 domClass.add(ww.domNode, "dijitOffScreen");
565 domClass.remove(this.displayNode, "dijitOffScreen");
566 domAttr.set(this.displayNode, "tabIndex", this._savedTabIndex);
567 if(focus){
568 fm.focus(this.displayNode);
569 }
570 },
571
572 save: function(/*Boolean*/ focus){
573 // summary:
574 // Save the contents of the editor and revert to display mode.
575 // focus: Boolean
576 // Focus on the display mode text
577 // tags:
578 // private
579
580 if(this.disabled || !this.editing){
581 return;
582 }
583 this._set('editing', false);
584
585 var ww = this.wrapperWidget;
586 var value = ww.getValue();
587 this.set('value', value); // display changed, formatted value
588
589 this._showText(focus); // set focus as needed
590 },
591
592 setValue: function(/*String*/ val){
593 // summary:
594 // Deprecated. Use set('value', ...) instead.
595 // tags:
596 // deprecated
597 kernel.deprecated("dijit.InlineEditBox.setValue() is deprecated. Use set('value', ...) instead.", "", "2.0");
598 return this.set("value", val);
599 },
600
601 _setValueAttr: function(/*String*/ val){
602 // summary:
603 // Hook to make set("value", ...) work.
604 // Inserts specified HTML value into this node, or an "input needed" character if node is blank.
605
606 val = lang.trim(val);
607 var renderVal = this.renderAsHtml ? val : val.replace(/&/gm, "&amp;").replace(/</gm, "&lt;").replace(/>/gm, "&gt;").replace(/"/gm, "&quot;").replace(/\n/g, "<br>");
608 if(this.editorParams && this.editorParams.type === 'password'){
609 this.displayNode.innerHTML = "********";
610 }else{
611 this.displayNode.innerHTML = renderVal || this.noValueIndicator;
612 }
613 this._set("value", val);
614
615 if(this._started){
616 // tell the world that we have changed
617 this.defer(function(){
618 this.onChange(val);
619 }); // defer prevents browser freeze for long-running event handlers
620 }
621 },
622
623 getValue: function(){
624 // summary:
625 // Deprecated. Use get('value') instead.
626 // tags:
627 // deprecated
628 kernel.deprecated("dijit.InlineEditBox.getValue() is deprecated. Use get('value') instead.", "", "2.0");
629 return this.get("value");
630 },
631
632 cancel: function(/*Boolean*/ focus){
633 // summary:
634 // Revert to display mode, discarding any changes made in the editor
635 // tags:
636 // private
637
638 if(this.disabled || !this.editing){
639 return;
640 }
641 this._set('editing', false);
642
643 // tell the world that we have no changes
644 this.defer("onCancel"); // defer prevents browser freeze for long-running event handlers
645
646 this._showText(focus);
647 }
648 });
649
650 if(has("dojo-bidi")){
651 InlineEditBox = declare("dijit.InlineEditBox", InlineEditBox, {
652 _setValueAttr: function(){
653 this.inherited(arguments);
654 this.applyTextDir(this.displayNode);
655 }
656 });
657 }
658
659 InlineEditBox._InlineEditor = InlineEditor; // for monkey patching
660
661 return InlineEditBox;
662 });