Repository navigation
Expand file tree
/
Copy pathtoolbar.js
More file actions
94 lines (85 loc) · 2.4 KB
/
Copy pathtoolbar.js
File metadata and controls
94 lines (85 loc) · 2.4 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
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
var oojs = (function(oojs){
var createToolbarItem = function(element){
var item = {
toggleActiveState: function(){
this.activated = !this.activated;
}
};
Object.defineProperties(item,{
el: {
value: element
},
enabled: {
get: function(){return !this.el.classList.contains('disabled');},
set: function(value){
if(value == true){
this.el.classList.remove('disabled');
}else{
this.el.classList.add('disabled');
}
}
},
activated: {
get: function(){return this.el.classList.contains('active');},
set: function(value){
if(value == true){
this.el.classList.add('active');
}else{
this.el.classList.remove('active');
}
}
}
});
return item;
}
var createToolbarItems = function(itemElements){
var items = [];
[].forEach.call(itemElements, function(el, index, array){
var item = createToolbarItem(el);
items.push(item);
});
return items;
};
oojs.createToolbar = function (elementId) {
var element = document.getElementById(elementId);
if(!element){
element = document.createElement("DIV");
element.id = elementId;
element.className = "toolbar";
}
var items = element.querySelectorAll(".toolbar-item");
var toolbar = {
add: function(options){
var span = document.createElement("SPAN");
span.className = "toolbar-item";
this.el.appendChild(span);
var item = createToolbarItem(span);
this.items.push(item);
},
remove: function(index){
var len = this.items.length
if(index > len || index < 0){
throw new Error("Index is out of the range");
}
var item = this.items[index];
this.items.splice(index, 1);
this.el.removeChild(item.el);
item = null;
},
appendTo: function(parentElement){
parentElement.appendChild(this.el);
}
};
Object.defineProperties(toolbar, {
el: {
value: element
},
items: {
value: createToolbarItems(items),
eumerable: true
}
});
return toolbar;
};
return oojs;
}(oojs || {}));