2016-12-25 15:41:57 +01:00
|
|
|
/**
|
|
|
|
* Code Plugin\
|
|
|
|
* Creates code tag and adds content to this tag
|
|
|
|
*/
|
2017-01-18 16:12:50 +01:00
|
|
|
var list = (function(list) {
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
var baseClass = "tool-list";
|
|
|
|
|
|
|
|
var elementClasses = {
|
2016-12-25 15:41:57 +01:00
|
|
|
li : "tool-list-li"
|
2017-01-18 16:12:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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.content.currentNode,
|
|
|
|
newEditable = ui.make(blockType),
|
|
|
|
oldEditable = currentBlock.querySelector("[contenteditable]");
|
|
|
|
|
|
|
|
newEditable.dataset.type = blockType;
|
|
|
|
newEditable.innerHTML = oldEditable.innerHTML;
|
|
|
|
newEditable.classList.add('ce-list');
|
|
|
|
|
|
|
|
codex.content.switchBlock(currentBlock, newEditable, 'list');
|
|
|
|
}
|
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make initial header block
|
|
|
|
* @param {object} JSON with block data
|
|
|
|
* @return {Element} element to append
|
|
|
|
*/
|
2017-01-18 16:12:50 +01:00
|
|
|
list.make = function () {
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
var tag = ui.make(),
|
|
|
|
li = ui.block("li", "tool-link-li");
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
var br = document.createElement("br");
|
|
|
|
|
|
|
|
li.appendChild(br);
|
|
|
|
tag.appendChild(li);
|
|
|
|
|
|
|
|
tag.classList.add('ce-list');
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to render HTML block from JSON
|
|
|
|
*/
|
2017-01-18 16:12:50 +01:00
|
|
|
list.render = function (data) {
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
var type = data.type == 'ordered' ? 'OL' : 'UL',
|
2017-01-18 16:12:50 +01:00
|
|
|
tag = ui.make(type);
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
tag.classList.add('ce-list');
|
|
|
|
|
|
|
|
data.items.forEach(function (element, index, array) {
|
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
var newLi = ui.block("li", listTool.elementClasses.li);
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
newLi.innerHTML = element;
|
|
|
|
|
|
|
|
tag.dataset.type = data.type;
|
|
|
|
tag.appendChild(newLi);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to extract JSON data from HTML block
|
|
|
|
*/
|
2017-01-18 16:12:50 +01:00
|
|
|
list.save = function (blockContent){
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
var data = {
|
2017-01-18 16:12:50 +01:00
|
|
|
type : null,
|
|
|
|
items : []
|
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
for(var index = 0; index < blockContent.childNodes.length; index++)
|
|
|
|
data.items[index] = blockContent.childNodes[index].textContent;
|
|
|
|
|
|
|
|
data.type = blockContent.dataset.type;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
list.makeSettings = function(data) {
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
var holder = document.createElement('DIV'),
|
|
|
|
selectTypeButton;
|
|
|
|
|
|
|
|
/** Add holder classname */
|
|
|
|
holder.className = 'ce_plugin_list--settings';
|
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
var orderedButton = ui.button("ordered"),
|
|
|
|
unorderedButton = ui.button("unordered");
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
orderedButton.addEventListener('click', function (event) {
|
2017-01-18 16:12:50 +01:00
|
|
|
methods.changeBlockStyle(event, 'OL');
|
2016-12-25 15:41:57 +01:00
|
|
|
codex.toolbar.settings.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
unorderedButton.addEventListener('click', function (event) {
|
2017-01-18 16:12:50 +01:00
|
|
|
methods.changeBlockStyle(event, 'UL');
|
2016-12-25 15:41:57 +01:00
|
|
|
codex.toolbar.settings.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
holder.appendChild(orderedButton);
|
|
|
|
holder.appendChild(unorderedButton);
|
|
|
|
|
|
|
|
return holder;
|
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
return list;
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
})({});
|