Statistics
| Branch: | Tag: | Revision:

root / snf-app / synnefo / ui / static / snf / js / lib / jquery.zclip.js @ 483c9197

History | View | Annotate | Download (16.3 kB)

1
/*
2
 * zClip :: jQuery ZeroClipboard v1.1.1
3
 * http://steamdev.com/zclip
4
 *
5
 * Copyright 2011, SteamDev
6
 * Released under the MIT license.
7
 * http://www.opensource.org/licenses/mit-license.php
8
 *
9
 * Date: Wed Jun 01, 2011
10
 */
11

    
12

    
13
(function ($) {
14

    
15
    $.fn.zclip = function (params) {
16

    
17
        if (typeof params == "object" && !params.length) {
18

    
19
            var settings = $.extend({
20

    
21
                path: 'ZeroClipboard.swf',
22
                copy: null,
23
                beforeCopy: null,
24
                afterCopy: null,
25
                clickAfter: true,
26
                setHandCursor: true,
27
                setCSSEffects: true
28

    
29
            }, params);
30
                        
31

    
32
            return this.each(function () {
33

    
34
                var o = $(this);
35

    
36
                if (o.is(':visible') && (typeof settings.copy == 'string' || $.isFunction(settings.copy))) {
37
                    ZeroClipboard.setMoviePath(settings.path);
38
                    var clip = new ZeroClipboard.Client();
39
                    
40
                    if($.isFunction(settings.copy)){
41
                            o.bind('zClip_copy',settings.copy);
42
                    }
43
                    if($.isFunction(settings.beforeCopy)){
44
                            o.bind('zClip_beforeCopy',settings.beforeCopy);
45
                    }
46
                    if($.isFunction(settings.afterCopy)){
47
                            o.bind('zClip_afterCopy',settings.afterCopy);
48
                    }                    
49

    
50
                    clip.setHandCursor(settings.setHandCursor);
51
                    clip.setCSSEffects(settings.setCSSEffects);
52
                    clip.addEventListener('mouseOver', function (client) {
53
                        o.trigger('mouseenter');
54
                    });
55
                    clip.addEventListener('mouseOut', function (client) {
56
                        o.trigger('mouseleave');
57
                    });
58
                    clip.addEventListener('mouseDown', function (client) {
59

    
60
                        o.trigger('mousedown');
61
                        
62
                        if(!$.isFunction(settings.copy)){
63
                           clip.setText(settings.copy);
64
                        } else {
65
                           clip.setText(o.triggerHandler('zClip_copy'));
66
                        }                        
67
                        
68
                        if ($.isFunction(settings.beforeCopy)) {
69
                            o.trigger('zClip_beforeCopy');                            
70
                        }
71

    
72
                    });
73

    
74
                    clip.addEventListener('complete', function (client, text) {
75

    
76
                        if ($.isFunction(settings.afterCopy)) {
77
                            
78
                            o.trigger('zClip_afterCopy');
79

    
80
                        } else {
81
                            if (text.length > 500) {
82
                                text = text.substr(0, 500) + "...\n\n(" + (text.length - 500) + " characters not shown)";
83
                            }
84
                                                        
85
                            o.removeClass('hover');
86
                        }
87

    
88
                        if (settings.clickAfter) {
89
                            o.trigger('click');
90
                        }
91

    
92
                    });
93

    
94
                                        
95
                    clip.glue(o[0], o.parent()[0]);
96
                                        
97
                    $(window).bind('load resize',function(){clip.reposition();});
98
                                        
99

    
100
                }
101

    
102
            });
103

    
104
        } else if (typeof params == "string") {
105

    
106
            return this.each(function () {
107

    
108
                var o = $(this);
109

    
110
                params = params.toLowerCase();
111
                var zclipId = o.data('zclipId');
112
                var clipElm = $('#' + zclipId + '.zclip');
113

    
114
                if (params == "remove") {
115

    
116
                    clipElm.remove();
117
                    o.removeClass('active hover');
118

    
119
                } else if (params == "hide") {
120

    
121
                    clipElm.hide();
122
                    o.removeClass('active hover');
123

    
124
                } else if (params == "show") {
125

    
126
                    clipElm.show();
127

    
128
                }
129

    
130
            });
131

    
132
        }
133

    
134
    }        
135
        
136
        
137

    
138
})(jQuery);
139

    
140

    
141

    
142

    
143

    
144

    
145

    
146
// ZeroClipboard
147
// Simple Set Clipboard System
148
// Author: Joseph Huckaby
149
var ZeroClipboard = {
150

    
151
    version: "1.0.7",
152
    clients: {},
153
    // registered upload clients on page, indexed by id
154
    moviePath: 'ZeroClipboard.swf',
155
    // URL to movie
156
    nextId: 1,
157
    // ID of next movie
158
    $: function (thingy) {
159
        // simple DOM lookup utility function
160
        if (typeof(thingy) == 'string') thingy = document.getElementById(thingy);
161
        if (!thingy.addClass) {
162
            // extend element with a few useful methods
163
            thingy.hide = function () {
164
                this.style.display = 'none';
165
            };
166
            thingy.show = function () {
167
                this.style.display = '';
168
            };
169
            thingy.addClass = function (name) {
170
                this.removeClass(name);
171
                this.className += ' ' + name;
172
            };
173
            thingy.removeClass = function (name) {
174
                var classes = this.className.split(/\s+/);
175
                var idx = -1;
176
                for (var k = 0; k < classes.length; k++) {
177
                    if (classes[k] == name) {
178
                        idx = k;
179
                        k = classes.length;
180
                    }
181
                }
182
                if (idx > -1) {
183
                    classes.splice(idx, 1);
184
                    this.className = classes.join(' ');
185
                }
186
                return this;
187
            };
188
            thingy.hasClass = function (name) {
189
                return !!this.className.match(new RegExp("\\s*" + name + "\\s*"));
190
            };
191
        }
192
        return thingy;
193
    },
194

    
195
    setMoviePath: function (path) {
196
        // set path to ZeroClipboard.swf
197
        this.moviePath = path;
198
    },
199

    
200
    dispatch: function (id, eventName, args) {
201
        // receive event from flash movie, send to client                
202
        var client = this.clients[id];
203
        if (client) {
204
            client.receiveEvent(eventName, args);
205
        }
206
    },
207

    
208
    register: function (id, client) {
209
        // register new client to receive events
210
        this.clients[id] = client;
211
    },
212

    
213
    getDOMObjectPosition: function (obj, stopObj) {
214
        // get absolute coordinates for dom element
215
        var info = {
216
            left: 0,
217
            top: 0,
218
            width: obj.width ? obj.width : obj.offsetWidth,
219
            height: obj.height ? obj.height : obj.offsetHeight
220
        };
221

    
222
        if (obj && (obj != stopObj)) {
223
                        info.left += obj.offsetLeft;
224
            info.top += obj.offsetTop;
225
        }
226

    
227
        return info;
228
    },
229

    
230
    Client: function (elem) {
231
        // constructor for new simple upload client
232
        this.handlers = {};
233

    
234
        // unique ID
235
        this.id = ZeroClipboard.nextId++;
236
        this.movieId = 'ZeroClipboardMovie_' + this.id;
237

    
238
        // register client with singleton to receive flash events
239
        ZeroClipboard.register(this.id, this);
240

    
241
        // create movie
242
        if (elem) this.glue(elem);
243
    }
244
};
245

    
246
ZeroClipboard.Client.prototype = {
247

    
248
    id: 0,
249
    // unique ID for us
250
    ready: false,
251
    // whether movie is ready to receive events or not
252
    movie: null,
253
    // reference to movie object
254
    clipText: '',
255
    // text to copy to clipboard
256
    handCursorEnabled: true,
257
    // whether to show hand cursor, or default pointer cursor
258
    cssEffects: true,
259
    // enable CSS mouse effects on dom container
260
    handlers: null,
261
    // user event handlers
262
    glue: function (elem, appendElem, stylesToAdd) {
263
        // glue to DOM element
264
        // elem can be ID or actual DOM element object
265
        this.domElement = ZeroClipboard.$(elem);
266

    
267
        // float just above object, or zIndex 99 if dom element isn't set
268
        var zIndex = 99;
269
        if (this.domElement.style.zIndex) {
270
            zIndex = parseInt(this.domElement.style.zIndex, 10) + 1;
271
        }
272

    
273
        if (typeof(appendElem) == 'string') {
274
            appendElem = ZeroClipboard.$(appendElem);
275
        } else if (typeof(appendElem) == 'undefined') {
276
            appendElem = document.getElementsByTagName('body')[0];
277
        }
278

    
279
        // find X/Y position of domElement
280
        var box = ZeroClipboard.getDOMObjectPosition(this.domElement, appendElem);
281

    
282
        // create floating DIV above element
283
        this.div = document.createElement('div');
284
        this.div.className = "zclip";
285
        this.div.id = "zclip-" + this.movieId;
286
        $(this.domElement).data('zclipId', 'zclip-' + this.movieId);
287
        var style = this.div.style;
288
        style.position = 'absolute';
289
        style.left = '' + box.left + 'px';
290
        style.top = '' + box.top + 'px';
291
        style.width = '' + box.width + 'px';
292
        style.height = '' + box.height + 'px';
293
        style.zIndex = zIndex;
294

    
295
        if (typeof(stylesToAdd) == 'object') {
296
            for (addedStyle in stylesToAdd) {
297
                style[addedStyle] = stylesToAdd[addedStyle];
298
            }
299
        }
300

    
301
        // style.backgroundColor = '#f00'; // debug
302
        appendElem.appendChild(this.div);
303

    
304
        this.div.innerHTML = this.getHTML(box.width, box.height);
305
    },
306

    
307
    getHTML: function (width, height) {
308
        // return HTML for movie
309
        var html = '';
310
        var flashvars = 'id=' + this.id + '&width=' + width + '&height=' + height;
311

    
312
        if (navigator.userAgent.match(/MSIE/)) {
313
            // IE gets an OBJECT tag
314
            var protocol = location.href.match(/^https/i) ? 'https://' : 'http://';
315
            html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="' + protocol + 'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="' + width + '" height="' + height + '" id="' + this.movieId + '" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="' + ZeroClipboard.moviePath + '" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="' + flashvars + '"/><param name="wmode" value="transparent"/></object>';
316
        } else {
317
            // all other browsers get an EMBED tag
318
            html += '<embed id="' + this.movieId + '" src="' + ZeroClipboard.moviePath + '" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="' + this.movieId + '" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="' + flashvars + '" wmode="transparent" />';
319
        }
320
        return html;
321
    },
322

    
323
    hide: function () {
324
        // temporarily hide floater offscreen
325
        if (this.div) {
326
            this.div.style.left = '-2000px';
327
        }
328
    },
329

    
330
    show: function () {
331
        // show ourselves after a call to hide()
332
        this.reposition();
333
    },
334

    
335
    destroy: function () {
336
        // destroy control and floater
337
        if (this.domElement && this.div) {
338
            this.hide();
339
            this.div.innerHTML = '';
340

    
341
            var body = document.getElementsByTagName('body')[0];
342
            try {
343
                body.removeChild(this.div);
344
            } catch (e) {;
345
            }
346

    
347
            this.domElement = null;
348
            this.div = null;
349
        }
350
    },
351

    
352
    reposition: function (elem) {
353
        // reposition our floating div, optionally to new container
354
        // warning: container CANNOT change size, only position
355
        if (elem) {
356
            this.domElement = ZeroClipboard.$(elem);
357
            if (!this.domElement) this.hide();
358
        }
359

    
360
        if (this.domElement && this.div) {
361
            var box = ZeroClipboard.getDOMObjectPosition(this.domElement);
362
            var style = this.div.style;
363
            style.left = '' + box.left + 'px';
364
            style.top = '' + box.top + 'px';
365
        }
366
    },
367

    
368
    setText: function (newText) {
369
        // set text to be copied to clipboard
370
        this.clipText = newText;
371
        if (this.ready) {
372
            this.movie.setText(newText);
373
        }
374
    },
375

    
376
    addEventListener: function (eventName, func) {
377
        // add user event listener for event
378
        // event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel
379
        eventName = eventName.toString().toLowerCase().replace(/^on/, '');
380
        if (!this.handlers[eventName]) {
381
            this.handlers[eventName] = [];
382
        }
383
        this.handlers[eventName].push(func);
384
    },
385

    
386
    setHandCursor: function (enabled) {
387
        // enable hand cursor (true), or default arrow cursor (false)
388
        this.handCursorEnabled = enabled;
389
        if (this.ready) {
390
            this.movie.setHandCursor(enabled);
391
        }
392
    },
393

    
394
    setCSSEffects: function (enabled) {
395
        // enable or disable CSS effects on DOM container
396
        this.cssEffects = !! enabled;
397
    },
398

    
399
    receiveEvent: function (eventName, args) {
400
        // receive event from flash
401
        eventName = eventName.toString().toLowerCase().replace(/^on/, '');
402

    
403
        // special behavior for certain events
404
        switch (eventName) {
405
        case 'load':
406
            // movie claims it is ready, but in IE this isn't always the case...
407
            // bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function
408
            this.movie = document.getElementById(this.movieId);
409
            if (!this.movie) {
410
                var self = this;
411
                setTimeout(function () {
412
                    self.receiveEvent('load', null);
413
                }, 1);
414
                return;
415
            }
416

    
417
            // firefox on pc needs a "kick" in order to set these in certain cases
418
            if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) {
419
                var self = this;
420
                setTimeout(function () {
421
                    self.receiveEvent('load', null);
422
                }, 100);
423
                this.ready = true;
424
                return;
425
            }
426

    
427
            this.ready = true;
428
            try {
429
                this.movie.setText(this.clipText);
430
            } catch (e) {}
431
            try {
432
                this.movie.setHandCursor(this.handCursorEnabled);
433
            } catch (e) {}
434
            break;
435

    
436
        case 'mouseover':
437
            if (this.domElement && this.cssEffects) {
438
                this.domElement.addClass('hover');
439
                if (this.recoverActive) {
440
                    this.domElement.addClass('active');
441
                }
442

    
443

    
444
            }
445

    
446

    
447
            break;
448

    
449
        case 'mouseout':
450
            if (this.domElement && this.cssEffects) {
451
                this.recoverActive = false;
452
                if (this.domElement.hasClass('active')) {
453
                    this.domElement.removeClass('active');
454
                    this.recoverActive = true;
455
                }
456
                this.domElement.removeClass('hover');
457

    
458
            }
459
            break;
460

    
461
        case 'mousedown':
462
            if (this.domElement && this.cssEffects) {
463
                this.domElement.addClass('active');
464
            }
465
            break;
466

    
467
        case 'mouseup':
468
            if (this.domElement && this.cssEffects) {
469
                this.domElement.removeClass('active');
470
                this.recoverActive = false;
471
            }
472
            break;
473
        } // switch eventName
474
        if (this.handlers[eventName]) {
475
            for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) {
476
                var func = this.handlers[eventName][idx];
477

    
478
                if (typeof(func) == 'function') {
479
                    // actual function reference
480
                    func(this, args);
481
                } else if ((typeof(func) == 'object') && (func.length == 2)) {
482
                    // PHP style object + method, i.e. [myObject, 'myMethod']
483
                    func[0][func[1]](this, args);
484
                } else if (typeof(func) == 'string') {
485
                    // name of function
486
                    window[func](this, args);
487
                }
488
            } // foreach event handler defined
489
        } // user defined handler for event
490
    }
491

    
492
};        
493