]> git.donarmstrong.com Git - roundcube.git/blobdiff - program/js/googiespell.js.src
Imported Upstream version 0.6+dfsg
[roundcube.git] / program / js / googiespell.js.src
index 428d7f0dc1a8cbb1610a7912c4243bd13d0b52c9..de8890f5fb95e6ef010833a16b0a9dc255564c11 100644 (file)
@@ -10,8 +10,8 @@
            Aleksander Machniak - alec [at] alec.pl
 */
 
-var SPELL_CUR_LANG = null;
-var GOOGIE_DEFAULT_LANG = 'en';
+var GOOGIE_CUR_LANG,
+    GOOGIE_DEFAULT_LANG = 'en';
 
 function GoogieSpell(img_dir, server_url) {
     var ref = this,
@@ -91,7 +91,7 @@ function GoogieSpell(img_dir, server_url) {
 
 
 this.decorateTextarea = function(id) {
-    this.text_area = typeof(id) == 'string' ? document.getElementById(id) : id;
+    this.text_area = typeof id === 'string' ? document.getElementById(id) : id;
 
     if (this.text_area) {
         if (!this.spell_container && this.decoration) {
@@ -120,7 +120,7 @@ this.decorateTextarea = function(id) {
 // API Functions (the ones that you can call)
 /////
 this.setSpellContainer = function(id) {
-    this.spell_container = typeof(id) == 'string' ? document.getElementById(id) : id;
+    this.spell_container = typeof id === 'string' ? document.getElementById(id) : id;
 };
 
 this.setLanguages = function(lang_dict) {
@@ -155,8 +155,8 @@ this.appendNewMenuItem = function(name, call_back_fn, checker) {
     this.extra_menu_items.push([name, call_back_fn, checker]);
 };
 
-this.appendCustomMenuBuilder = function(eval, builder) {
-    this.custom_menu_builder.push([eval, builder]);
+this.appendCustomMenuBuilder = function(eval_fn, builder) {
+    this.custom_menu_builder.push([eval_fn, builder]);
 };
 
 this.setFocus = function() {
@@ -193,7 +193,7 @@ this.getUrl = function() {
 };
 
 this.escapeSpecial = function(val) {
-    return val.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
+    return val ? val.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") : '';
 };
 
 this.createXMLReq = function (text) {
@@ -203,11 +203,48 @@ this.createXMLReq = function (text) {
 };
 
 this.spellCheck = function(ignore) {
+    this.prepare(ignore);
+
+    var req_text = this.escapeSpecial(this.orginal_text),
+        ref = this;
+
+    $.ajax({ type: 'POST', url: this.getUrl(),
+       data: this.createXMLReq(req_text), dataType: 'text',
+           error: function(o) {
+            if (ref.custom_ajax_error)
+                   ref.custom_ajax_error(ref);
+            else
+                   alert('An error was encountered on the server. Please try again later.');
+            if (ref.main_controller) {
+                   $(ref.spell_span).remove();
+                   ref.removeIndicator();
+            }
+            ref.checkSpellingState();
+           },
+        success: function(data) {
+           ref.processData(data);
+           if (!ref.results.length) {
+                   if (!ref.custom_no_spelling_error)
+                       ref.flashNoSpellingErrorState();
+               else
+                       ref.custom_no_spelling_error(ref);
+           }
+           ref.removeIndicator();
+           }
+    });
+};
+
+
+//////
+// Spell checking functions
+/////
+this.prepare = function(ignore, no_indicator)
+{
     this.cnt_errors_fixed = 0;
     this.cnt_errors = 0;
     this.setStateChanged('checking_spell');
 
-    if (this.main_controller)
+    if (!no_indicator && this.main_controller)
         this.appendIndicator(this.spell_span);
 
     this.error_links = [];
@@ -235,44 +272,8 @@ this.spellCheck = function(ignore) {
         $(this.spell_span).unbind('click');
 
     this.orginal_text = $(this.text_area).val();
-    var req_text = this.escapeSpecial(this.orginal_text);
-    var ref = this;
-
-    $.ajax({ type: 'POST', url: this.getUrl(),
-       data: this.createXMLReq(req_text), dataType: 'text',
-           error: function(o) {
-            if (ref.custom_ajax_error)
-                   ref.custom_ajax_error(ref);
-            else
-                   alert('An error was encountered on the server. Please try again later.');
-            if (ref.main_controller) {
-                   $(ref.spell_span).remove();
-                   ref.removeIndicator();
-            }
-            ref.checkSpellingState();
-           },
-        success: function(data) {
-               var r_text = data;
-           ref.results = ref.parseResult(r_text);
-           if (r_text.match(/<c.*>/) != null) {
-                   // Before parsing be sure that errors were found
-                   ref.showErrorsInIframe();
-                   ref.resumeEditingState();
-           } else {
-                   if (!ref.custom_no_spelling_error)
-                       ref.flashNoSpellingErrorState();
-               else
-               ref.custom_no_spelling_error(ref);
-           }
-           ref.removeIndicator();
-           }
-    });
 };
 
-
-//////
-// Spell checking functions
-/////
 this.parseResult = function(r_text) {
     // Returns an array: result[item] -> ['attrs'], ['suggestions']
     var re_split_attr_c = /\w+="(\d+|true)"/g,
@@ -311,6 +312,14 @@ this.parseResult = function(r_text) {
     return results;
 };
 
+this.processData = function(data)
+{
+    this.results = this.parseResult(data);
+    if (this.results.length) {
+           this.showErrorsInIframe();
+           this.resumeEditingState();
+    }
+};
 
 //////
 // Error menu functions
@@ -400,7 +409,7 @@ this.showErrorWindow = function(elm, id) {
     var changed = false;
     for (var k=0; k<this.custom_menu_builder.length; k++) {
         var eb = this.custom_menu_builder[k];
-        if(eb[0]((this.results[id]))){
+        if (eb[0](this.results[id])) {
             changed = eb[1](this, list, elm);
             break;
         }
@@ -931,7 +940,7 @@ this.checkSpellingState = function(fire) {
 // Misc. functions
 /////
 this.isDefined = function(o) {
-    return (o != 'undefined' && o != null)
+    return (o !== undefined && o !== null)
 };
 
 this.errorFixed = function() {