Skip to content

Commit

Permalink
Convert to use class syntax throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
paracycle committed Sep 12, 2023
1 parent 1b20beb commit 3eb48b0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 40 deletions.
17 changes: 7 additions & 10 deletions lib/assets/javascripts/turbograft/component_url.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* If an instance is created from a relative URL, the current document
* is used to fill in the missing attributes (protocol, host, port).
*/
window.ComponentUrl = (function() {
function ComponentUrl(original, link) {
window.ComponentUrl = class ComponentUrl {
constructor(original, link) {
if (original == null) {
original = document.location.href;
}
Expand All @@ -20,20 +20,20 @@ window.ComponentUrl = (function() {
this._parse();
}

ComponentUrl.prototype.withoutHash = function() {
withoutHash() {
return this.href.replace(this.hash, '');
};

// Intention revealing function alias
ComponentUrl.prototype.withoutHashForIE10compatibility = function() {
withoutHashForIE10compatibility() {
return this.withoutHash();
};

ComponentUrl.prototype.hasNoHash = function() {
hasNoHash() {
return this.hash.length === 0;
};

ComponentUrl.prototype._parse = function() {
_parse() {
this.link.href = this.original;
this.href = this.link.href;
this.protocol = this.link.protocol;
Expand All @@ -50,7 +50,4 @@ window.ComponentUrl = (function() {
this.relative = [this.pathname, this.search, this.hash].join('');
return this.absolute = this.href;
};

return ComponentUrl;

})();
};
48 changes: 18 additions & 30 deletions lib/assets/javascripts/turbograft/link.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,45 @@
(function() {
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty,
slice = [].slice;

/* The Link class derives from the ComponentUrl class, but is built from an
* existing link element. Provides verification functionality for Turbolinks
* to use in determining whether it should process the link when clicked.
*/
window.Link = (function(superClass) {
extend(Link, superClass);

Link.HTML_EXTENSIONS = ['html'];

Link.allowExtensions = function() {
/* The Link class derives from the ComponentUrl class, but is built from an
* existing link element. Provides verification functionality for Turbolinks
* to use in determining whether it should process the link when clicked.
*/
window.Link = class Link extends ComponentUrl {
HTML_EXTENSIONS = ['html'];

static allowExtensions() {
var extension, extensions, i, len;
extensions = 1 <= arguments.length ? slice.call(arguments, 0) : [];
extensions = 1 <= arguments.length ? [].slice.call(arguments, 0) : [];
for (i = 0, len = extensions.length; i < len; i++) {
extension = extensions[i];
Link.HTML_EXTENSIONS.push(extension);
}
return Link.HTML_EXTENSIONS;
};

function Link(link) {
constructor(link) {
if (link.constructor === Link) {
return link;
}
Link.__super__.constructor.call(this, link.href, link);
super(link.href, link);
}

Link.prototype.shouldIgnore = function() {
shouldIgnore() {
return this._crossOrigin() || this._anchored() || this._nonHtml() || this._optOut() || this._target();
};

Link.prototype._crossOrigin = function() {
_crossOrigin() {
return this.origin !== (new ComponentUrl).origin;
};

Link.prototype._anchored = function() {
_anchored() {
var current;
return ((this.hash && this.withoutHash()) === (current = new ComponentUrl).withoutHash()) || (this.href === current.href + '#');
};

Link.prototype._nonHtml = function() {
_nonHtml() {
return this.pathname.match(/\.[a-z]+$/g) && !this.pathname.match(new RegExp("\\.(?:" + (Link.HTML_EXTENSIONS.join('|')) + ")?$", 'g'));
};

Link.prototype._optOut = function() {
_optOut() {
var ignore, link;
link = this.link;
while (!(ignore || link === document || link === null)) {
Expand All @@ -56,12 +49,7 @@
return ignore;
};

Link.prototype._target = function() {
_target() {
return this.link.target.length !== 0;
};

return Link;

})(ComponentUrl);

}).call(this);
};

0 comments on commit 3eb48b0

Please sign in to comment.