-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
103 lines (83 loc) · 2.23 KB
/
index.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict';
var classes = require('classes')
, Emitter = require('emitter')
, noop = function() { };
module.exports = Accordion;
var defaults = {
deselect: true,
multiselect: false,
entry: '.hentry',
title: '.entry-title'
};
function Accordion(el,options) {
if (!(this instanceof Accordion)) return new Accordion(el,options);
options = merge(defaults, options || {})
this.selectBehavior = (options.multiselect ? noop : this.deselectAll);
this.reselectBehavior = (options.deselect ? this.deselect : noop);
this.entrySelector = options.entry;
this.entryTitle = options.title;
this.panels = [];
this.el = el;
if (el) this.bind(el);
this.deselectAll();
return this;
}
Accordion.prototype = new Emitter;
Accordion.prototype.bind = function(el){
if (typeof el=='string') el = document.querySelector(el);
var items = el.querySelectorAll(this.entrySelector);
for (var i=0;i<items.length;++i) {
this.panels.push(new Panel(this, items[i]));
}
classes(el).add('accordion');
return this;
}
Accordion.prototype.deselectAll = function(){
for (var i=0;i<this.panels.length;++i) {
this.deselect(this.panels[i])
}
return this;
}
Accordion.prototype.select = function(panel){
if (panel.selected){
this.reselectBehavior(panel);
} else {
this.selectBehavior(panel);
classes(panel.el).add('selected');
panel.selected = true;
this.emit('select',panel);
}
}
Accordion.prototype.deselect = function(panel){
classes(panel.el).remove('selected');
panel.selected = false;
this.emit('deselect', panel);
}
function Panel(container,el) {
if (!(this instanceof Panel)) return new Panel(container,el);
this.container = container;
this.el = el;
this.selected = false;
if (el) this.bind(el);
return this;
}
Panel.prototype.bind = function(el){
var self = this;
var title = el.querySelector(self.container.entryTitle);
title.onclick = function(){ self.container.select(self) };
}
// private
var has = Object.prototype.hasOwnProperty;
var mergeInto = function(a,b){
for (var key in b) {
if (has.call(b,key)){
a[key] = b[key];
}
}
return a;
}
// non-mutating merge
var merge = function(a,b){
var m = mergeInto({},a);
return mergeInto(m,b);
}