Aleksander Machniak
2015-08-16 3fa3f156cae81ef6c453ee3969b95a0b77bb1824
commit | author | age
b34d67 1 /**
TB 2  * Roundcube SpellCheck script
3  *
4  * jQuery'fied spell checker based on GoogieSpell 4.0
5  * (which was published under GPL "version 2 or any later version")
6  *
7  * @licstart  The following is the entire license notice for the
8  * JavaScript code in this file.
9  *
10  * Copyright (C) 2006 Amir Salihefendic
11  * Copyright (C) 2009 The Roundcube Dev Team
12  * Copyright (C) 2011 Kolab Systems AG
13  *
14  * The JavaScript code in this page is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU
16  * General Public License (GNU GPL) as published by the Free Software
17  * Foundation, either version 3 of the License, or (at your option)
18  * any later version.  The code is distributed WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.
21  *
22  * As additional permission under GNU GPL version 3 section 7, you
23  * may distribute non-source (e.g., minimized or compacted) forms of
24  * that code without the copy of the GNU GPL normally required by
25  * section 4, provided you include this license notice and a URL
26  * through which recipients can access the Corresponding Source.
27  *
28  * @licend  The above is the entire license notice
29  * for the JavaScript code in this file.
30  *
31  * @author 4mir Salihefendic <amix@amix.dk> 
32  * @author Aleksander Machniak - <alec [at] alec.pl>
33  */
309d2f 34
644e3a 35 var GOOGIE_CUR_LANG,
A 36     GOOGIE_DEFAULT_LANG = 'en';
dd53e2 37
66df08 38 function GoogieSpell(img_dir, server_url, has_dict)
A 39 {
ae8a2a 40     var ref = this,
ae7027 41         cookie_value = rcmail.get_cookie('language');
ae8a2a 42
A 43     GOOGIE_CUR_LANG = cookie_value != null ? cookie_value : GOOGIE_DEFAULT_LANG;
dd53e2 44
309d2f 45     this.array_keys = function(arr) {
e24eba 46         var res = [];
AM 47         for (var key in arr) { res.push([key]); }
48         return res;
309d2f 49     }
dd53e2 50
ffa6c1 51     this.img_dir = img_dir;
T 52     this.server_url = server_url;
dd53e2 53
309d2f 54     this.org_lang_to_word = {
e24eba 55         "da": "Dansk", "de": "Deutsch", "en": "English",
2b4855 56         "es": "Español", "fr": "Français", "it": "Italiano",
A 57         "nl": "Nederlands", "pl": "Polski", "pt": "Português",
58         "ru": "Русский", "fi": "Suomi", "sv": "Svenska"
309d2f 59     };
ffa6c1 60     this.lang_to_word = this.org_lang_to_word;
309d2f 61     this.langlist_codes = this.array_keys(this.lang_to_word);
ffa6c1 62     this.show_change_lang_pic = true;
309d2f 63     this.change_lang_pic_placement = 'right';
ffa6c1 64     this.report_state_change = true;
dd53e2 65
ffa6c1 66     this.ta_scroll_top = 0;
T 67     this.el_scroll_top = 0;
dd53e2 68
ffa6c1 69     this.lang_chck_spell = "Check spelling";
T 70     this.lang_revert = "Revert to";
71     this.lang_close = "Close";
72     this.lang_rsm_edt = "Resume editing";
73     this.lang_no_error_found = "No spelling errors found";
74     this.lang_no_suggestions = "No suggestions";
66df08 75     this.lang_learn_word = "Add to dictionary";
ae8a2a 76
309d2f 77     this.show_spell_img = false; // roundcube mod.
ffa6c1 78     this.decoration = true;
f48a94 79     this.use_close_btn = false;
ffa6c1 80     this.edit_layer_dbl_click = true;
T 81     this.report_ta_not_found = true;
dd53e2 82
ae8a2a 83     // Extensions
ffa6c1 84     this.custom_ajax_error = null;
T 85     this.custom_no_spelling_error = null;
ae8a2a 86     this.custom_menu_builder = []; // Should take an eval function and a build menu function
A 87     this.custom_item_evaulator = null; // Should take an eval function and a build menu function
ffa6c1 88     this.extra_menu_items = [];
T 89     this.custom_spellcheck_starter = null;
90     this.main_controller = true;
66df08 91     this.has_dictionary = has_dict;
dd53e2 92
ae8a2a 93     // Observers
ffa6c1 94     this.lang_state_observer = null;
T 95     this.spelling_state_observer = null;
96     this.show_menu_observer = null;
97     this.all_errors_fixed_observer = null;
dd53e2 98
ae8a2a 99     // Focus links - used to give the text box focus
ffa6c1 100     this.use_focus = false;
T 101     this.focus_link_t = null;
102     this.focus_link_b = null;
103
ae8a2a 104     // Counters
ffa6c1 105     this.cnt_errors = 0;
T 106     this.cnt_errors_fixed = 0;
ae8a2a 107
A 108     // Set document's onclick to hide the language and error menu
309d2f 109     $(document).bind('click', function(e) {
48e8b3 110         var target = $(e.target);
A 111         if(target.attr('googie_action_btn') != '1' && ref.isLangWindowShown())
e24eba 112             ref.hideLangWindow();
AM 113         if(target.attr('googie_action_btn') != '1' && ref.isErrorWindowShown())
309d2f 114             ref.hideErrorWindow();
A 115     });
dd53e2 116
T 117
66df08 118 this.decorateTextarea = function(id)
A 119 {
ef4f59 120     this.text_area = typeof id === 'string' ? document.getElementById(id) : id;
dd53e2 121
309d2f 122     if (this.text_area) {
A 123         if (!this.spell_container && this.decoration) {
ae8a2a 124             var table = document.createElement('table'),
A 125                 tbody = document.createElement('tbody'),
126                 tr = document.createElement('tr'),
127                 spell_container = document.createElement('td'),
128                 r_width = this.isDefined(this.force_width) ? this.force_width : this.text_area.offsetWidth,
129                 r_height = this.isDefined(this.force_height) ? this.force_height : 16;
dd53e2 130
ffa6c1 131             tr.appendChild(spell_container);
T 132             tbody.appendChild(tr);
309d2f 133             $(table).append(tbody).insertBefore(this.text_area).width('100%').height(r_height);
A 134             $(spell_container).height(r_height).width(r_width).css('text-align', 'right');
ffa6c1 135
309d2f 136             this.spell_container = spell_container;
ffa6c1 137         }
T 138
139         this.checkSpellingState();
dd53e2 140     }
b8d4fe 141     else if (this.report_ta_not_found)
A 142         alert('Text area not found');
ae8a2a 143 };
dd53e2 144
ffa6c1 145 //////
T 146 // API Functions (the ones that you can call)
147 /////
66df08 148 this.setSpellContainer = function(id)
A 149 {
ef4f59 150     this.spell_container = typeof id === 'string' ? document.getElementById(id) : id;
ae8a2a 151 };
dd53e2 152
66df08 153 this.setLanguages = function(lang_dict)
A 154 {
ffa6c1 155     this.lang_to_word = lang_dict;
309d2f 156     this.langlist_codes = this.array_keys(lang_dict);
ae8a2a 157 };
dd53e2 158
66df08 159 this.setCurrentLanguage = function(lan_code)
A 160 {
309d2f 161     GOOGIE_CUR_LANG = lan_code;
A 162
163     //Set cookie
164     var now = new Date();
165     now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
ae7027 166     rcmail.set_cookie('language', lan_code, now);
ae8a2a 167 };
309d2f 168
66df08 169 this.setForceWidthHeight = function(width, height)
A 170 {
309d2f 171     // Set to null if you want to use one of them
ffa6c1 172     this.force_width = width;
T 173     this.force_height = height;
ae8a2a 174 };
2c6337 175
66df08 176 this.setDecoration = function(bool)
A 177 {
ffa6c1 178     this.decoration = bool;
ae8a2a 179 };
dd53e2 180
66df08 181 this.dontUseCloseButtons = function()
A 182 {
ffa6c1 183     this.use_close_btn = false;
ae8a2a 184 };
dd53e2 185
66df08 186 this.appendNewMenuItem = function(name, call_back_fn, checker)
A 187 {
ffa6c1 188     this.extra_menu_items.push([name, call_back_fn, checker]);
ae8a2a 189 };
ffa6c1 190
784c0c 191 this.appendCustomMenuBuilder = function(eval_fn, builder)
66df08 192 {
784c0c 193     this.custom_menu_builder.push([eval_fn, builder]);
ae8a2a 194 };
ffa6c1 195
66df08 196 this.setFocus = function()
A 197 {
ffa6c1 198     try {
T 199         this.focus_link_b.focus();
200         this.focus_link_t.focus();
201         return true;
202     }
203     catch(e) {
204         return false;
205     }
ae8a2a 206 };
ffa6c1 207
T 208
209 //////
210 // Set functions (internal)
211 /////
66df08 212 this.setStateChanged = function(current_state)
A 213 {
ffa6c1 214     this.state = current_state;
309d2f 215     if (this.spelling_state_observer != null && this.report_state_change)
ffa6c1 216         this.spelling_state_observer(current_state, this);
ae8a2a 217 };
ffa6c1 218
66df08 219 this.setReportStateChange = function(bool)
A 220 {
ffa6c1 221     this.report_state_change = bool;
ae8a2a 222 };
ffa6c1 223
T 224
225 //////
226 // Request functions
227 /////
66df08 228 this.getUrl = function()
A 229 {
ffa6c1 230     return this.server_url + GOOGIE_CUR_LANG;
ae8a2a 231 };
ffa6c1 232
66df08 233 this.escapeSpecial = function(val)
A 234 {
de3686 235     return val ? val.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") : '';
ae8a2a 236 };
ffa6c1 237
66df08 238 this.createXMLReq = function (text)
A 239 {
309d2f 240     return '<?xml version="1.0" encoding="utf-8" ?>'
e24eba 241         + '<spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1">'
AM 242         + '<text>' + text + '</text></spellrequest>';
ae8a2a 243 };
ffa6c1 244
66df08 245 this.spellCheck = function(ignore)
A 246 {
340546 247     this.prepare(ignore);
A 248
249     var req_text = this.escapeSpecial(this.orginal_text),
250         ref = this;
251
66df08 252     $.ajax({ type: 'POST', url: this.getUrl(), data: this.createXMLReq(req_text), dataType: 'text',
e24eba 253         error: function(o) {
340546 254             if (ref.custom_ajax_error)
e24eba 255                 ref.custom_ajax_error(ref);
340546 256             else
e24eba 257                 alert('An error was encountered on the server. Please try again later.');
340546 258             if (ref.main_controller) {
e24eba 259                 $(ref.spell_span).remove();
AM 260                 ref.removeIndicator();
340546 261             }
A 262             ref.checkSpellingState();
e24eba 263         },
340546 264         success: function(data) {
e24eba 265             ref.processData(data);
AM 266             if (!ref.results.length) {
267                 if (!ref.custom_no_spelling_error)
268                     ref.flashNoSpellingErrorState();
269                 else
270                     ref.custom_no_spelling_error(ref);
271             }
272             ref.removeIndicator();
273         }
66df08 274     });
A 275 };
276
277 this.learnWord = function(word, id)
278 {
279     word = this.escapeSpecial(word.innerHTML);
280
281     var ref = this,
282         req_text = '<?xml version="1.0" encoding="utf-8" ?><learnword><text>' + word + '</text></learnword>';
283
284     $.ajax({ type: 'POST', url: this.getUrl(), data: req_text, dataType: 'text',
e24eba 285         error: function(o) {
66df08 286             if (ref.custom_ajax_error)
e24eba 287                 ref.custom_ajax_error(ref);
66df08 288             else
e24eba 289                 alert('An error was encountered on the server. Please try again later.');
AM 290         },
66df08 291         success: function(data) {
e24eba 292         }
340546 293     });
A 294 };
295
296
297 //////
298 // Spell checking functions
299 /////
300 this.prepare = function(ignore, no_indicator)
301 {
ffa6c1 302     this.cnt_errors_fixed = 0;
T 303     this.cnt_errors = 0;
309d2f 304     this.setStateChanged('checking_spell');
a54cff 305     this.orginal_text = '';
ffa6c1 306
340546 307     if (!no_indicator && this.main_controller)
ffa6c1 308         this.appendIndicator(this.spell_span);
T 309
310     this.error_links = [];
311     this.ta_scroll_top = this.text_area.scrollTop;
312     this.ignore = ignore;
309d2f 313     this.hideLangWindow();
ffa6c1 314
421ed1 315     var area = $(this.text_area);
AM 316
317     if (area.val() == '' || ignore) {
309d2f 318         if (!this.custom_no_spelling_error)
A 319             this.flashNoSpellingErrorState();
ffa6c1 320         else
309d2f 321             this.custom_no_spelling_error(this);
A 322         this.removeIndicator();
323         return;
ffa6c1 324     }
1617db 325
421ed1 326     this.createEditLayer(area.width(), area.height());
ffa6c1 327     this.createErrorWindow();
309d2f 328     $('body').append(this.error_window);
ffa6c1 329
ae8a2a 330     try { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); }
ffa6c1 331     catch (e) { }
T 332
309d2f 333     if (this.main_controller)
A 334         $(this.spell_span).unbind('click');
ffa6c1 335
421ed1 336     this.orginal_text = area.val();
ae8a2a 337 };
ffa6c1 338
66df08 339 this.parseResult = function(r_text)
A 340 {
309d2f 341     // Returns an array: result[item] -> ['attrs'], ['suggestions']
ae8a2a 342     var re_split_attr_c = /\w+="(\d+|true)"/g,
A 343         re_split_text = /\t/g,
344         matched_c = r_text.match(/<c[^>]*>[^<]*<\/c>/g),
345         results = [];
ffa6c1 346
309d2f 347     if (matched_c == null)
ffa6c1 348         return results;
ae8a2a 349
A 350     for (var i=0, len=matched_c.length; i < len; i++) {
351         var item = [];
ffa6c1 352         this.errorFound();
T 353
ae8a2a 354         // Get attributes
A 355         item['attrs'] = [];
356         var c_attr, val,
357             split_c = matched_c[i].match(re_split_attr_c);
309d2f 358         for (var j=0; j < split_c.length; j++) {
ae8a2a 359             c_attr = split_c[j].split(/=/);
A 360             val = c_attr[1].replace(/"/g, '');
309d2f 361             item['attrs'][c_attr[0]] = val != 'true' ? parseInt(val) : val;
ffa6c1 362         }
T 363
ae8a2a 364         // Get suggestions
A 365         item['suggestions'] = [];
366         var only_text = matched_c[i].replace(/<[^>]*>/g, ''),
367             split_t = only_text.split(re_split_text);
309d2f 368         for (var k=0; k < split_t.length; k++) {
e24eba 369             if(split_t[k] != '')
AM 370             item['suggestions'].push(split_t[k]);
371         }
ffa6c1 372         results.push(item);
T 373     }
1617db 374
ffa6c1 375     return results;
ae8a2a 376 };
ffa6c1 377
340546 378 this.processData = function(data)
A 379 {
380     this.results = this.parseResult(data);
381     if (this.results.length) {
e24eba 382         this.showErrorsInIframe();
AM 383         this.resumeEditingState();
340546 384     }
A 385 };
ffa6c1 386
T 387 //////
388 // Error menu functions
389 /////
66df08 390 this.createErrorWindow = function()
A 391 {
91a35e 392     this.error_window = document.createElement('div');
f75ade 393     $(this.error_window).addClass('googie_window popupmenu').attr('googie_action_btn', '1');
ae8a2a 394 };
ffa6c1 395
66df08 396 this.isErrorWindowShown = function()
A 397 {
309d2f 398     return $(this.error_window).is(':visible');
ae8a2a 399 };
ffa6c1 400
66df08 401 this.hideErrorWindow = function()
A 402 {
f75ade 403     $(this.error_window).hide();
A 404     $(this.error_window_iframe).hide();
ae8a2a 405 };
ffa6c1 406
66df08 407 this.updateOrginalText = function(offset, old_value, new_value, id)
A 408 {
ae8a2a 409     var part_1 = this.orginal_text.substring(0, offset),
A 410         part_2 = this.orginal_text.substring(offset+old_value.length),
411         add_2_offset = new_value.length - old_value.length;
412
ffa6c1 413     this.orginal_text = part_1 + new_value + part_2;
309d2f 414     $(this.text_area).val(this.orginal_text);
ae8a2a 415     for (var j=0, len=this.results.length; j<len; j++) {
A 416         // Don't edit the offset of the current item
309d2f 417         if (j != id && j > id)
ffa6c1 418             this.results[j]['attrs']['o'] += add_2_offset;
T 419     }
ae8a2a 420 };
ffa6c1 421
309d2f 422 this.saveOldValue = function(elm, old_value) {
ffa6c1 423     elm.is_changed = true;
T 424     elm.old_value = old_value;
ae8a2a 425 };
ffa6c1 426
66df08 427 this.createListSeparator = function()
A 428 {
ae8a2a 429     var td = document.createElement('td'),
A 430         tr = document.createElement('tr');
ffa6c1 431
309d2f 432     $(td).html(' ').attr('googie_action_btn', '1')
e24eba 433         .css({'cursor': 'default', 'font-size': '3px', 'border-top': '1px solid #ccc', 'padding-top': '3px'});
309d2f 434     tr.appendChild(td);
A 435
436     return tr;
ae8a2a 437 };
ffa6c1 438
66df08 439 this.correctError = function(id, elm, l_elm, rm_pre_space)
A 440 {
ae8a2a 441     var old_value = elm.innerHTML,
A 442         new_value = l_elm.nodeType == 3 ? l_elm.nodeValue : l_elm.innerHTML,
443         offset = this.results[id]['attrs']['o'];
ffa6c1 444
309d2f 445     if (rm_pre_space) {
ffa6c1 446         var pre_length = elm.previousSibling.innerHTML;
T 447         elm.previousSibling.innerHTML = pre_length.slice(0, pre_length.length-1);
448         old_value = " " + old_value;
449         offset--;
450     }
451
452     this.hideErrorWindow();
453     this.updateOrginalText(offset, old_value, new_value, id);
454
309d2f 455     $(elm).html(new_value).css('color', 'green').attr('is_corrected', true);
ffa6c1 456
T 457     this.results[id]['attrs']['l'] = new_value.length;
458
309d2f 459     if (!this.isDefined(elm.old_value))
ffa6c1 460         this.saveOldValue(elm, old_value);
ae8a2a 461
ffa6c1 462     this.errorFixed();
ae8a2a 463 };
ffa6c1 464
66df08 465 this.ignoreError = function(elm, id)
A 466 {
467     // @TODO: ignore all same words
468     $(elm).removeAttr('class').css('color', '').unbind();
469     this.hideErrorWindow();
470 };
471
472 this.showErrorWindow = function(elm, id)
473 {
309d2f 474     if (this.show_menu_observer)
ffa6c1 475         this.show_menu_observer(this);
T 476
ae8a2a 477     var ref = this,
A 478         pos = $(elm).offset(),
479         table = document.createElement('table'),
480         list = document.createElement('tbody');
481
b8d4fe 482     $(this.error_window).html('');
309d2f 483     $(table).addClass('googie_list').attr('googie_action_btn', '1');
ffa6c1 484
ae8a2a 485     // Check if we should use custom menu builder, if not we use the default
ffa6c1 486     var changed = false;
1617db 487     for (var k=0; k<this.custom_menu_builder.length; k++) {
A 488         var eb = this.custom_menu_builder[k];
784c0c 489         if (eb[0](this.results[id])) {
1617db 490             changed = eb[1](this, list, elm);
A 491             break;
ffa6c1 492         }
T 493     }
66df08 494
309d2f 495     if (!changed) {
ae8a2a 496         // Build up the result list
A 497         var suggestions = this.results[id]['suggestions'],
498             offset = this.results[id]['attrs']['o'],
499             len = this.results[id]['attrs']['l'],
500             row, item, dummy;
ffa6c1 501
66df08 502         // [Add to dictionary] button
A 503         if (this.has_dictionary && !$(elm).attr('is_corrected')) {
504             row = document.createElement('tr'),
505             item = document.createElement('td'),
506             dummy = document.createElement('span');
507
508             $(dummy).text(this.lang_learn_word);
509             $(item).attr('googie_action_btn', '1').css('cursor', 'default')
510                 .mouseover(ref.item_onmouseover)
511                 .mouseout(ref.item_onmouseout)
e24eba 512                 .click(function(e) {
AM 513                     ref.learnWord(elm, id);
514                     ref.ignoreError(elm, id);
515                 });
66df08 516
A 517             item.appendChild(dummy);
518             row.appendChild(item);
519             list.appendChild(row);
520         }
521 /*
309d2f 522         if (suggestions.length == 0) {
ae8a2a 523             row = document.createElement('tr'),
A 524             item = document.createElement('td'),
525             dummy = document.createElement('span');
309d2f 526
A 527             $(dummy).text(this.lang_no_suggestions);
528             $(item).attr('googie_action_btn', '1').css('cursor', 'default');
529
530             item.appendChild(dummy);
ffa6c1 531             row.appendChild(item);
T 532             list.appendChild(row);
533         }
66df08 534 */
ae8a2a 535         for (var i=0, len=suggestions.length; i < len; i++) {
A 536             row = document.createElement('tr'),
537             item = document.createElement('td'),
538             dummy = document.createElement('span');
309d2f 539
A 540             $(dummy).html(suggestions[i]);
ae8a2a 541
66df08 542             $(item).mouseover(this.item_onmouseover).mouseout(this.item_onmouseout)
eb0dec 543               .click(function(e) { ref.correctError(id, elm, e.target.firstChild) });
ffa6c1 544
309d2f 545             item.appendChild(dummy);
ffa6c1 546             row.appendChild(item);
T 547             list.appendChild(row);
548         }
549
66df08 550         // The element is changed, append the revert
309d2f 551         if (elm.is_changed && elm.innerHTML != elm.old_value) {
ae8a2a 552             var old_value = elm.old_value,
A 553                 revert_row = document.createElement('tr'),
554                 revert = document.createElement('td'),
555                 rev_span = document.createElement('span');
556
e24eba 557             $(rev_span).addClass('googie_list_revert').html(this.lang_revert + ' ' + old_value);
ffa6c1 558
66df08 559             $(revert).mouseover(this.item_onmouseover).mouseout(this.item_onmouseout)
e24eba 560                 .click(function(e) {
AM 561                     ref.updateOrginalText(offset, elm.innerHTML, old_value, id);
562                     $(elm).removeAttr('is_corrected').css('color', '#b91414').html(old_value);
563                     ref.hideErrorWindow();
564                 });
309d2f 565
ffa6c1 566             revert.appendChild(rev_span);
T 567             revert_row.appendChild(revert);
568             list.appendChild(revert_row);
569         }
1617db 570
ae8a2a 571         // Append the edit box
A 572         var edit_row = document.createElement('tr'),
573             edit = document.createElement('td'),
574             edit_input = document.createElement('input'),
575             ok_pic = document.createElement('img'),
e24eba 576             edit_form = document.createElement('form');
ffa6c1 577
T 578         var onsub = function () {
309d2f 579             if (edit_input.value != '') {
A 580                 if (!ref.isDefined(elm.old_value))
581                     ref.saveOldValue(elm, elm.innerHTML);
ffa6c1 582
309d2f 583                 ref.updateOrginalText(offset, elm.innerHTML, edit_input.value, id);
eb0dec 584                 $(elm).attr('is_corrected', true).css('color', 'green').text(edit_input.value);
309d2f 585                 ref.hideErrorWindow();
ffa6c1 586             }
T 587             return false;
588         };
589
eb0dec 590         $(edit_input).width(120)
AM 591           .css({'margin': 0, 'padding': 0})
592           .val($(elm).text()).attr('googie_action_btn', '1');
e24eba 593         $(edit).css('cursor', 'default').attr('googie_action_btn', '1');
ffa6c1 594
e24eba 595         $(ok_pic).attr('src', this.img_dir + 'ok.gif')
AM 596             .width(32).height(16)
597             .css({'cursor': 'pointer', 'margin-left': '2px', 'margin-right': '2px'})
598             .click(onsub);
309d2f 599
A 600         $(edit_form).attr('googie_action_btn', '1')
e24eba 601             .css({'margin': 0, 'padding': 0, 'cursor': 'default', 'white-space': 'nowrap'})
AM 602             .submit(onsub);
ae8a2a 603
e24eba 604         edit_form.appendChild(edit_input);
AM 605         edit_form.appendChild(ok_pic);
ffa6c1 606         edit.appendChild(edit_form);
T 607         edit_row.appendChild(edit);
608         list.appendChild(edit_row);
609
ae8a2a 610         // Append extra menu items
309d2f 611         if (this.extra_menu_items.length > 0)
e24eba 612             list.appendChild(this.createListSeparator());
ae8a2a 613
ffa6c1 614         var loop = function(i) {
ae8a2a 615             if (i < ref.extra_menu_items.length) {
A 616                 var e_elm = ref.extra_menu_items[i];
ffa6c1 617
ae8a2a 618                 if (!e_elm[2] || e_elm[2](elm, ref)) {
A 619                     var e_row = document.createElement('tr'),
620                       e_col = document.createElement('td');
ffa6c1 621
e24eba 622                     $(e_col).html(e_elm[0])
66df08 623                         .mouseover(ref.item_onmouseover)
e24eba 624                         .mouseout(ref.item_onmouseout)
AM 625                         .click(function() { return e_elm[1](elm, ref) });
ae8a2a 626
e24eba 627                     e_row.appendChild(e_col);
ae8a2a 628                     list.appendChild(e_row);
ffa6c1 629                 }
ae8a2a 630                 loop(i+1);
A 631             }
632         };
633
ffa6c1 634         loop(0);
T 635         loop = null;
636
637         //Close button
309d2f 638         if (this.use_close_btn) {
e24eba 639             list.appendChild(this.createCloseButton(this.hideErrorWindow));
ffa6c1 640         }
T 641     }
642
643     table.appendChild(list);
644     this.error_window.appendChild(table);
645
b8d4fe 646     // calculate and set position
A 647     var height = $(this.error_window).height(),
648         width = $(this.error_window).width(),
649         pageheight = $(document).height(),
650         pagewidth = $(document).width(),
651         top = pos.top + height + 20 < pageheight ? pos.top + 20 : pos.top - height,
652         left = pos.left + width < pagewidth ? pos.left : pos.left - width;
653
f75ade 654     $(this.error_window).css({'top': top+'px', 'left': left+'px'}).show();
b8d4fe 655
ae8a2a 656     // Dummy for IE - dropdown bug fix
e24eba 657     if (document.all && !window.opera) {
AM 658         if (!this.error_window_iframe) {
1617db 659             var iframe = $('<iframe>').css({'position': 'absolute', 'z-index': -1});
e24eba 660             $('body').append(iframe);
AM 661             this.error_window_iframe = iframe;
309d2f 662         }
1617db 663
e24eba 664         $(this.error_window_iframe)
AM 665             .css({'top': this.error_window.offsetTop, 'left': this.error_window.offsetLeft,
666                 'width': this.error_window.offsetWidth, 'height': this.error_window.offsetHeight})
667             .show();
ffa6c1 668     }
ae8a2a 669 };
ffa6c1 670
T 671
672 //////
673 // Edit layer (the layer where the suggestions are stored)
674 //////
66df08 675 this.createEditLayer = function(width, height)
A 676 {
91a35e 677     this.edit_layer = document.createElement('div');
b8d4fe 678     $(this.edit_layer).addClass('googie_edit_layer').attr('id', 'googie_edit_layer')
421ed1 679         .width(width).height(height);
ffa6c1 680
91a35e 681     if (this.text_area.nodeName.toLowerCase() != 'input' || $(this.text_area).val() == '') {
421ed1 682         $(this.edit_layer).css('overflow', 'auto');
309d2f 683     } else {
A 684         $(this.edit_layer).css('overflow', 'hidden');
dd53e2 685     }
T 686
309d2f 687     var ref = this;
48e8b3 688
309d2f 689     if (this.edit_layer_dbl_click) {
48e8b3 690         $(this.edit_layer).dblclick(function(e) {
309d2f 691             if (e.target.className != 'googie_link' && !ref.isErrorWindowShown()) {
A 692                 ref.resumeEditing();
ffa6c1 693                 var fn1 = function() {
309d2f 694                     $(ref.text_area).focus();
ffa6c1 695                     fn1 = null;
T 696                 };
309d2f 697                 window.setTimeout(fn1, 10);
ffa6c1 698             }
T 699             return false;
309d2f 700         });
ffa6c1 701     }
ae8a2a 702 };
ffa6c1 703
66df08 704 this.resumeEditing = function()
A 705 {
309d2f 706     this.setStateChanged('ready');
ffa6c1 707
309d2f 708     if (this.edit_layer)
ffa6c1 709         this.el_scroll_top = this.edit_layer.scrollTop;
T 710
711     this.hideErrorWindow();
712
309d2f 713     if (this.main_controller)
A 714         $(this.spell_span).removeClass().addClass('googie_no_style');
ffa6c1 715
309d2f 716     if (!this.ignore) {
A 717         if (this.use_focus) {
718             $(this.focus_link_t).remove();
719             $(this.focus_link_b).remove();
ffa6c1 720         }
T 721
309d2f 722         $(this.edit_layer).remove();
A 723         $(this.text_area).show();
ffa6c1 724
309d2f 725         if (this.el_scroll_top != undefined)
ffa6c1 726             this.text_area.scrollTop = this.el_scroll_top;
T 727     }
728     this.checkSpellingState(false);
ae8a2a 729 };
ffa6c1 730
66df08 731 this.createErrorLink = function(text, id)
A 732 {
ae8a2a 733     var elm = document.createElement('span'),
A 734         ref = this,
735         d = function (e) {
e24eba 736             ref.showErrorWindow(elm, id);
AM 737             d = null;
738             return false;
ae8a2a 739         };
A 740
66df08 741     $(elm).html(text).addClass('googie_link').click(d).removeAttr('is_corrected')
e24eba 742         .attr({'googie_action_btn' : '1', 'g_id' : id});
ffa6c1 743
T 744     return elm;
ae8a2a 745 };
ffa6c1 746
66df08 747 this.createPart = function(txt_part)
A 748 {
309d2f 749     if (txt_part == " ")
A 750         return document.createTextNode(" ");
ffa6c1 751
309d2f 752     txt_part = this.escapeSpecial(txt_part);
ffa6c1 753     txt_part = txt_part.replace(/\n/g, "<br>");
T 754     txt_part = txt_part.replace(/    /g, " &nbsp;");
755     txt_part = txt_part.replace(/^ /g, "&nbsp;");
756     txt_part = txt_part.replace(/ $/g, "&nbsp;");
ae8a2a 757
91a35e 758     var span = document.createElement('span');
309d2f 759     $(span).html(txt_part);
A 760     return span;
ae8a2a 761 };
ffa6c1 762
66df08 763 this.showErrorsInIframe = function()
A 764 {
ae8a2a 765     var output = document.createElement('div'),
A 766         pointer = 0,
767         results = this.results;
ffa6c1 768
309d2f 769     if (results.length > 0) {
ae8a2a 770         for (var i=0, length=results.length; i < length; i++) {
A 771             var offset = results[i]['attrs']['o'],
772                 len = results[i]['attrs']['l'],
773                 part_1_text = this.orginal_text.substring(pointer, offset),
774                 part_1 = this.createPart(part_1_text);
775
ffa6c1 776             output.appendChild(part_1);
T 777             pointer += offset - pointer;
ae8a2a 778
A 779             // If the last child was an error, then insert some space
ffa6c1 780             var err_link = this.createErrorLink(this.orginal_text.substr(offset, len), i);
T 781             this.error_links.push(err_link);
782             output.appendChild(err_link);
783             pointer += len;
784         }
ae8a2a 785
A 786         // Insert the rest of the orginal text
787         var part_2_text = this.orginal_text.substr(pointer, this.orginal_text.length),
788             part_2 = this.createPart(part_2_text);
ffa6c1 789
T 790         output.appendChild(part_2);
791     }
792     else
793         output.innerHTML = this.orginal_text;
794
309d2f 795     $(output).css('text-align', 'left');
A 796
ffa6c1 797     var me = this;
309d2f 798     if (this.custom_item_evaulator)
A 799         $.map(this.error_links, function(elm){me.custom_item_evaulator(me, elm)});
ae8a2a 800
309d2f 801     $(this.edit_layer).append(output);
ffa6c1 802
ae8a2a 803     // Hide text area and show edit layer
309d2f 804     $(this.text_area).hide();
A 805     $(this.edit_layer).insertBefore(this.text_area);
ffa6c1 806
309d2f 807     if (this.use_focus) {
ffa6c1 808         this.focus_link_t = this.createFocusLink('focus_t');
T 809         this.focus_link_b = this.createFocusLink('focus_b');
810
309d2f 811         $(this.focus_link_t).insertBefore(this.edit_layer);
A 812         $(this.focus_link_b).insertAfter(this.edit_layer);
ffa6c1 813     }
T 814
309d2f 815 //    this.edit_layer.scrollTop = this.ta_scroll_top;
ae8a2a 816 };
ffa6c1 817
T 818
819 //////
820 // Choose language menu
821 //////
66df08 822 this.createLangWindow = function()
A 823 {
91a35e 824     this.language_window = document.createElement('div');
f75ade 825     $(this.language_window).addClass('googie_window popupmenu')
e24eba 826         .width(100).attr('googie_action_btn', '1');
ffa6c1 827
ae8a2a 828     // Build up the result list
A 829     var table = document.createElement('table'),
830         list = document.createElement('tbody'),
831         ref = this,
832         row, item, span;
ffa6c1 833
309d2f 834     $(table).addClass('googie_list').width('100%');
ae8a2a 835     this.lang_elms = [];
ffa6c1 836
309d2f 837     for (i=0; i < this.langlist_codes.length; i++) {
ae8a2a 838         row = document.createElement('tr');
A 839         item = document.createElement('td');
840         span = document.createElement('span');
841
e24eba 842         $(span).text(this.lang_to_word[this.langlist_codes[i]]);
ffa6c1 843         this.lang_elms.push(item);
T 844
309d2f 845         $(item).attr('googieId', this.langlist_codes[i])
e24eba 846             .bind('click', function(e) {
AM 847                 ref.deHighlightCurSel();
848                 ref.setCurrentLanguage($(this).attr('googieId'));
ffa6c1 849
e24eba 850                 if (ref.lang_state_observer != null) {
AM 851                     ref.lang_state_observer();
852                 }
ffa6c1 853
e24eba 854                 ref.highlightCurSel();
AM 855                 ref.hideLangWindow();
856             })
857             .bind('mouseover', function(e) {
858                 if (this.className != "googie_list_selected")
859                     this.className = "googie_list_onhover";
860             })
861             .bind('mouseout', function(e) {
862                 if (this.className != "googie_list_selected")
863                     this.className = "googie_list_onout";
864             });
ffa6c1 865
e24eba 866         item.appendChild(span);
ffa6c1 867         row.appendChild(item);
T 868         list.appendChild(row);
869     }
870
ae8a2a 871     // Close button
309d2f 872     if (this.use_close_btn) {
A 873         list.appendChild(this.createCloseButton(function () { ref.hideLangWindow.apply(ref) }));
ffa6c1 874     }
T 875
876     this.highlightCurSel();
877
878     table.appendChild(list);
879     this.language_window.appendChild(table);
ae8a2a 880 };
ffa6c1 881
66df08 882 this.isLangWindowShown = function()
A 883 {
48e8b3 884     return $(this.language_window).is(':visible');
ae8a2a 885 };
ffa6c1 886
66df08 887 this.hideLangWindow = function()
A 888 {
f75ade 889     $(this.language_window).hide();
309d2f 890     $(this.switch_lan_pic).removeClass().addClass('googie_lang_3d_on');
ae8a2a 891 };
ffa6c1 892
66df08 893 this.showLangWindow = function(elm)
A 894 {
087c7d 895     if (this.show_menu_observer)
A 896         this.show_menu_observer(this);
897
898     this.createLangWindow();
899     $('body').append(this.language_window);
900
901     var pos = $(elm).offset(),
902         height = $(elm).height(),
903         width = $(elm).width(),
904         h = $(this.language_window).height(),
905         pageheight = $(document).height(),
906         left = this.change_lang_pic_placement == 'right' ?
e24eba 907             pos.left - 100 + width : pos.left + width,
087c7d 908         top = pos.top + h < pageheight ? pos.top + height : pos.top - h - 4;
A 909
f75ade 910     $(this.language_window).css({'top' : top+'px','left' : left+'px'}).show();
087c7d 911
A 912     this.highlightCurSel();
913 };
914
66df08 915 this.deHighlightCurSel = function()
A 916 {
309d2f 917     $(this.lang_cur_elm).removeClass().addClass('googie_list_onout');
ae8a2a 918 };
ffa6c1 919
66df08 920 this.highlightCurSel = function()
A 921 {
309d2f 922     if (GOOGIE_CUR_LANG == null)
ffa6c1 923         GOOGIE_CUR_LANG = GOOGIE_DEFAULT_LANG;
309d2f 924     for (var i=0; i < this.lang_elms.length; i++) {
A 925         if ($(this.lang_elms[i]).attr('googieId') == GOOGIE_CUR_LANG) {
087c7d 926             this.lang_elms[i].className = 'googie_list_selected';
ffa6c1 927             this.lang_cur_elm = this.lang_elms[i];
T 928         }
929         else {
087c7d 930             this.lang_elms[i].className = 'googie_list_onout';
ffa6c1 931         }
T 932     }
ae8a2a 933 };
ffa6c1 934
66df08 935 this.createChangeLangPic = function()
A 936 {
309d2f 937     var img = $('<img>')
e24eba 938         .attr({src: this.img_dir + 'change_lang.gif', 'alt': 'Change language', 'googie_action_btn': '1'}),
ae8a2a 939         switch_lan = document.createElement('span');
A 940         ref = this;
ffa6c1 941
309d2f 942     $(switch_lan).addClass('googie_lang_3d_on')
e24eba 943         .append(img)
AM 944         .bind('click', function(e) {
945             var elm = this.tagName.toLowerCase() == 'img' ? this.parentNode : this;
946             if($(elm).hasClass('googie_lang_3d_click')) {
947                 elm.className = 'googie_lang_3d_on';
948                 ref.hideLangWindow();
949             }
950             else {
951                 elm.className = 'googie_lang_3d_click';
952                 ref.showLangWindow(elm);
953             }
954         });
309d2f 955
ffa6c1 956     return switch_lan;
ae8a2a 957 };
ffa6c1 958
66df08 959 this.createSpellDiv = function()
A 960 {
91a35e 961     var span = document.createElement('span');
ffa6c1 962
309d2f 963     $(span).addClass('googie_check_spelling_link').text(this.lang_chck_spell);
A 964
965     if (this.show_spell_img) {
e24eba 966         $(span).append(' ').append($('<img>').attr('src', this.img_dir + 'spellc.gif'));
309d2f 967     }
A 968     return span;
ae8a2a 969 };
ffa6c1 970
T 971
972 //////
973 // State functions
974 /////
66df08 975 this.flashNoSpellingErrorState = function(on_finish)
A 976 {
309d2f 977     this.setStateChanged('no_error_found');
ffa6c1 978
309d2f 979     var ref = this;
A 980     if (this.main_controller) {
e24eba 981         var no_spell_errors;
AM 982         if (on_finish) {
983             var fn = function() {
984                 on_finish();
985                 ref.checkSpellingState();
986             };
987             no_spell_errors = fn;
988         }
989         else
990             no_spell_errors = function () { ref.checkSpellingState() };
ffa6c1 991
309d2f 992         var rsm = $('<span>').text(this.lang_no_error_found);
677e1f 993
309d2f 994         $(this.switch_lan_pic).hide();
e24eba 995         $(this.spell_span).empty().append(rsm)
AM 996         .removeClass().addClass('googie_check_spelling_ok');
ffa6c1 997
309d2f 998         window.setTimeout(no_spell_errors, 1000);
ffa6c1 999     }
ae8a2a 1000 };
ffa6c1 1001
66df08 1002 this.resumeEditingState = function()
A 1003 {
309d2f 1004     this.setStateChanged('resume_editing');
ffa6c1 1005
T 1006     //Change link text to resume
309d2f 1007     if (this.main_controller) {
A 1008         var rsm = $('<span>').text(this.lang_rsm_edt);
e24eba 1009     var ref = this;
ffa6c1 1010
309d2f 1011         $(this.switch_lan_pic).hide();
A 1012         $(this.spell_span).empty().unbind().append(rsm)
e24eba 1013             .bind('click', function() { ref.resumeEditing() })
AM 1014             .removeClass().addClass('googie_resume_editing');
ffa6c1 1015     }
T 1016
1017     try { this.edit_layer.scrollTop = this.ta_scroll_top; }
309d2f 1018     catch (e) {};
ae8a2a 1019 };
ffa6c1 1020
66df08 1021 this.checkSpellingState = function(fire)
A 1022 {
309d2f 1023     if (fire)
A 1024         this.setStateChanged('ready');
ffa6c1 1025
309d2f 1026     if (this.show_change_lang_pic)
ffa6c1 1027         this.switch_lan_pic = this.createChangeLangPic();
T 1028     else
91a35e 1029         this.switch_lan_pic = document.createElement('span');
ffa6c1 1030
ae8a2a 1031     var span_chck = this.createSpellDiv(),
A 1032         ref = this;
ffa6c1 1033
309d2f 1034     if (this.custom_spellcheck_starter)
A 1035         $(span_chck).bind('click', function(e) { ref.custom_spellcheck_starter() });
ffa6c1 1036     else {
309d2f 1037         $(span_chck).bind('click', function(e) { ref.spellCheck() });
A 1038     }
1039
1040     if (this.main_controller) {
1041         if (this.change_lang_pic_placement == 'left') {
e24eba 1042             $(this.spell_container).empty().append(this.switch_lan_pic).append(' ').append(span_chck);
309d2f 1043         } else {
e24eba 1044             $(this.spell_container).empty().append(span_chck).append(' ').append(this.switch_lan_pic);
AM 1045         }
ffa6c1 1046     }
T 1047
1048     this.spell_span = span_chck;
ae8a2a 1049 };
ffa6c1 1050
T 1051
1052 //////
1053 // Misc. functions
1054 /////
66df08 1055 this.isDefined = function(o)
A 1056 {
ef4f59 1057     return (o !== undefined && o !== null)
ae8a2a 1058 };
ffa6c1 1059
66df08 1060 this.errorFixed = function()
A 1061 {
1062     this.cnt_errors_fixed++;
309d2f 1063     if (this.all_errors_fixed_observer)
A 1064         if (this.cnt_errors_fixed == this.cnt_errors) {
1065             this.hideErrorWindow();
1066             this.all_errors_fixed_observer();
1067         }
ae8a2a 1068 };
ffa6c1 1069
66df08 1070 this.errorFound = function()
A 1071 {
309d2f 1072     this.cnt_errors++;
ae8a2a 1073 };
ffa6c1 1074
66df08 1075 this.createCloseButton = function(c_fn)
A 1076 {
309d2f 1077     return this.createButton(this.lang_close, 'googie_list_close', c_fn);
ae8a2a 1078 };
ffa6c1 1079
66df08 1080 this.createButton = function(name, css_class, c_fn)
A 1081 {
ae8a2a 1082     var btn_row = document.createElement('tr'),
A 1083         btn = document.createElement('td'),
1084         spn_btn;
309d2f 1085
A 1086     if (css_class) {
91a35e 1087         spn_btn = document.createElement('span');
e24eba 1088         $(spn_btn).addClass(css_class).html(name);
309d2f 1089     } else {
A 1090         spn_btn = document.createTextNode(name);
ffa6c1 1091     }
309d2f 1092
A 1093     $(btn).bind('click', c_fn)
e24eba 1094         .bind('mouseover', this.item_onmouseover)
AM 1095         .bind('mouseout', this.item_onmouseout);
309d2f 1096
ffa6c1 1097     btn.appendChild(spn_btn);
T 1098     btn_row.appendChild(btn);
1099
1100     return btn_row;
ae8a2a 1101 };
ffa6c1 1102
66df08 1103 this.removeIndicator = function(elm)
A 1104 {
309d2f 1105     //$(this.indicator).remove();
A 1106     // roundcube mod.
ffa6c1 1107     if (window.rcmail)
ad334a 1108         rcmail.set_busy(false, null, this.rc_msg_id);
ae8a2a 1109 };
ffa6c1 1110
66df08 1111 this.appendIndicator = function(elm)
A 1112 {
ffa6c1 1113     // modified by roundcube
T 1114     if (window.rcmail)
e24eba 1115         this.rc_msg_id = rcmail.set_busy(true, 'checking');
ae8a2a 1116 /*
91a35e 1117     this.indicator = document.createElement('img');
309d2f 1118     $(this.indicator).attr('src', this.img_dir + 'indicator.gif')
e24eba 1119         .css({'margin-right': '5px', 'text-decoration': 'none'}).width(16).height(16);
ae8a2a 1120
309d2f 1121     if (elm)
e24eba 1122         $(this.indicator).insertBefore(elm);
309d2f 1123     else
e24eba 1124         $('body').append(this.indicator);
ae8a2a 1125 */
ffa6c1 1126 }
T 1127
66df08 1128 this.createFocusLink = function(name)
A 1129 {
91a35e 1130     var link = document.createElement('a');
309d2f 1131     $(link).attr({'href': 'javascript:;', 'name': name});
A 1132     return link;
ae8a2a 1133 };
309d2f 1134
66df08 1135 this.item_onmouseover = function(e)
A 1136 {
ae8a2a 1137     if (this.className != 'googie_list_revert' && this.className != 'googie_list_close')
A 1138         this.className = 'googie_list_onhover';
309d2f 1139     else
ae8a2a 1140         this.parentNode.className = 'googie_list_onhover';
A 1141 };
66df08 1142
A 1143 this.item_onmouseout = function(e)
1144 {
ae8a2a 1145     if (this.className != 'googie_list_revert' && this.className != 'googie_list_close')
A 1146         this.className = 'googie_list_onout';
309d2f 1147     else
ae8a2a 1148         this.parentNode.className = 'googie_list_onout';
A 1149 };
309d2f 1150
A 1151
1152 };