| 1 |
define([ |
| 2 |
"require", |
| 3 |
"dojo/_base/array", |
| 4 |
"dojo/aspect", |
| 5 |
"dojo/_base/declare", |
| 6 |
"dojo/dom-attr", |
| 7 |
"dojo/dom-class", |
| 8 |
"dojo/dom-construct", |
| 9 |
"dojo/dom-style", |
| 10 |
"dojo/i18n", |
| 11 |
"dojo/_base/kernel", |
| 12 |
"dojo/keys", |
| 13 |
"dojo/_base/lang", |
| 14 |
"dojo/on", |
| 15 |
"dojo/sniff", |
| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
var InlineEditor = declare("dijit._InlineEditor", [_Widget, _TemplatedMixin, _WidgetsInTemplateMixin], { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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 |
|
| 65 |
|
| 66 |
var Cls = typeof this.editor == "string" ? (lang.getObject(this.editor) || require(this.editor)) : this.editor; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 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 |
|
| 89 |
editStyle += "width:100%;"; |
| 90 |
this.domNode.style.display = "block"; |
| 91 |
}else{ |
| 92 |
|
| 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 |
|
| 102 |
|
| 103 |
this.editWidget = new Cls(editorParams, this.editorPlaceholder); |
| 104 |
|
| 105 |
if(this.inlineEditBox.autoSave){ |
| 106 |
|
| 107 |
|
| 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 |
|
| 123 |
aspect.after(ew, "onChange", lang.hitch(this, "_onChange"), true), |
| 124 |
|
| 125 |
|
| 126 |
on(ew, "keydown", lang.hitch(this, "_onKeyDown")) |
| 127 |
); |
| 128 |
}else{ |
| 129 |
|
| 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(){ |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
this.saveButton.set("disabled", (this.getValue() == this._resetValue) || !this.enableSave()); |
| 148 |
}, |
| 149 |
|
| 150 |
destroy: function(){ |
| 151 |
this.editWidget.destroy(true); |
| 152 |
this.inherited(arguments); |
| 153 |
}, |
| 154 |
|
| 155 |
getValue: function(){ |
| 156 |
|
| 157 |
|
| 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 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
if(this.inlineEditBox.autoSave && this.inlineEditBox.editing){ |
| 171 |
if(e.altKey || e.ctrlKey){ |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
if(e.keyCode == keys.ESCAPE){ |
| 176 |
e.stopPropagation(); |
| 177 |
e.preventDefault(); |
| 178 |
this.cancel(true); |
| 179 |
}else if(e.keyCode == keys.ENTER && e.target.tagName == "INPUT"){ |
| 180 |
e.stopPropagation(); |
| 181 |
e.preventDefault(); |
| 182 |
this._onChange(); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
} |
| 199 |
}, |
| 200 |
|
| 201 |
_onBlur: function(){ |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 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 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if(this.inlineEditBox.autoSave && this.inlineEditBox.editing && this.enableSave()){ |
| 226 |
fm.focus(this.inlineEditBox.displayNode); |
| 227 |
} |
| 228 |
}, |
| 229 |
|
| 230 |
enableSave: function(){ |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
return this.editWidget.isValid ? this.editWidget.isValid() : true; |
| 237 |
}, |
| 238 |
|
| 239 |
focus: function(){ |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
this.editWidget.focus(); |
| 246 |
|
| 247 |
if(this.editWidget.focusNode){ |
| 248 |
|
| 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 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
editing: false, |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
autoSave: true, |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
buttonSave: "", |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
buttonCancel: "", |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
renderAsHtml: false, |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
editor: TextBox, |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
editorWrapper: InlineEditor, |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
editorParams: {}, |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
disabled: false, |
| 319 |
|
| 320 |
onChange: function(){ |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
}, |
| 326 |
|
| 327 |
onCancel: function(){ |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
}, |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
width: "100%", |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
value: "", |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
noValueIndicator: has("ie") <= 6 ? |
| 345 |
"<span style='font-family: wingdings; text-decoration: underline;'>    ✍    </span>" : |
| 346 |
"<span style='text-decoration: underline;'>    ✍    </span>", |
| 347 |
|
| 348 |
constructor: function(){ |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
this.editorParams = {}; |
| 362 |
}, |
| 363 |
|
| 364 |
postMixInProperties: function(){ |
| 365 |
this.inherited(arguments); |
| 366 |
|
| 367 |
|
| 368 |
this.displayNode = this.srcNodeRef; |
| 369 |
|
| 370 |
|
| 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)){ |
| 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( disabled){ |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 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( disabled){ |
| 403 |
|
| 404 |
|
| 405 |
|
| 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 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
if(!this.disabled){ |
| 422 |
domClass.add(this.displayNode, "dijitInlineEditBoxDisplayModeHover"); |
| 423 |
} |
| 424 |
}, |
| 425 |
|
| 426 |
_onMouseOut: function(){ |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
domClass.remove(this.displayNode, "dijitInlineEditBoxDisplayModeHover"); |
| 432 |
}, |
| 433 |
|
| 434 |
_onClick: function( e){ |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
if(this.disabled){ |
| 440 |
return; |
| 441 |
} |
| 442 |
if(e){ |
| 443 |
e.stopPropagation(); |
| 444 |
e.preventDefault(); |
| 445 |
} |
| 446 |
this._onMouseOut(); |
| 447 |
|
| 448 |
|
| 449 |
this.defer("edit"); |
| 450 |
}, |
| 451 |
|
| 452 |
edit: function(){ |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
if(this.disabled || this.editing){ |
| 459 |
return; |
| 460 |
} |
| 461 |
this._set('editing', true); |
| 462 |
|
| 463 |
|
| 464 |
this._savedTabIndex = domAttr.get(this.displayNode, "tabIndex") || "0"; |
| 465 |
|
| 466 |
if(!this.wrapperWidget){ |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
var placeholder = domConstruct.create("span", null, this.domNode, "before"); |
| 471 |
|
| 472 |
|
| 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 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 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"); |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
var ew = ww.editWidget; |
| 513 |
var self = this; |
| 514 |
when(ew.onLoadDeferred, lang.hitch(ww, function(){ |
| 515 |
|
| 516 |
ew.set(("displayedValue" in ew || "_setDisplayedValueAttr" in ew) ? "displayedValue" : "value", self.value); |
| 517 |
this.defer(function(){ |
| 518 |
|
| 519 |
if(ww.saveButton){ |
| 520 |
ww.saveButton.set("disabled", "intermediateChanges" in ew); |
| 521 |
} |
| 522 |
this.focus(); |
| 523 |
this._resetValue = this.getValue(); |
| 524 |
}); |
| 525 |
})); |
| 526 |
}, |
| 527 |
|
| 528 |
_onBlur: function(){ |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
this.inherited(arguments); |
| 536 |
if(!this.editing){ |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 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( focus){ |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
var ww = this.wrapperWidget; |
| 563 |
domStyle.set(ww.domNode, { visibility: "hidden" }); |
| 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( focus){ |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 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); |
| 588 |
|
| 589 |
this._showText(focus); |
| 590 |
}, |
| 591 |
|
| 592 |
setValue: function( val){ |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 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( val){ |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
val = lang.trim(val); |
| 607 |
var renderVal = this.renderAsHtml ? val : val.replace(/&/gm, "&").replace(/</gm, "<").replace(/>/gm, ">").replace(/"/gm, """).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 |
|
| 617 |
this.defer(function(){ |
| 618 |
this.onChange(val); |
| 619 |
}); |
| 620 |
} |
| 621 |
}, |
| 622 |
|
| 623 |
getValue: function(){ |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
kernel.deprecated("dijit.InlineEditBox.getValue() is deprecated. Use get('value') instead.", "", "2.0"); |
| 629 |
return this.get("value"); |
| 630 |
}, |
| 631 |
|
| 632 |
cancel: function( focus){ |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
if(this.disabled || !this.editing){ |
| 639 |
return; |
| 640 |
} |
| 641 |
this._set('editing', false); |
| 642 |
|
| 643 |
|
| 644 |
this.defer("onCancel"); |
| 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; |
| 660 |
|
| 661 |
return InlineEditBox; |
| 662 |
}); |