-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
193 lines (166 loc) · 5.86 KB
/
script.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* Javascript for DokuWiki Plugin snippets
* @author Michael Klier <[email protected]>
*/
snippets = {
keepopen: false,
// Attach all events to elements
attach: function(obj) {
if(!obj) return;
if(!opener) return;
// add keepopen checkbox
var opts = $('plugin_snippets__opts');
if(opts) {
var kobox = document.createElement('input');
kobox.type = 'checkbox';
kobox.id = 'snippets__keepopen';
if(DokuCookie.getValue('snippets_keepopen')){
kobox.checked = true;
kobox.defaultChecked = true; //IE wants this
media.keepopen = true;
}
addEvent(kobox, 'click', function(event){
snippets.togglekeepopen(this); });
var kolbl = document.createElement('label');
kolbl.htmlFor = 'snippets__keepopen';
kolbl.innerHTML = LANG['keepopen'];
var kobr = document.createElement('br');
opts.appendChild(kobox);
opts.appendChild(kolbl);
opts.appendChild(kobr);
}
// attach events
links = getElementsByClass('wikilink1', obj, 'a');
if(links) {
for(var i = 0; i < links.length; i ++) {
link = links[i];
page = link.title;
div = link.parentNode;
span = document.createElement('span');
span.innerHTML = link.innerHTML;
div.removeChild(link);
preview = document.createElement('a');
preview.className = 'plugin_snippets_preview';
preview.title = LANG['plugins']['snippets']['preview'];
preview.href = page;
addEvent(preview, 'click', function(event) {
event.preventDefault();
event.stopPropagation();
snippets.preview(this.href);
return false; });
div.appendChild(preview);
insert = document.createElement('a');
insert.className = 'plugin_snippets_insert';
insert.title = LANG['plugins']['snippets']['insert'];
insert.href = page;
addEvent(insert, 'click', function(event) {
event.preventDefault();
event.stopPropagation();
snippets.insert(this.href);
return false; });
div.appendChild(insert);
div.appendChild(span);
}
}
// strip out links to non-existing pages
links = getElementsByClass('wikilink2', obj, 'a');
if(links) {
for(var i = 0; i < links.length; i ++) {
link = links[i];
span = document.createElement('span');
span.innerHTML = link.innerHTML;
div = link.parentNode;
div.removeChild(link);
div.appendChild(span);
}
}
// add toggle to sub lists
lists = obj.getElementsByTagName('ul');
if(lists) {
for(var i = 1; i < lists.length; i++) {
list = lists[i];
list.style.display = 'none';
div = list.previousSibling;
if(div.nodeType != 1) {
// IE7 and FF treat whitespace different
div = div.previousSibling;
}
div.className = 'li closed';
addEvent(div, 'click', function(event) { snippets.toggle(this); });
}
}
},
// toggle open/close state in template list
toggle: function(obj) {
if(!obj) return;
list = obj.nextSibling;
if(list.nodeType != 1) {
list = list.nextSibling;
}
if(list.style.display == 'none') {
list.style.display = 'block';
obj.className = 'li open';
} else {
list.style.display = 'none';
obj.className = 'li closed';
}
return false;
},
/**
* Toggles the keep open state
*
* @author Andreas Gohr <[email protected]>
*/
togglekeepopen: function(cb){
if(cb.checked){
DokuCookie.setValue('snippets_keepopen',1);
snippets.keepopen = true;
}else{
DokuCookie.setValue('snippets_keepopen','');
snippets.keepopen = false;
}
},
// perform AJAX preview
preview: function(page) {
preview = $('plugin_snippets__preview');
if(!preview) return;
preview.innerHTML = '<img src="'+DOKU_BASE+'/lib/images/throbber.gif" />';
var ajax = new sack(DOKU_BASE+'lib/exe/ajax.php');
ajax.AjaxFailedAlert = '';
ajax.encodeURIString = false;
ajax.setVar('call', 'snippet_preview');
ajax.setVar('id', page);
ajax.onCompletion = function(){
var data = this.response;
if(data === '') return;
preview.innerHTML = data;
};
ajax.runAJAX();
return false;
},
// perform AJAX insert
insert: function(page) {
if(!opener) return;
var ajax = new sack(DOKU_BASE+'lib/exe/ajax.php');
ajax.AjaxFailedAlert = '';
ajax.encodeURIString = false;
ajax.setVar('call', 'snippet_insert');
ajax.setVar('id', page);
ajax.onCompletion = function(){
var data = this.response;
opener.insertAtCarret('wiki__text', data, '');
if(!snippets.keepopen) {
window.close();
}
opener.focus();
};
ajax.runAJAX();
return false;
}
};
addInitEvent(function(){
var idx = $('plugin_snippets__idx');
if(!idx) return;
snippets.attach(idx);
});
// vim:ts=4:sw=4:et:enc=utf-8: