dokuwiki-indexmenu-plugin – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * The Indexmenu Wizard |
||
3 | * |
||
4 | * @author Gerrit Uitslag |
||
5 | * based on Linkwiz by |
||
6 | * @author Andreas Gohr <gohr@cosmocode.de> |
||
7 | * @author Pierre Spring <pierre.spring@caillou.ch> |
||
8 | * and the concepts of the old indexmenu wizard |
||
9 | */ |
||
10 | var indexmenu_wiz = { |
||
11 | $wiz: null, |
||
12 | timer: null, |
||
13 | textArea: null, |
||
14 | |||
15 | defaulttheme: 'default', |
||
16 | fields: { |
||
17 | div1: { |
||
18 | elems: { |
||
19 | js: {} |
||
20 | } |
||
21 | }, |
||
22 | div2: { |
||
23 | tlbclass: 'js theme', |
||
24 | elems: { |
||
25 | el1: {headerid: 'theme'} |
||
26 | } |
||
27 | }, |
||
28 | div3: { |
||
29 | elems: { |
||
30 | el2: {headerid: 'navigation'}, |
||
31 | navbar: {}, |
||
32 | context: {}, |
||
33 | nocookie: {tlbclass: 'js'}, |
||
34 | noscroll: {tlbclass: 'js'}, |
||
35 | notoc: {tlbclass: 'js'}, |
||
36 | scorespace: {tlbclass: 'js'} |
||
37 | } |
||
38 | }, |
||
39 | div4: { |
||
40 | elems: { |
||
41 | el3: {headerid: 'sort'}, |
||
42 | tsort: {}, |
||
43 | dsort: {}, |
||
44 | msort: {}, |
||
45 | hsort: {}, |
||
46 | rsort: {}, |
||
47 | nsort: {} |
||
48 | } |
||
49 | }, |
||
50 | div5: { |
||
51 | elems: { |
||
52 | el4: {headerid: 'filter'}, |
||
53 | nons: {}, |
||
54 | nopg: {} |
||
55 | } |
||
56 | }, |
||
57 | div6: { |
||
58 | tlbclass: 'js', |
||
59 | elems: { |
||
60 | el5: {headerid: 'performance'}, |
||
61 | max: {tlbclass: 'js', number: ['maxn', 'maxm']}, |
||
62 | maxjs: {tlbclass: 'js', number: ['maxjsn']}, |
||
63 | id: {tlbclass: 'js', number: ['idn']} |
||
64 | } |
||
65 | } |
||
66 | }, |
||
67 | |||
68 | /** |
||
69 | * Initialize the indexmenu_wiz by creating the needed HTML |
||
70 | * and attaching the eventhandlers |
||
71 | */ |
||
72 | init: function($editor){ |
||
73 | // position relative to the text area |
||
74 | var pos = $editor.position(); |
||
75 | |||
76 | // create HTML Structure |
||
77 | indexmenu_wiz.$wiz = jQuery(document.createElement('div')) |
||
78 | .dialog({ |
||
79 | autoOpen: false, |
||
80 | draggable: true, |
||
81 | title: LANG.plugins.indexmenu.indexmenuwizard, |
||
82 | resizable: false |
||
83 | }) |
||
84 | .html( |
||
85 | '<fieldset class="index"><legend>'+LANG.plugins.indexmenu.index+'</legend>' + |
||
86 | '<div><label>'+LANG.plugins.indexmenu.namespace+'<input id="namespace" type="text"></label></div>' + |
||
87 | '<div><label class="number">'+LANG.plugins.indexmenu.nsdepth+' #<input id="nsdepth" type="text" value=1></label></div>' + |
||
88 | '</fieldset>' + |
||
89 | |||
90 | '<fieldset class="options"><legend>'+LANG.plugins.indexmenu.options+'</legend>' + |
||
91 | '</fieldset>' + |
||
92 | '<input type="submit" value="'+LANG.plugins.indexmenu.insert+'" class="button" id="indexmenu__insert">'+ |
||
93 | |||
94 | '<fieldset class="metanumber">' + |
||
95 | '<label class="number">'+LANG.plugins.indexmenu.metanum+'<input type="text" id="metanumber"></label>' + |
||
96 | '<input type="submit" value="'+LANG.plugins.indexmenu.insertmetanum+'" class="button" id="indexmenu__insertmetanum">' + |
||
97 | '</fieldset>' |
||
98 | ) |
||
99 | .parent() |
||
100 | .attr('id','indexmenu__wiz') |
||
101 | .css({ |
||
102 | 'position': 'absolute', |
||
103 | 'top': (pos.top+20)+'px', |
||
104 | 'left': (pos.left+80)+'px' |
||
105 | }) |
||
106 | .hide() |
||
107 | .appendTo('.dokuwiki:first'); |
||
108 | |||
109 | indexmenu_wiz.textArea = $editor[0]; |
||
110 | var $opt_fieldset = jQuery('#indexmenu__wiz fieldset.options'); |
||
111 | |||
112 | jQuery.each(indexmenu_wiz.fields, function(i,section) { |
||
113 | var div = jQuery('<div>').addClass(section.tlbclass); |
||
114 | |||
115 | jQuery.each(section.elems, function(elid,props){ |
||
116 | if(props.headerid) { |
||
117 | div.append('<strong>'+LANG.plugins.indexmenu[props.headerid]+'</strong><br />'); |
||
118 | } else { |
||
119 | //checkbox |
||
120 | jQuery("<label>") |
||
121 | .addClass(props.tlbclass).addClass(props.number ? ' num':'') |
||
122 | .html('<input id="'+elid+'" type="checkbox">'+elid) |
||
123 | .attr({title: LANG.plugins.indexmenu[elid]}) |
||
124 | .appendTo(div); |
||
125 | |||
126 | //number inputs |
||
127 | if(props.number) { |
||
128 | jQuery.each(props.number, function(j,numid){ |
||
129 | jQuery("<label>") |
||
130 | .attr({title: LANG.plugins.indexmenu[elid]}) |
||
131 | .addClass("number "+props.tlbclass ) |
||
132 | .html('#<input type="text" id="'+numid+'">') |
||
133 | .appendTo(div); |
||
134 | }); |
||
135 | } |
||
136 | } |
||
137 | }); |
||
138 | $opt_fieldset.append(div); |
||
139 | }); |
||
140 | |||
141 | indexmenu_wiz.includeThemes(); |
||
142 | |||
143 | if(JSINFO && JSINFO.namespace){ |
||
144 | jQuery('#namespace').val(':'+JSINFO.namespace); |
||
145 | } |
||
146 | |||
147 | // attach event handlers |
||
148 | |||
149 | //toggle js fields |
||
150 | jQuery('#js') |
||
151 | .change(function(){ |
||
152 | jQuery('#indexmenu__wiz .js').toggle(this.checked); |
||
153 | }).change() |
||
154 | .parent().css({display: 'inline-block', width: '40px'}); //enlarge clickable area of label |
||
155 | |||
156 | //interactive number fields |
||
157 | jQuery('label.number input').bind('keydown keyup', function(){ |
||
158 | //allow only numbers |
||
159 | indexmenu_wiz.filterNumberinput(this); |
||
160 | //checked the option if a number in input |
||
161 | indexmenu_wiz.autoCheckboxForNumbers(this); |
||
162 | }); |
||
163 | |||
164 | jQuery('#indexmenu__insert').click(indexmenu_wiz.insertIndexmenu); |
||
165 | jQuery('#indexmenu__insertmetanum').click(indexmenu_wiz.insertMetaNumber); |
||
166 | |||
167 | jQuery('#indexmenu__wiz').find('.ui-dialog-titlebar-close').click(indexmenu_wiz.hide); |
||
168 | }, |
||
169 | |||
170 | /** |
||
171 | * Request and include themes in wizard |
||
172 | */ |
||
173 | includeThemes: function() { |
||
174 | |||
175 | var addButtons = function(data) { |
||
176 | var $themebar = jQuery('<div>') |
||
177 | .attr('id','themebar') |
||
178 | .addClass('toolbar') |
||
179 | .appendTo('div.theme'); |
||
180 | |||
181 | jQuery.each(data.themes, function(i, theme){ |
||
182 | var themeName =theme.split('.'); |
||
183 | |||
184 | var $ico = jQuery('<div>') |
||
185 | .css({ background: 'url('+ DOKU_BASE + data.themebase + '/' + theme + '/base.' + IndexmenuUtils.determineExtension(theme)+') no-repeat center' }); |
||
186 | var $btn = jQuery('<button>') |
||
187 | .addClass('themebutton toolbutton') |
||
188 | .attr('id', theme) |
||
189 | .attr('title', themeName[0]) |
||
190 | .append($ico) |
||
191 | .click(indexmenu_wiz.selectTheme) |
||
192 | .appendTo('div#themebar'); |
||
193 | }); |
||
194 | |||
195 | //select default theme |
||
196 | jQuery('#themebar button#'+indexmenu_wiz.defaulttheme).click(); |
||
197 | }; |
||
198 | |||
199 | jQuery.post( |
||
200 | DOKU_BASE + 'lib/exe/ajax.php', |
||
201 | {call: 'indexmenu', req: 'local'}, |
||
202 | addButtons, |
||
203 | 'json' |
||
204 | ); |
||
205 | }, |
||
206 | |||
207 | /** |
||
208 | * set class 'selected' to clicked theme, remove from other |
||
209 | */ |
||
210 | selectTheme: function(){ |
||
211 | jQuery('.themebutton').toggleClass('selected', false); |
||
212 | jQuery(this).toggleClass('selected', true); |
||
213 | }, |
||
214 | |||
215 | /** |
||
216 | * Allow only number, by direct removing other characters from input |
||
217 | */ |
||
218 | filterNumberinput: function(elem){ |
||
219 | if(elem.value.match(/\D/)) { |
||
220 | elem.value=this.value.replace(/\D/g,''); |
||
221 | } |
||
222 | }, |
||
223 | |||
224 | /** |
||
225 | * When a number larger than zero is inputted, check the checkbox |
||
226 | */ |
||
227 | autoCheckboxForNumbers: function(elem){ |
||
228 | var checkboxid = elem.id.substr(0,elem.id.length-1); |
||
229 | var value = elem.value; |
||
230 | //exception for second number field of max: only uncheck when first field is also empty |
||
231 | if(elem.id=='maxm' && !(elem.value>0)) { |
||
232 | value = parseInt(jQuery('input#maxn').val()); |
||
233 | } |
||
234 | jQuery('input#'+checkboxid).prop('checked', value>0 ); |
||
235 | }, |
||
236 | |||
237 | /** |
||
238 | * Insert the indexmenu with options to the textarea, |
||
239 | * replacing the current selection or at the cursor position. |
||
240 | */ |
||
241 | insertIndexmenu: function(){ |
||
242 | var options = ''; |
||
243 | jQuery('fieldset.options input').each(function(i, input){ |
||
244 | var $label = jQuery(this).parent(); |
||
245 | |||
246 | if(input.checked && ( !$label.hasClass('js')||jQuery('input#js').is(':checked') )){ |
||
247 | //add option |
||
248 | options += ' '+input.id; |
||
249 | |||
250 | //add numbers |
||
251 | if($label.hasClass('num')){ |
||
252 | jQuery.each(indexmenu_wiz.fields.div6.elems[input.id].number, function(j,numid){ |
||
253 | var num = parseInt(jQuery('input#'+numid).val()); |
||
254 | options += num ? '#'+num : ''; |
||
255 | }); |
||
256 | } |
||
257 | //add theme |
||
258 | if(input.id == 'js') { |
||
259 | var themename = jQuery('#themebar button.selected').attr('id'); |
||
260 | if(indexmenu_wiz.defaulttheme !== themename) { //skip default theme |
||
261 | options += '#'+jQuery('#themebar button.selected').attr('id'); |
||
262 | } |
||
263 | |||
264 | } |
||
265 | } |
||
266 | |||
267 | }); |
||
268 | options = options ? '|'+jQuery.trim(options) : ''; |
||
269 | |||
270 | var sel, ns, depth, syntax, eo; |
||
271 | |||
272 | // XXX: Compatibility Fix for 2014-05-05 "Ponder Stibbons", splitbrain/dokuwiki#505 |
||
273 | if(DWgetSelection) { |
||
274 | sel = DWgetSelection(indexmenu_wiz.textArea); |
||
275 | } else { |
||
276 | sel = getSelection(indexmenu_wiz.textArea); |
||
277 | } |
||
278 | |||
279 | |||
280 | ns = jQuery('#namespace').val(); |
||
281 | depth = parseInt(jQuery('#nsdepth').val()); |
||
282 | depth = depth ? '#'+depth : ''; |
||
283 | |||
284 | syntax = '{{indexmenu>'+ns+depth+options+'}}'; |
||
285 | eo = depth.length + options.length + 2; |
||
286 | |||
287 | pasteText(sel, syntax,{startofs: 12, endofs: eo}); |
||
288 | indexmenu_wiz.hide(); |
||
289 | }, |
||
290 | |||
291 | /** |
||
292 | * Insert meta number for sorting in textarea |
||
293 | * Takes number from input, otherwise tries the selection in textarea |
||
294 | */ |
||
295 | insertMetaNumber: function(){ |
||
296 | var sel, selnum, syntax, number; |
||
297 | |||
298 | // XXX: Compatibility Fix for 2014-05-05 "Ponder Stibbons", splitbrain/dokuwiki#505 |
||
299 | if(DWgetSelection) { |
||
300 | sel = DWgetSelection(indexmenu_wiz.textArea); |
||
301 | } else { |
||
302 | sel = getSelection(indexmenu_wiz.textArea); |
||
303 | } |
||
304 | |||
305 | selnum = parseInt(sel.getText()); |
||
306 | number = parseInt(jQuery('input#metanumber').val()); |
||
307 | number = number || selnum || 1; |
||
308 | syntax = '{{indexmenu_n>'+number+'}}'; |
||
309 | |||
310 | pasteText(sel, syntax,{startofs: 14, endofs: 2}); |
||
311 | indexmenu_wiz.hide(); |
||
312 | }, |
||
313 | |||
314 | /** |
||
315 | * Show the indexmenu wizard |
||
316 | */ |
||
317 | show: function(){ |
||
318 | // XXX: Compatibility Fix for 2014-05-05 "Ponder Stibbons", splitbrain/dokuwiki#505 |
||
319 | if(DWgetSelection) { |
||
320 | indexmenu_wiz.selection = DWgetSelection(indexmenu_wiz.textArea); |
||
321 | } else { |
||
322 | indexmenu_wiz.selection = getSelection(indexmenu_wiz.textArea); |
||
323 | } |
||
324 | |||
325 | indexmenu_wiz.$wiz.show(); |
||
326 | jQuery('#namespace').focus(); |
||
327 | }, |
||
328 | |||
329 | /** |
||
330 | * Hide the indexmenu wizard |
||
331 | */ |
||
332 | hide: function(){ |
||
333 | indexmenu_wiz.$wiz.hide(); |
||
334 | indexmenu_wiz.textArea.focus(); |
||
335 | }, |
||
336 | |||
337 | /** |
||
338 | * Toggle the indexmenu wizard |
||
339 | */ |
||
340 | toggle: function(){ |
||
341 | if(indexmenu_wiz.$wiz.css('display') == 'none'){ |
||
342 | indexmenu_wiz.show(); |
||
343 | }else{ |
||
344 | indexmenu_wiz.hide(); |
||
345 | } |
||
346 | } |
||
347 | }; |