Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove coffeescript from this library #188

Merged
merged 7 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ PATH
remote: .
specs:
turbograft (0.4.8)
coffee-rails

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -87,20 +86,12 @@ GEM
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
coderay (1.1.3)
coffee-rails (5.0.0)
coffee-script (>= 2.2.0)
railties (>= 5.2.0)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.2.2)
crass (1.0.6)
daemons (1.4.1)
date (3.3.3)
erubi (1.12.0)
eventmachine (1.2.7)
execjs (2.9.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.1)
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ There is an example app that you can boot to play with TurboGraft. Open the con

When turbograft replaces or removes a node it uses native DOM API to do so. If any objects use jQuery to listen to events on a node then these objects will leak when the node is replaced because jQuery will still have references to it. To clean these up you'll need to tell jQuery that they're removed. This can be done with something like:

```coffeescript
document.addEventListener 'page:after-node-removed', (event) ->
$(event.data).remove()
```js
document.addEventListener('page:after-node-removed', function(event) {
$(event.data).remove();
});
```

## Contributing
Expand Down
30 changes: 0 additions & 30 deletions lib/assets/javascripts/turbograft.coffee

This file was deleted.

37 changes: 37 additions & 0 deletions lib/assets/javascripts/turbograft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//= require_self
//= require_tree ./turbograft

if (window.TurboGraft == null) { window.TurboGraft = { handlers: {} }; }

TurboGraft.tgAttribute = function(attr) {
if (attr.slice(0, 3) === 'tg-') {
return `data-${attr}`;
} else {
return `data-tg-${attr}`;
gmcgibbon marked this conversation as resolved.
Show resolved Hide resolved
}
};

TurboGraft.getTGAttribute = function(node, attr) {
const tgAttr = TurboGraft.tgAttribute(attr);
return node.getAttribute(tgAttr) || node.getAttribute(attr);
};

TurboGraft.removeTGAttribute = function(node, attr) {
const tgAttr = TurboGraft.tgAttribute(attr);
node.removeAttribute(tgAttr);
node.removeAttribute(attr);
};

TurboGraft.hasTGAttribute = function(node, attr) {
const tgAttr = TurboGraft.tgAttribute(attr);
return node.hasAttribute(tgAttr) || node.hasAttribute(attr);
};

TurboGraft.querySelectorAllTGAttribute = function(node, attr, value = null) {
const tgAttr = TurboGraft.tgAttribute(attr);
if (value) {
return node.querySelectorAll(`[${tgAttr}=${value}], [${attr}=${value}]`);
} else {
return node.querySelectorAll(`[${tgAttr}], [${attr}]`);
}
};
34 changes: 0 additions & 34 deletions lib/assets/javascripts/turbograft/click.coffee

This file was deleted.

44 changes: 44 additions & 0 deletions lib/assets/javascripts/turbograft/click.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// The Click class handles clicked links, verifying if Turbolinks should
// take control by inspecting both the event and the link. If it should,
// the page change process is initiated. If not, control is passed back
// to the browser for default functionality.
window.Click = class Click {
static installHandlerLast(event) {
if (!event.defaultPrevented) {
document.removeEventListener('click', Click.handle, false);
document.addEventListener('click', Click.handle, false);
}
}

static handle(event) {
return new Click(event);
}

constructor(event) {
this.event = event;
if (this.event.defaultPrevented) { return; }
this._extractLink();
if (this._validForTurbolinks()) {
Turbolinks.visit(this.link.href);
this.event.preventDefault();
}
}

_extractLink() {
let link = this.event.target;
while (!!link.parentNode && (link.nodeName !== 'A')) { link = link.parentNode; }
if ((link.nodeName === 'A') && (link.href.length !== 0)) { this.link = new Link(link); }
}

_validForTurbolinks() {
return (this.link != null) && !this.link.shouldIgnore() && !this._nonStandardClick();
}

_nonStandardClick() {
return (this.event.which > 1) ||
this.event.metaKey ||
this.event.ctrlKey ||
this.event.shiftKey ||
this.event.altKey;
}
};
24 changes: 0 additions & 24 deletions lib/assets/javascripts/turbograft/component_url.coffee

This file was deleted.

56 changes: 56 additions & 0 deletions lib/assets/javascripts/turbograft/component_url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* The ComponentUrl class converts a basic URL string into an object
* that behaves similarly to document.location.
*
* 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() {
paracycle marked this conversation as resolved.
Show resolved Hide resolved
function ComponentUrl(original, link) {
if (original == null) {
original = document.location.href;
}
if (link == null) {
link = document.createElement('a');
}
if (original.constructor === ComponentUrl) {
return original;
}
this.original = original;
this.link = link;
this._parse();
}

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

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

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

ComponentUrl.prototype._parse = function() {
this.link.href = this.original;
this.href = this.link.href;
this.protocol = this.link.protocol;
this.host = this.link.host;
this.hostname = this.link.hostname
this.port = this.link.port;
this.pathname = this.link.pathname;
this.search = this.link.search;
this.hash = this.link.hash;
this.origin = [this.protocol, '//', this.hostname].join('');
if (this.port.length !== 0) {
this.origin += ":" + this.port;
}
this.relative = [this.pathname, this.search, this.hash].join('');
return this.absolute = this.href;
};

return ComponentUrl;

})();
9 changes: 0 additions & 9 deletions lib/assets/javascripts/turbograft/csrf_token.coffee

This file was deleted.

23 changes: 23 additions & 0 deletions lib/assets/javascripts/turbograft/csrf_token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
window.CSRFToken = class CSRFToken {
static get(doc) {
if (!doc) { doc = document; }
const tag = doc.querySelector('meta[name="csrf-token"]');

const object = {
node: tag
};

if (tag) {
object.token = tag.getAttribute('content');
}

return object;
}

static update(latest) {
const current = this.get();
if ((current.token != null) && (latest != null) && (current.token !== latest)) {
current.node.setAttribute('content', latest);
}
}
};
11 changes: 0 additions & 11 deletions lib/assets/javascripts/turbograft/document.coffee

This file was deleted.

20 changes: 20 additions & 0 deletions lib/assets/javascripts/turbograft/document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
TurboGraft.Document = {
create(html) {
let doc;
if (/<(html|body)/i.test(html)) {
doc = document.documentElement.cloneNode();
doc.innerHTML = html;
} else {
doc = document.documentElement.cloneNode(true);
doc.querySelector('body').innerHTML = html;
}
doc.head = doc.querySelector('head');
doc.body = doc.querySelector('body');
return doc;
}
};
Loading