From 3eb48b0576e50bfd2bcfab0a29cfff9610766494 Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Wed, 13 Sep 2023 00:56:23 +0300 Subject: [PATCH] Convert to use `class` syntax throughout --- .../javascripts/turbograft/component_url.js | 17 +++---- lib/assets/javascripts/turbograft/link.js | 48 +++++++------------ 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/lib/assets/javascripts/turbograft/component_url.js b/lib/assets/javascripts/turbograft/component_url.js index eabcd22..62bb397 100644 --- a/lib/assets/javascripts/turbograft/component_url.js +++ b/lib/assets/javascripts/turbograft/component_url.js @@ -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; } @@ -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; @@ -50,7 +50,4 @@ window.ComponentUrl = (function() { this.relative = [this.pathname, this.search, this.hash].join(''); return this.absolute = this.href; }; - - return ComponentUrl; - -})(); +}; diff --git a/lib/assets/javascripts/turbograft/link.js b/lib/assets/javascripts/turbograft/link.js index a6095d0..bbcc288 100644 --- a/lib/assets/javascripts/turbograft/link.js +++ b/lib/assets/javascripts/turbograft/link.js @@ -1,20 +1,13 @@ -(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); @@ -22,31 +15,31 @@ 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)) { @@ -56,12 +49,7 @@ return ignore; }; - Link.prototype._target = function() { + _target() { return this.link.target.length !== 0; }; - - return Link; - - })(ComponentUrl); - -}).call(this); +};