-
Notifications
You must be signed in to change notification settings - Fork 1
/
css_editor.js
110 lines (91 loc) · 2.66 KB
/
css_editor.js
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
var LiveCSSEditor = (function () {
var is_enabled = false,
cssCache = '',
timer = null;
function get(id) {
return document.getElementById('LiveCSSEditor-' + id);
}
function toggleBottom() {
var panel = get('panel');
if (panel.className.indexOf('bottom') === -1) {
panel.className += ' bottom';
} else {
panel.className = panel.className.replace(' bottom', '');
}
}
function removeEditor() {
if (confirm('This will erase the changes you have made. Are you sure you want to do this?')) {
var css = get('PageCSS'),
panel = get('panel');
clearInterval(timer);
css.parentElement.removeChild(css);
panel.parentElement.removeChild(panel);
is_enabled = false;
}
}
function activateButtons() {
var bottomButton = get('bot'),
closeButton = get('close');
bottomButton.onclick = toggleBottom;
closeButton.onclick = removeEditor;
}
function addEditorPane() {
var objPanel = document.createElement('div');
objPanel.setAttribute('id', 'LiveCSSEditor-panel');
objPanel.innerHTML = '<div id="LiveCSSEditor-actions"><div id="LiveCSSEditor-close">Bottom</div><div id="LiveCSSEditor-bot">Bottom</div></div><div id="LiveCSSEditor-pad"><div id="LiveCSSEditor-label">Live CSS Editor</div><textarea id="LiveCSSEditor-code"></textarea></div>';
document.body.appendChild(objPanel);
activateButtons();
}
function addStyleTag() {
var head = document.getElementsByTagName('head')[0],
obj = document.createElement('style');
obj.id = 'LiveCSSEditor-PageCSS';
obj.setAttribute("type", "text/css");
head.appendChild(obj);
}
function fillStyleTag(css) {
var obj = get('PageCSS');
obj.innerHTML = css;
cssCache = css;
}
function autoUpdate() {
var source = get('code');
/* Don't bother replacing the CSS if it hasn't changed */
if (cssCache === source.value) {
return false;
}
fillStyleTag(source.value);
}
function startAutoUpdate() {
timer = setInterval(autoUpdate, 1000);
}
function init() {
addStyleTag();
fillStyleTag();
addEditorPane();
is_enabled = true;
startAutoUpdate();
}
return {
startIt: function () {
init();
},
stopIt: function () {
removeEditor();
},
enabled: function () {
return is_enabled;
}
};
}());
function handleMessage(msgEvent) {
var messageName = msgEvent.name;
if (messageName === "LiveCSSEditor") {
if (LiveCSSEditor.enabled() === true) {
LiveCSSEditor.stopIt();
} else {
LiveCSSEditor.startIt();
}
}
}
safari.self.addEventListener("message", handleMessage, false);