Monday, May 21, 2007

malayalam plug in for tinyMCE


 
tinyMCE.importPluginLanguagePack('template');
 
tinyMCE.importPluginLanguagePack('template');
var vowels = "(a(a|e|i|u)?)|(A|U|E|O|I|H)|(i(i)?)|(e(e|i)?)|(u(u)?)|(o(o|a|u)?)|(r~|r_|r |r`)|(R~|R_|R |R`)|(m_|m )|(n_|n )|(N_|N )|(l_|l )|(L_|L )|(_|~)"
 
var consonants = "(b(h)?)|(B(h)?)|(c(h(h)?)?)|(C(h)?)|(d(h)?)|(D(h)?)|(f|F)|(g(h)?)|(G)|(h)|(j(h)?)|(J(h)?)|(k(h)?)|(K)|(l)|(L)|(m|M)|(N(t)?)|(n(g|j|k|n|t(h)?)?)|(ph?)|(P)|(q)|(Q)|(r)|(R)|(s(h)?)|(S)|(t(t|h(h)?)?)|(T(h)?)|(v|V)|(w|W)|(x|X)|(y|Y)|(z(h)?)|(Z)"
 
var letter_codes = {
  "~a": "അ",
  "~aa": "ആ", "~A": "ആ",
  "~i": "ഇ",
  "~ee": "ഈ", "~ii": "ഈ", "~I": "ഈ",
  "~u": "ഉ",
  "~oo": "ഊ", "~uu": "ഊ", "~U": "ഊ",
  "~r~": "ഋ", "~R~": "ഋ",
  "~e": "എ",
  "~ae": "ഏ", "~E": "ഏ",
  "~ai": "ഐ", "~ei": "ഐ",
  "~o": "ഒ",
  "~oa": "ഓ", "~O": "ഓ",
  "~au": "ഔ", "~ou": "ഔ",
  "~m_": "ം", "~m ": "ം ", //anuswara
  "~H": "ഃ",  //visarga
  "~n_": "ന്‍", "~n ": "ന്‍ ", //chillu
  "~N_": "ണ്‍", "~N ": "ണ്‍ ",
  "~l_": "ല്‍", "~l ": "ല്‍ ", 
  "~L_": "ള്‍", "~L ": "ള്‍ ",
  "~r_": "ര്‍", "~r ": "ര്‍ ", 
  "~R_": "ര്‍", "~R ": "ര്‍ ",
  "~_": "്‌", //chandrakkala(Virama)
  "~~": "്‌", //chandrakkala(Virama)
  "~r`": "ര്‌", //ra with anusvara
  "~R`": "റ്‌", //rra with anusvara
  
  "m_": "ം",  "m ": "ം ​", //anuswara
  "H": "ഃ",   //visarga
  "a": "",
  "*": "്",
  "aa": "ാ", "A": "ാ",
  "i": "ി",
  "ee": "ീ", "ii": "ീ", "I": "ീ",
  "u": "ു",
  "oo": "ൂ", "uu": "ൂ", "U": "ൂ",
  "r~": "ൃ", "R~": "ൃ",
  "e": "െ",
  "ae": "േ", "E": "േ",
  "ai": "ൈ", "ei": "ൈ",
  "o": "ൊ",
  "oa": "ോ", "O": "ോ",
  "au": "ൌ", "ou": "ൌ",
  "n_": "ന്‍", "n ": "ന്‍ ", //chillu
  "N_": "ണ്‍", "N ": "ണ്‍ ", //chillu
   "l_": "ല്‍", "l ": "ല്‍ ", 
   "L_": "ള്‍", "L ": "ള്‍ ",
  "r_": "ര്‍", "r ": "ര്‍ ",
  "R_": "ര്‍", "R ": "ര്‍ ",
  "_": "്‌", //chandrakkala(Virama)
  "~": "്‌", //chandrakkala(Virama)
  "r``": "ര്‌", //ra with anusvara
  "R`": "റ്‌", //rra with anusvara
 
  "k": "ക", "c": "ക",
  "kh": "ഖ", "q": "ഖ", "Q": "ഖ",
  "g": "ഗ",
  "gh": "ഘ",
  "ng": "ങ",
  "ch": "ച", "C": "ച",
  "chh": "ഛ", "Ch": "ഛ",
  "j": "ജ",
  "jh": "ഝ",  "Jh": "ഝ",
  "nj": "ഞ",
  "T": "ട",
  "Th": "ഠ",
  "D": "ഡ",
  "Dh": "ഢ",
  "N": "ണ",
  "th": "ത",
  "thh": "ഥ",
  "d": "ദ",
  "dh": "ധ",
  "n": "ന",
  "p": "പ",
  "f": "ഫ",  "ph": "ഫ", "F": "ഫ",
  "b": "ബ",
  "bh": "ഭ",
  "m": "മ",
  "y": "യ",
  "r": "ര",
  "l": "ല",
  "v": "വ",   "w": "വ",
  "S": "ശ",   "z": "ശ",
  "sh": "ഷ",
  "s": "സ",
  "h": "ഹ",
  "L": "ള",
  "zh": "ഴ",
  "R": "റ",
  "mp": "മ്പ",
  "t": "റ്റ",
  "tt": "ട്ട",
  "nt": "ന്റ",
  "nth": "ന്ത",
  "nk": "ങ്ക",
  "nn": "ന്ന",
  "Nt": "ണ്ട",
  "B": "ബ്ബ",
  "G": "ഗ്ഗ",
  "J": "ജ്ജ",
  "K": "ക്ക",
  "M": "മ്മ",
  "P": "പ്പ",
  "V": "വ്വ", "W": "വ്വ",
  "x": "ക്ഷ", "X": "ക്ഷ",
  "Y": "യ്യ",
  "Z": "ശ്ശ"
  }

