1/**
2 * Copyright (c) 2014-2016, CKSource - Frederico Knabben. All rights reserved.
3 * Licensed under the terms of the MIT License (see LICENSE.md).
4 *
5 * The abbr plugin dialog window definition.
6 *
7 * Created out of the CKEditor Plugin SDK:
8 * http://docs.ckeditor.com/#!/guide/plugin_sdk_sample_1
9 */
10
11// Our dialog definition.
12CKEDITOR.dialog.add( 'geshiDialog', function( editor ) {
13    var radio, ckg_geshi_langopts = new Array();
14    var href, id,geshi_dialog, t_display,s_display;
15
16    var  getHref = function() {
17       var data = window.location.pathname;
18       var qs = window.location.search;
19       var matches = data.match(/\/(.*?)\/(doku.php)?\/?(.*)/);
20
21       if(qs_match = qs.match(/id=([\w:_\.]+)\b/)) { //none
22           id = qs_match[1];
23           href = matches[0];
24  }
25       else if(!matches[2])
26       {
27           id = matches[3];
28           href = matches[1] + '/doku.php';
29       }
30       else {
31          id = matches[3];
32          href = matches[2] + '/doku.php';
33       }
34       if(!href) href='doku.php';
35       if(!id) id = 'start';
36        return {'href':href, 'id':id};
37  };
38
39    var downloadable_header = function(type,fname) {
40    var id = 'start';
41    var file = fname ? fname: 'temp.' + type;
42    var href_vals = getHref();
43    return  '<dl class="file">'
44    +'<dt><a href="' + href_vals.href + '?do=export_code&id=' + href_vals.id+ '&codeblock=0" title="Download Snippet" class="mediafile mf_' + type +'">' +file +'</a></dt> <dd><pre class="file ' + type+ '">';
45 };
46
47    var downloadable_footer = function() {
48    return "</pre> </dd></dl>";
49  }
50
51
52     ckg_geshi_langopts = editor.config.geshi_opts;
53     if(!ckg_geshi_langopts.match(/ENotfound/)) {
54        ckg_geshi_langopts = ckg_geshi_langopts.split(';;');
55        var tmp;
56        for(var i=0; i<ckg_geshi_langopts.length; i++) {
57            tmp = ckg_geshi_langopts[i] ;
58            ckg_geshi_langopts[i] = new Array(tmp);
59    }
60      ckg_geshi_langopts.unshift(['Not Set']);
61        s_display = 'display:block';
62        t_display = 'display:none';
63    }
64    else {
65        t_display = 'display:inline';
66        s_display = 'display:none';
67        ckg_geshi_langopts = [];
68    }
69	return {
70		// Basic properties of the dialog window: title, minimum size.
71		title: editor.lang.geshi.window_title,
72		minWidth: 600,
73		minHeight: 350,
74
75		// Dialog window content definition.
76		contents: [
77			{
78				// Definition of the Basic Settings dialog tab (page).
79				id: 'tab-basic',
80				label: editor.lang.geshi.basic_settings,
81
82				// The tab content.
83				elements: [
84					{
85						// Text input field for the abbreviation text.
86						type: 'textarea',
87                        rows:18,
88                        cols:  80,
89						id: 'geshi',
90						label: editor.lang.geshi.code,
91						// Validation checking whether the field is not empty.
92						validate: CKEDITOR.dialog.validate.notEmpty(editor.lang.geshi.code_empty)
93					},
94                    {
95                        type: 'hbox',
96                        widths: [ '33%', '33%','33%'],
97                        children: [
98                                        {
99                                            type: 'select',
100                                             id: 'ckg_geshi_lang',
101                                             label: "Select language", //editor.lang.geshi.lang,
102                                              items:  ckg_geshi_langopts,
103                                              'default':ckg_geshi_langopts[0],
104                                              style:  s_display,
105                                              onChange: function( api ) {
106                                                //  geshi_dialog.getContentElement(  'tab-basic', 'language' ).setValue(this.getValue());
107                                             }
108                                         },
109                                        {
110                                            type: 'text',
111                                            id: 'language',
112                                            label:   "<html><span title='"+editor.lang.geshi.tooltip+"' style = 'color:blue;text-decoration:underline;'  onmouseover='this.style.cursor=\"pointer\";'>" + editor.lang.geshi.quick_srch+"</span></html>", //editor.lang.geshi.lang || 'Programming Language',
113                                            width: '125px',
114                                             onChange: function( api ) {
115                                                 var srch = this.getValue().toLowerCase();
116                                                 srch = srch.escapeRegExpCkg(srch);
117                                                   var regex = new RegExp('^' +srch);
118                                                    for(var i = 1; i< ckg_geshi_langopts.length; i++) {
119                                                            if(regex.test(ckg_geshi_langopts[i])) {
120                                                                 srch=ckg_geshi_langopts[i];
121                                                                 break;
122                                                             }
123                                                    }
124                                                    if(srch)  geshi_dialog.getContentElement(  'tab-basic', 'ckg_geshi_lang' ).setValue(srch);
125                                             }
126                                        },
127
128                                        {
129                                            type: 'text',
130                                            id: 'file',
131                                            label: editor.lang.geshi.file || 'File name',
132                                            width: '175px',
133                                        },
134                                        {
135                                            type: 'radio',
136                                            id: 'which',
137                                            label: editor.lang.geshi.which,
138                                            items: [ [ editor.lang.geshi.codeblock, 'block' ], [ editor.lang.geshi.snippet, 'snippet' ], [ editor.lang.geshi.plain_text, 'text' ]],
139                                            'default': 'block',
140                                            style: 'color: green',
141                                              onClick: function() {
142                                                 radio = this.getValue();
143                                            }
144                                        },
145                        ]          //hbox children
146                    },            //hbox
147
148                       {
149                        type: 'hbox',
150                        widths: [ '60%','40%'],
151                        children: [
152                                        {
153                                            type: 'radio',
154                                            id: 'numbers',
155                                            label: editor.lang.geshi.numbers_header||'For syntax numbering, select type',
156                                             items: [ [ editor.lang.geshi.nonumbers, 'nonumbers' ], [ editor.lang.geshi.numonly, 'numonly' ], [ editor.lang.geshi.startat, 'startat' ], [ editor.lang.geshi.xtra, 'xtra' ]],
157                                            'default': 'nonumbers',
158                                            style: 'color: green',
159                                              onClick: function() {
160                                                 radio = this.getValue();
161                                            }
162                                        },
163                                            {
164                                            type: 'text',
165                                            id: 'linenums',
166                                            label: editor.lang.geshi.linenums ||'Line number or numbers: line n or n1,n2..',
167                                            width: '175px',
168                                        },
169                       ]
170                     },//hbox
171
172				]  //elements
173			},  //contents
174
175		], //contents
176
177       onShow : function()
178       {
179             var dialog = this;
180             geshi_dialog = dialog;
181             selection = editor.getSelection();
182             var text = selection.getSelectedText();
183             dialog.getContentElement(  'tab-basic', 'geshi' ).setValue( text );
184             String.prototype.escapeRegExpCkg = function(str) {
185                   return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
186             };
187         //   dialog.getContentElement(  'tab-basic', 'language' ).style='display:block';
188          //  alert( 'Current value: ' + dialog.getValueOf( 'tab-basic', 'ckg_geshi_lang' ) );
189       },
190
191
192		onOk: function() {
193			// The context of this function is the dialog object itself.
194			// http://docs.ckeditor.com/#!/api/CKEDITOR.dialog
195
196			 var dialog = this, retval;
197	         var text = dialog.getValueOf( 'tab-basic', 'geshi' );
198			 text = text.replace(/</gm,'&lt;');
199			 text = text.replace(/>/gm,'&gt;');
200             var which = dialog.getValueOf( 'tab-basic', 'which' );
201             var highlight = dialog.getValueOf( 'tab-basic', 'numbers' );
202			 var hiliting = '/*[enable_line_numbers="true"';
203			 if(highlight != 'nonumbers') {//'numonly'
204				 if(highlight == 'numonly'){
205			        text = "\n" + hiliting +']' +"*/\n" + text;
206				 }
207				 else if(highlight == 'startat'){
208					var num =  dialog.getValueOf( 'tab-basic', 'linenums' );
209				    text = "\n" + hiliting + ', start_line_numbers_at="'+num +'"]' +"*/\n" + text;
210				 }
211				 else {  //hilite individual lines
212					 var num =  dialog.getValueOf( 'tab-basic', 'linenums' );
213					 text = "\n" + hiliting + ', highlight_lines_extra="'+num +'"]' +"*/\n" + text;
214				 }
215			 }
216
217            var p_lang = dialog.getValueOf( 'tab-basic', 'ckg_geshi_lang' );
218            if(p_lang.match(/Not Set/i)) {
219				         if(which == 'text') {
220						     p_lang = 'text';
221						 }
222                         else p_lang = "";
223              }
224
225            if(!p_lang) {
226                    if(confirm('Language not found. Try again?'))  {
227                         return false;
228                    }
229                }
230
231             if(p_lang) {
232                 if(p_lang == 'text') {
233					 text = text.replace(/^(.*?)\n$/gm,"<p>$1</p>");
234					 retval = text;
235				 }
236                 else if(which == 'block') {
237                     retval = '<pre class="code ' + p_lang+ '">' + text + '</pre>';
238                 }
239                 else retval = downloadable_header(p_lang,dialog.getValueOf( 'tab-basic', 'file' ) ) + text + downloadable_footer();
240                 editor.insertHtml(retval);
241           }
242
243		}
244	};
245});
246