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     ckg_geshi_langopts = editor.config.geshi_opts;
52     if(!ckg_geshi_langopts.match(/ENotfound/)) {
53        ckg_geshi_langopts = ckg_geshi_langopts.split(';;');
54        var tmp;
55        for(var i=0; i<ckg_geshi_langopts.length; i++) {
56            tmp = ckg_geshi_langopts[i] ;
57            ckg_geshi_langopts[i] = new Array(tmp);
58    }
59      ckg_geshi_langopts.unshift(['Not Set']);
60        s_display = 'display:block';
61        t_display = 'display:none';
62    }
63    else {
64        t_display = 'display:inline';
65        s_display = 'display:none';
66        ckg_geshi_langopts = [];
67    }
68	return {
69		// Basic properties of the dialog window: title, minimum size.
70		title: 'Abbreviation Properties',
71		minWidth: 600,
72		minHeight: 350,
73
74		// Dialog window content definition.
75		contents: [
76			{
77				// Definition of the Basic Settings dialog tab (page).
78				id: 'tab-basic',
79				label: 'Basic Settings',
80
81				// The tab content.
82				elements: [
83					{
84						// Text input field for the abbreviation text.
85						type: 'textarea',
86                        rows:18,
87                        cols:  80,
88						id: 'geshi',
89						label: editor.lang.geshi.code,
90						// Validation checking whether the field is not empty.
91						validate: CKEDITOR.dialog.validate.notEmpty(editor.lang.geshi.code_empty)
92					},
93                    {
94                        type: 'hbox',
95                        widths: [ '33%', '33%','33%'],
96                        children: [
97                                        {
98                                            type: 'select',
99                                             id: 'ckg_geshi_lang',
100                                             label: "Select language", //editor.lang.geshi.lang,
101                                              items:  ckg_geshi_langopts,
102                                              'default':ckg_geshi_langopts[0],
103                                              style:  s_display,
104                                              onChange: function( api ) {
105                                                //  geshi_dialog.getContentElement(  'tab-basic', 'language' ).setValue(this.getValue());
106                                             }
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                                            type: 'text',
129                                            id: 'file',
130                                            label: editor.lang.geshi.file || 'File name',
131                                            width: '175px',
132                                        },
133                                        {
134                                            type: 'radio',
135                                            id: 'which',
136                                            label: editor.lang.geshi.which,
137                                            items: [ [ editor.lang.geshi.codeblock, 'block' ], [ editor.lang.geshi.snippet, 'snippet' ] ],
138                                            'default': 'block',
139                                            style: 'color: green',
140                                              onClick: function() {
141                                                 radio = this.getValue();
142                                            }
143                                        },
144                        ]          //hbox children
145                    },            //hbox
146
147				]  //elements
148			},  //contents
149
150		], //contents
151
152       onShow : function()
153       {
154            var dialog = this;
155            geshi_dialog = dialog;
156             selection = editor.getSelection();
157             var text = selection.getSelectedText();
158            dialog.getContentElement(  'tab-basic', 'geshi' ).setValue( text );
159             String.prototype.escapeRegExpCkg = function(str) {
160                   return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
161             };
162         //   dialog.getContentElement(  'tab-basic', 'language' ).style='display:block';
163          //  alert( 'Current value: ' + dialog.getValueOf( 'tab-basic', 'ckg_geshi_lang' ) );
164       },
165
166
167		onOk: function() {
168			// The context of this function is the dialog object itself.
169			// http://docs.ckeditor.com/#!/api/CKEDITOR.dialog
170
171			var dialog = this, retval;
172	         var text = dialog.getValueOf( 'tab-basic', 'geshi' );
173             var which = dialog.getValueOf( 'tab-basic', 'which' );
174
175             var p_lang = dialog.getValueOf( 'tab-basic', 'ckg_geshi_lang' );
176            if(p_lang.match(/Not Set/i)) {
177                         p_lang = "";
178              }
179
180            if(!p_lang) {
181                    if(confirm('Language not found. Try again?'))  {
182                         return false;
183                    }
184                }
185
186             if(p_lang) {
187             if(which == 'block') {
188                 retval = '<pre class="code ' + p_lang+ '">' + text + '</pre>';
189             }
190             else retval = downloadable_header(p_lang,dialog.getValueOf( 'tab-basic', 'file' ) ) + text + downloadable_footer();
191             editor.insertHtml(retval);
192           }
193
194		}
195	};
196});
197