| 
									
										
										
										
											2016-04-22 07:36:29 +00:00
										 |  |  | /*\ | 
					
						
							|  |  |  | title: $:/core/modules/widgets/edit-shortcut.js | 
					
						
							|  |  |  | type: application/javascript | 
					
						
							|  |  |  | module-type: widget | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Widget to display an editable keyboard shortcut | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \*/ | 
					
						
							|  |  |  | (function(){ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*jslint node: true, browser: true */ | 
					
						
							|  |  |  | /*global $tw: false */ | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var Widget = require("$:/core/modules/widgets/widget.js").widget; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var EditShortcutWidget = function(parseTreeNode,options) { | 
					
						
							|  |  |  | 	this.initialise(parseTreeNode,options); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | Inherit from the base widget class | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | EditShortcutWidget.prototype = new Widget(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | Render this widget into the DOM | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | EditShortcutWidget.prototype.render = function(parent,nextSibling) { | 
					
						
							|  |  |  | 	this.parentDomNode = parent; | 
					
						
							|  |  |  | 	this.computeAttributes(); | 
					
						
							|  |  |  | 	this.execute(); | 
					
						
							|  |  |  | 	this.inputNode = this.document.createElement("input"); | 
					
						
							|  |  |  | 	// Assign classes
 | 
					
						
							|  |  |  | 	if(this.shortcutClass) { | 
					
						
							| 
									
										
										
										
											2021-05-30 18:20:17 +00:00
										 |  |  | 		this.inputNode.className = this.shortcutClass; | 
					
						
							| 
									
										
										
										
											2016-04-22 07:36:29 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	// Assign other attributes
 | 
					
						
							|  |  |  | 	if(this.shortcutStyle) { | 
					
						
							|  |  |  | 		this.inputNode.setAttribute("style",this.shortcutStyle); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(this.shortcutTooltip) { | 
					
						
							|  |  |  | 		this.inputNode.setAttribute("title",this.shortcutTooltip); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(this.shortcutPlaceholder) { | 
					
						
							|  |  |  | 		this.inputNode.setAttribute("placeholder",this.shortcutPlaceholder); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(this.shortcutAriaLabel) { | 
					
						
							|  |  |  | 		this.inputNode.setAttribute("aria-label",this.shortcutAriaLabel); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Assign the current shortcut
 | 
					
						
							|  |  |  | 	this.updateInputNode(); | 
					
						
							|  |  |  | 	// Add event handlers
 | 
					
						
							|  |  |  | 	$tw.utils.addEventListeners(this.inputNode,[ | 
					
						
							|  |  |  | 		{name: "keydown", handlerObject: this, handlerMethod: "handleKeydownEvent"} | 
					
						
							|  |  |  | 	]); | 
					
						
							|  |  |  | 	// Link into the DOM
 | 
					
						
							|  |  |  | 	parent.insertBefore(this.inputNode,nextSibling); | 
					
						
							|  |  |  | 	this.domNodes.push(this.inputNode); | 
					
						
							| 
									
										
										
										
											2019-03-02 16:25:49 +00:00
										 |  |  | 	// Focus the input Node if focus === "yes" or focus === "true"
 | 
					
						
							|  |  |  | 	if(this.shortcutFocus === "yes" || this.shortcutFocus === "true") { | 
					
						
							|  |  |  | 		this.focus(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-22 07:36:29 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | Compute the internal state of the widget | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | EditShortcutWidget.prototype.execute = function() { | 
					
						
							|  |  |  | 	this.shortcutTiddler = this.getAttribute("tiddler"); | 
					
						
							|  |  |  | 	this.shortcutField = this.getAttribute("field"); | 
					
						
							|  |  |  | 	this.shortcutIndex = this.getAttribute("index"); | 
					
						
							|  |  |  | 	this.shortcutPlaceholder = this.getAttribute("placeholder"); | 
					
						
							|  |  |  | 	this.shortcutDefault = this.getAttribute("default",""); | 
					
						
							|  |  |  | 	this.shortcutClass = this.getAttribute("class"); | 
					
						
							|  |  |  | 	this.shortcutStyle = this.getAttribute("style"); | 
					
						
							|  |  |  | 	this.shortcutTooltip = this.getAttribute("tooltip"); | 
					
						
							|  |  |  | 	this.shortcutAriaLabel = this.getAttribute("aria-label"); | 
					
						
							| 
									
										
										
										
											2019-03-02 17:16:55 +00:00
										 |  |  | 	this.shortcutFocus = this.getAttribute("focus"); | 
					
						
							| 
									
										
										
										
											2016-04-22 07:36:29 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | Update the value of the input node | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | EditShortcutWidget.prototype.updateInputNode = function() { | 
					
						
							|  |  |  | 	if(this.shortcutField) { | 
					
						
							|  |  |  | 		var tiddler = this.wiki.getTiddler(this.shortcutTiddler); | 
					
						
							|  |  |  | 		if(tiddler && $tw.utils.hop(tiddler.fields,this.shortcutField)) { | 
					
						
							|  |  |  | 			this.inputNode.value = tiddler.getFieldString(this.shortcutField); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			this.inputNode.value = this.shortcutDefault; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else if(this.shortcutIndex) { | 
					
						
							|  |  |  | 		this.inputNode.value = this.wiki.extractTiddlerDataItem(this.shortcutTiddler,this.shortcutIndex,this.shortcutDefault); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		this.inputNode.value = this.wiki.getTiddlerText(this.shortcutTiddler,this.shortcutDefault); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | Handle a dom "keydown" event | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | EditShortcutWidget.prototype.handleKeydownEvent = function(event) { | 
					
						
							|  |  |  | 	// Ignore shift, ctrl, meta, alt
 | 
					
						
							|  |  |  | 	if(event.keyCode && $tw.keyboardManager.getModifierKeys().indexOf(event.keyCode) === -1) { | 
					
						
							|  |  |  | 		// Get the shortcut text representation
 | 
					
						
							|  |  |  | 		var value = $tw.keyboardManager.getPrintableShortcuts([{ | 
					
						
							|  |  |  | 			ctrlKey: event.ctrlKey, | 
					
						
							|  |  |  | 			shiftKey: event.shiftKey, | 
					
						
							|  |  |  | 			altKey: event.altKey, | 
					
						
							|  |  |  | 			metaKey: event.metaKey, | 
					
						
							|  |  |  | 			keyCode: event.keyCode | 
					
						
							|  |  |  | 		}]); | 
					
						
							|  |  |  | 		if(value.length > 0) { | 
					
						
							|  |  |  | 			this.wiki.setText(this.shortcutTiddler,this.shortcutField,this.shortcutIndex,value[0]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// Ignore the keydown if it was already handled
 | 
					
						
							|  |  |  | 		event.preventDefault(); | 
					
						
							|  |  |  | 		event.stopPropagation(); | 
					
						
							| 
									
										
										
										
											2021-05-30 18:20:17 +00:00
										 |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2016-04-22 07:36:29 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-02 16:25:49 +00:00
										 |  |  | /* | 
					
						
							|  |  |  | focus the input node | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | EditShortcutWidget.prototype.focus = function() { | 
					
						
							|  |  |  | 	if(this.inputNode.focus && this.inputNode.select) { | 
					
						
							|  |  |  | 		this.inputNode.focus(); | 
					
						
							|  |  |  | 		this.inputNode.select(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 07:36:29 +00:00
										 |  |  | /* | 
					
						
							|  |  |  | Selectively refreshes the widget if needed. Returns true if the widget needed re-rendering | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | EditShortcutWidget.prototype.refresh = function(changedTiddlers) { | 
					
						
							|  |  |  | 	var changedAttributes = this.computeAttributes(); | 
					
						
							| 
									
										
										
										
											2019-03-02 17:16:55 +00:00
										 |  |  | 	if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedAttributes.placeholder || changedAttributes["default"] || changedAttributes["class"] || changedAttributes.style || changedAttributes.tooltip || changedAttributes["aria-label"] || changedAttributes.focus) { | 
					
						
							| 
									
										
										
										
											2016-04-22 07:36:29 +00:00
										 |  |  | 		this.refreshSelf(); | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} else if(changedTiddlers[this.shortcutTiddler]) { | 
					
						
							|  |  |  | 		this.updateInputNode(); | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2021-05-30 18:20:17 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2016-04-22 07:36:29 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exports["edit-shortcut"] = EditShortcutWidget; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | })(); |