forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsunlight-plugin.menu.js
159 lines (130 loc) · 5.14 KB
/
sunlight-plugin.menu.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
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
/**
* Sunlight menu plugin
*
* This creates the menu in the upper right corner for block-level elements.
* This plugin is not supported for IE6.
*
* Options:
* - showMenu: true/false (default is false)
* - autoCollapse: true/false (default is false)
*/
(function(sunlight, document, undefined){
if (sunlight === undefined) {
throw "Include sunlight.js before including plugin files";
}
//http://dean.edwards.name/weblog/2007/03/sniff/#comment83695
//eval()'d so that it compresses correctly
var ieVersion = eval("0 /*@cc_on+ScriptEngineMajorVersion()@*/");
function createLink(href, title, text) {
var link = document.createElement("a");
link.setAttribute("href", href);
link.setAttribute("title", title);
if (text) {
link.appendChild(document.createTextNode(text));
}
return link;
}
function getTextRecursive(node) {
var text = "",
i = 0;
if (node.nodeType === 3) {
return node.nodeValue;
}
text = "";
for (i = 0; i < node.childNodes.length; i++) {
text += getTextRecursive(node.childNodes[i]);
}
return text;
}
sunlight.bind("afterHighlightNode", function(context) {
var menu,
sunlightIcon,
ul,
collapse,
mDash,
collapseLink,
viewRaw,
viewRawLink,
about,
aboutLink,
icon;
if ((ieVersion && ieVersion < 7) || !this.options.showMenu || sunlight.util.getComputedStyle(context.node, "display") !== "block") {
return;
}
menu = document.createElement("div");
menu.className = this.options.classPrefix + "menu";
sunlightIcon =
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAJ" +
"cEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAFBhaW50Lk5FVCB2My41Ljg3O4BdAAAAl0lEQVQ4jWP4" +
"P9n9PyWYgTYGzAr+///Q9P//Ty/HjhfEETDg1oH/YPDgNKbm4wsIuGBO+H84WJJKhhd2dkA0v3tEZhjcPQox4MVN" +
"7P7fUEHAgM112DX++Qkx+PEFMqPxwSmIAQenkWHAvCicAUucAbCAfX2PQCCCEtDGKkz86RXEgL39BAwAKcAFbh/6" +
"/39GIL3yAj0NAAB+LQeDCZ9tvgAAAABJRU5ErkJggg==";
ul = document.createElement("ul");
collapse = document.createElement("li");
mDash = String.fromCharCode(0x2014);
collapseLink = createLink("#", "collapse code block", mDash);
collapseLink.onclick = function() {
var originalHeight = sunlight.util.getComputedStyle(context.codeContainer, "height"),
originalOverflow = sunlight.util.getComputedStyle(context.codeContainer, "overflowY");
return function() {
var needsToExpand = sunlight.util.getComputedStyle(context.codeContainer, "height") !== originalHeight;
this.replaceChild(document.createTextNode(needsToExpand ? mDash : "+"), this.firstChild);
this.setAttribute("title", (needsToExpand ? "collapse" : "expand") + " clode block");
context.codeContainer.style.height = needsToExpand ? originalHeight : "0px";
context.codeContainer.style.overflowY = needsToExpand ? originalOverflow : "hidden";
return false;
}
}();
collapse.appendChild(collapseLink);
viewRaw = document.createElement("li");
viewRawLink = createLink("#", "view raw code", "raw");
viewRawLink.onclick = function() {
var textarea;
return function() {
var rawCode;
if (textarea) {
textarea.parentNode.removeChild(textarea);
textarea = null;
context.node.style.display = "block";
this.replaceChild(document.createTextNode("raw"), this.firstChild);
this.setAttribute("title", "view raw code");
} else {
//hide the codeContainer, flatten all text nodes, create a <textarea>, append it
rawCode = getTextRecursive(context.node);
textarea = document.createElement("textarea");
textarea.value = rawCode;
textarea.setAttribute("readonly", "readonly");
textarea.style.width = (parseInt(sunlight.util.getComputedStyle(context.node, "width")) - 5) + "px"; //IE, Safari and Chrome can't handle the actual width
textarea.style.height = sunlight.util.getComputedStyle(context.node, "height");
textarea.style.border = "none";
textarea.style.overflowX = "hidden"; //IE requires this
textarea.setAttribute("wrap", "off"); //prevent line wrapping lol
context.codeContainer.insertBefore(textarea, context.node);
context.node.style.display = "none";
this.replaceChild(document.createTextNode("highlighted"), this.firstChild);
this.setAttribute("title", "view highlighted code");
textarea.select(); //highlight everything
}
return false;
}
}();
viewRaw.appendChild(viewRawLink);
about = document.createElement("li");
aboutLink = createLink("http://sunlightjs.com/", "Sunlight: JavaScript syntax highlighter by Tommy Montgomery");
icon = document.createElement("img");
icon.setAttribute("src", "data:image/png;base64," + sunlightIcon);
icon.setAttribute("alt", "about");
aboutLink.appendChild(icon);
about.appendChild(aboutLink);
ul.appendChild(about);
ul.appendChild(viewRaw);
ul.appendChild(collapse);
menu.appendChild(ul);
context.container.insertBefore(menu, context.container.firstChild);
if (this.options.autoCollapse) {
collapseLink.onclick.call(collapseLink);
}
});
sunlight.globalOptions.showMenu = false;
sunlight.globalOptions.autoCollapse = false;
}(this["Sunlight"], document));