~dantrevino/ubuntu-us-florida-website/main

« back to all changes in this revision

Viewing changes to plugins/editors/jce/libraries/js/plugin.js

  • Committer: Dan Trevnio
  • Date: 2009-03-24 20:37:18 UTC
  • Revision ID: dantrevino@gmail.com-20090324203718-pg0e3lp4ztjjku9o
initialĀ siteĀ import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Class: Plugin
 
3
        Base plugin class for creating a JCE plugin object.
 
4
 
 
5
Arguments:
 
6
        options - optional, an object containing options.
 
7
 
 
8
Options:
 
9
        site - the base site url.
 
10
        plugin - the plugin name
 
11
        lang - the language code, eg: en.
 
12
        params - parameter object.
 
13
 
 
14
Example:
 
15
        var advlink = new Plugin('advlink', {params: {'key': 'value'}});
 
16
*/
 
17
var Plugin = new Class({
 
18
        // Dialog box default
 
19
        _dialog                 : [],
 
20
        // Arbitrary variable
 
21
        _vars                   : null,
 
22
        // Current plugin
 
23
        _plugin                 : null,
 
24
        getOptions : function(){
 
25
                return {
 
26
                        site : tinyMCEPopup.getParam('document_base_url'),
 
27
                        lang : 'en',
 
28
                        params : {},
 
29
                        alerts : null
 
30
                }
 
31
        },
 
32
        initialize : function(plugin, options){
 
33
                this.setOptions(this.getOptions(), options);            
 
34
                this._plugin = plugin;
 
35
 
 
36
                // fallback for no plugin set
 
37
                if(!this._plugin){
 
38
                        var q = string.query(document.location.href);
 
39
                        this._plugin = q['plugin'];
 
40
                }
 
41
                // show any alert dialogs
 
42
                this.showAlerts();
 
43
                // initialize tooltips
 
44
                this.initToolTip();
 
45
        },
 
46
        /*
 
47
         * Return site url option
 
48
         * @param {String} The site url variable
 
49
        */
 
50
        getSite : function(){
 
51
                return this.options.site;       
 
52
        },
 
53
        /*
 
54
         * Store custom plugin parameters
 
55
         * Example: {'animals': ['dog', 'cat', 'mouse']}
 
56
         * @param {Object} The parameters object
 
57
        */
 
58
        setParams : function(p){
 
59
                for(n in p){
 
60
                        this.setParam(n, p[n]);
 
61
                }
 
62
        },
 
63
        /*
 
64
         * Store a custom plugin parameter
 
65
         * Example: 'animals', ['dog', 'cat', 'mouse']
 
66
         * @param {string} The parameter key/name
 
67
         * @param {string/array/object} The value
 
68
        */
 
69
        setParam : function(p, v){
 
70
                this.options.params[p] = v;
 
71
        },
 
72
        /*
 
73
         * Return a custom plugin parameter
 
74
         * @param {string} The parameter key/name
 
75
        */
 
76
        getParam : function(p){
 
77
                return this.options.params[p] || false;
 
78
        },
 
79
        /*
 
80
         * Set the plugin as current
 
81
         * @param {string} The plugin name
 
82
        */
 
83
        setPlugin : function(p){
 
84
                this._plugin = p;
 
85
        },
 
86
        /*
 
87
         * Return the current plugin
 
88
         * @return {string} The plugin name
 
89
        */
 
90
        getPlugin : function(){
 
91
                return this._plugin;    
 
92
        },
 
93
        /*
 
94
         * Return a full resource url
 
95
         * @param {string} The url type, eg: img, plugin
 
96
         * @return {string} The url
 
97
        */
 
98
        getUrl : function( type ){
 
99
                if( type == 'plugins' ){
 
100
                        type = 'tiny_mce/plugins/' + this.getPlugin();
 
101
                }
 
102
                return string.path(this.options.site, '/plugins/editors/jce/' + type);
 
103
        },
 
104
        /*
 
105
         * Return a full image url
 
106
         * @param {string} The image name
 
107
         * @return {string} The url
 
108
        */
 
109
        getImage : function(name){
 
110
                var parts       = name.split('.');
 
111
                var path        = parts[0].replace(/[^a-z0-9-_]/i, '');
 
112
                var file        = parts[1].replace(/[^a-z0-9-_]/i, '');
 
113
                var ext         = parts[2].replace(/[^a-z0-9-_]/i, '');
 
114
                
 
115
                return this.getUrl(path) + '/img/' + file + '.' + ext;
 
116
        },
 
117
        /*
 
118
         * Resolve a TinyMCE language string
 
119
         * @param {string} The variable name
 
120
         * @param {string} The default translation
 
121
         * @return {string} The language string
 
122
        */
 
123
        getLang : function(s, dv){
 
124
                return tinyMCEPopup.getLang(s, dv);
 
125
        },
 
126
        /*
 
127
         * Loads a TinyMCE plugin or theme dialog language file. Requires asset.js
 
128
         * @param {string} The variable name
 
129
         * @param {string} The default translation
 
130
         * @return {string} The language string
 
131
        */
 
132
        loadLanguage : function(name){
 
133
                var path = '', parts = '', file = '';
 
134
                if(name){
 
135
                        parts   = name.split('.');
 
136
                        path    = parts[0].replace(/[^a-z0-9-_]/i, '');
 
137
                        file    = parts[1].replace(/[^a-z0-9-_]/i, '');
 
138
                        path    = path + '/' + file + '/';
 
139
                }
 
140
                var u = this.options.site + '/plugins/editors/jce/tiny_mce/' + path + 'langs/' + this.options.lang + '_dlg.js';
 
141
                new Asset.javascript(u);
 
142
        },
 
143
        setVars: function(vars){
 
144
                this._vars = vars;
 
145
        },
 
146
        getVars: function(){
 
147
                return this._vars;
 
148
        },
 
149
        /*
 
150
        * Add a dialog object
 
151
        * @param {String} The dialog name
 
152
        * @param {String} The dialog object
 
153
        */
 
154
        addDialog : function(name, dialog){
 
155
                this._dialog[name] = dialog;
 
156
        },
 
157
        /*
 
158
        * Get a dialog object
 
159
        * @param {String} The dialog name
 
160
        * @return the dialog object
 
161
        */
 
162
        getDialog : function(name){
 
163
                return this._dialog[name] || '';
 
164
        },
 
165
        /*
 
166
        * Remove a dialog object
 
167
        * Shortcut for closing a dialog too
 
168
        * @param {String} The dialog name
 
169
        */
 
170
        removeDialog : function(name){
 
171
                if(typeof this._dialog[name].close() != 'undefined'){
 
172
                        this._dialog[name].close();     
 
173
                }
 
174
                delete this._dialog[name];
 
175
        },
 
176
        /*
 
177
         * Open help window for current language
 
178
        */
 
179
        openHelp : function(type){
 
180
                if(!type) type = 'standard';
 
181
                tinyMCE.activeEditor.windowManager.open({
 
182
                        url : this.options.site + 'index.php?option=com_jce&task=help&lang='+ this.options.lang +'&plugin='+ this._plugin +'&type='+ type +'&file=help',
 
183
                        width : 640,
 
184
                    height : 480,
 
185
                    resizable : "yes",
 
186
            inline : "yes",
 
187
                close_previous : "no"
 
188
                });
 
189
        },
 
190
        iframe : function(fn, cb){
 
191
                new IFrame({
 
192
                        auto: true,
 
193
                        action: fn,
 
194
                        onComplete: function(o){
 
195
                                if(o.error){
 
196
                                        alert(o.error);
 
197
                                }else{
 
198
                                        r = o.result || {error: false};
 
199
                                        if(cb){
 
200
                                                cb.pass(r, this)();     
 
201
                                        }else{
 
202
                                                return r;       
 
203
                                        }
 
204
                                }       
 
205
                        }
 
206
                });
 
207
        },
 
208
        /*
 
209
         * XHR request. Requires json.js
 
210
         * @param {string} The target function to call
 
211
         * @param {array} An array of arguments
 
212
         * @param {function} The callback function on success
 
213
        */
 
214
        xhr : function(fn, args, cb){
 
215
                new Json.Remote(document.location.href, {
 
216
                        onComplete: function(o){
 
217
                                if(o.error){
 
218
                                        alert(o.error); 
 
219
                                }
 
220
                                r = o.result || {error: false};
 
221
                                if(cb){
 
222
                                        cb.pass(r, this)();     
 
223
                                }else{
 
224
                                        return r;       
 
225
                                }
 
226
                        }.bind(this),
 
227
                        onFailure: function(x){
 
228
                                alert('Request failed with status code: '+ x.status);   
 
229
                        }
 
230
                }).send({'fn': fn, 'args': args});
 
231
        },
 
232
        /*
 
233
         * Alerts. Requires window.js
 
234
        */
 
235
        showAlerts : function(){
 
236
                var alerts = this.options.alerts || [];
 
237
                if(alerts.length){
 
238
                        var h = '<dl class="alert">';
 
239
                        alerts.each(function(a){
 
240
                                h += '<dt class="' + a['class'] + '">' + a['title'] + '</dt><dd>' + a['text'] + '</dd>';                                
 
241
                        });
 
242
                        h += '</dl>'
 
243
                        new Alert(h, {height: 150 + alerts.length * 50});
 
244
                }
 
245
        },
 
246
        initToolTip : function(elms){
 
247
                this.tooltip =  new Tips($$('.hastip'), {
 
248
                        className : 'tooltip',
 
249
                        fixed: true,
 
250
                        offsets: {'x': 24, 'y': 24}
 
251
                });     
 
252
        },
 
253
        addToolTip : function(el){
 
254
                if($type(el) != 'array'){
 
255
                        el = [el];      
 
256
                }
 
257
                el.each(function(e){
 
258
                        this.tooltip.build(e);                           
 
259
                }.bind(this))
 
260
        }
 
261
});
 