function split_word(word)
{
  var syllables = new Array(0);
  var vowel_start_p = true;
  while (word.length) {
    re = new RegExp(vowels);
    var index = word.search(vowels);
    if (index == 0) {  //the vowel's at the start of word
      var matched = re.exec(word)[0]; //what is it?
      if (vowel_start_p) {
    syllables.push(("~"+matched)); //one more to the syllables
      } else {
    syllables.push(matched);
      }
      vowel_start_p = true;
      word = word.substring(matched.length);
    } else {
      re = new RegExp(consonants);
      var index = word.search(consonants);
      if (index == 0) {
    var matched = re.exec(word)[0];
    syllables.push(matched);
    vowel_start_p = false;
    word = word.substring(matched.length);
 
    //look ahead for virama setting
    var next = word.search(vowels);
    if (next != 0 || word.length == 0)
      syllables.push('*');
      } else {
    syllables.push(word.charAt(0));
    word = word.substring(1);
      }
    }
  }
  return syllables;
}
 
function match_code(syllable_mcc)
{
  var matched = letter_codes[syllable_mcc];
 
  if (matched != null) return matched;
  return syllable_mcc;
}
 
function one_word(word_ow)
{
  if (!word_ow) return "";
  var syllables_ow = split_word(word_ow);
  var letters_ow = new Array(0);
 
  for (var i_ow = 0; i_ow < syllables_ow.length; i_ow++) {
    letters_ow.push(match_code(syllables_ow[i_ow]));
  }
  return letters_ow.join("");
}
 
function many_words(sentence)
{
  var regex = "((" + vowels + ")|(" + consonants + "))+";
  var words = new Array(0);
  while (sentence.length >= 1) {
    re = new RegExp("^``" + regex);
    var match = re.exec(sentence);
    if (match != null) {
      match = match[0];
      words.push("`");
      words.push(one_word(match.substring(2)));
      sentence = sentence.substring(match.length);
    } else {
      re = new RegExp("^`" + regex);
      match = re.exec(sentence);
      if (match != null) {
    match = match[0];
    words.push(match.substring(1));
    sentence = sentence.substring(match.length);
      } else {
    re = new RegExp("^" + regex);
    match = re.exec(sentence);
    if (match != null) {
      match = match[0];
      words.push(one_word(match));
      sentence = sentence.substring(match.length);
    } else {
      words.push(sentence.charAt(0));
      sentence = sentence.substring(1);
    }
      }
    }
  }
  return words.join("");
}


function pass_ml(oldstring1)
{

 var newstring
 var iter=0
oldstring=">"+oldstring1+"<"
newstring=oldstring.match(/>[a-z]*</g)
oldstring
 for(iter=0;iter< newstring.length;iter++)
 {
     
    oldstring=oldstring.replace(newstring[iter],">"+conv_ml(newstring[iter].substring(1,newstring[iter].length-1))+"<")

 }
oldstring1=oldstring.substring (1,oldstring.length-1)

return(oldstring1)

}
function conv_ml(index_pmw)
{

 
  var text_pmw = many_words(index_pmw);
 
  var ans = "";
  while (text_pmw.length) {
    var unicode_chars = /&#[0-9]+;/;
    re = unicode_chars;
    var matche = re.exec(text_pmw);
    if (matche != null) {
      matche = matche[0];
      search = text_pmw.search(unicode_chars);
      ans += text_pmw.substring(0, search);
      ans += String.fromCharCode(matche.match(/[0-9]+/));
      text_pmw = text_pmw.substring(search + matche.length);
    } else {
      ans += text_pmw.substring(0);
      text_pmw = "";
    }
  }

 
return ans;
  
}

