Skip to content

Commit

Permalink
Add data attribute support to button and other widgets (Jermolene#7769)
Browse files Browse the repository at this point in the history
* Add data attribute support to button widget

* Fix typo

* Refactor ready for making mechanism more generic

* Apply more generic implementation to multiplate widgets

* Refactor to use existing widget.assignAttributes() method

* Fix typo

* Clarify docs

* Update docs

* Update select widget to support style.* attributes

* Remove obsolete comment

* Fixes refresh issues for checkbox and links widgets for data attributes (Jermolene#7846)

* fix: refresh issues with checkbox and links widgets

* fix: indenting

* Feat: add support for data attributes to Draggable and Droppable widgets (Jermolene#7845)

* Docs clarification

* docs: add style and data attributes to Draggable and Droppable widget docs (Jermolene#7850)

* Refactors Select widget to directly create DOM node (Jermolene#7848)

* fix: refactored SelectWidget to directly create DOM nodes

* fix: refactored SelectWidget to directly create DOM nodes

* fix: improve refresh handling for select widget

* Fixes issues in the PR "Button widget data attributes" (Jermolene#7852)

* fix: fixed ordered attributes handling and improved tests to catch event attributes

* fix: clean up code from testing

* fix: more tests and refactoring

* fix: use lowercase when checking for event attribute prefix

* fix: use lowercase when checking for event attribute prefix

* fix: changed comment wording

* fix: minor refactoring

* refactor: for brevity

---------

Co-authored-by: Saq Imtiaz <saq.imtiaz@gmail.com>
  • Loading branch information
2 people authored and munnox committed Dec 24, 2023
1 parent 3b545ca commit d8ea293
Show file tree
Hide file tree
Showing 30 changed files with 399 additions and 69 deletions.
6 changes: 5 additions & 1 deletion core/modules/utils/fakedom.js
Expand Up @@ -104,7 +104,11 @@ TW_Element.prototype.setAttribute = function(name,value) {
if(this.isRaw) {
throw "Cannot setAttribute on a raw TW_Element";
}
this.attributes[name] = value + "";
if(name === "style") {
this.style = value;
} else {
this.attributes[name] = value + "";
}
};

TW_Element.prototype.setAttributeNS = function(namespace,name,value) {
Expand Down
10 changes: 10 additions & 0 deletions core/modules/widgets/browse.js
Expand Up @@ -70,6 +70,11 @@ BrowseWidget.prototype.render = function(parent,nextSibling) {
}
return false;
},false);
// Assign data- attributes
this.assignAttributes(domNode,{
sourcePrefix: "data-",
destPrefix: "data-"
});
// Insert element
parent.insertBefore(domNode,nextSibling);
this.renderChildren(domNode,null);
Expand All @@ -95,6 +100,11 @@ BrowseWidget.prototype.execute = function() {
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/
BrowseWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes();
if($tw.utils.count(changedAttributes) > 0) {
this.refreshSelf();
return true;
}
return false;
};

Expand Down
18 changes: 15 additions & 3 deletions core/modules/widgets/button.js
Expand Up @@ -59,6 +59,11 @@ ButtonWidget.prototype.render = function(parent,nextSibling) {
$tw.utils.pushTop(classes,"tc-popup-handle");
}
domNode.className = classes.join(" ");
// Assign data- attributes
this.assignAttributes(domNode,{
sourcePrefix: "data-",
destPrefix: "data-"
});
// Assign other attributes
if(this.style) {
domNode.setAttribute("style",this.style);
Expand Down Expand Up @@ -250,7 +255,7 @@ ButtonWidget.prototype.updateDomNodeClasses = function() {
//Add new classes from updated class attribute.
$tw.utils.pushTop(domNodeClasses,newClasses);
this.domNode.className = domNodeClasses.join(" ");
}
};

/*
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
Expand All @@ -260,8 +265,15 @@ ButtonWidget.prototype.refresh = function(changedTiddlers) {
if(changedAttributes.actions || changedAttributes.to || changedAttributes.message || changedAttributes.param || changedAttributes.set || changedAttributes.setTo || changedAttributes.popup || changedAttributes.hover || changedAttributes.selectedClass || changedAttributes.style || changedAttributes.dragFilter || changedAttributes.dragTiddler || (this.set && changedTiddlers[this.set]) || (this.popup && changedTiddlers[this.popup]) || (this.popupTitle && changedTiddlers[this.popupTitle]) || changedAttributes.popupAbsCoords || changedAttributes.setTitle || changedAttributes.setField || changedAttributes.setIndex || changedAttributes.popupTitle || changedAttributes.disabled || changedAttributes["default"]) {
this.refreshSelf();
return true;
} else if(changedAttributes["class"]) {
this.updateDomNodeClasses();
} else {
if(changedAttributes["class"]) {
this.updateDomNodeClasses();
}
this.assignAttributes(this.domNodes[0],{
changedAttributes: changedAttributes,
sourcePrefix: "data-",
destPrefix: "data-"
});
}
return this.refreshChildren(changedTiddlers);
};
Expand Down
11 changes: 11 additions & 0 deletions core/modules/widgets/checkbox.js
Expand Up @@ -53,6 +53,11 @@ CheckboxWidget.prototype.render = function(parent,nextSibling) {
this.labelDomNode.appendChild(this.inputDomNode);
this.spanDomNode = this.document.createElement("span");
this.labelDomNode.appendChild(this.spanDomNode);
// Assign data- attributes
this.assignAttributes(this.inputDomNode,{
sourcePrefix: "data-",
destPrefix: "data-"
});
// Add a click event handler
$tw.utils.addEventListeners(this.inputDomNode,[
{name: "change", handlerObject: this, handlerMethod: "handleChangeEvent"}
Expand Down Expand Up @@ -325,10 +330,16 @@ CheckboxWidget.prototype.refresh = function(changedTiddlers) {
$tw.utils.removeClass(this.labelDomNode,"tc-checkbox-checked");
}
}
this.assignAttributes(this.inputDomNode,{
changedAttributes: changedAttributes,
sourcePrefix: "data-",
destPrefix: "data-"
});
return this.refreshChildren(changedTiddlers) || refreshed;
}
};

exports.checkbox = CheckboxWidget;

})();

21 changes: 16 additions & 5 deletions core/modules/widgets/draggable.js
Expand Up @@ -52,6 +52,11 @@ DraggableWidget.prototype.render = function(parent,nextSibling) {
classes.push("tc-draggable");
}
domNode.setAttribute("class",classes.join(" "));
// Assign data- attributes and style. attributes
this.assignAttributes(domNode,{
sourcePrefix: "data-",
destPrefix: "data-"
});
// Insert the node into the DOM and render any children
parent.insertBefore(domNode,nextSibling);
this.renderChildren(domNode,null);
Expand Down Expand Up @@ -108,13 +113,19 @@ DraggableWidget.prototype.updateDomNodeClasses = function() {
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/
DraggableWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes(),
changedAttributesCount = $tw.utils.count(changedAttributes);
if(changedAttributesCount === 1 && changedAttributes["class"]) {
this.updateDomNodeClasses();
} else if(changedAttributesCount > 0) {
var changedAttributes = this.computeAttributes();
if(changedAttributes.tag || changedAttributes.selector || changedAttributes.dragimagetype || changedAttributes.enable || changedAttributes.startactions || changedAttributes.endactions) {
this.refreshSelf();
return true;
} else {
if(changedAttributes["class"]) {
this.assignDomNodeClasses();
}
this.assignAttributes(this.domNodes[0],{
changedAttributes: changedAttributes,
sourcePrefix: "data-",
destPrefix: "data-"
});
}
return this.refreshChildren(changedTiddlers);
};
Expand Down
16 changes: 14 additions & 2 deletions core/modules/widgets/droppable.js
Expand Up @@ -42,6 +42,11 @@ DroppableWidget.prototype.render = function(parent,nextSibling) {
domNode = this.document.createElement(tag);
this.domNode = domNode;
this.assignDomNodeClasses();
// Assign data- attributes and style. attributes
this.assignAttributes(domNode,{
sourcePrefix: "data-",
destPrefix: "data-"
});
// Add event handlers
if(this.droppableEnable) {
$tw.utils.addEventListeners(domNode,[
Expand Down Expand Up @@ -166,8 +171,15 @@ DroppableWidget.prototype.refresh = function(changedTiddlers) {
if(changedAttributes.tag || changedAttributes.enable || changedAttributes.disabledClass || changedAttributes.actions || changedAttributes.effect) {
this.refreshSelf();
return true;
} else if(changedAttributes["class"]) {
this.assignDomNodeClasses();
} else {
if(changedAttributes["class"]) {
this.assignDomNodeClasses();
}
this.assignAttributes(this.domNodes[0],{
changedAttributes: changedAttributes,
sourcePrefix: "data-",
destPrefix: "data-"
});
}
return this.refreshChildren(changedTiddlers);
};
Expand Down
14 changes: 12 additions & 2 deletions core/modules/widgets/link.js
Expand Up @@ -43,6 +43,11 @@ LinkWidget.prototype.render = function(parent,nextSibling) {
} else {
// Just insert the link text
var domNode = this.document.createElement("span");
// Assign data- attributes
this.assignAttributes(domNode,{
sourcePrefix: "data-",
destPrefix: "data-"
});
parent.insertBefore(domNode,nextSibling);
this.renderChildren(domNode,null);
this.domNodes.push(domNode);
Expand Down Expand Up @@ -138,6 +143,11 @@ LinkWidget.prototype.renderLink = function(parent,nextSibling) {
widget: this
});
}
// Assign data- attributes
this.assignAttributes(domNode,{
sourcePrefix: "data-",
destPrefix: "data-"
});
// Insert the link into the DOM and render any children
parent.insertBefore(domNode,nextSibling);
this.renderChildren(domNode,null);
Expand Down Expand Up @@ -207,8 +217,7 @@ Selectively refreshes the widget if needed. Returns true if the widget or any of
*/
LinkWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes();
if(changedAttributes.to || changedTiddlers[this.to] || changedAttributes["aria-label"] || changedAttributes.tooltip ||
changedAttributes["class"] || changedAttributes.tabindex || changedAttributes.draggable || changedAttributes.tag) {
if($tw.utils.count(changedAttributes) > 0) {
this.refreshSelf();
return true;
}
Expand All @@ -218,3 +227,4 @@ LinkWidget.prototype.refresh = function(changedTiddlers) {
exports.link = LinkWidget;

})();

4 changes: 4 additions & 0 deletions core/modules/widgets/radio.js
Expand Up @@ -40,6 +40,10 @@ RadioWidget.prototype.render = function(parent,nextSibling) {
);
this.inputDomNode = this.document.createElement("input");
this.inputDomNode.setAttribute("type","radio");
this.assignAttributes(this.inputDomNode,{
sourcePrefix: "data-",
destPrefix: "data-"
});
if(isChecked) {
this.inputDomNode.checked = true;
}
Expand Down
4 changes: 4 additions & 0 deletions core/modules/widgets/range.js
Expand Up @@ -50,6 +50,10 @@ RangeWidget.prototype.render = function(parent,nextSibling) {
this.inputDomNode.setAttribute("disabled",true);
}
this.inputDomNode.value = this.getValue();
this.assignAttributes(this.inputDomNode,{
sourcePrefix: "data-",
destPrefix: "data-"
});
// Add a click event handler
$tw.utils.addEventListeners(this.inputDomNode,[
{name:"mousedown", handlerObject:this, handlerMethod:"handleMouseDownEvent"},
Expand Down
60 changes: 34 additions & 26 deletions core/modules/widgets/select.js
Expand Up @@ -40,7 +40,31 @@ SelectWidget.prototype.render = function(parent,nextSibling) {
this.parentDomNode = parent;
this.computeAttributes();
this.execute();
this.renderChildren(parent,nextSibling);
//Create element
var domNode = this.document.createElement("select");
if(this.selectClass) {
domNode.classname = this.selectClass;
}
// Assign data- attributes
this.assignAttributes(domNode,{
sourcePrefix: "data-",
destPrefix: "data-"
});
if(this.selectMultiple) {
domNode.setAttribute("multiple","multiple");
}
if(this.selectSize) {
domNode.setAttribute("size",this.selectSize);
}
if(this.selectTabindex) {
domNode.setAttribute("tabindex",this.selectTabindex);
}
if(this.selectTooltip) {
domNode.setAttribute("title",this.selectTooltip);
}
this.renderChildren(domNode,nextSibling);
this.parentDomNode.insertBefore(domNode,nextSibling);
this.domNodes.push(domNode);
this.setSelectValue();
if(this.selectFocus == "yes") {
this.getSelectDomNode().focus();
Expand Down Expand Up @@ -113,7 +137,7 @@ SelectWidget.prototype.setSelectValue = function() {
Get the DOM node of the select element
*/
SelectWidget.prototype.getSelectDomNode = function() {
return this.children[0].domNodes[0];
return this.domNodes[0];
};

// Return an array of the selected opion values
Expand Down Expand Up @@ -149,27 +173,7 @@ SelectWidget.prototype.execute = function() {
this.selectTooltip = this.getAttribute("tooltip");
this.selectFocus = this.getAttribute("focus");
// Make the child widgets
var selectNode = {
type: "element",
tag: "select",
children: this.parseTreeNode.children
};
if(this.selectClass) {
$tw.utils.addAttributeToParseTreeNode(selectNode,"class",this.selectClass);
}
if(this.selectMultiple) {
$tw.utils.addAttributeToParseTreeNode(selectNode,"multiple","multiple");
}
if(this.selectSize) {
$tw.utils.addAttributeToParseTreeNode(selectNode,"size",this.selectSize);
}
if(this.selectTabindex) {
$tw.utils.addAttributeToParseTreeNode(selectNode,"tabindex",this.selectTabindex);
}
if(this.selectTooltip) {
$tw.utils.addAttributeToParseTreeNode(selectNode,"title",this.selectTooltip);
}
this.makeChildWidgets([selectNode]);
this.makeChildWidgets();
};

/*
Expand All @@ -178,17 +182,21 @@ Selectively refreshes the widget if needed. Returns true if the widget or any of
SelectWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes();
// If we're using a different tiddler/field/index then completely refresh ourselves
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedAttributes.tooltip) {
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedAttributes.tooltip || changedAttributes.tabindex) {
this.refreshSelf();
return true;
// If the target tiddler value has changed, just update setting and refresh the children
} else {
if(changedAttributes.class) {
this.selectClass = this.getAttribute("class");
this.getSelectDomNode().setAttribute("class",this.selectClass);
}

this.assignAttributes(this.getSelectDomNode(),{
changedAttributes: changedAttributes,
sourcePrefix: "data-",
destPrefix: "data-"
});
var childrenRefreshed = this.refreshChildren(changedTiddlers);
// If the target tiddler value has changed, just update setting and refresh the children
if(changedTiddlers[this.selectTitle] || childrenRefreshed) {
this.setSelectValue();
}
Expand Down

0 comments on commit d8ea293

Please sign in to comment.