262
Plugin.implement(new Events, new Options);
 
263
/* IFrame class for pseudo ajax/json stuff */
 
264
var IFrame = new Class({
 
265
        getOptions : function(){
 
266
                return {                        
 
267
                        form: $E('form'),
 
268
                        frame: 'iframe',
 
269
                        auto: false,
 
270
                        action: null,
 
271
                        onStart: Class.empty,
 
272
                        onComplete: Class.empty
 
273
                };
 
274
        },
 
275
        initialize : function(options){
 
276
                this.setOptions(this.getOptions(), options);
 
277
                if (this.options.initialize) this.options.initialize.call(this);
 
278
                
 
279
                if($(this.options.frame)){
 
280
                        $(this.options.frame).remove(); 
 
281
                }
 
282
                this.frame      = new Element('iframe').setProperties({
 
283
                        'src': 'about:blank', 
 
284
                        'name': this.options.frame,
 
285
                        'id': this.options.frame
 
286
                }).setStyle('display', 'none').injectInside($E('form'))
 
287
                
 
288
                if(window.ie){
 
289
                        window.frames[this.frame.id].name = this.frame.name;
 
290
                }
 
291
                this.options.form.setAttribute('target', this.frame.name);
 
292
                
 
293
                this.action = this.options.form.action;
 
294
                this.setAction();
 
295
                
 
296
                this.options.form.addEvent('submit', function(){
 
297
                        this.fireEvent('onStart');
 
298
                }.bind(this));
 
299
                
 
300
                this.frame.addEvent('load', function(){
 
301
                        var f   = $(this.frame);
 
302
                        var el  = f.contentWindow.document || f.contentDocument || window.frames[f.id].document;
 
303
                        if(el.location.href == 'about:blank') return;
 
304
                        var res = el.body.innerHTML;
 
305
                        if(res !== ''){
 
306
                                this.fireEvent('onComplete', Json.evaluate(res, true));
 
307
                        }
 
308
                        this.resetAction();
 
309
                }.bind(this))
 
310
                
 
311
                if(this.options.auto){
 
312
                        this.options.form.submit();     
 
313
                }
 
314
        },
 
315
        setAction : function(){
 
316
                this.resetAction();
 
317
                if(this.options.action){
 
318
                        this.action += '&action=' + this.options.action;        
 
319
                }
 
320
                this.options.form.setAttribute('action', this.action);
 
321
        },
 
322
        resetAction : function(){
 
323
                this.action = this.action.replace(/&action=([^&]+)/i, '');
 
324
                this.options.form.setAttribute('action', this.action);
 
325
        }
 
326
});
 
327
IFrame.implement(new Options, new Events);
 
 
b'\\ No newline at end of file'