1 | var autocompleting = false; |
---|
2 | var autocompletelength = 2; |
---|
3 | var editor = ''; |
---|
4 | |
---|
5 | var saved_editor_sessions = []; |
---|
6 | var saved_undo_manager = []; |
---|
7 | var last_added_editor_session = 0; |
---|
8 | var current_editor_session = 0; |
---|
9 | |
---|
10 | var EditSession = require('ace/edit_session').EditSession; |
---|
11 | var UndoManager = require('ace/undomanager').UndoManager; |
---|
12 | var Search = require("ace/search").Search; |
---|
13 | var TokenIterator = require("ace/token_iterator").TokenIterator; |
---|
14 | |
---|
15 | var oHandler; |
---|
16 | |
---|
17 | function onSessionChange(e) { |
---|
18 | |
---|
19 | if( editor.getSession().enable_autocomplete === false){ |
---|
20 | return; |
---|
21 | } |
---|
22 | |
---|
23 | //don't continue with autocomplete if /n entered |
---|
24 | try { |
---|
25 | if ( e.data.text.charCodeAt(0) === 10 ){ |
---|
26 | return; |
---|
27 | } |
---|
28 | }catch(error){} |
---|
29 | |
---|
30 | try { |
---|
31 | if ( e.data.action == 'removeText' ){ |
---|
32 | |
---|
33 | if (autocompleting) { |
---|
34 | autocompletelength = (autocompletelength - 1) ; |
---|
35 | }else{ |
---|
36 | return; |
---|
37 | } |
---|
38 | } |
---|
39 | }catch(error){} |
---|
40 | |
---|
41 | |
---|
42 | //get current cursor position |
---|
43 | range = editor.getSelectionRange(); |
---|
44 | //take note of selection row to compare with search |
---|
45 | cursor_row = range.start.row; |
---|
46 | |
---|
47 | try{ |
---|
48 | //quit autocomplete if we are writing a "string" |
---|
49 | var iterator = new TokenIterator(editor.getSession(), range.start.row, range.start.column); |
---|
50 | var current_token_type = iterator.getCurrentToken().type; |
---|
51 | if(current_token_type == "string" || current_token_type == "comment"){ |
---|
52 | return; |
---|
53 | } |
---|
54 | }catch(error){} |
---|
55 | |
---|
56 | if (range.start.column > 0){ |
---|
57 | |
---|
58 | //search for command text user has entered that we need to try match functions against |
---|
59 | var search = new Search().set({ |
---|
60 | needle: "[\\n \.\)\(]", |
---|
61 | backwards: true, |
---|
62 | wrap: false, |
---|
63 | caseSensitive: false, |
---|
64 | wholeWord: false, |
---|
65 | regExp: true |
---|
66 | }); |
---|
67 | //console.log(search.find(editor.getSession())); |
---|
68 | |
---|
69 | range = search.find(editor.getSession()); |
---|
70 | |
---|
71 | if (range) range.start.column++; |
---|
72 | |
---|
73 | }else{ //change this to look char position, if it's starting at 0 then do this |
---|
74 | |
---|
75 | range.start.column = 0; |
---|
76 | } |
---|
77 | |
---|
78 | if (! range || range.start.row < cursor_row ){ |
---|
79 | //forse the autocomplete check on this row starting at column 0 |
---|
80 | range = editor.getSelectionRange(); |
---|
81 | range.start.column = 0; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | //console.log("search result - start row " + range.start.row + "-" + range.end.row + ", column " + range.start.column+ "-" + range.end.column); |
---|
86 | //console.log(editor.getSelection().getRange()); |
---|
87 | |
---|
88 | range.end.column = editor.getSession().getSelection().getCursor().column +1;//set end column as cursor pos |
---|
89 | |
---|
90 | //console.log("[ \.] based: " + editor.getSession().doc.getTextRange(range)); |
---|
91 | |
---|
92 | //no column lower than 1 thanks |
---|
93 | if (range.start.column < 1) { |
---|
94 | range.start.column = 0; |
---|
95 | } |
---|
96 | |
---|
97 | //console.log("after row " + range.start.row + "-" + range.end.row + ", column " + range.start.column+ "-" + range.end.column); |
---|
98 | //get the editor text based on that range |
---|
99 | var text = editor.getSession().doc.getTextRange(range); |
---|
100 | $quit_onchange = false; |
---|
101 | |
---|
102 | //console.log(text); |
---|
103 | |
---|
104 | //console.log("Searching for text \""+text+"\" length: "+ text.length); |
---|
105 | if (text.length < 3){ |
---|
106 | |
---|
107 | wpide_close_autocomplete(); |
---|
108 | return; |
---|
109 | } |
---|
110 | |
---|
111 | autocompletelength = text.length; |
---|
112 | |
---|
113 | //create the dropdown for autocomplete |
---|
114 | var sel = editor.getSelection(); |
---|
115 | var session = editor.getSession(); |
---|
116 | var lead = sel.getSelectionLead(); |
---|
117 | |
---|
118 | var pos = editor.renderer.textToScreenCoordinates(lead.row, lead.column); |
---|
119 | var ac = document.getElementById('ac'); // #ac is auto complete html select element |
---|
120 | |
---|
121 | |
---|
122 | |
---|
123 | if( typeof ac !== 'undefined' ){ |
---|
124 | |
---|
125 | //add editor click listener |
---|
126 | //editor clicks should hide the autocomplete dropdown |
---|
127 | editor.container.addEventListener('click', function(e){ |
---|
128 | |
---|
129 | wpide_close_autocomplete(); |
---|
130 | |
---|
131 | autocompleting=false; |
---|
132 | autocompletelength = 2; |
---|
133 | |
---|
134 | }, false); |
---|
135 | |
---|
136 | } //end - create initial autocomplete dropdown and related actions |
---|
137 | |
---|
138 | |
---|
139 | //calulate the editor container offset |
---|
140 | var obj=editor.container; |
---|
141 | |
---|
142 | var curleft = 0; |
---|
143 | var curtop = 0; |
---|
144 | |
---|
145 | if (obj.offsetParent) { |
---|
146 | |
---|
147 | do { |
---|
148 | curleft += obj.offsetLeft; |
---|
149 | curtop += obj.offsetTop; |
---|
150 | } while (obj = obj.offsetParent); |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | //position autocomplete |
---|
156 | ac.style.top= ((pos.pageY - curtop)+20) + "px"; |
---|
157 | ac.style.left= ((pos.pageX - curleft)+10) + "px"; |
---|
158 | ac.style.display='block'; |
---|
159 | ac.style.background='white'; |
---|
160 | |
---|
161 | |
---|
162 | //remove all options, starting a fresh list |
---|
163 | ac.options.length = 0; |
---|
164 | |
---|
165 | |
---|
166 | //loop through WP tags and check for a match |
---|
167 | if (autocomplete_wordpress){ |
---|
168 | var tag; |
---|
169 | for(i in autocomplete_wordpress) { |
---|
170 | //if(!html_tags.hasOwnProperty(i) ){ |
---|
171 | // continue; |
---|
172 | //} |
---|
173 | |
---|
174 | tag= i; |
---|
175 | //see if the tag is a match |
---|
176 | if( text !== tag.substr(0,text.length) ){ |
---|
177 | continue; |
---|
178 | } |
---|
179 | |
---|
180 | //add parentheses |
---|
181 | tag = tag + "()"; |
---|
182 | |
---|
183 | var option = document.createElement('option'); |
---|
184 | option.text = tag; |
---|
185 | option.value = tag; |
---|
186 | option.setAttribute('title', wpide_app_path + 'images/wpac.png');//path to icon image or wpac.png |
---|
187 | |
---|
188 | |
---|
189 | try { |
---|
190 | ac.add(option, null); // standards compliant; doesn't work in IE |
---|
191 | } |
---|
192 | catch(ex) { |
---|
193 | ac.add(option); // IE only |
---|
194 | } |
---|
195 | |
---|
196 | }//end for |
---|
197 | }//end php autocomplete |
---|
198 | |
---|
199 | //loop through PHP tags and check for a match |
---|
200 | if (autocomplete_php){ |
---|
201 | var tag; |
---|
202 | for(i in autocomplete_php) { |
---|
203 | //if(!html_tags.hasOwnProperty(i) ){ |
---|
204 | // continue; |
---|
205 | //} |
---|
206 | |
---|
207 | tag= i; |
---|
208 | //see if the tag is a match |
---|
209 | if( text !== tag.substr(0,text.length) ){ |
---|
210 | continue; |
---|
211 | } |
---|
212 | |
---|
213 | //add parentheses |
---|
214 | tag = tag + "()"; |
---|
215 | |
---|
216 | var option = document.createElement('option'); |
---|
217 | option.text = tag; |
---|
218 | option.value = tag; |
---|
219 | option.setAttribute('title', wpide_app_path + 'images/phpac.png');//path to icon image or wpac.png |
---|
220 | |
---|
221 | try { |
---|
222 | ac.add(option, null); // standards compliant; doesn't work in IE |
---|
223 | } |
---|
224 | catch(ex) { |
---|
225 | ac.add(option); // IE only |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | }//end for |
---|
230 | }//end php autocomplete |
---|
231 | |
---|
232 | |
---|
233 | //check for matches |
---|
234 | if ( ac.length === 0 ) { |
---|
235 | wpide_close_autocomplete(); |
---|
236 | } else { |
---|
237 | |
---|
238 | ac.selectedIndex=0; |
---|
239 | autocompleting=true; |
---|
240 | oHandler = jQuery("#ac").msDropDown({visibleRows:10, rowHeight:20}).data("dd"); |
---|
241 | |
---|
242 | jQuery("#ac_child").click(function(item){ |
---|
243 | //get the link node and pass to select AC item function |
---|
244 | if (typeof item.srcElement != 'undefined'){ |
---|
245 | var link_node = item.srcElement; //works on chrome |
---|
246 | }else{ |
---|
247 | var link_node = item.target; //works on Firefox etc |
---|
248 | } |
---|
249 | |
---|
250 | selectACitem(link_node); |
---|
251 | }); |
---|
252 | |
---|
253 | jQuery("#ac_child a").mouseover(function(item){ |
---|
254 | //show the code in the info panel |
---|
255 | |
---|
256 | //get the link ID |
---|
257 | if (typeof item.srcElement != 'undefined'){ |
---|
258 | var link_id = item.srcElement.id; //works on chrome |
---|
259 | }else{ |
---|
260 | var link_id = item.target.id; //works on Firefox etc |
---|
261 | } |
---|
262 | |
---|
263 | if (link_id == '') return; //if the link doesn't have an id it's not valid so just stop |
---|
264 | |
---|
265 | |
---|
266 | //if this command item is enabled |
---|
267 | if (jQuery("#"+link_id).hasClass("enabled")){ |
---|
268 | |
---|
269 | var selected_item_index = jQuery("#"+link_id).index(); |
---|
270 | |
---|
271 | if (selected_item_index > -1){ //if select item is valid |
---|
272 | |
---|
273 | //set the selected menu item |
---|
274 | oHandler.selectedIndex(selected_item_index); |
---|
275 | //show command help panel for this command |
---|
276 | wpide_function_help(); |
---|
277 | |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | }); |
---|
282 | |
---|
283 | |
---|
284 | jQuery("#ac_child").css("z-index", "9999"); |
---|
285 | jQuery("#ac_child").css("background-color", "#ffffff"); |
---|
286 | jQuery("#ac_msdd").css("z-index", "9999"); |
---|
287 | jQuery("#ac_msdd").css("position", "absolute"); |
---|
288 | jQuery("#ac_msdd").css("top", ac.style.top); |
---|
289 | jQuery("#ac_msdd").css("left", ac.style.left); |
---|
290 | |
---|
291 | //show command help panel for this command |
---|
292 | wpide_function_help(); |
---|
293 | |
---|
294 | } |
---|
295 | |
---|
296 | } |
---|
297 | |
---|
298 | function token_test(){ |
---|
299 | |
---|
300 | var iterator = new TokenIterator(editor.getSession(), range.start.row, range.start.column); |
---|
301 | var current_token_type = iterator.getCurrentToken().type; |
---|
302 | return iterator.getCurrentToken(); |
---|
303 | } |
---|
304 | |
---|
305 | function wpide_close_autocomplete(){ |
---|
306 | if (typeof document.getElementById('ac') != 'undefined') document.getElementById('ac').style.display='none'; |
---|
307 | if (typeof oHandler != 'undefined') oHandler.close(); |
---|
308 | |
---|
309 | autocompleting = false; |
---|
310 | |
---|
311 | //clear the text in the command help panel |
---|
312 | //jQuery("#wpide_info_content").html(""); |
---|
313 | } |
---|
314 | |
---|
315 | function selectionChanged(e) { |
---|
316 | var selected_text = editor.getSession().doc.getTextRange(editor.getSelectionRange()); |
---|
317 | |
---|
318 | //check for hex colour match |
---|
319 | if ( selected_text.match('^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$') != null ){ |
---|
320 | |
---|
321 | var therange = editor.getSelectionRange(); |
---|
322 | therange.end.column = therange.start.column; |
---|
323 | therange.start.column = therange.start.column-1; |
---|
324 | |
---|
325 | // only show color assist if the character before the selection indicates a hex color (#) |
---|
326 | if ( editor.getSession().doc.getTextRange( therange ) == "#" ){ |
---|
327 | jQuery("#wpide_color_assist").show(); |
---|
328 | } |
---|
329 | |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | function wpide_function_help() { |
---|
334 | //mouse over |
---|
335 | |
---|
336 | try |
---|
337 | { |
---|
338 | var selected_command_item = jQuery("#ac_child a.selected"); |
---|
339 | |
---|
340 | |
---|
341 | key = selected_command_item.find("span.ddTitleText").text().replace("()",""); |
---|
342 | |
---|
343 | //wordpress autocomplete |
---|
344 | if ( selected_command_item.find("img").attr("src").indexOf("wpac.png") >= 0){ |
---|
345 | |
---|
346 | if (autocomplete_wordpress[key].desc != undefined){ |
---|
347 | |
---|
348 | //compose the param info |
---|
349 | var param_text =""; |
---|
350 | for(i=0; i<autocomplete_wordpress[key].params.length; i++) { |
---|
351 | |
---|
352 | //wrap params in a span to highlight not required |
---|
353 | if (autocomplete_wordpress[key].params[i].required == "no"){ |
---|
354 | param_text = param_text + "<span class='wpide_func_arg_notrequired'>" + autocomplete_wordpress[key].params[i]['param'] + "<em>optional</em></span><br /> <br />"; |
---|
355 | }else{ |
---|
356 | param_text = param_text + autocomplete_wordpress[key].params[i]['param'] + "<br /> <br />"; |
---|
357 | } |
---|
358 | |
---|
359 | } |
---|
360 | //compose returns text |
---|
361 | if (autocomplete_wordpress[key].returns.length > 0){ |
---|
362 | returns_text = "<br /><br /><strong>Returns:</strong> " + autocomplete_wordpress[key].returns; |
---|
363 | }else{ |
---|
364 | returns_text = ""; |
---|
365 | } |
---|
366 | |
---|
367 | |
---|
368 | //output command info |
---|
369 | jQuery("#wpide_info_content").html( |
---|
370 | "<strong class='wpide_func_highlight_black'>Function: </strong><strong class='wpide_func_highlight'>" + key + "(</strong><br />" + |
---|
371 | "<span class='wpide_func_desc'>" + autocomplete_wordpress[key].desc + "</span><br /><br /><em class='wpide_func_params'>" + |
---|
372 | param_text + "</em>"+ |
---|
373 | "<strong class='wpide_func_highlight'>)</strong> " + |
---|
374 | returns_text + |
---|
375 | "<p><a href='http://codex.wordpress.org/Function_Reference/" + key + "' target='_blank'>See " + key + "() in the WordPress codex</a></p>" |
---|
376 | ); |
---|
377 | } |
---|
378 | |
---|
379 | } |
---|
380 | |
---|
381 | //php autocomplete |
---|
382 | if ( selected_command_item.find("img").attr("src").indexOf("phpac.png") >= 0){ |
---|
383 | |
---|
384 | if (autocomplete_php[key].returns != undefined){ |
---|
385 | |
---|
386 | //params text |
---|
387 | var param_text =""; |
---|
388 | for(i=0; i<autocomplete_php[key].params.length; i++) { |
---|
389 | |
---|
390 | //wrap params in a span to highlight not required |
---|
391 | if (autocomplete_php[key].params[i].required == "no"){ |
---|
392 | param_text = param_text + "<span class='wpide_func_arg_notrequired'>" + autocomplete_php[key].params[i]['param'] + "<em>optional</em></span><br /> <br />"; |
---|
393 | }else{ |
---|
394 | param_text = param_text + autocomplete_php[key].params[i]['param'] + "<br /> <br />"; |
---|
395 | } |
---|
396 | |
---|
397 | } |
---|
398 | //compose returns text |
---|
399 | if (autocomplete_php[key].returns.length > 0){ |
---|
400 | returns_text = "<br /><br /><strong>Returns:</strong> " + autocomplete_php[key].returns; |
---|
401 | }else{ |
---|
402 | returns_text = ""; |
---|
403 | } |
---|
404 | |
---|
405 | jQuery("#wpide_info_content").html( |
---|
406 | "<strong class='wpide_func_highlight_black'>Function: </strong><strong class='wpide_func_highlight'>" + key + "(</strong><br />" + |
---|
407 | autocomplete_php[key].desc + "<br /><br /><em class='wpide_func_params'>" + |
---|
408 | param_text + "</em>" + |
---|
409 | "<strong class='wpide_func_highlight'>)</strong>" + |
---|
410 | returns_text + |
---|
411 | "<p><a href='http://php.net/manual/en/function." + key.replace(/_/g, "-") + ".php' target='_blank'>See " + key + "() in the PHP manual</a></p>" |
---|
412 | ); |
---|
413 | |
---|
414 | } |
---|
415 | |
---|
416 | } |
---|
417 | |
---|
418 | |
---|
419 | |
---|
420 | } |
---|
421 | catch(err) |
---|
422 | { |
---|
423 | //Handle errors here |
---|
424 | } |
---|
425 | |
---|
426 | } |
---|
427 | |
---|
428 | //open another file and add to editor |
---|
429 | function wpide_set_file_contents(file, callback_func){ |
---|
430 | "use strict"; |
---|
431 | |
---|
432 | //ajax call to get file contents we are about to edit |
---|
433 | var data = { action: 'wpide_get_file', filename: file, _wpnonce: jQuery('#_wpnonce').val(), _wp_http_referer: jQuery('#_wp_http_referer').val() }; |
---|
434 | |
---|
435 | jQuery.post(ajaxurl, data, function(response) { |
---|
436 | var the_path = file.replace(/^.*[\\\/]/, ''); |
---|
437 | var the_id = "wpide_tab_" + last_added_editor_session; |
---|
438 | |
---|
439 | //enable editor now we have a file open |
---|
440 | jQuery('#fancyeditordiv textarea').removeAttr("disabled"); |
---|
441 | |
---|
442 | jQuery("#wpide_toolbar_tabs").append('<span id="'+the_id+'" sessionrel="'+last_added_editor_session+'" title=" '+file+' " rel="'+file+'" class="wpide_tab">'+ the_path +'</a> <a class="close_tab" href="#">x</a> '); |
---|
443 | |
---|
444 | saved_editor_sessions[last_added_editor_session] = new EditSession(response);//set saved session |
---|
445 | saved_editor_sessions[last_added_editor_session].on('change', onSessionChange); |
---|
446 | saved_undo_manager[last_added_editor_session] = new UndoManager(editor.getSession().getUndoManager());//new undo manager for this session |
---|
447 | |
---|
448 | last_added_editor_session++; //increment session counter |
---|
449 | |
---|
450 | //add click event for the new tab. |
---|
451 | //We are actually clearing the click event and adding it again for all tab elements, it's the only way I could get the click handler listening on all dynamically added tabs |
---|
452 | jQuery(".wpide_tab").off('click').on("click", function(event){ |
---|
453 | event.preventDefault(); |
---|
454 | |
---|
455 | jQuery('input[name=filename]').val( jQuery(this).attr('rel') ); |
---|
456 | |
---|
457 | //save current editor into session |
---|
458 | //get old editor out of session and apply to editor |
---|
459 | var clicksesh = jQuery(this).attr('sessionrel'); //editor id number |
---|
460 | saved_editor_sessions[ clicksesh ].setUndoManager(saved_undo_manager[ clicksesh ]); |
---|
461 | editor.setSession( saved_editor_sessions[ clicksesh ] ); |
---|
462 | |
---|
463 | //set this tab as active |
---|
464 | jQuery(".wpide_tab").removeClass('active'); |
---|
465 | jQuery(this).addClass('active'); |
---|
466 | |
---|
467 | var currentFilename = jQuery(this).attr('rel'); |
---|
468 | var mode; |
---|
469 | |
---|
470 | //turn autocomplete off initially, then enable as needed |
---|
471 | editor.getSession().enable_autocomplete = false; |
---|
472 | |
---|
473 | //set the editor mode based on file name |
---|
474 | if (/\.css$/.test(currentFilename)) { |
---|
475 | mode = require("ace/mode/css").Mode; |
---|
476 | } |
---|
477 | else if (/\.less$/.test(currentFilename)) { |
---|
478 | mode = require("ace/mode/css").Mode; |
---|
479 | } |
---|
480 | else if (/\.js$/.test(currentFilename)) { |
---|
481 | mode = require("ace/mode/javascript").Mode; |
---|
482 | } |
---|
483 | else { |
---|
484 | mode = require("ace/mode/php").Mode; //default to php |
---|
485 | //only enable session change / auto complete if needed |
---|
486 | editor.getSession().enable_autocomplete = true; |
---|
487 | } |
---|
488 | editor.getSession().setMode(new mode()); |
---|
489 | |
---|
490 | editor.getSession().on('change', onSessionChange); |
---|
491 | |
---|
492 | editor.getSession().selection.on('changeSelection', selectionChanged); |
---|
493 | |
---|
494 | editor.resize(); |
---|
495 | editor.focus(); |
---|
496 | //make a note of current editor |
---|
497 | current_editor_session = clicksesh; |
---|
498 | |
---|
499 | }); |
---|
500 | |
---|
501 | //add click event for tab close. |
---|
502 | //We are actually clearing the click event and adding it again for all tab elements, it's the only way I could get the click handler listening on all dynamically added tabs |
---|
503 | jQuery(".close_tab").off('click').on("click", function(event){ |
---|
504 | event.preventDefault(); |
---|
505 | var clicksesh = jQuery(this).parent().attr('sessionrel'); |
---|
506 | var activeFallback; |
---|
507 | |
---|
508 | //if the currently selected tab is being removed then remember to make the first tab active |
---|
509 | if ( jQuery("#wpide_tab_"+clicksesh).hasClass('active') ) { |
---|
510 | activeFallback = true; |
---|
511 | }else{ |
---|
512 | activeFallback = false; |
---|
513 | } |
---|
514 | |
---|
515 | //remove tab |
---|
516 | jQuery(this).parent().remove(); |
---|
517 | |
---|
518 | //clear session and undo |
---|
519 | saved_undo_manager[clicksesh] = undefined; |
---|
520 | saved_editor_sessions[clicksesh] = undefined; |
---|
521 | |
---|
522 | //Clear the active editor if all tabs closed or activate first tab if required since the active tab may have been deleted |
---|
523 | if (jQuery(".wpide_tab").length == 0){ |
---|
524 | editor.getSession().setValue( "" ); |
---|
525 | }else if ( activeFallback ){ |
---|
526 | jQuery( "#" + jQuery(".wpide_tab")[0].id ).click(); |
---|
527 | } |
---|
528 | |
---|
529 | }); |
---|
530 | |
---|
531 | jQuery("#"+the_id).click(); |
---|
532 | |
---|
533 | if (callback_func != null) { |
---|
534 | callback_func(response); |
---|
535 | } |
---|
536 | |
---|
537 | }); |
---|
538 | |
---|
539 | |
---|
540 | } |
---|
541 | |
---|
542 | function saveDocument() { |
---|
543 | //ajax call to save the file and generate a backup if needed |
---|
544 | var data = { action: 'wpide_save_file', filename: jQuery('input[name=filename]').val(), _wpnonce: jQuery('#_wpnonce').val(), _wp_http_referer: jQuery('#_wp_http_referer').val(), content: editor.getSession().getValue() }; |
---|
545 | jQuery.post(ajaxurl, data, function(response) { |
---|
546 | if (response === 'success') { |
---|
547 | jQuery("#wpide_message").html('<span>File saved.</span>'); |
---|
548 | jQuery("#wpide_message").show(); |
---|
549 | jQuery("#wpide_message").fadeOut(5000); |
---|
550 | } else { |
---|
551 | alert("error: " + response); |
---|
552 | } |
---|
553 | }); |
---|
554 | } |
---|
555 | |
---|
556 | //enter/return command |
---|
557 | function selectACitem (item) { |
---|
558 | if( document.getElementById('ac').style.display === 'block' && oHandler.visible() == 'block' ){ |
---|
559 | var ac_dropdwn = document.getElementById('ac'); |
---|
560 | var tag = ac_dropdwn.options[ac_dropdwn.selectedIndex].value; |
---|
561 | var sel = editor.selection.getRange(); |
---|
562 | var line = editor.getSession().getLine(sel.start.row); |
---|
563 | sel.start.column = sel.start.column - autocompletelength; |
---|
564 | |
---|
565 | if (item.length){ |
---|
566 | tag = item; //get tag from new msdropdown passed as arg |
---|
567 | }else{ |
---|
568 | tag = jQuery("#ac_msdd a.selected").children("span.ddTitleText").text(); //get tag from new msdropdown |
---|
569 | } |
---|
570 | |
---|
571 | //clean up the tag/command |
---|
572 | tag = tag.replace(")", ""); //remove end parenthesis |
---|
573 | |
---|
574 | //console.log(tag); |
---|
575 | editor.selection.setSelectionRange(sel); |
---|
576 | editor.insert(tag); |
---|
577 | |
---|
578 | wpide_close_autocomplete(); |
---|
579 | } else { |
---|
580 | editor.insert('\n'); |
---|
581 | } |
---|
582 | } |
---|
583 | |
---|
584 | |
---|
585 | jQuery(document).ready(function($) { |
---|
586 | $("#wpide_save").click(saveDocument); |
---|
587 | |
---|
588 | // drag and drop colour picker image |
---|
589 | $("#wpide_color_assist").on('drop', function(e) { |
---|
590 | e.preventDefault(); |
---|
591 | e.originalEvent.dataTransfer.items[0].getAsString(function(url){ |
---|
592 | |
---|
593 | $(".ImageColorPickerCanvas", $("#side-info-column") ).remove(); |
---|
594 | $("img", $("#wpide_color_assist")).attr('src', url ); |
---|
595 | |
---|
596 | }); |
---|
597 | }); |
---|
598 | |
---|
599 | $("#wpide_color_assist").on('dragover', function(e) { |
---|
600 | $(this).addClass("hover"); |
---|
601 | }).on('dragleave', function(e) { |
---|
602 | $(this).removeClass("hover"); |
---|
603 | }); |
---|
604 | |
---|
605 | |
---|
606 | //add div for ace editor to latch on to |
---|
607 | $('#template').prepend("<div style='width:80%;height:500px;margin-right:0!important;' id='fancyeditordiv'></div>"); |
---|
608 | //create the editor instance |
---|
609 | editor = ace.edit("fancyeditordiv"); |
---|
610 | //set the editor theme |
---|
611 | editor.setTheme("ace/theme/dawn"); |
---|
612 | //get a copy of the initial file contents (the file being edited) |
---|
613 | //var intialData = $('#newcontent').val() |
---|
614 | var intialData = "Use the file manager to find a file you wish edit, click the file name to edit. \n\n"; |
---|
615 | |
---|
616 | |
---|
617 | //startup info - usefull for debugging |
---|
618 | var data = { action: 'wpide_startup_check', _wpnonce: jQuery('#_wpnonce').val(), _wp_http_referer: jQuery('#_wp_http_referer').val() }; |
---|
619 | |
---|
620 | jQuery.post(ajaxurl, data, function(response) { |
---|
621 | if (response == "-1"){ |
---|
622 | intialData = intialData + "Permission/security problem with ajax request. Refresh WPide and try again. \n\n"; |
---|
623 | }else{ |
---|
624 | intialData = intialData + response; |
---|
625 | } |
---|
626 | |
---|
627 | editor.getSession().setValue( intialData ); |
---|
628 | |
---|
629 | }); |
---|
630 | |
---|
631 | |
---|
632 | |
---|
633 | //make initial editor read only |
---|
634 | $('#fancyeditordiv textarea').attr("disabled", "disabled"); |
---|
635 | |
---|
636 | //use editors php mode |
---|
637 | var phpMode = require("ace/mode/php").Mode; |
---|
638 | editor.getSession().setMode(new phpMode()); |
---|
639 | |
---|
640 | //START AUTOCOMPLETE |
---|
641 | //create the autocomplete dropdown |
---|
642 | var ac = document.createElement('select'); |
---|
643 | ac.id = 'ac'; |
---|
644 | ac.name = 'ac'; |
---|
645 | ac.style.position='absolute'; |
---|
646 | ac.style.zIndex=100; |
---|
647 | ac.style.width='auto'; |
---|
648 | ac.style.display='none'; |
---|
649 | ac.style.height='auto'; |
---|
650 | ac.size=10; |
---|
651 | editor.container.appendChild(ac); |
---|
652 | |
---|
653 | //hook onto any change in editor contents |
---|
654 | editor.getSession().on('change', onSessionChange);//end editor change event |
---|
655 | |
---|
656 | |
---|
657 | |
---|
658 | //START COMMANDS |
---|
659 | |
---|
660 | //Key up command |
---|
661 | editor.commands.addCommand({ |
---|
662 | name: "up", |
---|
663 | bindKey: { |
---|
664 | win: "Up", |
---|
665 | mac: "Up", |
---|
666 | sender: "editor" |
---|
667 | }, |
---|
668 | |
---|
669 | exec: function(env, args, request) { |
---|
670 | if (oHandler && oHandler.visible() === 'block'){ |
---|
671 | oHandler.previous(); |
---|
672 | |
---|
673 | //show command help panel for this command |
---|
674 | wpide_function_help(); |
---|
675 | //console.log("handler is visible"); |
---|
676 | |
---|
677 | }else if( document.getElementById('ac').style.display === 'block' ) { |
---|
678 | var select=document.getElementById('ac'); |
---|
679 | if( select.selectedIndex === 0 ) { |
---|
680 | select.selectedIndex = select.options.length-1; |
---|
681 | } else { |
---|
682 | select.selectedIndex = select.selectedIndex-1; |
---|
683 | } |
---|
684 | //console.log("ac is visible"); |
---|
685 | } else { |
---|
686 | var range = editor.getSelectionRange(); |
---|
687 | editor.clearSelection(); |
---|
688 | editor.moveCursorTo(range.end.row - 1, range.end.column); |
---|
689 | } |
---|
690 | } |
---|
691 | }); |
---|
692 | |
---|
693 | |
---|
694 | //key down command |
---|
695 | editor.commands.addCommand({ |
---|
696 | name: "down", |
---|
697 | bindKey: { |
---|
698 | win: "Down", |
---|
699 | mac: "Down", |
---|
700 | sender: "editor" |
---|
701 | }, |
---|
702 | exec: function(env, args, request) { |
---|
703 | |
---|
704 | if (oHandler && oHandler.visible() === 'block'){ |
---|
705 | oHandler.next(); |
---|
706 | |
---|
707 | //show command help panel for this command |
---|
708 | wpide_function_help(); |
---|
709 | |
---|
710 | }else if ( document.getElementById('ac').style.display === 'block' ) { |
---|
711 | var select=document.getElementById('ac'); |
---|
712 | if ( select.selectedIndex === select.options.length-1 ) { |
---|
713 | select.selectedIndex=0; |
---|
714 | } else { |
---|
715 | select.selectedIndex=select.selectedIndex+1; |
---|
716 | } |
---|
717 | } else { |
---|
718 | var range = editor.getSelectionRange(); |
---|
719 | editor.clearSelection(); |
---|
720 | editor.moveCursorTo(range.end.row +1, range.end.column); |
---|
721 | } |
---|
722 | } |
---|
723 | }); |
---|
724 | |
---|
725 | |
---|
726 | |
---|
727 | editor.commands.addCommand({ |
---|
728 | name: "enter", |
---|
729 | bindKey: { |
---|
730 | win: "Return", |
---|
731 | mac: "Return", |
---|
732 | sender: "editor" |
---|
733 | }, |
---|
734 | exec: selectACitem |
---|
735 | }); |
---|
736 | |
---|
737 | // save command: |
---|
738 | editor.commands.addCommand({ |
---|
739 | name: "save", |
---|
740 | bindKey: { |
---|
741 | win: "Ctrl-S", |
---|
742 | mac: "Command-S", |
---|
743 | sender: "editor" |
---|
744 | }, |
---|
745 | exec: saveDocument |
---|
746 | }); |
---|
747 | |
---|
748 | //END COMMANDS |
---|
749 | |
---|
750 | |
---|
751 | //click action for new directory/file submit link |
---|
752 | $("#wpide_create_new_directory, #wpide_create_new_file").click(function(e){ |
---|
753 | e.preventDefault(); |
---|
754 | |
---|
755 | var data_input = jQuery(this).parent().find("input.has_data"); |
---|
756 | var item = eval('('+ data_input.attr("rel") +')'); |
---|
757 | |
---|
758 | //item.path file|directory |
---|
759 | var data = { action: 'wpide_create_new', path: item.path, type: item.type, file: data_input.val(), _wpnonce: jQuery('#_wpnonce').val(), _wp_http_referer: jQuery('#_wp_http_referer').val() }; |
---|
760 | |
---|
761 | jQuery.post(ajaxurl, data, function(response) { |
---|
762 | |
---|
763 | if (response == "1"){ |
---|
764 | //remove the file/dir name from the text input |
---|
765 | data_input.val(""); |
---|
766 | |
---|
767 | if ( jQuery("ul.jqueryFileTree a[rel='"+ item.path +"']").length == 0){ |
---|
768 | |
---|
769 | //if no parent then we are adding something to the wp-content folder so regenerate the whole filetree |
---|
770 | the_filetree(); |
---|
771 | |
---|
772 | } |
---|
773 | |
---|
774 | //click the parent once to hide |
---|
775 | jQuery("ul.jqueryFileTree a[rel='"+ item.path +"']").click(); |
---|
776 | |
---|
777 | //hide the parent input block |
---|
778 | data_input.parent().hide(); |
---|
779 | |
---|
780 | //click the parent once again to show with new folder and focus on this area |
---|
781 | jQuery("ul.jqueryFileTree a[rel='"+ item.path +"']").click(); |
---|
782 | jQuery("ul.jqueryFileTree a[rel='"+ item.path +"']").focus(); |
---|
783 | |
---|
784 | }else if (response == "-1"){ |
---|
785 | alert("Permission/security problem. Refresh WPide and try again."); |
---|
786 | }else{ |
---|
787 | alert(response); |
---|
788 | } |
---|
789 | |
---|
790 | |
---|
791 | }); |
---|
792 | |
---|
793 | }); |
---|
794 | |
---|
795 | });//end jquery load |
---|