Statistics
| Branch: | Tag: | Revision:

root / static / js / tinymce / plugins / advlist / editor_plugin_src.js @ 6ecbf4ec

History | View | Annotate | Download (4 kB)

1
/**
2
 * editor_plugin_src.js
3
 *
4
 * Copyright 2009, Moxiecode Systems AB
5
 * Released under LGPL License.
6
 *
7
 * License: http://tinymce.moxiecode.com/license
8
 * Contributing: http://tinymce.moxiecode.com/contributing
9
 */
10

    
11
(function() {
12
        var each = tinymce.each;
13

    
14
        tinymce.create('tinymce.plugins.AdvListPlugin', {
15
                init : function(ed, url) {
16
                        var t = this;
17

    
18
                        t.editor = ed;
19

    
20
                        function buildFormats(str) {
21
                                var formats = [];
22

    
23
                                each(str.split(/,/), function(type) {
24
                                        formats.push({
25
                                                title : 'advlist.' + (type == 'default' ? 'def' : type.replace(/-/g, '_')),
26
                                                styles : {
27
                                                        listStyleType : type == 'default' ? '' : type
28
                                                }
29
                                        });
30
                                });
31

    
32
                                return formats;
33
                        };
34

    
35
                        // Setup number formats from config or default
36
                        t.numlist = ed.getParam("advlist_number_styles") || buildFormats("default,lower-alpha,lower-greek,lower-roman,upper-alpha,upper-roman");
37
                        t.bullist = ed.getParam("advlist_bullet_styles") || buildFormats("default,circle,disc,square");
38
                },
39

    
40
                createControl: function(name, cm) {
41
                        var t = this, btn, format;
42

    
43
                        if (name == 'numlist' || name == 'bullist') {
44
                                // Default to first item if it's a default item
45
                                if (t[name][0].title == 'advlist.def')
46
                                        format = t[name][0];
47

    
48
                                function hasFormat(node, format) {
49
                                        var state = true;
50

    
51
                                        each(format.styles, function(value, name) {
52
                                                // Format doesn't match
53
                                                if (t.editor.dom.getStyle(node, name) != value) {
54
                                                        state = false;
55
                                                        return false;
56
                                                }
57
                                        });
58

    
59
                                        return state;
60
                                };
61

    
62
                                function applyListFormat() {
63
                                        var list, ed = t.editor, dom = ed.dom, sel = ed.selection;
64

    
65
                                        // Check for existing list element
66
                                        list = dom.getParent(sel.getNode(), 'ol,ul');
67

    
68
                                        // Switch/add list type if needed
69
                                        if (!list || list.nodeName == (name == 'bullist' ? 'OL' : 'UL') || hasFormat(list, format))
70
                                                ed.execCommand(name == 'bullist' ? 'InsertUnorderedList' : 'InsertOrderedList');
71

    
72
                                        // Append styles to new list element
73
                                        if (format) {
74
                                                list = dom.getParent(sel.getNode(), 'ol,ul');
75

    
76
                                                if (list) {
77
                                                        dom.setStyles(list, format.styles);
78
                                                        list.removeAttribute('_mce_style');
79
                                                }
80
                                        }
81
                                };
82

    
83
                                btn = cm.createSplitButton(name, {
84
                                        title : 'advanced.' + name + '_desc',
85
                                        'class' : 'mce_' + name,
86
                                        onclick : function() {
87
                                                applyListFormat();
88
                                        }
89
                                });
90

    
91
                                btn.onRenderMenu.add(function(btn, menu) {
92
                                        menu.onShowMenu.add(function() {
93
                                                var dom = t.editor.dom, list = dom.getParent(t.editor.selection.getNode(), 'ol,ul'), fmtList;
94

    
95
                                                if (list || format) {
96
                                                        fmtList = t[name];
97

    
98
                                                        // Unselect existing items
99
                                                        each(menu.items, function(item) {
100
                                                                var state = true;
101

    
102
                                                                item.setSelected(0);
103

    
104
                                                                if (list && !item.isDisabled()) {
105
                                                                        each(fmtList, function(fmt) {
106
                                                                                if (fmt.id == item.id) {
107
                                                                                        if (!hasFormat(list, fmt)) {
108
                                                                                                state = false;
109
                                                                                                return false;
110
                                                                                        }
111
                                                                                }
112
                                                                        });
113

    
114
                                                                        if (state)
115
                                                                                item.setSelected(1);
116
                                                                }
117
                                                        });
118

    
119
                                                        // Select the current format
120
                                                        if (!list)
121
                                                                menu.items[format.id].setSelected(1);
122
                                                }
123
                                        });
124

    
125
                                        menu.add({id : t.editor.dom.uniqueId(), title : 'advlist.types', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
126

    
127
                                        each(t[name], function(item) {
128
                                                item.id = t.editor.dom.uniqueId();
129

    
130
                                                menu.add({id : item.id, title : item.title, onclick : function() {
131
                                                        format = item;
132
                                                        applyListFormat();
133
                                                }});
134
                                        });
135
                                });
136

    
137
                                return btn;
138
                        }
139
                },
140

    
141
                getInfo : function() {
142
                        return {
143
                                longname : 'Advanced lists',
144
                                author : 'Moxiecode Systems AB',
145
                                authorurl : 'http://tinymce.moxiecode.com',
146
                                infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advlist',
147
                                version : tinymce.majorVersion + "." + tinymce.minorVersion
148
                        };
149
                }
150
        });
151

    
152
        // Register plugin
153
        tinymce.PluginManager.add('advlist', tinymce.plugins.AdvListPlugin);
154
})();