source: trunk/plugins/LangMarks/lang-marks.js @ 999

Last change on this file since 999 was 999, checked in by ray, 11 years ago

#1195 Allow to specify an external url to load a plugin from

  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
File size: 3.6 KB
Line 
1// Mask Language plugin for Xinha
2// Implementation by Udo Schmal
3//
4// (c) Udo Schmal & Schaffrath NeueMedien 2004
5// Distributed under the same terms as HTMLArea itself.
6// This notice MUST stay intact for use (see license.txt).
7
8function LangMarks(editor, args) {
9  this.editor = editor;
10  var cfg = editor.config;
11  var self = this;
12  var options = {};
13  options[this._lc("— language —")] = '';
14        for (var i in cfg.LangMarks)
15        {
16                if (typeof i != 'string') continue;
17                options[this._lc(i)] = cfg.LangMarks[i];
18        }
19
20  cfg.registerDropdown({
21    id  : "langmarks",
22    tooltip     : this._lc("language select"),
23    options     : options,
24    action      : function(editor) { self.onSelect(editor, this); },
25    refresh     : function(editor) { self.updateValue(editor, this); }
26  });
27  cfg.addToolbarElement("langmarks", "inserthorizontalrule", 1);
28}
29
30LangMarks._pluginInfo = {
31  name          : "LangMarks",
32  version       : "1.0",
33  developer     : "Udo Schmal",
34  developer_url : "",
35  sponsor       : "L.N.Schaffrath NeueMedien",
36  sponsor_url   : "http://www.schaffrath-neuemedien.de/",
37  c_owner       : "Udo Schmal & Schaffrath NeueMedien",
38  license       : "htmlArea"
39};
40
41Xinha.Config.prototype.LangMarks = {
42  "Greek" : "el",
43  "English" : "en",
44  "French" : "fr",
45  "Latin" : "la"
46};
47
48LangMarks.prototype._lc = function(string) {
49  return Xinha._lc(string, 'LangMarks');
50};
51
52LangMarks.prototype.onGenerate = function() {
53         this.editor.addEditorStylesheet(Xinha.getPluginDir("LangMarks") + '/lang-marks.css');
54};
55
56LangMarks.prototype.onSelect = function(editor, obj, context, updatecontextclass) {
57  var tbobj = editor._toolbarObjects[obj.id];
58  var index = tbobj.element.selectedIndex;
59  var language = tbobj.element.value;
60
61  // retrieve parent element of the selection
62  var parent = editor.getParentElement();
63  var surround = true;
64
65  var is_span = (parent && parent.tagName.toLowerCase() == "span");
66  var update_parent = (context && updatecontextclass && parent && parent.tagName.toLowerCase() == context);
67
68  if (update_parent) {
69    parent.className = "haslang";
70    parent.lang = language;
71    editor.updateToolbar();
72    return;
73  }
74
75  if (is_span && index == 0 && !/\S/.test(parent.style.cssText)) {
76    while (parent.firstChild) {
77      parent.parentNode.insertBefore(parent.firstChild, parent);
78    }
79    parent.parentNode.removeChild(parent);
80    editor.updateToolbar();
81    return;
82  }
83
84  if (is_span) {
85    // maybe we could simply change the class of the parent node?
86    if (parent.childNodes.length == 1) {
87      parent.className = "haslang";
88      parent.lang = language;
89      surround = false;
90      // in this case we should handle the toolbar updation
91      // ourselves.
92      editor.updateToolbar();
93    }
94  }
95
96  // Other possibilities could be checked but require a lot of code.  We
97  // can't afford to do that now.
98  if (surround) {
99    // shit happens ;-) most of the time.  this method works, but
100    // it's dangerous when selection spans multiple block-level
101    // elements.
102    editor.surroundHTML('<span lang="' + language + '" class="haslang">', '</span>');
103  }
104};
105
106LangMarks.prototype.updateValue = function(editor, obj) {
107  var select = editor._toolbarObjects[obj.id].element;
108  var parents = editor.getAllAncestors();
109  var parent;
110        for (var i=0;i<parents.length;i++)
111        {
112                if (parents[i].lang) parent = parents[i];
113        }
114        if (parent) {
115    var options = select.options;
116    var value = parent.lang;
117    for (var i = options.length; --i >= 0;) {
118      var option = options[i];
119      if (value == option.value) {
120        select.selectedIndex = i;
121        return;
122      }
123    }
124  }
125  else select.selectedIndex = 0;
126
127};
Note: See TracBrowser for help on using the repository browser.