var jey_jey=0;
var jey_btn=0;
var txteng="";
var txt2ml=""
var TinyMCE_TemplatePlugin = {
    getInfo : function() {
        return {
            longname : 'Template plugin',
            author : 'Moxiecode Systems AB',
            authorurl : 'http://www.moxiecode.com',
            infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/template',
            version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
        };
    },
 
    initInstance : function(inst) {
        var  content, x = 0, key, value, rvals, ds = inst.getData('template');
 
 
        // classes that specify where selected content should go
        content = tinyMCE.getParam("template_selected_content_classes", '').split(/\s+/);
 
         
 
        for (x = 0; x < content.length; x++)
            TinyMCE_TemplatePlugin.functions[content[x]] = TinyMCE_TemplatePlugin.functions['selectedContent'];
 
        // special template functions for replacing template content
        rvals = tinyMCE.getParam("template_replace_values", false);
        for (key in rvals) {
            value = rvals[key];
 
            if (typeof value == "function")
                TinyMCE_TemplatePlugin.functions[key] = value;
            else
                TinyMCE_TemplatePlugin.functions[key] = TinyMCE_TemplatePlugin.functions['generateReplacer'](value);
        }
 
        // Setup replace_items
        rvals = tinyMCE.getParam("template_replace_values", false);
        ds.replace_items = {};
 
        for (key in rvals)
            ds.replace_items[key] = rvals[key];
 
        inst.addShortcut('ctrl', 't', 'lang_template_desc', 'mceTemplate');
 
        // Setup data storage
        ds.currentAction = "insert";
        ds.currentTmplNode = null;
    },
 
    getControlHTML : function(cn) {
        switch (cn) {
            case "template":
                return tinyMCE.getButtonHTML(cn, 'lang_template_desc', '{$pluginurl}/images/template.gif', 'mceTemplate', true);
        }
 
        return "";
    },
 
    execCommand : function(editor_id, element, command, user_interface, value) {
        var nodeArray, current, newTmpl, x, inst = tinyMCE.getInstanceById(editor_id), ds = inst.getData('template'), telm;
 
        switch (command) {
            case "mceTemplate":
                if (user_interface)
                    {
                    jey_jey=(jey_jey+.5)%2;
                    if(jey_jey==0)
                        {
                        jey_btn=0;
                        alert("English Mode");
                        txteng="";
                       
                        }
                    else if(jey_jey==1)
                            {
                        jey_btn=1;
                        alert("Malayalam Mode");
                        txteng=tinyMCE.getContent();
                        txt2ml="";
                        }
                       
                        }
                     
 
                 
        }
 
        return false;
    },
 
    handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
        var inst = tinyMCE.getInstanceById(editor_id), ds = inst.getData('template');
 
        if (tinyMCE.hasCSSClass(node, TinyMCE_TemplatePlugin.TMPL_ELEMENT) || tinyMCE.hasCSSClass(node.parentNode, TinyMCE_TemplatePlugin.TMPL_ELEMENT)) {
            tinyMCE.switchClass(editor_id + '_template', 'mceButtonSelected');
            ds.currentAction = "update";
            ds.currentTmplNode = node;
 
            return true;
        }
 
        ds.currentAction = "insert";
        ds.currentTmplNode = null;
        tinyMCE.switchClass(editor_id + '_template', 'mceButtonNormal');
 
        return false;
    },
