Skip to content

Commit

Permalink
Leverage native (or polyfilled) Promise, Element#classList, and Eleme…
Browse files Browse the repository at this point in the history
…nt#matches.

Refs #520.
  • Loading branch information
wkeese committed Mar 11, 2020
1 parent 586ae4d commit 220f85f
Show file tree
Hide file tree
Showing 21 changed files with 2,674 additions and 397 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"maxlen": 120,
"indent": 4,
"maxerr": 250,
"predef": [ "require", "requirejs", "define", "intern" ],
"predef": [ "require", "requirejs", "define", "intern", "Promise" ],
"quotmark": "double",
"maxcomplexity": 10
}
3 changes: 1 addition & 2 deletions ArrayQueryAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
define([
"dcl/dcl",
"ibm-decor/Evented",
"requirejs-dplugins/Promise!"
], function (dcl, Evented, Promise) {
], function (dcl, Evented) {

/**
* Returns a thenable on some static data, but unlike Promise, it executes synchronously.
Expand Down
13 changes: 6 additions & 7 deletions DisplayContainer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/** @module delite/DisplayContainer */
define(["dcl/dcl", "requirejs-dplugins/Promise!", "./Container"],
function (dcl, Promise, Container) {

define(["dcl/dcl", "./Container"], function (dcl, Container) {

/**
* Dispatched before child is shown.
* @example
Expand All @@ -11,7 +10,7 @@ define(["dcl/dcl", "requirejs-dplugins/Promise!", "./Container"],
* @event module:delite/DisplayContainer#delite-before-show
* @property {Element} child - reference to child element
*/

/**
* Dispatched after child is shown.
* @example
Expand All @@ -21,7 +20,7 @@ define(["dcl/dcl", "requirejs-dplugins/Promise!", "./Container"],
* @event module:delite/DisplayContainer#delite-after-show
* @property {Element} child - reference to child element
*/

/**
* Dispatched to let an application level listener create/load the child node.
* @example
Expand All @@ -38,7 +37,7 @@ define(["dcl/dcl", "requirejs-dplugins/Promise!", "./Container"],
* @event module:delite/DisplayContainer#delite-display-load
* @property {Function} setChild - method to set child element, or Promise for child element
*/

/**
* Dispatched before child is hidden.
* @example
Expand All @@ -48,7 +47,7 @@ define(["dcl/dcl", "requirejs-dplugins/Promise!", "./Container"],
* @event module:delite/DisplayContainer#delite-before-hide
* @property {Element} child - reference to child element
*/

/**
* Dispatched after child is hidden.
* @example
Expand Down
2 changes: 0 additions & 2 deletions HasDropDown.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/** @module delite/HasDropDown */
define([
"dcl/dcl",
"requirejs-dplugins/Promise!",
"./on",
"./place",
"./popup",
Expand All @@ -10,7 +9,6 @@ define([
"./activationTracker" // for delite-deactivated event
], function (
dcl,
Promise,
on,
place,
popup,
Expand Down
3 changes: 1 addition & 2 deletions KeyNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ define([
isNavigable: function (elem) {
// Setup function to check which child nodes are navigable.
if (typeof this.descendantSelector === "string") {
var matchesFuncName = has("dom-matches");
return elem[matchesFuncName](this.descendantSelector);
return elem.matches(this.descendantSelector);
} else if (this.descendantSelector) {
return this.descendantSelector(elem);
} else {
Expand Down
11 changes: 4 additions & 7 deletions Scrollable.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/** @module delite/Scrollable */
define([
"dcl/dcl",
"./classList",
"./ScrollAnimation",
"./Widget",
"requirejs-dplugins/css!./Scrollable/Scrollable.css"
], function (dcl, classList, ScrollAnimation, Widget) {
], function (dcl, ScrollAnimation, Widget) {

/**
* A mixin which adds scrolling capabilities to a widget.
Expand Down Expand Up @@ -83,12 +82,10 @@ define([

refreshRendering: function (oldVals) {
if ("scrollDirection" in oldVals) {
classList.toggleClass(this.scrollableNode,
"d-scrollable", this.scrollDirection !== "none");
classList.toggleClass(this.scrollableNode,
this.scrollableNode.classList.toggle("d-scrollable", this.scrollDirection !== "none");
this.scrollableNode.classList.toggle(
"d-scrollable-h", /^(both|horizontal)$/.test(this.scrollDirection));
classList.toggleClass(this.scrollableNode,
"d-scrollable-v", /^(both|vertical)$/.test(this.scrollDirection));
this.scrollableNode.classList.toggle("d-scrollable-v", /^(both|vertical)$/.test(this.scrollDirection));
}
},

Expand Down
3 changes: 1 addition & 2 deletions Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ define([
"dcl/dcl",
"requirejs-dplugins/has",
"ibm-decor/Invalidating",
"requirejs-dplugins/Promise!",
"./ArrayQueryAdapter",
"./DstoreQueryAdapter"
], function (dcl, has, Invalidating, Promise, ArrayQueryAdapter, DstoreQueryAdapter) {
], function (dcl, has, Invalidating, ArrayQueryAdapter, DstoreQueryAdapter) {

/**
* Dispatched once the query has been executed and the `renderItems` array
Expand Down
27 changes: 18 additions & 9 deletions Widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ define([
"ibm-decor/Invalidating",
"./CustomElement",
"./register",
"./classList",
"requirejs-dplugins/css!./css/common.css"
], function (
dcl,
has,
Invalidating,
CustomElement,
register,
classList
register
) {
// Used to generate unique id for each widget
var cnt = 0;
Expand Down Expand Up @@ -210,10 +208,15 @@ define([
* @protected
*/
setClassComponent: function (component, value, node) {
value = value && value.trim();
if (!node) { node = this; }
var oldValProp = "_" + component + "Class";
classList.removeClass(node, node[oldValProp]);
classList.addClass(node, value);
if (node[oldValProp]) {
node.classList.remove.apply(node.classList, node[oldValProp].split(/ +/));
}
if (value) {
node.classList.add.apply(node.classList, value.split(/ +/));
}
node[oldValProp] = value;
},

Expand Down Expand Up @@ -370,7 +373,9 @@ define([
* @returns {module:delite/Widget} This widget, for chaining.
*/
toggleClass: function (value, force) {
classList.toggleClass(this, value, force);
value.split(" ").forEach(function (singleValue) {
this.classList.toggle(singleValue, force);
}, this);
return this;
},

Expand All @@ -382,7 +387,9 @@ define([
* @returns {module:delite/Widget} This widget, for chaining.
*/
addClass: function (value) {
classList.addClass(this, value);
if (value) {
this.classList.add.apply(this.classList, value.split(" "));
}
return this;
},

Expand All @@ -393,7 +400,9 @@ define([
* @returns {module:delite/Widget} This widget, for chaining.
*/
removeClass: function (value) {
classList.removeClass(this, value);
if (value) {
this.classList.remove.apply(this.classList, value.split(" "));
}
return this;
},

Expand All @@ -404,7 +413,7 @@ define([
* @returns {boolean} True if this widget contains the given class. False otherwise.
*/
hasClass: function (className) {
return classList.hasClass(this, className);
return this.classList.has(className);
}
});

Expand Down
74 changes: 0 additions & 74 deletions classList.js

This file was deleted.

19 changes: 0 additions & 19 deletions features.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,5 @@ define(["requirejs-dplugins/has"], function (has) {
// A background iframe is useful to prevent problems with popups appearing behind applets/pdf files.
has.add("config-bgIframe", false);

if (typeof window !== "undefined") {
// Returns the name of the method to test if an element matches a CSS selector.
has.add("dom-matches", function () {
var node = document.body;
if (node.matches) {
return "matches";
}
if (node.webkitMatchesSelector) {
return "webkitMatchesSelector";
}
if (node.mozMatchesSelector) {
return "mozMatchesSelector";
}
if (node.msMatchesSelector) {
return "msMatchesSelector";
}
});
}

return has;
});
3 changes: 1 addition & 2 deletions handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ define([
"module",
"require",
"requirejs-dplugins/has",
"requirejs-dplugins/Promise!",
"requirejs-text/text",
"./Template"
], function (module, moduleRequire, has, Promise, textPlugin, Template) {
], function (module, moduleRequire, has, textPlugin, Template) {

/**
* Given a string like "hello {{foo}} world", generate JS code to output that string,
Expand Down
4 changes: 4 additions & 0 deletions intern.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

"basePath": "../",

"plugins": [
"../delite/tests/ie-polyfills.js"
],

"node": {
"loader": {
"script": "./intern-requirejs-loader.js",
Expand Down
Loading

0 comments on commit 220f85f

Please sign in to comment.