2 Last Modified: 28/04/06 16:28:09
5 A very small library with DOM and Ajax functions.
6 For a much larger script look on http://www.mochikit.com/
8 4mir Salihefendic (http://amix.dk) - amix@amix.dk
10 Copyright (c) 2006 Amir Salihefendic. All rights reserved.
11 Copyright (c) 2005 Bob Ippolito. All rights reserved.
12 http://www.opensource.org/licenses/mit-license.php
24 * @returns The element with the id
26 getElement: function(id) {
27 if(typeof(id) == "string")
28 return document.getElementById(id);
34 * @returns The elements with the ids
36 getElements: function(/*id1, id2, id3*/) {
37 var elements = new Array();
38 for (var i = 0; i < arguments.length; i++) {
39 var element = this.getElement(arguments[i]);
40 elements.push(element);
46 * @returns The GET query argument
48 getQueryArgument: function(var_name) {
49 var query = window.location.search.substring(1);
50 var vars = query.split("&");
51 for (var i=0;i<vars.length;i++) {
52 var pair = vars[i].split("=");
53 if (pair[0] == var_name) {
61 * @returns If the browser is Internet Explorer
64 return (navigator.userAgent.toLowerCase().indexOf("msie") != -1 && navigator.userAgent.toLowerCase().indexOf("opera") == -1);
68 * @returns The document body
71 return this.getElementsByTagAndClassName('body')[0]
75 * @returns All the elements that have a specific tag name or class name
77 getElementsByTagAndClassName: function(tag_name, class_name, /*optional*/ parent) {
78 var class_elements = new Array();
79 if(!this.isDefined(parent))
81 if(!this.isDefined(tag_name))
84 var els = parent.getElementsByTagName(tag_name);
85 var els_len = els.length;
86 var pattern = new RegExp("(^|\\s)" + class_name + "(\\s|$)");
88 for (i = 0, j = 0; i < els_len; i++) {
89 if ( pattern.test(els[i].className) || class_name == null ) {
90 class_elements[j] = els[i];
94 return class_elements;
102 * Appends some nodes to a node
104 appendChildNodes: function(node/*, nodes...*/) {
105 if(arguments.length >= 2) {
106 for(var i=1; i < arguments.length; i++) {
107 var n = arguments[i];
108 if(typeof(n) == "string")
109 n = document.createTextNode(n);
110 if(this.isDefined(n))
118 * Replaces a nodes children with another node(s)
120 replaceChildNodes: function(node/*, nodes...*/) {
122 while ((child = node.firstChild)) {
123 node.removeChild(child);
125 if (arguments.length < 2) {
128 return this.appendChildNodes.apply(this, arguments);
133 * Insert a node after another node
135 insertAfter: function(node, referenceNode) {
136 referenceNode.parentNode.insertBefore(node, referenceNode.nextSibling);
140 * Insert a node before another node
142 insertBefore: function(node, referenceNode) {
143 referenceNode.parentNode.insertBefore(node, referenceNode);
149 showElement: function(elm) {
150 elm.style.display = '';
156 hideElement: function(elm) {
157 elm.style.display = 'none';
160 isElementHidden: function(elm) {
161 return elm.style.visibility == "hidden";
165 * Swaps one element with another. To delete use swapDOM(elm, null)
167 swapDOM: function(dest, src) {
168 dest = this.getElement(dest);
169 var parent = dest.parentNode;
171 src = this.getElement(src);
172 parent.replaceChild(src, dest);
174 parent.removeChild(dest);
180 * Removes an element from the world
182 removeElement: function(elm) {
183 this.swapDOM(elm, null);
187 * @returns Is an object a dictionary?
189 isDict: function(o) {
190 var str_repr = String(o);
191 return str_repr.indexOf(" Object") != -1;
195 * Creates a DOM element
196 * @param {String} name The elements DOM name
197 * @param {Dict} attrs Attributes sent to the function
199 createDOM: function(name, attrs) {
201 elm = document.createElement(name);
203 if(this.isDict(attrs[i])) {
206 elm.style.cssText = attrs[0][k];
207 else if(k == "class")
208 elm.className = attrs[0][k];
210 elm.setAttribute(k, attrs[0][k]);
218 for(i; i < attrs.length; i++) {
220 if(this.isDefined(n)) {
221 if(typeof(n) == "string")
222 n = document.createTextNode(n);
229 UL: function() { return this.createDOM.apply(this, ["ul", arguments]); },
230 LI: function() { return this.createDOM.apply(this, ["li", arguments]); },
231 TD: function() { return this.createDOM.apply(this, ["td", arguments]); },
232 TR: function() { return this.createDOM.apply(this, ["tr", arguments]); },
233 TH: function() { return this.createDOM.apply(this, ["th", arguments]); },
234 TBODY: function() { return this.createDOM.apply(this, ["tbody", arguments]); },
235 TABLE: function() { return this.createDOM.apply(this, ["table", arguments]); },
236 INPUT: function() { return this.createDOM.apply(this, ["input", arguments]); },
237 SPAN: function() { return this.createDOM.apply(this, ["span", arguments]); },
238 B: function() { return this.createDOM.apply(this, ["b", arguments]); },
239 A: function() { return this.createDOM.apply(this, ["a", arguments]); },
240 DIV: function() { return this.createDOM.apply(this, ["div", arguments]); },
241 IMG: function() { return this.createDOM.apply(this, ["img", arguments]); },
242 BUTTON: function() { return this.createDOM.apply(this, ["button", arguments]); },
243 H1: function() { return this.createDOM.apply(this, ["h1", arguments]); },
244 H2: function() { return this.createDOM.apply(this, ["h2", arguments]); },
245 H3: function() { return this.createDOM.apply(this, ["h3", arguments]); },
246 BR: function() { return this.createDOM.apply(this, ["br", arguments]); },
247 TEXTAREA: function() { return this.createDOM.apply(this, ["textarea", arguments]); },
248 FORM: function() { return this.createDOM.apply(this, ["form", arguments]); },
249 P: function() { return this.createDOM.apply(this, ["p", arguments]); },
250 SELECT: function() { return this.createDOM.apply(this, ["select", arguments]); },
251 OPTION: function() { return this.createDOM.apply(this, ["option", arguments]); },
252 TN: function(text) { return document.createTextNode(text); },
253 IFRAME: function() { return this.createDOM.apply(this, ["iframe", arguments]); },
254 SCRIPT: function() { return this.createDOM.apply(this, ["script", arguments]); },
260 * @returns A new XMLHttpRequest object
262 getXMLHttpRequest: function() {
264 function () { return new XMLHttpRequest(); },
265 function () { return new ActiveXObject('Msxml2.XMLHTTP'); },
266 function () { return new ActiveXObject('Microsoft.XMLHTTP'); },
267 function () { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); },
268 function () { throw "Browser does not support XMLHttpRequest"; }
270 for (var i = 0; i < try_these.length; i++) {
271 var func = try_these[i];
280 * Use this function to do a simple HTTP Request
282 doSimpleXMLHttpRequest: function(url) {
283 var req = this.getXMLHttpRequest();
284 req.open("GET", url, true);
285 return this.sendXMLHttpRequest(req);
288 getRequest: function(url, data) {
289 var req = this.getXMLHttpRequest();
290 req.open("POST", url, true);
291 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
292 return this.sendXMLHttpRequest(req);
296 * Send a XMLHttpRequest
298 sendXMLHttpRequest: function(req, data) {
299 var d = new AJSDeferred(req);
301 var onreadystatechange = function () {
302 if (req.readyState == 4) {
304 var status = req.status;
307 if(status == 200 || status == 304 || req.responseText == null) {
308 d.callback(req, data);
315 req.onreadystatechange = onreadystatechange;
320 * Represent an object as a string
322 reprString: function(o) {
323 return ('"' + o.replace(/(["\\])/g, '\\$1') + '"'
324 ).replace(/[\f]/g, "\\f"
325 ).replace(/[\b]/g, "\\b"
326 ).replace(/[\n]/g, "\\n"
327 ).replace(/[\t]/g, "\\t"
328 ).replace(/[\r]/g, "\\r");
332 * Serialize an object to JSON notation
334 serializeJSON: function(o) {
335 var objtype = typeof(o);
336 if (objtype == "undefined") {
338 } else if (objtype == "number" || objtype == "boolean") {
340 } else if (o === null) {
343 if (objtype == "string") {
344 return this.reprString(o);
346 var me = arguments.callee;
348 if (typeof(o.__json__) == "function") {
349 newObj = o.__json__();
354 if (typeof(o.json) == "function") {
360 if (objtype != "function" && typeof(o.length) == "number") {
362 for (var i = 0; i < o.length; i++) {
364 if (typeof(val) != "string") {
369 return "[" + res.join(",") + "]";
374 if (typeof(k) == "number") {
375 useKey = '"' + k + '"';
376 } else if (typeof(k) == "string") {
377 useKey = this.reprString(k);
379 // skip non-string or number keys
383 if (typeof(val) != "string") {
384 // skip non-serializable values
387 res.push(useKey + ":" + val);
389 return "{" + res.join(",") + "}";
393 * Send and recive JSON using GET
395 loadJSONDoc: function(url) {
396 var d = this.getRequest(url);
397 var eval_req = function(req) {
398 var text = req.responseText;
399 return eval('(' + text + ')');
401 d.addCallback(eval_req);
410 * Alert the objects key attrs
412 keys: function(obj) {
414 for (var prop in obj) {
420 urlencode: function(str) {
421 return encodeURIComponent(str.toString());
425 * @returns True if the object is defined, otherwise false
427 isDefined: function(o) {
428 return (o != "undefined" && o != null)
432 * @returns True if an object is a array, false otherwise
434 isArray: function(obj) {
435 try { return (typeof(obj.length) == "undefined") ? false : true; }
440 isObject: function(obj) {
441 return (obj && typeof obj == 'object');
445 * Export DOM elements to the global namespace
447 exportDOMElements: function() {
461 BUTTON = this.BUTTON;
466 TEXTAREA = this.TEXTAREA;
469 SELECT = this.SELECT;
470 OPTION = this.OPTION;
472 IFRAME = this.IFRAME;
473 SCRIPT = this.SCRIPT;
477 * Export AmiJS functions to the global namespace
479 exportToGlobalScope: function() {
480 getElement = this.getElement;
481 getQueryArgument = this.getQueryArgument;
484 getElements = this.getElements;
485 getBody = this.getBody;
486 getElementsByTagAndClassName = this.getElementsByTagAndClassName;
487 appendChildNodes = this.appendChildNodes;
488 ACN = appendChildNodes;
489 replaceChildNodes = this.replaceChildNodes;
490 RCN = replaceChildNodes;
491 insertAfter = this.insertAfter;
492 insertBefore = this.insertBefore;
493 showElement = this.showElement;
494 hideElement = this.hideElement;
495 isElementHidden = this.isElementHidden;
496 swapDOM = this.swapDOM;
497 removeElement = this.removeElement;
498 isDict = this.isDict;
499 createDOM = this.createDOM;
500 this.exportDOMElements();
501 getXMLHttpRequest = this.getXMLHttpRequest;
502 doSimpleXMLHttpRequest = this.doSimpleXMLHttpRequest;
503 getRequest = this.getRequest;
504 sendXMLHttpRequest = this.sendXMLHttpRequest;
505 reprString = this.reprString;
506 serializeJSON = this.serializeJSON;
507 loadJSONDoc = this.loadJSONDoc;
509 isDefined = this.isDefined;
510 isArray = this.isArray;
516 AJSDeferred = function(req) {
520 this.callback = function (res) {
521 while (this.callbacks.length > 0) {
522 var fn = this.callbacks.pop();
527 this.errback = function(e){
528 alert("Error encountered:\n" + e);
531 this.addErrback = function(fn) {
535 this.addCallback = function(fn) {
536 this.callbacks.unshift(fn);
539 this.addCallbacks = function(fn1, fn2) {
540 this.addCallback(fn1);
541 this.addErrback(fn2);
544 this.sendReq = function(data) {
545 if(AJS.isObject(data)) {
548 post_data.push(k + "=" + AJS.urlencode(data[k]));
550 post_data = post_data.join("&");
551 this.req.send(post_data);
553 else if(AJS.isDefined(data))
560 AJSDeferred.prototype = new AJSDeferred();
568 Last Modified: 28/04/06 15:26:06
571 Google spell checker for your own web-apps :)
572 Copyright Amir Salihefendic 2006
574 GPL (see gpl.txt for more information)
575 This basically means that you can't use this script with/in proprietary software!
576 There is another license that permits you to use this script with proprietary software. Check out:... for more info.
578 4mir Salihefendic (http://amix.dk) - amix@amix.dk
582 var GOOGIE_CUR_LANG = "en";
584 function GoogieSpell(img_dir, server_url) {
587 cookie_value = getCookie('language');
589 if(cookie_value != null)
590 GOOGIE_CUR_LANG = cookie_value;
592 this.img_dir = img_dir;
593 this.server_url = server_url;
595 this.lang_to_word = {"da": "Dansk", "de": "Deutsch", "en": "English",
596 "es": "Español", "fr": "Français", "it": "Italiano",
597 "nl": "Nederlands", "pl": "Polski", "pt": "Português",
598 "fi": "Suomi", "sv": "Svenska"};
599 this.langlist_codes = AJS.keys(this.lang_to_word);
601 this.show_change_lang_pic = true;
603 this.lang_state_observer = null;
605 this.spelling_state_observer = null;
608 this.error_window = null;
609 this.language_window = null;
610 this.edit_layer = null;
611 this.orginal_text = null;
613 this.text_area = null;
615 this.ta_scroll_top = 0;
616 this.el_scroll_top = 0;
618 this.lang_chck_spell = "Check spelling";
619 this.lang_rsm_edt = "Resume editing";
620 this.lang_close = "Close";
621 this.lang_no_error_found = "No spelling errors found";
622 this.lang_revert = "Revert to";
623 this.show_spell_img = false; // modified by roundcube
626 GoogieSpell.prototype.setStateChanged = function(current_state) {
627 if(this.spelling_state_observer != null)
628 this.spelling_state_observer(current_state);
631 GoogieSpell.item_onmouseover = function(e) {
632 var elm = GoogieSpell.getEventElm(e);
633 if(elm.className != "googie_list_close" && elm.className != "googie_list_revert")
634 elm.className = "googie_list_onhover";
636 elm.parentNode.className = "googie_list_onhover";
639 GoogieSpell.item_onmouseout = function(e) {
640 var elm = GoogieSpell.getEventElm(e);
641 if(elm.className != "googie_list_close" && elm.className != "googie_list_revert")
642 elm.className = "googie_list_onout";
644 elm.parentNode.className = "googie_list_onout";
647 GoogieSpell.prototype.getGoogleUrl = function() {
648 return this.server_url + GOOGIE_CUR_LANG;
651 GoogieSpell.prototype.spellCheck = function(elm, name) {
652 this.ta_scroll_top = this.text_area.scrollTop;
654 this.appendIndicator(elm);
657 this.hideLangWindow();
663 this.createEditLayer(this.text_area.offsetWidth, this.text_area.offsetHeight);
665 this.createErrorWindow();
666 AJS.getBody().appendChild(this.error_window);
668 try { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); }
671 this.gselm.onclick = null;
673 this.orginal_text = this.text_area.value;
677 var d = AJS.getRequest(this.getGoogleUrl());
678 var reqdone = function(req) {
679 var r_text = req.responseText;
680 if(r_text.match(/<c.*>/) != null) {
681 var results = GoogieSpell.parseResult(r_text);
682 //Before parsing be sure that errors were found
683 me.results = results;
684 me.showErrorsInIframe(results);
685 me.resumeEditingState();
688 me.flashNoSpellingErrorState();
690 me.removeIndicator();
693 var reqfailed = function(req) {
694 alert("An error was encountered on the server. Please try again later.");
695 AJS.removeElement(me.gselm);
696 me.checkSpellingState();
697 me.removeIndicator();
700 d.addCallback(reqdone);
701 d.addErrback(reqfailed);
703 var req_text = GoogieSpell.escapeSepcial(this.orginal_text);
704 d.sendReq(GoogieSpell.createXMLReq(req_text));
707 GoogieSpell.escapeSepcial = function(val) {
708 return val.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
711 GoogieSpell.createXMLReq = function (text) {
712 return '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' + text + '</text></spellrequest>';
716 //result[item] -> ['attrs']
718 GoogieSpell.parseResult = function(r_text) {
719 var re_split_attr_c = /\w="\d+"/g;
720 var re_split_text = /\t/g;
722 var matched_c = r_text.match(/<c[^>]*>[^<]*<\/c>/g);
723 var results = new Array();
725 for(var i=0; i < matched_c.length; i++) {
726 var item = new Array();
729 item['attrs'] = new Array();
730 var split_c = matched_c[i].match(re_split_attr_c);
731 for(var j=0; j < split_c.length; j++) {
732 var c_attr = split_c[j].split(/=/);
733 item['attrs'][c_attr[0]] = parseInt(c_attr[1].replace('"', ''));
737 item['suggestions'] = new Array();
738 var only_text = matched_c[i].replace(/<[^>]*>/g, "");
739 var split_t = only_text.split(re_split_text);
740 for(var k=0; k < split_t.length; k++) {
742 item['suggestions'].push(split_t[k]);
750 Error window (the drop-down window)
752 GoogieSpell.prototype.createErrorWindow = function() {
753 this.error_window = AJS.DIV();
754 this.error_window.className = "googie_window";
757 GoogieSpell.prototype.hideErrorWindow = function() {
758 this.error_window.style.visibility = "hidden";
761 GoogieSpell.prototype.updateOrginalText = function(offset, old_value, new_value, id) {
762 var part_1 = this.orginal_text.substring(0, offset);
763 var part_2 = this.orginal_text.substring(offset+old_value.length);
764 this.orginal_text = part_1 + new_value + part_2;
765 var add_2_offset = new_value.length - old_value.length;
766 for(var j=0; j < this.results.length; j++) {
767 //Don't edit the offset of the current item
768 if(j != id && j > id){
769 this.results[j]['attrs']['o'] += add_2_offset;
774 GoogieSpell.prototype.saveOldValue = function (id, old_value) {
775 this.results[id]['is_changed'] = true;
776 this.results[id]['old_value'] = old_value;
779 GoogieSpell.prototype.showErrorWindow = function(elm, id) {
782 var abs_pos = GoogieSpell.absolutePosition(elm);
783 abs_pos.y -= this.edit_layer.scrollTop;
784 this.error_window.style.visibility = "visible";
785 this.error_window.style.top = (abs_pos.y+20) + "px";
786 this.error_window.style.left = (abs_pos.x) + "px";
787 this.error_window.innerHTML = "";
789 //Build up the result list
790 var table = AJS.TABLE({'class': 'googie_list'});
791 var list = AJS.TBODY();
793 var suggestions = this.results[id]['suggestions'];
794 var offset = this.results[id]['attrs']['o'];
795 var len = this.results[id]['attrs']['l'];
797 if(suggestions.length == 0) {
800 var dummy = AJS.SPAN();
801 item.appendChild(AJS.TN("No suggestions :("));
802 row.appendChild(item);
803 list.appendChild(row);
806 for(i=0; i < suggestions.length; i++) {
809 var dummy = AJS.SPAN();
810 dummy.innerHTML = suggestions[i];
811 item.appendChild(AJS.TN(dummy.innerHTML));
813 item.onclick = function(e) {
814 var l_elm = GoogieSpell.getEventElm(e);
815 var old_value = elm.innerHTML;
816 var new_value = l_elm.innerHTML;
818 elm.style.color = "green";
819 elm.innerHTML = l_elm.innerHTML;
820 me.hideErrorWindow();
822 me.updateOrginalText(offset, old_value, new_value, id);
824 //Update to the new length
825 me.results[id]['attrs']['l'] = new_value.length;
826 me.saveOldValue(id, old_value);
828 item.onmouseover = GoogieSpell.item_onmouseover;
829 item.onmouseout = GoogieSpell.item_onmouseout;
830 row.appendChild(item);
831 list.appendChild(row);
834 //The element is changed, append the revert
835 if(this.results[id]['is_changed']) {
836 var old_value = this.results[id]['old_value'];
837 var offset = this.results[id]['attrs']['o'];
838 var revert_row = AJS.TR();
839 var revert = AJS.TD();
841 revert.onmouseover = GoogieSpell.item_onmouseover;
842 revert.onmouseout = GoogieSpell.item_onmouseout;
843 var rev_span = AJS.SPAN({'class': 'googie_list_revert'});
844 rev_span.innerHTML = this.lang_revert + " " + old_value;
845 revert.appendChild(rev_span);
847 revert.onclick = function(e) {
848 me.updateOrginalText(offset, elm.innerHTML, old_value, id);
849 elm.style.color = "#b91414";
850 elm.innerHTML = old_value;
851 me.hideErrorWindow();
854 revert_row.appendChild(revert);
855 list.appendChild(revert_row);
858 //Append the edit box
859 var edit_row = AJS.TR();
862 var edit_input = AJS.INPUT({'style': 'width: 120px; margin:0; padding:0'});
864 var onsub = function () {
865 if(edit_input.value != "") {
866 me.saveOldValue(id, elm.innerHTML);
867 me.updateOrginalText(offset, elm.innerHTML, edit_input.value, id);
868 elm.style.color = "green"
869 elm.innerHTML = edit_input.value;
871 me.hideErrorWindow();
876 var ok_pic = AJS.IMG({'src': this.img_dir + "ok.gif", 'style': 'width: 32px; height: 16px; margin-left: 2px; margin-right: 2px;'});
877 var edit_form = AJS.FORM({'style': 'margin: 0; padding: 0'}, edit_input, ok_pic);
878 ok_pic.onclick = onsub;
879 edit_form.onsubmit = onsub;
881 edit.appendChild(edit_form);
882 edit_row.appendChild(edit);
883 list.appendChild(edit_row);
886 var close_row = AJS.TR();
887 var close = AJS.TD();
889 close.onmouseover = GoogieSpell.item_onmouseover;
890 close.onmouseout = GoogieSpell.item_onmouseout;
892 var spn_close = AJS.SPAN({'class': 'googie_list_close'});
893 spn_close.innerHTML = this.lang_close;
894 close.appendChild(spn_close);
895 close.onclick = function() { me.hideErrorWindow()};
896 close_row.appendChild(close);
897 list.appendChild(close_row);
899 table.appendChild(list);
900 this.error_window.appendChild(table);
905 Edit layer (the layer where the suggestions are stored)
907 GoogieSpell.prototype.createEditLayer = function(width, height) {
908 this.edit_layer = AJS.DIV({'class': 'googie_edit_layer'});
910 //Set the style so it looks like edit areas
911 this.edit_layer.className = this.text_area.className;
912 this.edit_layer.style.border = "1px solid #999";
913 this.edit_layer.style.overflow = "auto";
914 this.edit_layer.style.backgroundColor = "#F1EDFE";
915 this.edit_layer.style.padding = "3px";
917 this.edit_layer.style.width = (width-8) + "px";
918 this.edit_layer.style.height = height + "px";
921 GoogieSpell.prototype.resumeEditing = function(e, me) {
922 this.setStateChanged("check_spelling");
923 me.switch_lan_pic.style.display = "inline";
925 this.el_scroll_top = me.edit_layer.scrollTop;
927 var elm = GoogieSpell.getEventElm(e);
928 AJS.replaceChildNodes(elm, this.createSpellDiv());
930 elm.onclick = function(e) {
931 me.spellCheck(elm, me.text_area.id);
933 me.hideErrorWindow();
935 //Remove the EDIT_LAYER
936 me.edit_layer.parentNode.removeChild(me.edit_layer);
938 me.text_area.value = me.orginal_text;
939 AJS.showElement(me.text_area);
940 me.gselm.className = "googie_no_style";
942 me.text_area.scrollTop = this.el_scroll_top;
944 elm.onmouseout = null;
947 GoogieSpell.prototype.createErrorLink = function(text, id) {
948 var elm = AJS.SPAN({'class': 'googie_link'});
950 elm.onclick = function () {
951 me.showErrorWindow(elm, id);
953 elm.innerHTML = text;
957 GoogieSpell.createPart = function(txt_part) {
960 var result = AJS.SPAN();
963 var is_safari = (navigator.userAgent.toLowerCase().indexOf("safari") != -1);
965 var part = AJS.SPAN();
966 txt_part = GoogieSpell.escapeSepcial(txt_part);
967 txt_part = txt_part.replace(/\n/g, "<br>");
968 txt_part = txt_part.replace(/ /g, " ");
969 txt_part = txt_part.replace(/^ /g, " ");
970 txt_part = txt_part.replace(/ $/g, " ");
972 part.innerHTML = txt_part;
977 GoogieSpell.prototype.showErrorsInIframe = function(results) {
978 var output = AJS.DIV();
979 output.style.textAlign = "left";
981 for(var i=0; i < results.length; i++) {
982 var offset = results[i]['attrs']['o'];
983 var len = results[i]['attrs']['l'];
985 var part_1_text = this.orginal_text.substring(pointer, offset);
986 var part_1 = GoogieSpell.createPart(part_1_text);
987 output.appendChild(part_1);
988 pointer += offset - pointer;
990 //If the last child was an error, then insert some space
991 output.appendChild(this.createErrorLink(this.orginal_text.substr(offset, len), i));
994 //Insert the rest of the orginal text
995 var part_2_text = this.orginal_text.substr(pointer, this.orginal_text.length);
997 var part_2 = GoogieSpell.createPart(part_2_text);
998 output.appendChild(part_2);
1000 this.edit_layer.appendChild(output);
1003 AJS.hideElement(this.text_area);
1004 this.text_area.parentNode.insertBefore(this.edit_layer, this.text_area.nextSibling);
1005 this.edit_layer.scrollTop = this.ta_scroll_top;
1008 GoogieSpell.Position = function(x, y) {
1013 //Get the absolute position of menu_slide
1014 GoogieSpell.absolutePosition = function(element) {
1015 //Create a new object that has elements y and x pos...
1016 var posObj = new GoogieSpell.Position(element.offsetLeft, element.offsetTop);
1018 //Check if the element has an offsetParent - if it has .. loop until it has not
1019 if(element.offsetParent) {
1020 var temp_pos = GoogieSpell.absolutePosition(element.offsetParent);
1021 posObj.x += temp_pos.x;
1022 posObj.y += temp_pos.y;
1027 GoogieSpell.getEventElm = function(e) {
1029 if (!e) var e = window.event;
1030 if (e.target) targ = e.target;
1031 else if (e.srcElement) targ = e.srcElement;
1032 if (targ.nodeType == 3) // defeat Safari bug
1033 targ = targ.parentNode;
1037 GoogieSpell.prototype.removeIndicator = function(elm) {
1038 // modified by roundcube
1039 if (window.rcube_webmail_client)
1040 rcube_webmail_client.set_busy(false);
1041 //AJS.removeElement(this.indicator);
1044 GoogieSpell.prototype.appendIndicator = function(elm) {
1045 // modified by roundcube
1046 if (window.rcube_webmail_client)
1047 rcube_webmail_client.set_busy(true, 'checking');
1049 var img = AJS.IMG({'src': this.img_dir + 'indicator.gif', 'style': 'margin-right: 5px;'});
1050 img.style.width = "16px";
1051 img.style.height = "16px";
1052 this.indicator = img;
1053 img.style.textDecoration = "none";
1054 AJS.insertBefore(img, elm);
1061 GoogieSpell.prototype.createLangWindow = function() {
1062 this.language_window = AJS.DIV({'class': 'googie_window'});
1063 this.language_window.style.width = "130px";
1065 //Build up the result list
1066 var table = AJS.TABLE({'class': 'googie_list'});
1067 var list = AJS.TBODY();
1069 this.lang_elms = new Array();
1071 for(i=0; i < this.langlist_codes.length; i++) {
1073 var item = AJS.TD();
1074 item.googieId = this.langlist_codes[i];
1075 this.lang_elms.push(item);
1076 var lang_span = AJS.SPAN();
1077 lang_span.innerHTML = this.lang_to_word[this.langlist_codes[i]];
1078 item.appendChild(AJS.TN(lang_span.innerHTML));
1082 item.onclick = function(e) {
1083 var elm = GoogieSpell.getEventElm(e);
1084 me.deHighlightCurSel();
1086 me.setCurrentLanguage(elm.googieId);
1088 if(me.lang_state_observer != null) {
1089 me.lang_state_observer();
1092 me.highlightCurSel();
1093 me.hideLangWindow();
1096 item.onmouseover = function(e) {
1097 var i_it = GoogieSpell.getEventElm(e);
1098 if(i_it.className != "googie_list_selected")
1099 i_it.className = "googie_list_onhover";
1101 item.onmouseout = function(e) {
1102 var i_it = GoogieSpell.getEventElm(e);
1103 if(i_it.className != "googie_list_selected")
1104 i_it.className = "googie_list_onout";
1107 row.appendChild(item);
1108 list.appendChild(row);
1111 this.highlightCurSel();
1114 var close_row = AJS.TR();
1115 var close = AJS.TD();
1116 close.onmouseover = GoogieSpell.item_onmouseover;
1117 close.onmouseout = GoogieSpell.item_onmouseout;
1118 var spn_close = AJS.SPAN({'class': 'googie_list_close'});
1119 spn_close.innerHTML = this.lang_close;
1120 close.appendChild(spn_close);
1122 close.onclick = function(e) {
1123 me.hideLangWindow(); GoogieSpell.item_onmouseout(e);
1125 close_row.appendChild(close);
1126 list.appendChild(close_row);
1128 table.appendChild(list);
1129 this.language_window.appendChild(table);
1132 GoogieSpell.prototype.setCurrentLanguage = function(lan_code) {
1133 GOOGIE_CUR_LANG = lan_code;
1136 var now = new Date();
1137 now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
1138 setCookie('language', lan_code, now);
1141 GoogieSpell.prototype.hideLangWindow = function() {
1142 this.language_window.style.visibility = "hidden";
1143 this.switch_lan_pic.className = "googie_lang_3d_on";
1146 GoogieSpell.prototype.deHighlightCurSel = function() {
1147 this.lang_cur_elm.className = "googie_list_onout";
1150 GoogieSpell.prototype.highlightCurSel = function() {
1151 for(var i=0; i < this.lang_elms.length; i++) {
1152 if(this.lang_elms[i].googieId == GOOGIE_CUR_LANG) {
1153 this.lang_elms[i].className = "googie_list_selected";
1154 this.lang_cur_elm = this.lang_elms[i];
1157 this.lang_elms[i].className = "googie_list_onout";
1162 GoogieSpell.prototype.showLangWindow = function(elm, ofst_top, ofst_left) {
1163 if(!AJS.isDefined(ofst_top))
1165 if(!AJS.isDefined(ofst_left))
1168 this.createLangWindow();
1169 AJS.getBody().appendChild(this.language_window);
1171 var abs_pos = GoogieSpell.absolutePosition(elm);
1172 AJS.showElement(this.language_window);
1173 this.language_window.style.top = (abs_pos.y+ofst_top) + "px";
1174 this.language_window.style.left = (abs_pos.x+ofst_left-this.language_window.offsetWidth) + "px";
1175 this.highlightCurSel();
1176 this.language_window.style.visibility = "visible";
1179 GoogieSpell.prototype.flashNoSpellingErrorState = function() {
1180 this.setStateChanged("no_error_found");
1182 AJS.hideElement(this.switch_lan_pic);
1183 this.gselm.innerHTML = this.lang_no_error_found;
1184 this.gselm.className = "googie_check_spelling_ok";
1185 this.gselm.style.textDecoration = "none";
1186 this.gselm.style.cursor = "default";
1187 var fu = function() {
1188 AJS.removeElement(me.gselm);
1189 me.checkSpellingState();
1191 setTimeout(fu, 1000);
1194 GoogieSpell.prototype.resumeEditingState = function() {
1195 this.setStateChanged("resume_editing");
1197 AJS.hideElement(me.switch_lan_pic);
1199 //Change link text to resume
1200 me.gselm.innerHTML = this.lang_rsm_edt;
1201 me.gselm.onclick = function(e) {
1202 me.resumeEditing(e, me);
1204 me.gselm.className = "googie_check_spelling_ok";
1205 me.edit_layer.scrollTop = me.ta_scroll_top;
1208 GoogieSpell.prototype.createChangeLangPic = function() {
1209 var switch_lan = AJS.A({'class': 'googie_lang_3d_on', 'style': 'padding-left: 6px;'}, AJS.IMG({'src': this.img_dir + 'change_lang.gif', 'alt': "Change language"}));
1210 switch_lan.onmouseover = function() {
1211 if(this.className != "googie_lang_3d_click")
1212 this.className = "googie_lang_3d_on";
1216 switch_lan.onclick = function() {
1217 if(this.className == "googie_lang_3d_click") {
1218 me.hideLangWindow();
1221 me.showLangWindow(switch_lan);
1222 this.className = "googie_lang_3d_click";
1228 GoogieSpell.prototype.createSpellDiv = function() {
1229 var chk_spell = AJS.SPAN({'class': 'googie_check_spelling_link'});
1230 chk_spell.innerHTML = this.lang_chck_spell;
1231 var spell_img = null;
1232 if(this.show_spell_img)
1233 spell_img = AJS.IMG({'src': this.img_dir + "spellc.gif"});
1234 return AJS.SPAN(spell_img, " ", chk_spell);
1237 GoogieSpell.prototype.checkSpellingState = function() {
1238 this.setStateChanged("check_spelling");
1240 if(this.show_change_lang_pic)
1241 this.switch_lan_pic = this.createChangeLangPic();
1243 this.switch_lan_pic = AJS.SPAN();
1245 var span_chck = this.createSpellDiv();
1246 span_chck.onclick = function() {
1247 me.spellCheck(span_chck);
1249 AJS.appendChildNodes(this.spell_container, span_chck, " ", this.switch_lan_pic);
1250 // modified by roundcube
1251 this.check_link = span_chck;
1254 GoogieSpell.prototype.setLanguages = function(lang_dict) {
1255 this.lang_to_word = lang_dict;
1256 this.langlist_codes = AJS.keys(lang_dict);
1259 GoogieSpell.prototype.decorateTextarea = function(id, /*optional*/spell_container_id, force_width) {
1262 if(typeof(id) == "string")
1263 this.text_area = AJS.getElement(id);
1265 this.text_area = id;
1269 if(this.text_area != null) {
1270 if(AJS.isDefined(spell_container_id)) {
1271 if(typeof(spell_container_id) == "string")
1272 this.spell_container = AJS.getElement(spell_container_id);
1274 this.spell_container = spell_container_id;
1277 var table = AJS.TABLE();
1278 var tbody = AJS.TBODY();
1280 if(AJS.isDefined(force_width)) {
1281 r_width = force_width;
1284 r_width = this.text_area.offsetWidth + "px";
1287 var spell_container = AJS.TD();
1288 this.spell_container = spell_container;
1290 tr.appendChild(spell_container);
1292 tbody.appendChild(tr);
1293 table.appendChild(tbody);
1295 AJS.insertBefore(table, this.text_area);
1298 table.style.width = '100%'; // modified by roundcube (old: r_width)
1299 spell_container.style.width = r_width;
1300 spell_container.style.textAlign = "right";
1303 this.checkSpellingState();
1306 alert("Text area not found");