2013-10-15 13:32:27 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/edit-text.js
|
2013-10-15 13:32:27 +00:00
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-10-15 13:32:27 +00:00
|
|
|
|
|
|
|
Edit-text widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2014-10-25 09:09:17 +00:00
|
|
|
var DEFAULT_MIN_TEXT_AREA_HEIGHT = "100px"; // Minimum height of textareas in pixels
|
2013-10-15 13:32:27 +00:00
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
2013-10-15 13:32:27 +00:00
|
|
|
|
|
|
|
var EditTextWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
EditTextWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
EditTextWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
var self = this;
|
|
|
|
// Save the parent dom node
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
// Compute our attributes
|
|
|
|
this.computeAttributes();
|
|
|
|
// Execute our logic
|
|
|
|
this.execute();
|
|
|
|
// Create our element
|
2015-03-10 12:34:50 +00:00
|
|
|
var editInfo = this.getEditInfo(),
|
|
|
|
tag = this.editTag;
|
|
|
|
if($tw.config.htmlUnsafeElements.indexOf(tag) !== -1) {
|
|
|
|
tag = "input";
|
|
|
|
}
|
|
|
|
var domNode = this.document.createElement(tag);
|
2013-10-15 13:32:27 +00:00
|
|
|
if(this.editType) {
|
|
|
|
domNode.setAttribute("type",this.editType);
|
|
|
|
}
|
2013-12-30 01:18:00 +00:00
|
|
|
if(editInfo.value === "" && this.editPlaceholder) {
|
2013-10-21 21:54:48 +00:00
|
|
|
domNode.setAttribute("placeholder",this.editPlaceholder);
|
|
|
|
}
|
2014-05-05 17:42:25 +00:00
|
|
|
if(this.editSize) {
|
|
|
|
domNode.setAttribute("size",this.editSize);
|
|
|
|
}
|
2013-10-15 13:32:27 +00:00
|
|
|
// Assign classes
|
2013-12-12 18:52:51 +00:00
|
|
|
if(this.editClass) {
|
|
|
|
domNode.className = this.editClass;
|
|
|
|
}
|
2013-10-15 13:32:27 +00:00
|
|
|
// Set the text
|
|
|
|
if(this.editTag === "textarea") {
|
|
|
|
domNode.appendChild(this.document.createTextNode(editInfo.value));
|
|
|
|
} else {
|
2014-02-20 09:57:44 +00:00
|
|
|
domNode.value = editInfo.value;
|
2013-10-15 13:32:27 +00:00
|
|
|
}
|
|
|
|
// Add an input event handler
|
2013-10-23 16:41:12 +00:00
|
|
|
$tw.utils.addEventListeners(domNode,[
|
|
|
|
{name: "focus", handlerObject: this, handlerMethod: "handleFocusEvent"},
|
|
|
|
{name: "input", handlerObject: this, handlerMethod: "handleInputEvent"}
|
|
|
|
]);
|
2013-10-15 13:32:27 +00:00
|
|
|
// Insert the element into the DOM
|
|
|
|
parent.insertBefore(domNode,nextSibling);
|
|
|
|
this.domNodes.push(domNode);
|
2013-10-25 08:15:56 +00:00
|
|
|
if(this.postRender) {
|
|
|
|
this.postRender();
|
|
|
|
}
|
2013-10-15 13:32:27 +00:00
|
|
|
// Fix height
|
|
|
|
this.fixHeight();
|
2014-11-25 11:39:08 +00:00
|
|
|
// Focus field
|
|
|
|
if(this.editFocus === "true") {
|
2015-01-06 18:19:55 +00:00
|
|
|
if(domNode.focus && domNode.select) {
|
|
|
|
domNode.focus();
|
|
|
|
domNode.select();
|
|
|
|
}
|
2014-11-25 11:39:08 +00:00
|
|
|
}
|
2013-10-15 13:32:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get the tiddler being edited and current value
|
|
|
|
*/
|
|
|
|
EditTextWidget.prototype.getEditInfo = function() {
|
|
|
|
// Get the edit value
|
2013-11-09 10:55:18 +00:00
|
|
|
var self = this,
|
|
|
|
value,
|
|
|
|
update;
|
2013-10-16 15:29:51 +00:00
|
|
|
if(this.editIndex) {
|
|
|
|
value = this.wiki.extractTiddlerDataItem(this.editTitle,this.editIndex,this.editDefault);
|
2013-11-09 10:55:18 +00:00
|
|
|
update = function(value) {
|
2013-11-09 13:15:54 +00:00
|
|
|
var data = self.wiki.getTiddlerData(self.editTitle,{});
|
2013-11-09 14:17:08 +00:00
|
|
|
if(data[self.editIndex] !== value) {
|
|
|
|
data[self.editIndex] = value;
|
2013-11-09 13:15:54 +00:00
|
|
|
self.wiki.setTiddlerData(self.editTitle,data);
|
2013-11-09 10:55:18 +00:00
|
|
|
}
|
|
|
|
};
|
2013-10-16 15:29:51 +00:00
|
|
|
} else {
|
2013-10-15 13:32:27 +00:00
|
|
|
// Get the current tiddler and the field name
|
2013-11-09 10:55:18 +00:00
|
|
|
var tiddler = this.wiki.getTiddler(this.editTitle);
|
2013-10-15 13:32:27 +00:00
|
|
|
if(tiddler) {
|
|
|
|
// If we've got a tiddler, the value to display is the field string value
|
|
|
|
value = tiddler.getFieldString(this.editField);
|
|
|
|
} else {
|
|
|
|
// Otherwise, we need to construct a default value for the editor
|
|
|
|
switch(this.editField) {
|
|
|
|
case "text":
|
|
|
|
value = "Type the text for the tiddler '" + this.editTitle + "'";
|
|
|
|
break;
|
|
|
|
case "title":
|
|
|
|
value = this.editTitle;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = "";
|
|
|
|
break;
|
|
|
|
}
|
2013-11-01 12:43:23 +00:00
|
|
|
if(this.editDefault !== undefined) {
|
2013-10-30 14:27:55 +00:00
|
|
|
value = this.editDefault;
|
|
|
|
}
|
2013-10-15 13:32:27 +00:00
|
|
|
}
|
2013-11-09 10:55:18 +00:00
|
|
|
update = function(value) {
|
|
|
|
var tiddler = self.wiki.getTiddler(self.editTitle),
|
|
|
|
updateFields = {
|
|
|
|
title: self.editTitle
|
|
|
|
};
|
|
|
|
updateFields[self.editField] = value;
|
|
|
|
self.wiki.addTiddler(new $tw.Tiddler(self.wiki.getCreationFields(),tiddler,updateFields,self.wiki.getModificationFields()));
|
|
|
|
};
|
2013-10-15 13:32:27 +00:00
|
|
|
}
|
2013-11-09 10:55:18 +00:00
|
|
|
return {value: value, update: update};
|
2013-10-15 13:32:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
EditTextWidget.prototype.execute = function() {
|
|
|
|
// Get our parameters
|
2013-10-30 13:36:44 +00:00
|
|
|
this.editTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
|
2013-10-15 13:32:27 +00:00
|
|
|
this.editField = this.getAttribute("field","text");
|
|
|
|
this.editIndex = this.getAttribute("index");
|
2013-11-01 12:43:23 +00:00
|
|
|
this.editDefault = this.getAttribute("default");
|
2013-10-15 13:32:27 +00:00
|
|
|
this.editClass = this.getAttribute("class");
|
2013-10-21 21:54:48 +00:00
|
|
|
this.editPlaceholder = this.getAttribute("placeholder");
|
2014-05-05 17:42:25 +00:00
|
|
|
this.editSize = this.getAttribute("size");
|
2014-10-25 09:09:17 +00:00
|
|
|
this.editAutoHeight = this.getAttribute("autoHeight","yes") === "yes";
|
2014-10-25 09:25:55 +00:00
|
|
|
this.editMinHeight = this.getAttribute("minHeight",DEFAULT_MIN_TEXT_AREA_HEIGHT);
|
2013-10-31 12:37:22 +00:00
|
|
|
this.editFocusPopup = this.getAttribute("focusPopup");
|
2014-11-25 11:39:08 +00:00
|
|
|
this.editFocus = this.getAttribute("focus");
|
2013-10-15 13:32:27 +00:00
|
|
|
// Get the editor element tag and type
|
|
|
|
var tag,type;
|
|
|
|
if(this.editField === "text") {
|
|
|
|
tag = "textarea";
|
|
|
|
} else {
|
|
|
|
tag = "input";
|
|
|
|
var fieldModule = $tw.Tiddler.fieldModules[this.editField];
|
2013-11-07 19:39:50 +00:00
|
|
|
if(fieldModule && fieldModule.editTag) {
|
|
|
|
tag = fieldModule.editTag;
|
|
|
|
}
|
2013-10-15 13:32:27 +00:00
|
|
|
if(fieldModule && fieldModule.editType) {
|
|
|
|
type = fieldModule.editType;
|
|
|
|
}
|
|
|
|
type = type || "text";
|
|
|
|
}
|
|
|
|
// Get the rest of our parameters
|
|
|
|
this.editTag = this.getAttribute("tag",tag);
|
|
|
|
this.editType = this.getAttribute("type",type);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
EditTextWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
|
|
|
// Completely rerender if any of our attributes have changed
|
2014-10-25 09:09:17 +00:00
|
|
|
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedAttributes["default"] || changedAttributes["class"] || changedAttributes.placeholder || changedAttributes.size || changedAttributes.autoHeight || changedAttributes.minHeight || changedAttributes.focusPopup) {
|
2013-10-15 13:32:27 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
2013-10-25 08:15:56 +00:00
|
|
|
} else if(changedTiddlers[this.editTitle]) {
|
|
|
|
this.updateEditor(this.getEditInfo().value);
|
|
|
|
return true;
|
2013-10-15 13:32:27 +00:00
|
|
|
}
|
2015-10-12 16:54:51 +00:00
|
|
|
// Fix the height anyway in case there has been a reflow
|
|
|
|
this.fixHeight();
|
2013-10-15 13:32:27 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-10-25 08:15:56 +00:00
|
|
|
/*
|
|
|
|
Update the editor with new text. This method is separate from updateEditorDomNode()
|
|
|
|
so that subclasses can override updateEditor() and still use updateEditorDomNode()
|
|
|
|
*/
|
|
|
|
EditTextWidget.prototype.updateEditor = function(text) {
|
|
|
|
this.updateEditorDomNode(text);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Update the editor dom node with new text
|
|
|
|
*/
|
|
|
|
EditTextWidget.prototype.updateEditorDomNode = function(text) {
|
|
|
|
// Replace the edit value if the tiddler we're editing has changed
|
|
|
|
var domNode = this.domNodes[0];
|
|
|
|
if(!domNode.isTiddlyWikiFakeDom) {
|
|
|
|
if(this.document.activeElement !== domNode) {
|
|
|
|
domNode.value = text;
|
|
|
|
}
|
|
|
|
// Fix the height if needed
|
|
|
|
this.fixHeight();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-26 20:22:10 +00:00
|
|
|
/*
|
|
|
|
Get the first parent element that has scrollbars or use the body as fallback.
|
|
|
|
*/
|
|
|
|
EditTextWidget.prototype.getScrollContainer = function(el) {
|
|
|
|
while(el.parentNode) {
|
|
|
|
el = el.parentNode;
|
|
|
|
if(el.scrollTop) {
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.document.body;
|
|
|
|
};
|
|
|
|
|
2013-10-15 13:32:27 +00:00
|
|
|
/*
|
|
|
|
Fix the height of textareas to fit their content
|
|
|
|
*/
|
|
|
|
EditTextWidget.prototype.fixHeight = function() {
|
2015-10-26 20:22:10 +00:00
|
|
|
var domNode = this.domNodes[0];
|
2014-10-25 09:09:17 +00:00
|
|
|
if(this.editAutoHeight && domNode && !domNode.isTiddlyWikiFakeDom && this.editTag === "textarea") {
|
2014-11-13 11:19:21 +00:00
|
|
|
// Resize the textarea to fit its content, preserving scroll position
|
2015-10-26 20:22:10 +00:00
|
|
|
// Get the scroll container and register the current scroll position
|
|
|
|
var container = this.getScrollContainer(domNode),
|
|
|
|
scrollTop = container.scrollTop;
|
|
|
|
// Measure the specified minimum height
|
|
|
|
domNode.style.height = this.editMinHeight;
|
2014-11-13 11:19:21 +00:00
|
|
|
var minHeight = domNode.offsetHeight;
|
|
|
|
// Set its height to auto so that it snaps to the correct height
|
|
|
|
domNode.style.height = "auto";
|
|
|
|
// Calculate the revised height
|
|
|
|
var newHeight = Math.max(domNode.scrollHeight + domNode.offsetHeight - domNode.clientHeight,minHeight);
|
|
|
|
// Only try to change the height if it has changed
|
|
|
|
if(newHeight !== domNode.offsetHeight) {
|
2015-10-26 20:22:10 +00:00
|
|
|
domNode.style.height = newHeight + "px";
|
2014-11-13 11:19:21 +00:00
|
|
|
// Make sure that the dimensions of the textarea are recalculated
|
|
|
|
$tw.utils.forceLayout(domNode);
|
2015-10-26 20:22:10 +00:00
|
|
|
// Set the container to the position we registered at the beginning
|
|
|
|
container.scrollTop = scrollTop;
|
2014-11-13 11:19:21 +00:00
|
|
|
}
|
2013-10-15 13:32:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Handle a dom "input" event
|
|
|
|
*/
|
|
|
|
EditTextWidget.prototype.handleInputEvent = function(event) {
|
2013-10-25 08:15:56 +00:00
|
|
|
this.saveChanges(this.domNodes[0].value);
|
2013-10-15 13:32:27 +00:00
|
|
|
this.fixHeight();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2013-10-23 16:41:12 +00:00
|
|
|
EditTextWidget.prototype.handleFocusEvent = function(event) {
|
2013-10-31 12:37:22 +00:00
|
|
|
if(this.editFocusPopup) {
|
2013-10-23 16:41:12 +00:00
|
|
|
$tw.popup.triggerPopup({
|
|
|
|
domNode: this.domNodes[0],
|
2013-10-31 12:37:22 +00:00
|
|
|
title: this.editFocusPopup,
|
2013-10-23 16:41:12 +00:00
|
|
|
wiki: this.wiki,
|
|
|
|
force: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2013-10-25 08:15:56 +00:00
|
|
|
EditTextWidget.prototype.saveChanges = function(text) {
|
2013-11-09 10:55:18 +00:00
|
|
|
var editInfo = this.getEditInfo();
|
|
|
|
if(text !== editInfo.value) {
|
|
|
|
editInfo.update(text);
|
2013-10-15 13:32:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports["edit-text"] = EditTextWidget;
|
|
|
|
|
|
|
|
})();
|