source: temp/trunk/html/test/naka/HTMLArea/plugins/CSS/css.js @ 1694

Revision 1694, 3.3 KB checked in by naka, 20 years ago (diff)

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// Simple CSS (className) plugin for the editor
2// Sponsored by http://www.miro.com.au
3// Implementation by Mihai Bazon, http://dynarch.com/mishoo.
4//
5// (c) dynarch.com 2003
6// Distributed under the same terms as HTMLArea itself.
7// This notice MUST stay intact for use (see license.txt).
8//
9// $Id$
10
11function CSS(editor, params) {
12    this.editor = editor;
13    var cfg = editor.config;
14    var toolbar = cfg.toolbar;
15    var self = this;
16    var i18n = CSS.I18N;
17    var plugin_config = params[0];
18    var combos = plugin_config.combos;
19
20    var first = true;
21    for (var i = combos.length; --i >= 0;) {
22        var combo = combos[i];
23        var id = "CSS-class" + i;
24        var css_class = {
25            id         : id,
26            options    : combo.options,
27            action     : function(editor) { self.onSelect(editor, this, combo.context, combo.updatecontextclass); },
28            refresh    : function(editor) { self.updateValue(editor, this); },
29            context    : combo.context
30        };
31        cfg.registerDropdown(css_class);
32
33        // prepend to the toolbar
34        toolbar[1].splice(0, 0, first ? "separator" : "space");
35        toolbar[1].splice(0, 0, id);
36        if (combo.label)
37            toolbar[1].splice(0, 0, "T[" + combo.label + "]");
38        first = false;
39    }
40};
41
42CSS._pluginInfo = {
43    name          : "CSS",
44    version       : "1.0",
45    developer     : "Mihai Bazon",
46    developer_url : "http://dynarch.com/mishoo/",
47    c_owner       : "Mihai Bazon",
48    sponsor       : "Miro International",
49    sponsor_url   : "http://www.miro.com.au",
50    license       : "htmlArea"
51};
52
53CSS.prototype.onSelect = function(editor, obj, context, updatecontextclass) {
54    var tbobj = editor._toolbarObjects[obj.id];
55    var index = tbobj.element.selectedIndex;
56    var className = tbobj.element.value;
57
58    // retrieve parent element of the selection
59    var parent = editor.getParentElement();
60    var surround = true;
61
62    var is_span = (parent && parent.tagName.toLowerCase() == "span");
63    var update_parent = (context && updatecontextclass && parent && parent.tagName.toLowerCase() == context);
64
65    if (update_parent) {
66        parent.className = className;
67        editor.updateToolbar();
68        return;
69    }
70
71    if (is_span && index == 0 && !/\S/.test(parent.style.cssText)) {
72        while (parent.firstChild) {
73            parent.parentNode.insertBefore(parent.firstChild, parent);
74        }
75        parent.parentNode.removeChild(parent);
76        editor.updateToolbar();
77        return;
78    }
79
80    if (is_span) {
81        // maybe we could simply change the class of the parent node?
82        if (parent.childNodes.length == 1) {
83            parent.className = className;
84            surround = false;
85            // in this case we should handle the toolbar updation
86            // ourselves.
87            editor.updateToolbar();
88        }
89    }
90
91    // Other possibilities could be checked but require a lot of code.  We
92    // can't afford to do that now.
93    if (surround) {
94        // shit happens ;-) most of the time.  this method works, but
95        // it's dangerous when selection spans multiple block-level
96        // elements.
97        editor.surroundHTML("<span class='" + className + "'>", "</span>");
98    }
99};
100
101CSS.prototype.updateValue = function(editor, obj) {
102    var select = editor._toolbarObjects[obj.id].element;
103    var parent = editor.getParentElement();
104    if (typeof parent.className != "undefined" && /\S/.test(parent.className)) {
105        var options = select.options;
106        var value = parent.className;
107        for (var i = options.length; --i >= 0;) {
108            var option = options[i];
109            if (value == option.value) {
110                select.selectedIndex = i;
111                return;
112            }
113        }
114    }
115    select.selectedIndex = 0;
116};
Note: See TracBrowser for help on using the repository browser.