Repository navigation
Expand file tree
/
Copy pathlist.js
More file actions
183 lines (121 loc) · 4.21 KB
/
Copy pathlist.js
File metadata and controls
183 lines (121 loc) · 4.21 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Code Plugin\
* Creates code tag and adds content to this tag
*/
var list = (function(list) {
var baseClass = "tool-list";
var elementClasses = {
li : "tool-list-li"
};
var ui = {
make: function (blockType) {
var wrapper = this.block(blockType || 'UL', baseClass);
wrapper.dataset.type = 'UL';
wrapper.contentEditable = true;
return wrapper;
},
block: function (blockType, blockClass) {
var block = document.createElement(blockType);
if (blockClass) block.classList.add(blockClass);
return block;
},
button: function (buttonType) {
var types = {
unordered: '<i class="ce-icon-list-bullet"></i>Обычный список',
ordered: '<i class="ce-icon-list-numbered"></i>Нумерованный список'
},
button = document.createElement('SPAN');
button.innerHTML = types[buttonType];
button.className = 'ce_plugin_list--select_button';
return button;
}
};
var methods = {
/**
* Changes block type => OL or UL
* @param event
* @param blockType
*/
changeBlockStyle : function (event, blockType) {
var currentBlock = codex.editor.content.currentNode,
newEditable = ui.make(blockType),
oldEditable = currentBlock.querySelector("[contenteditable]");
newEditable.dataset.type = blockType;
newEditable.innerHTML = oldEditable.innerHTML;
newEditable.classList.add('ce-list');
codex.editor.content.switchBlock(currentBlock, newEditable, 'list');
}
};
/**
* Make initial header block
* @param {object} JSON with block data
* @return {Element} element to append
*/
list.make = function () {
var tag = ui.make(),
li = ui.block("li", "tool-link-li");
var br = document.createElement("br");
li.appendChild(br);
tag.appendChild(li);
tag.classList.add('ce-list');
return tag;
};
/**
* Method to render HTML block from JSON
*/
list.render = function (data) {
var type = data.type == 'ordered' ? 'OL' : 'UL',
tag = ui.make(type);
tag.classList.add('ce-list');
data.items.forEach(function (element, index, array) {
var newLi = ui.block("li", listTool.elementClasses.li);
newLi.innerHTML = element;
tag.dataset.type = data.type;
tag.appendChild(newLi);
});
return tag;
};
list.validate = function(data) {
var items = data.items.every(function(item){
return item.trim() != '';
});
if (!items)
return;
if (data.type != 'UL' && data.type != 'OL')
return;
return true;
};
/**
* Method to extract JSON data from HTML block
*/
list.save = function (blockContent){
var data = {
type : null,
items : []
};
for(var index = 0; index < blockContent.childNodes.length; index++)
data.items[index] = blockContent.childNodes[index].textContent;
data.type = blockContent.dataset.type;
return data;
};
list.makeSettings = function(data) {
var holder = document.createElement('DIV'),
selectTypeButton;
/** Add holder classname */
holder.className = 'ce_plugin_list--settings';
var orderedButton = ui.button("ordered"),
unorderedButton = ui.button("unordered");
orderedButton.addEventListener('click', function (event) {
methods.changeBlockStyle(event, 'OL');
codex.editor.toolbar.settings.close();
});
unorderedButton.addEventListener('click', function (event) {
methods.changeBlockStyle(event, 'UL');
codex.editor.toolbar.settings.close();
});
holder.appendChild(orderedButton);
holder.appendChild(unorderedButton);
return holder;
};
return list;
})({});