-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (40 loc) · 1.14 KB
/
Copy pathindex.js
File metadata and controls
42 lines (40 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* jshint undef: true, unused: true, esversion: 6 */
/* globals $, document */
(($) => {
"use strict";
const attr = {
syrup: 'data-syrup',
ready: 'data-syrup-ready'
};
const buildEventObj = ($el) => {
return $el.attr(attr.syrup)
.split(',')
.map(str => str.trim()) // trim string
.map(str => str.split(':', 2)) // split each pair over ":"
.map(pair => [ // convert second element of each pair to a publish function
pair[0],
(function (e) {
$.publish(this, e, $el);
}).bind(pair[1])
])
.reduce((acc, cur) => { // reduce the array of pairs to an object
acc[cur[0]] = cur[1];
return acc;
}, {});
};
$.fn.syrup = function() {
const $el = $(this);
let syrupEls = $el.find('[' + attr.syrup + ']');
if ($el.attr(attr.syrup)) syrupEls.add($el);
syrupEls.each((i, el) => {
var $el = $(el);
if ($el.attr(attr.ready)) return; // already initialized
else {
$el.on(buildEventObj($(el)));
$el.attr(attr.ready, 'true');
}
});
};
// init all syrup elements
$(() => $(document.body).syrup());
})($);