nightTab/js/control.js

100 lines
2.3 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"
};
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() {
_add();
}, false);
controlEdit.addEventListener("click", function() {
_edit();
}, false);
2018-12-27 00:46:53 +01:00
controlLinkBlock.addEventListener("click", function() {
_toggleListStyle("block");
data.save();
}, false);
controlLinkList.addEventListener("click", function() {
_toggleListStyle("list");
data.save();
}, false);
2018-12-26 08:45:53 +01:00
};
var _add = function() {
links.add();
};
var _edit = function() {
var body = helper.e("body");
var controlEdit = helper.e(".control-edit");
if (state.edit) {
helper.removeClass(body, "is-edit");
helper.removeClass(controlEdit, "active");
state.edit = false;
links.tabindex();
} else {
helper.addClass(body, "is-edit");
helper.addClass(controlEdit, "active");
state.edit = true;
links.tabindex();
};
};
2018-12-27 00:46:53 +01:00
var _toggleListStyle = function(style) {
state.style = style;
render();
};
var render = function() {
var html = helper.e("html");
var controlLinkBlock = helper.e(".control-link-blocks");
var controlLinkList = helper.e(".control-link-list");
var action = {
block: function() {
helper.addClass(html, "is-link-block");
helper.removeClass(html, "is-link-list");
helper.addClass(controlLinkBlock, "active");
helper.removeClass(controlLinkList, "active");
},
list: function() {
helper.removeClass(html, "is-link-block");
helper.addClass(html, "is-link-list");
helper.removeClass(controlLinkBlock, "active");
helper.addClass(controlLinkList, "active");
}
};
action[state.style]();
};
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
};
})();