zeroclipboard-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Henrik Wenz
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # Zeroclipboard::Rails
2
+
3
+ Adds the Zeroclipboard libary to your Rails 3.x app.
4
+ If you would like to use JQuery try the zClip libary instead.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'zeroclipboard-rails'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Add this line to your application.js file:
17
+
18
+ //= require zero-clipboard
19
+
20
+ ## Usage
21
+
22
+ ### Clients
23
+
24
+ Now you are ready to create one or more Clients. A client is a single instance of the clipboard library on the page, linked to a particular button or other DOM element. You probably only need a single instance, but if you have multiple copy-to-clipboard buttons on your page, potentially containing different text, you can activate an instance for each. Here is how to create a client instance:
25
+
26
+ var clip = new ZeroClipboard.Client();
27
+
28
+ Next, you can set some options.
29
+
30
+ ### Setting Options
31
+
32
+ Once you have your client instance, you can set some options. These include setting the initial text to be copied, amongst other things. The following subsections describe all the available options you can set.
33
+
34
+ ### Text To Copy
35
+
36
+ This function allows you to set the text to be copied to the clipboard, once the user clicks on the control. You can call this function at any time; when the page first loads, or later in an onMouseOver or onMouseDown handler. Example:
37
+
38
+ clip.setText( "Copy me!" );
39
+
40
+ ### Hand Cursor
41
+
42
+ This setting controls whether the "hand" or "arrow" cursor is shown when the mouse hovers over the Flash movie. Here is an example:
43
+
44
+ clip.setHandCursor( true );
45
+
46
+ The only values accepted are true (show "hand" cursor), or false (show "arrow" cursor). The default is true. You can set this option at any time.
47
+
48
+ ### Gluing
49
+
50
+ Gluing refers to the process of "linking" the Flash movie to a DOM element on the page. Meaning, the library will automatically generate a movie that is the exact size of the DOM element, and float it just above the element. Since the Flash movie is completely transparent, the user sees nothing out of the ordinary.
51
+
52
+ The Flash movie receives the click event and copies the current text (last set with setText()) to the clipboard (a requirement of Flash Player 10 is that a user click event inside the movie must initiate the thread that interacts with the clipboard). Also, mouse actions like hovering and mouse-down generate events that you can capture (see Event Handlers below) and set CSS classes on your DOM element too (see CSS Effects below).
53
+
54
+ Here is how to glue your clip library instance to a DOM element:
55
+
56
+ clip.glue( 'd_clip_button' );
57
+
58
+ You can pass in a DOM element ID (as shown above), or a reference to the actual DOM element object itself. The rest all happens automatically -- the movie is created, all your options set, and it is floated above the element, awaiting clicks from the user.
59
+
60
+ The glue system is an optional implementation. If you would prefer to handle your own implementation of the Flash movie, see Custom Implementation below.
61
+
62
+ ### Recommended Implementation
63
+
64
+ It is highly recommended you create a "container" DIV element around your button, set its CSS "position" to "relative", and place your button just inside. Then, pass two arguments to glue(), your button DOM element or ID, and the container DOM element or ID. This way Zero Clipboard can position the floating Flash movie relative to the container DIV (not the page body), resulting in much more exact positioning. Example (HTML):
65
+
66
+ <div id="d_clip_container" style="position:relative">
67
+ <div id="d_clip_button">Copy to Clipboard</div>
68
+ </div>
69
+
70
+ And the code:
71
+
72
+ clip.glue( 'd_clip_button', 'd_clip_container' );
73
+
74
+ Note that gluing to a container element does not work with the reposition() method (see next section).
75
+
76
+
77
+ For all usage options please visit the [zeroclipboard doc page](http://code.google.com/p/zeroclipboard/wiki/Instructions)
78
+
79
+ ## Browser Support
80
+
81
+ The Zero Clipboard Library has been tested on the following browsers / platforms:
82
+
83
+ <table>
84
+ <tr>
85
+ <td> <strong>Browser</strong></td>
86
+ <td> <strong>Windows XP SP3</strong></td>
87
+ <td> <strong>Windows Vista</strong></td>
88
+ <td> <strong>Mac OS X Leopard</strong></td>
89
+ </tr>
90
+ <tr>
91
+ <td> Internet Exploder </td>
92
+ <td> 7.0 </td>
93
+ <td> 7.0 </td>
94
+ <td> - </td>
95
+ </tr> <tr>
96
+ <td> Firefox </td>
97
+ <td> 3.0 </td>
98
+ <td> 3.0 </td>
99
+ <td> 3.0 </td>
100
+ </tr> <tr>
101
+ <td> Safari </td>
102
+ <td> - </td>
103
+ <td> - </td>
104
+ <td> 3.0 </td>
105
+ </tr> <tr>
106
+ <td> Google Chrome </td>
107
+ <td> 1.0 </td>
108
+ <td> 1.0 </td>
109
+ <td> - </td>
110
+ </tr>
111
+ </table>
112
+
113
+ Adobe Flash Flash Player versions 9 and 10 are supported.
114
+
115
+ ### Credits
116
+
117
+ Thanks to Joseph Huckaby and all the [contributers](http://code.google.com/u/100866768200529838600/)!
118
+
119
+
120
+ ## Contributing
121
+
122
+ 1. Fork it
123
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
124
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
125
+ 4. Push to the branch (`git push origin my-new-feature`)
126
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ require "zeroclipboard-rails/version"
2
+
3
+ module Zeroclipboard
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ module Zeroclipboard
2
+ module Rails
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ //= require "./zero-clipboard/zero-clipboard"
2
+ //= require "./zero-clipboard/asset-path"
@@ -0,0 +1 @@
1
+ ZeroClipboard.setMoviePath( '<%= asset_path 'ZeroClipboard10.swf' %>' );
@@ -0,0 +1,337 @@
1
+ // Simple Set Clipboard System
2
+ // Author: Joseph Huckaby
3
+
4
+ window.ZeroClipboard = {
5
+
6
+ version: "1.0.8",
7
+ clients: {}, // registered upload clients on page, indexed by id
8
+ moviePath: 'ZeroClipboard.swf', // URL to movie
9
+ nextId: 1, // ID of next movie
10
+
11
+ $: function(thingy) {
12
+ // simple DOM lookup utility function
13
+ if (typeof(thingy) == 'string') thingy = document.getElementById(thingy);
14
+ if (!thingy.addClass) {
15
+ // extend element with a few useful methods
16
+ thingy.hide = function() { this.style.display = 'none'; };
17
+ thingy.show = function() { this.style.display = ''; };
18
+ thingy.addClass = function(name) { this.removeClass(name); this.className += ' ' + name; };
19
+ thingy.removeClass = function(name) {
20
+ var classes = this.className.split(/\s+/);
21
+ var idx = -1;
22
+ for (var k = 0; k < classes.length; k++) {
23
+ if (classes[k] == name) { idx = k; k = classes.length; }
24
+ }
25
+ if (idx > -1) {
26
+ classes.splice( idx, 1 );
27
+ this.className = classes.join(' ');
28
+ }
29
+ return this;
30
+ };
31
+ thingy.hasClass = function(name) {
32
+ return !!this.className.match( new RegExp("\\s*" + name + "\\s*") );
33
+ };
34
+ }
35
+ return thingy;
36
+ },
37
+
38
+ setMoviePath: function(path) {
39
+ // set path to ZeroClipboard.swf
40
+ this.moviePath = path;
41
+ },
42
+
43
+ // use this method in JSNI calls to obtain a new Client instance
44
+ newClient: function() {
45
+ return new ZeroClipboard.Client();
46
+ },
47
+
48
+ dispatch: function(id, eventName, args) {
49
+ // receive event from flash movie, send to client
50
+ var client = this.clients[id];
51
+ if (client) {
52
+ client.receiveEvent(eventName, args);
53
+ }
54
+ },
55
+
56
+ register: function(id, client) {
57
+ // register new client to receive events
58
+ this.clients[id] = client;
59
+ },
60
+
61
+ getDOMObjectPosition: function(obj, stopObj) {
62
+ // get absolute coordinates for dom element
63
+ var info = {
64
+ left: 0,
65
+ top: 0,
66
+ width: obj.width ? obj.width : obj.offsetWidth,
67
+ height: obj.height ? obj.height : obj.offsetHeight
68
+ };
69
+
70
+ while (obj && (obj != stopObj)) {
71
+ info.left += obj.offsetLeft;
72
+ info.left += obj.style.borderLeftWidth ? parseInt(obj.style.borderLeftWidth) : 0;
73
+ info.top += obj.offsetTop;
74
+ info.top += obj.style.borderTopWidth ? parseInt(obj.style.borderTopWidth) : 0;
75
+ obj = obj.offsetParent;
76
+ }
77
+
78
+ return info;
79
+ },
80
+
81
+ Client: function(elem) {
82
+ // constructor for new simple upload client
83
+ this.handlers = {};
84
+
85
+ // unique ID
86
+ this.id = ZeroClipboard.nextId++;
87
+ this.movieId = 'ZeroClipboardMovie_' + this.id;
88
+
89
+ // register client with singleton to receive flash events
90
+ ZeroClipboard.register(this.id, this);
91
+
92
+ // create movie
93
+ if (elem) this.glue(elem);
94
+ }
95
+ };
96
+
97
+ ZeroClipboard.Client.prototype = {
98
+
99
+ id: 0, // unique ID for us
100
+ title: "", // tooltip for the flash element
101
+ ready: false, // whether movie is ready to receive events or not
102
+ movie: null, // reference to movie object
103
+ clipText: '', // text to copy to clipboard
104
+ handCursorEnabled: true, // whether to show hand cursor, or default pointer cursor
105
+ cssEffects: true, // enable CSS mouse effects on dom container
106
+ handlers: null, // user event handlers
107
+ zIndex: 99, // default zIndex of the movie object
108
+
109
+ glue: function(elem, appendElem, stylesToAdd) {
110
+ // glue to DOM element
111
+ // elem can be ID or actual DOM element object
112
+ this.domElement = ZeroClipboard.$(elem);
113
+
114
+ // float just above object, or default zIndex if dom element isn't set
115
+ if (this.domElement.style.zIndex) {
116
+ this.zIndex = parseInt(this.domElement.style.zIndex, 10) + 1;
117
+ }
118
+
119
+ // check if the element has a title
120
+ if (this.domElement.getAttribute("title") != null) {
121
+ this.title = this.domElement.getAttribute("title");
122
+ }
123
+
124
+ if (typeof(appendElem) == 'string') {
125
+ appendElem = ZeroClipboard.$(appendElem);
126
+ }
127
+ else if (typeof(appendElem) == 'undefined') {
128
+ appendElem = document.getElementsByTagName('body')[0];
129
+ }
130
+
131
+ // find X/Y position of domElement
132
+ var box = ZeroClipboard.getDOMObjectPosition(this.domElement, appendElem);
133
+
134
+ // create floating DIV above element
135
+ this.div = document.createElement('div');
136
+ var style = this.div.style;
137
+ style.position = 'absolute';
138
+ style.left = '' + box.left + 'px';
139
+ style.top = '' + box.top + 'px';
140
+ style.width = '' + box.width + 'px';
141
+ style.height = '' + box.height + 'px';
142
+ style.zIndex = this.zIndex;
143
+
144
+ if (typeof(stylesToAdd) == 'object') {
145
+ for (var addedStyle in stylesToAdd) {
146
+ style[addedStyle] = stylesToAdd[addedStyle];
147
+ }
148
+ }
149
+
150
+ // style.backgroundColor = '#f00'; // debug
151
+
152
+ appendElem.appendChild(this.div);
153
+
154
+ this.div.innerHTML = this.getHTML( box.width, box.height );
155
+ },
156
+
157
+ getHTML: function(width, height) {
158
+ // return HTML for movie
159
+ var html = '';
160
+ var flashvars = 'id=' + this.id +
161
+ '&width=' + width +
162
+ '&height=' + height,
163
+ title = this.title ? ' title="' + this.title + '"' : '';
164
+
165
+ if (navigator.userAgent.match(/MSIE/)) {
166
+ // IE gets an OBJECT tag
167
+ var protocol = location.href.match(/^https/i) ? 'https://' : 'http://';
168
+ html += '<object' + title + ' 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+'"><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>';
169
+ }
170
+ else {
171
+ // all other browsers get an EMBED tag
172
+ html += '<embed' + title + ' id="'+this.movieId+'" src="'+ZeroClipboard.moviePath+'" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="'+this.movieId+'" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'" wmode="transparent" />';
173
+ }
174
+ return html;
175
+ },
176
+
177
+ hide: function() {
178
+ // temporarily hide floater offscreen
179
+ if (this.div) {
180
+ this.div.style.left = '-2000px';
181
+ }
182
+ },
183
+
184
+ show: function() {
185
+ // show ourselves after a call to hide()
186
+ this.reposition();
187
+ },
188
+
189
+ destroy: function() {
190
+ // destroy control and floater
191
+ if (this.domElement && this.div) {
192
+ this.hide();
193
+ this.div.innerHTML = '';
194
+
195
+ var body = document.getElementsByTagName('body')[0];
196
+ try { body.removeChild( this.div ); } catch(e) {}
197
+
198
+ this.domElement = null;
199
+ this.div = null;
200
+ }
201
+ },
202
+
203
+ reposition: function(elem) {
204
+ // reposition our floating div, optionally to new container
205
+ // warning: container CANNOT change size, only position
206
+ if (elem) {
207
+ this.domElement = ZeroClipboard.$(elem);
208
+ if (!this.domElement) this.hide();
209
+ }
210
+
211
+ if (this.domElement && this.div) {
212
+ var box = ZeroClipboard.getDOMObjectPosition(this.domElement);
213
+ var style = this.div.style;
214
+ style.left = '' + box.left + 'px';
215
+ style.top = '' + box.top + 'px';
216
+ }
217
+ },
218
+
219
+ setText: function(newText) {
220
+ // set text to be copied to clipboard
221
+ this.clipText = newText;
222
+ if (this.ready) this.movie.setText(newText);
223
+ },
224
+
225
+ setTitle: function(newTitle) {
226
+ // set title of flash element
227
+ this.title = newTitle;
228
+ if (this.ready) this.movie.setTitle(newTitle);
229
+ },
230
+
231
+ addEventListener: function(eventName, func) {
232
+ // add user event listener for event
233
+ // event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel
234
+ eventName = eventName.toString().toLowerCase().replace(/^on/, '');
235
+ if (!this.handlers[eventName]) this.handlers[eventName] = [];
236
+ this.handlers[eventName].push(func);
237
+ },
238
+
239
+ setHandCursor: function(enabled) {
240
+ // enable hand cursor (true), or default arrow cursor (false)
241
+ this.handCursorEnabled = enabled;
242
+ if (this.ready) this.movie.setHandCursor(enabled);
243
+ },
244
+
245
+ setCSSEffects: function(enabled) {
246
+ // enable or disable CSS effects on DOM container
247
+ this.cssEffects = !!enabled;
248
+ },
249
+
250
+ receiveEvent: function(eventName, args) {
251
+ // receive event from flash
252
+ eventName = eventName.toString().toLowerCase().replace(/^on/, '');
253
+
254
+ // special behavior for certain events
255
+ switch (eventName) {
256
+ case 'load':
257
+ // movie claims it is ready, but in IE this isn't always the case...
258
+ // bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function
259
+ this.movie = document.getElementById(this.movieId);
260
+ if (!this.movie) {
261
+ var self = this;
262
+ setTimeout( function() { self.receiveEvent('load', null); }, 1 );
263
+ return;
264
+ }
265
+
266
+ // firefox on pc needs a "kick" in order to set these in certain cases
267
+ if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) {
268
+ var self = this;
269
+ setTimeout( function() { self.receiveEvent('load', null); }, 100 );
270
+ this.ready = true;
271
+ return;
272
+ }
273
+
274
+ this.ready = true;
275
+ this.movie.setText( this.clipText );
276
+ this.movie.setTitle( this.title );
277
+ this.movie.setHandCursor( this.handCursorEnabled );
278
+ break;
279
+
280
+ case 'mouseover':
281
+ if (this.domElement && this.cssEffects) {
282
+ this.domElement.addClass('hover');
283
+ if (this.recoverActive) this.domElement.addClass('active');
284
+ }
285
+ break;
286
+
287
+ case 'mouseout':
288
+ if (this.domElement && this.cssEffects) {
289
+ this.recoverActive = false;
290
+ if (this.domElement.hasClass('active')) {
291
+ this.domElement.removeClass('active');
292
+ this.recoverActive = true;
293
+ }
294
+ this.domElement.removeClass('hover');
295
+ }
296
+ break;
297
+
298
+ case 'mousedown':
299
+ if (this.domElement && this.cssEffects) {
300
+ this.domElement.addClass('active');
301
+ }
302
+ break;
303
+
304
+ case 'mouseup':
305
+ if (this.domElement && this.cssEffects) {
306
+ this.domElement.removeClass('active');
307
+ this.recoverActive = false;
308
+ }
309
+ break;
310
+ } // switch eventName
311
+
312
+ if (this.handlers[eventName]) {
313
+ for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) {
314
+ var func = this.handlers[eventName][idx];
315
+
316
+ if (typeof(func) == 'function') {
317
+ // actual function reference
318
+ func(this, args);
319
+ }
320
+ else if ((typeof(func) == 'object') && (func.length == 2)) {
321
+ // PHP style object + method, i.e. [myObject, 'myMethod']
322
+ func[0][ func[1] ](this, args);
323
+ }
324
+ else if (typeof(func) == 'string') {
325
+ // name of function
326
+ window[func](this, args);
327
+ }
328
+ } // foreach event handler defined
329
+ } // user defined handler for event
330
+ }
331
+
332
+ };
333
+
334
+ if (typeof module !== "undefined") {
335
+ module.exports = ZeroClipboard;
336
+ }
337
+
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zeroclipboard-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Henrik Wenz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ description: Adds the Javascript ZeroClipboard libary to Rails 3.x
31
+ email:
32
+ - handtrix@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/zeroclipboard-rails/version.rb
38
+ - lib/zeroclipboard-rails.rb
39
+ - vendor/assets/images/ZeroClipboard.swf
40
+ - vendor/assets/images/ZeroClipboard10.swf
41
+ - vendor/assets/javascripts/zero-clipboard/asset-path.js.erb
42
+ - vendor/assets/javascripts/zero-clipboard/zero-clipboard.js
43
+ - vendor/assets/javascripts/zero-clipboard.js
44
+ - LICENSE
45
+ - README.md
46
+ homepage: ''
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.24
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Adds the Javascript ZeroClipboard libary to Rails 3.x
70
+ test_files: []