handleEvent : function(e) {
        var inst = tinyMCE.selectedInstance;
        var w = inst.getWin(), le = inst._lastStyleElm, e;
 
        if (tinyMCE.isGecko) {
            if(e.type=='keyup'&&jey_btn==1)
                {
        txt=tinyMCE.getContent();
        last_char=(txt).charAt(txt.length-1)
        txt2ml=txt2ml+last_char ;
        tinyMCE.setContent("");
        tinyMCE.selectedInstance.execCommand('mceInsertContent',true,txteng+pass_ml(txt2ml)); }
            }
        return true;
    },
 
    cleanup : function(type, content, inst) {
        var nodes = [];
 
        switch (type) {
             
            case "insert_to_editor":
                break;
 
         
        }
 
        return content;
    },
 
    // Private plugin internal methods
 
     
    _convertToNode : function(html) {
        var elm = document.createElement('div');
 
        elm.innerHTML = html;
 
        return elm;
    },
 
    /**
     * pass an array of template html elements and they will have the template class name added and any template functions applied
     *
     * @param {array} template HTML elements
     * @return array of template HTML elements
     * @type array
     */
    _prepareTemplateContent : function(elms) {
        var x, n, nodes = [];
 
        if (!elms)
            return {};
 
        if (!elms.length)
            elms = [elms];
 
        for (x = 0; x<elms.length; x++)
            tinyMCE.getNodeTree(elms[x], nodes, 1);
 
        for (n = 0; n<nodes.length; n++) {
            tinyMCE.addCSSClass(nodes[n], TinyMCE_TemplatePlugin.TMPL_ELEMENT);
            TinyMCE_TemplatePlugin._applyFunctions(nodes[n], TinyMCE_TemplatePlugin.TMPL_TEMPLATE_EVENT);
        }
 
        return elms;
    },
 
    _replaceValues : function(s) {
        var t = this, ds = tinyMCE.selectedInstance.getData('template');
 
        return s.replace(/\{\$([^\}]+)\}/g, function(a, b) {
            var it = ds.replace_items[b];
 
            if (it) {
                // Only supports text for now
                if (typeof(it) != 'function')
                    return it;
            }
 
            return b;
        });
    },
 
    /**
     * Applies any special functions to the template elements
     *
     * @param {array} template HTML elements
     * @return array of template HTML elements
     * @type array
     */
    _applyFunctions : function(elms, editor_event) {
        var x, elm, names, c, f;
 
        if (!elms)
            return {};
 
        if (!elms.length)
            elms = [elms];
 
        for(x = 0; x < elms.length; x++) {
            elm = elms[x];
 
            if (elm.className){
                names = elm.className.split(/\s+/);
 
                for (c = 0; c < names.length; c++){
                    if (names[c] == TinyMCE_TemplatePlugin.TMPL_ELEMENT)
                        continue;
 
                    f = (TinyMCE_TemplatePlugin.functions[names[c]] ? TinyMCE_TemplatePlugin.functions[names[c]] : TinyMCE_TemplatePlugin.functions['blank']);
                    f(elm, editor_event);
                }
            }
        }
 
        return elms;
    },
 
    /**
     * Given one node reference this function will collect all the nodes of the template to which it belongs.
     * It does this by finding the parent template wrapper DIV and returning all child nodes.
     *
     * @param {HTMLElement} a HTMLElement which is part of a template
     * @return array of template HTML elements
     * @type array
     */
    _collectTemplateElements : function(node) {
        var nodeArray = [], p;
 
        p = tinyMCE.getParentElement(node, 'DIV', function(n) {
            return tinyMCE.hasCSSClass(n, TinyMCE_TemplatePlugin.TMPL);
        });
 
        if (p)
            tinyMCE.getNodeTree(p, nodeArray);
 
        return nodeArray;
    },
 
    /**
     * Simply calls TinyMCE_TemplatePlugin._deleteTemplateContent and then TinyMCE_TemplatePlugin._insertTemplate
     *
     * @param {HTMLElement} currently selected template node in editor
     * @param {string} id of editor instance
     * @param {HTMLElement} template contents as a HTMLElement (the parent DIV wrapper)
     * @param {string} title of template (unused as yet)
     * @param {string} source URI of the template file (unused as yet)
     * @return array of template HTML elements
     * @type array
     */
    _replaceTemplateContent : function(currentNode, editor_id, newTemplate, title, tsrc) {
        TinyMCE_TemplatePlugin._deleteTemplateContent(currentNode);
        TinyMCE_TemplatePlugin._insertTemplate(editor_id, newTemplate, title, tsrc, false);
    },
 
    /**
     * Deletes a template from the editor content
     * Finds the parent DIV wrapper and deletes it and all children
     * @param {HTMLElement} currently selected template node in editor
     */
    _deleteTemplateContent : function(node) {
        var p = tinyMCE.getParentElement(node, 'DIV', function(n) {
            return tinyMCE.hasCSSClass(n, TinyMCE_TemplatePlugin.TMPL);
        });
 
        if (p)
            p.parentNode.removeChild(p, true);
    },
 
    /**
     *  Inserts a template into the specified editor
     *
     * @param {string} id of editor instance
     * @param {HTMLElement} template contents as a HTMLElement (the parent DIV wrapper)
     * @param {string} title of template (unused as yet)
     * @param {string} source URI of the template file (unused as yet)
     */
    _insertTemplate : function(editor_id, elm, title, tsrc, incComments) {
        var html;
 
        TinyMCE_TemplatePlugin._prepareTemplateContent(elm);
 
        html = '<div class="' + TinyMCE_TemplatePlugin.TMPL + '">';
        html += elm.innerHTML;
        html += '<!-- ' + TinyMCE_TemplatePlugin.TMPL_ENDS + ' --></div>';
 
        tinyMCE.execInstanceCommand(editor_id, 'mceInsertContent', false, html);
    },
     

     
    /**
     * template functions - functions for modifying template content
     */
    functions : { 
        blank : function(elm, editor_event) {},
         
         
         
 
        /**
         * This will insert the currently selected editor content into the template element.
         * It only does this if the template inserted is a new one and if the element does not have the special class.
         * The special class name prevents this from happening more than once.
         */
        selectedContent : function(elm, editor_event) {
            var ds = tinyMCE.selectedInstance.getData('template');
     
            if (editor_event != TinyMCE_TemplatePlugin.TMPL_TEMPLATE_EVENT)
                return;
 
            if (ds.currentAction == "insert-new" && !tinyMCE.hasCSSClass(elm, TinyMCE_TemplatePlugin.TMPL_SEL_HTML_DONE)) {
                elm.innerHTML = tinyMCE.selectedInstance.selection.getSelectedHTML();
                tinyMCE.addCSSClass(elm, TinyMCE_TemplatePlugin.TMPL_SEL_HTML_DONE);
            }
        },
 
        /**
         * When the plugin is initialised this generates the functions that insert configured strings into template elements.
         */
        generateReplacer : function(s) {
            return function(elm, editor_event) {elm.innerHTML = "" + s;};
        }
    },
 
    /**
     * formats a date according to the format string - straight from the 'insert date/time' plugin
     *
     * @param {Date} date object
     * @param {string} format string
     * @return formatted date
     * @type string
     */
    _getDateTime : function(d,fmt) {
            if (!fmt)
                return "";
 
            function addZeros(value, len) {
                var i;
 
                value = "" + value;
 
                if (value.length < len) {
                    for (i=0; i<(len-value.length); i++)
                        value = "0" + value;
                }
 
                return value;
            }
 
            fmt = fmt.replace("%D", "%m/%d/%y");
            fmt = fmt.replace("%r", "%I:%M:%S %p");
            fmt = fmt.replace("%Y", "" + d.getFullYear());
            fmt = fmt.replace("%y", "" + d.getYear());
            fmt = fmt.replace("%m", addZeros(d.getMonth()+1, 2));
            fmt = fmt.replace("%d", addZeros(d.getDate(), 2));
            fmt = fmt.replace("%H", "" + addZeros(d.getHours(), 2));
            fmt = fmt.replace("%M", "" + addZeros(d.getMinutes(), 2));
            fmt = fmt.replace("%S", "" + addZeros(d.getSeconds(), 2));
            fmt = fmt.replace("%I", "" + ((d.getHours() + 11) % 12 + 1));
            fmt = fmt.replace("%p", "" + (d.getHours() < 12 ? "AM" : "PM"));
            fmt = fmt.replace("%B", "" + tinyMCE.getLang("lang_template_months_long")[d.getMonth()]);
            fmt = fmt.replace("%b", "" + tinyMCE.getLang("lang_template_months_short")[d.getMonth()]);
            fmt = fmt.replace("%A", "" + tinyMCE.getLang("lang_template_day_long")[d.getDay()]);
            fmt = fmt.replace("%a", "" + tinyMCE.getLang("lang_template_day_short")[d.getDay()]);
            fmt = fmt.replace("%%", "%");
 
            return fmt;
    },
 
    TMPL_ELEMENT : 'mceTmplElm',
    TMPL : 'mceTmpl',
    TMPL_BEGINS : 'mceTmplBegins',
    TMPL_SEL_HTML_DONE : 'mceSelHTMLDone',
    TMPL_ENDS : 'mceTmplEnds',
    TMPL_DATE_SRC_ATTR : 'mcetmpldtesrc',
    TMPL_TEMPLATE_EVENT : 'prepare_template'
};
 
tinyMCE.addPlugin("template", TinyMCE_TemplatePlugin);
 

No comments:

Post a Comment