nightTab/js/control.js

114 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-12-26 08:45:53 +01:00
var control = (function() {
var state = {
2018-12-27 00:46:53 +01:00
edit: false,
style: {
block: true,
list: false
}
2018-12-27 00:46:53 +01:00
};
var get = function() {
return state;
2018-12-26 08:45:53 +01:00
};
var bind = function() {
var controlAdd = helper.e(".control-add");
var controlEdit = helper.e(".control-edit");
2018-12-27 00:46:53 +01:00
var controlLinkBlock = helper.e(".control-link-blocks");
var controlLinkList = helper.e(".control-link-list");
2018-12-26 08:45:53 +01:00
controlAdd.addEventListener("click", function() {
links.add();
2018-12-26 08:45:53 +01:00
}, false);
controlEdit.addEventListener("click", function() {
_toggleEdit();
render();
links.tabIndex();
2018-12-26 08:45:53 +01:00
}, false);
2018-12-27 00:46:53 +01:00
controlLinkBlock.addEventListener("click", function() {
_toggleListStyle("block");
render();
2018-12-27 00:46:53 +01:00
data.save();
}, false);
controlLinkList.addEventListener("click", function() {
_toggleListStyle("list");
render();
2018-12-27 00:46:53 +01:00
data.save();
}, false);
2018-12-26 08:45:53 +01:00
};
var _toggleEdit = function() {
2018-12-26 08:45:53 +01:00
if (state.edit) {
state.edit = false;
} else {
state.edit = true;
};
};
2018-12-27 00:46:53 +01:00
var _toggleListStyle = function(style) {
var action = {
block: function() {
state.style.block = true;
state.style.list = false;
},
list: function() {
state.style.block = false;
state.style.list = true;
}
};
action[style]();
2018-12-27 00:46:53 +01:00
};
var render = function() {
var html = helper.e("html");
var controlEdit = helper.e(".control-edit");
2018-12-27 00:46:53 +01:00
var controlLinkBlock = helper.e(".control-link-blocks");
var controlLinkList = helper.e(".control-link-list");
var _renderEdit = function() {
if (state.edit) {
helper.addClass(html, "is-edit");
helper.addClass(controlEdit, "active");
} else {
helper.removeClass(html, "is-edit");
helper.removeClass(controlEdit, "active");
};
};
var _renderStyle = function() {
if (state.style.block) {
2018-12-27 00:46:53 +01:00
helper.addClass(html, "is-link-block");
helper.removeClass(html, "is-link-list");
helper.addClass(controlLinkBlock, "active");
helper.removeClass(controlLinkList, "active");
} else if (state.style.list) {
2018-12-27 00:46:53 +01:00
helper.removeClass(html, "is-link-block");
helper.addClass(html, "is-link-list");
helper.removeClass(controlLinkBlock, "active");
helper.addClass(controlLinkList, "active");
};
2018-12-27 00:46:53 +01:00
};
_renderEdit();
_renderStyle();
2018-12-27 00:46:53 +01:00
};
var restore = function(object) {
if (object) {
state = object;
render();
};
};
2018-12-26 08:45:53 +01:00
var init = function() {
bind();
2018-12-27 00:46:53 +01:00
render();
2018-12-26 08:45:53 +01:00
};
// exposed methods
return {
init: init,
2018-12-27 00:46:53 +01:00
get: get,
restore: restore,
render: render
2018-12-26 08:45:53 +01:00
};
})();