jquery-onoff-rails 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +31 -0
  3. data/README.md +73 -0
  4. data/Rakefile +12 -0
  5. data/lib/jquery-onoff-rails.rb +2 -0
  6. data/lib/onoff/engine.rb +15 -0
  7. data/lib/onoff/version.rb +3 -0
  8. data/lib/onoff/view_helpers/action_view.rb +20 -0
  9. data/test/dummy/app/assets/javascripts/javascript-require.js +1 -0
  10. data/test/dummy/app/assets/stylesheets/sass-import.css.sass +1 -0
  11. data/test/dummy/app/assets/stylesheets/scss-import.css.scss +1 -0
  12. data/test/dummy/app/assets/stylesheets/sprockets-require.css +3 -0
  13. data/test/dummy/app/controllers/pages_controller.rb +2 -0
  14. data/test/dummy/app/views/pages/switchers.html.erb +1 -0
  15. data/test/dummy/config.ru +4 -0
  16. data/test/dummy/config/application.rb +19 -0
  17. data/test/dummy/config/boot.rb +10 -0
  18. data/test/dummy/config/environment.rb +5 -0
  19. data/test/dummy/config/initializers/secret_token.rb +8 -0
  20. data/test/dummy/config/routes.rb +3 -0
  21. data/test/dummy/log/test.log +4182 -0
  22. data/test/dummy/tmp/cache/assets/test/sass/67d301ac236284fc6036bcad3f3945d51f68be02/jquery.onoff-es.cssc +0 -0
  23. data/test/dummy/tmp/cache/assets/test/sass/83f97a26edb29c02795c340da7b07a9253d73d37/sass-import.css.sassc +0 -0
  24. data/test/dummy/tmp/cache/assets/test/sass/83f97a26edb29c02795c340da7b07a9253d73d37/scss-import.css.scssc +0 -0
  25. data/test/dummy/tmp/cache/assets/test/sass/9b9fbd410cd81b624ac82d399f12887b9f371585/sass-import.css.sassc +0 -0
  26. data/test/dummy/tmp/cache/assets/test/sass/9b9fbd410cd81b624ac82d399f12887b9f371585/scss-import.css.scssc +0 -0
  27. data/test/dummy/tmp/cache/assets/test/sass/f62cd835705e92699226acb4ce21a67485f27b88/rateit.css.erbc +0 -0
  28. data/test/dummy/tmp/cache/assets/test/sprockets/09bedf7974ec7026dffcf30d10e52450 +0 -0
  29. data/test/dummy/tmp/cache/assets/test/sprockets/123cee991f91ea0e037bdc6887f461a7 +0 -0
  30. data/test/dummy/tmp/cache/assets/test/sprockets/17e0c1fd557b401106f7d5b9adcfaa75 +0 -0
  31. data/test/dummy/tmp/cache/assets/test/sprockets/1c95390810828cb2159626dcecba2f76 +0 -0
  32. data/test/dummy/tmp/cache/assets/test/sprockets/1da4824fc510b334f318038472fee43a +0 -0
  33. data/test/dummy/tmp/cache/assets/test/sprockets/1f1880604d016d0348361f21ff9221df +0 -0
  34. data/test/dummy/tmp/cache/assets/test/sprockets/2bcf1a6abb45b7249df5d5129647733c +0 -0
  35. data/test/dummy/tmp/cache/assets/test/sprockets/2d1f3173b98f71b2fe7c0df0ebff791d +0 -0
  36. data/test/dummy/tmp/cache/assets/test/sprockets/3eec795189f18f7c8d2bcf42f55a8a9b +0 -0
  37. data/test/dummy/tmp/cache/assets/test/sprockets/42b57af0c2e9ec9751b232c30a53f77b +0 -0
  38. data/test/dummy/tmp/cache/assets/test/sprockets/54ab4264de205557ce131c90c583e667 +0 -0
  39. data/test/dummy/tmp/cache/assets/test/sprockets/66affc045b7ef1ce546fe4b65501818a +0 -0
  40. data/test/dummy/tmp/cache/assets/test/sprockets/6ba3609b4c96cfb0521d64d290708ad0 +0 -0
  41. data/test/dummy/tmp/cache/assets/test/sprockets/6f780baf45437a55c54ea8fff88bf64d +0 -0
  42. data/test/dummy/tmp/cache/assets/test/sprockets/70f8c8957b5476fd4c9d2a9721a26991 +0 -0
  43. data/test/dummy/tmp/cache/assets/test/sprockets/8c4cec2d2da7febbebfc572bdbe8e82a +0 -0
  44. data/test/dummy/tmp/cache/assets/test/sprockets/8f69d79f9710d90e610e62279a944ba9 +0 -0
  45. data/test/dummy/tmp/cache/assets/test/sprockets/95826e0c0bfd277720c073efe8900e67 +0 -0
  46. data/test/dummy/tmp/cache/assets/test/sprockets/96150dba6af87bec7a290486a056738f +0 -0
  47. data/test/dummy/tmp/cache/assets/test/sprockets/9b976b8e41806018e7ec0b01f1c3572f +0 -0
  48. data/test/dummy/tmp/cache/assets/test/sprockets/9d6e192e25deb38a4d110a39a480dc53 +0 -0
  49. data/test/dummy/tmp/cache/assets/test/sprockets/a62b1a1e0e4fdafe36f4f6b630d4506f +0 -0
  50. data/test/dummy/tmp/cache/assets/test/sprockets/aa520d7b73f7d80ff4dad95d565bf9e1 +0 -0
  51. data/test/dummy/tmp/cache/assets/test/sprockets/aafed2c5291a4bc8d460c3678eac9204 +0 -0
  52. data/test/dummy/tmp/cache/assets/test/sprockets/b1c3f1c6b58f44b3a937d89c720c2b56 +0 -0
  53. data/test/dummy/tmp/cache/assets/test/sprockets/b3b77f41ac08070bde0fd29481ac4c9b +0 -0
  54. data/test/dummy/tmp/cache/assets/test/sprockets/b80f39d9d08557e0df207a899862a3b5 +0 -0
  55. data/test/dummy/tmp/cache/assets/test/sprockets/bdd626744eaf2886cce699c13c228455 +0 -0
  56. data/test/dummy/tmp/cache/assets/test/sprockets/bf404574f248f5b161ceebcc15a1a8d5 +0 -0
  57. data/test/dummy/tmp/cache/assets/test/sprockets/cc3ff837bd6049eb50d980928b9c8bbf +0 -0
  58. data/test/dummy/tmp/cache/assets/test/sprockets/cd3ab6286149fd61d7a95722f1057bda +0 -0
  59. data/test/dummy/tmp/cache/assets/test/sprockets/d2b82f427e7a7d6386bddbafad47c3fb +0 -0
  60. data/test/dummy/tmp/cache/assets/test/sprockets/d6560c24aa340710a9ffacf4271675bf +0 -0
  61. data/test/dummy/tmp/cache/assets/test/sprockets/d701000668eb3e83f3158a466abee1c6 +0 -0
  62. data/test/dummy/tmp/cache/assets/test/sprockets/da826b3145610b16763eb2a9b3885818 +0 -0
  63. data/test/dummy/tmp/cache/assets/test/sprockets/e12294e95b6674a83009be57eae06abc +0 -0
  64. data/test/dummy/tmp/cache/assets/test/sprockets/e6dab59e63d077d0e34c14a882311af9 +0 -0
  65. data/test/dummy/tmp/cache/assets/test/sprockets/ff0e86d64e78e6a946577b9adacbb83c +0 -0
  66. data/test/dummy/tmp/cache/assets/test/sprockets/ff8a6fc0442034987550825a90d86778 +0 -0
  67. data/test/jquery_onoff_rails_test.rb +64 -0
  68. data/test/test_helper.rb +7 -0
  69. data/vendor/assets/javascripts/jquery.onoff.js +413 -0
  70. data/vendor/assets/stylesheets/jquery.onoff-en.css +83 -0
  71. data/vendor/assets/stylesheets/jquery.onoff-es.css +83 -0
  72. metadata +222 -0
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ class JqueryOnoffRailsTest < ActionDispatch::IntegrationTest
4
+
5
+ test "Load engine" do
6
+ assert_equal ::Rails::Engine, Onoff::Rails::Engine.superclass
7
+ end
8
+
9
+ test "Spanish Stylesheet found" do
10
+ get "/assets/jquery.onoff-es"
11
+ assert_onoff(response)
12
+ end
13
+
14
+ test "English Stylesheet found" do
15
+ get "/assets/jquery.onoff-en"
16
+ assert_onoff(response)
17
+ end
18
+
19
+ test "JavaScript found" do
20
+ get "/assets/jquery.onoff.js"
21
+ assert_onoff(response)
22
+ end
23
+
24
+ test "Stylesheet is available in a css sprockets require" do
25
+ get "/assets/sprockets-require.css"
26
+ assert_onoff(response)
27
+ end
28
+
29
+ test "Stylesheet is available in a sass import" do
30
+ get "/assets/sass-import.css"
31
+ assert_onoff(response)
32
+ end
33
+
34
+ test "Stylesheet is available in a scss import" do
35
+ get "/assets/scss-import.css"
36
+ assert_onoff(response)
37
+ end
38
+
39
+ test "JavaScript is available in a JS require" do
40
+ get "/assets/javascript-require.js"
41
+ assert_onoff(response)
42
+ end
43
+
44
+ test "helpers should be available in the view" do
45
+ get "/switchers"
46
+ assert_response :success
47
+ assert_select "div.onoffswitch"
48
+ assert_select "input.onoffswitch-checkbox"
49
+ assert_select "label.onoffswitch-label"
50
+ assert_select "span.onoffswitch-inner"
51
+ assert_select "span.onoffswitch-switch"
52
+ end
53
+
54
+ private
55
+
56
+ def clean_sprockets_cache
57
+ FileUtils.rm_rf File.expand_path("../dummy/tmp", __FILE__)
58
+ end
59
+
60
+ def assert_onoff(response)
61
+ assert_response :success
62
+ assert_match(/.onoffswitch/, response.body)
63
+ end
64
+ end
@@ -0,0 +1,7 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+
7
+ Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,413 @@
1
+ /** jquery.onoff - v0.4.0 - 2014-10-30
2
+ * https://github.com/timmywil/jquery.onoff
3
+ * Copyright (c) 2014 Timmy Willison; Licensed MIT */
4
+ (function(global, factory) {
5
+ // AMD
6
+ if (typeof define === 'function' && define.amd) {
7
+ define([ 'jquery' ], factory);
8
+ // CommonJS/Browserify
9
+ } else if (typeof exports === 'object') {
10
+ factory(require('jquery'));
11
+ // Global
12
+ } else {
13
+ factory(global.jQuery);
14
+ }
15
+ }(this, function($) {
16
+ 'use strict';
17
+
18
+ // Common properties to lift for touch or pointer events
19
+ var list = 'over out down up move enter leave cancel'.split(' ');
20
+ var hook = $.extend({}, $.event.mouseHooks);
21
+ var events = {};
22
+
23
+ // Support pointer events in IE11+ if available
24
+ if ( window.PointerEvent ) {
25
+ $.each(list, function( i, name ) {
26
+ // Add event name to events property and add fixHook
27
+ $.event.fixHooks[
28
+ (events[name] = 'pointer' + name)
29
+ ] = hook;
30
+ });
31
+ } else {
32
+ var mouseProps = hook.props;
33
+ // Add touch properties for the touch hook
34
+ hook.props = mouseProps.concat(['touches', 'changedTouches', 'targetTouches', 'altKey', 'ctrlKey', 'metaKey', 'shiftKey']);
35
+
36
+ /**
37
+ * Support: Android
38
+ * Android sets pageX/Y to 0 for any touch event
39
+ * Attach first touch's pageX/pageY and clientX/clientY if not set correctly
40
+ */
41
+ hook.filter = function( event, originalEvent ) {
42
+ var touch;
43
+ var i = mouseProps.length;
44
+ if ( !originalEvent.pageX && originalEvent.touches && (touch = originalEvent.touches[0]) ) {
45
+ // Copy over all mouse properties
46
+ while(i--) {
47
+ event[mouseProps[i]] = touch[mouseProps[i]];
48
+ }
49
+ }
50
+ return event;
51
+ };
52
+
53
+ $.each(list, function( i, name ) {
54
+ // No equivalent touch events for over and out
55
+ if (i < 2) {
56
+ events[ name ] = 'mouse' + name;
57
+ } else {
58
+ var touch = 'touch' +
59
+ (name === 'down' ? 'start' : name === 'up' ? 'end' : name);
60
+ // Add fixHook
61
+ $.event.fixHooks[ touch ] = hook;
62
+ // Add event names to events property
63
+ events[ name ] = touch + ' mouse' + name;
64
+ }
65
+ });
66
+ }
67
+
68
+ $.pointertouch = events;
69
+
70
+ var count = 1;
71
+ var slice = Array.prototype.slice;
72
+
73
+ /**
74
+ * Create an OnOff object for a given element
75
+ * @constructor
76
+ * @param {Element} elem - Element to use pan and zoom
77
+ * @param {Object} [options] - An object literal containing options
78
+ * to override default options (See OnOff.defaults)
79
+ */
80
+ function OnOff(elem, options) {
81
+
82
+ // Allow instantiation without `new` keyword
83
+ if (!(this instanceof OnOff)) {
84
+ return new OnOff(elem, options);
85
+ }
86
+
87
+ // Sanity checks
88
+ if (elem.nodeName.toLowerCase() !== 'input' || elem.type !== 'checkbox') {
89
+ return $.error('OnOff should be called on checkboxes');
90
+ }
91
+
92
+ // Don't remake
93
+ var d = $.data(elem, OnOff.datakey);
94
+ if (d) {
95
+ return d;
96
+ }
97
+
98
+ // Extend default with given object literal
99
+ // Each instance gets its own options
100
+ this.options = options = $.extend({}, OnOff.defaults, options);
101
+ this.elem = elem;
102
+ this.$elem = $(elem).addClass(options.className);
103
+ this.$doc = $(elem.ownerDocument || document);
104
+
105
+ // Add guid to event namespace
106
+ options.namespace += $.guid++;
107
+
108
+ // Add an ID if none has been added
109
+ if (!elem.id) {
110
+ elem.id = 'onoffswitch' + count++;
111
+ }
112
+
113
+ // Enable
114
+ this.enable();
115
+
116
+ // Save the instance
117
+ $.data(elem, OnOff.datakey, this);
118
+ }
119
+
120
+ OnOff.datakey = '_onoff';
121
+
122
+ OnOff.defaults = {
123
+ // The event namespace
124
+ // Should always be non-empty
125
+ // Used to bind jQuery events without collisions
126
+ namespace: '.onoff',
127
+
128
+ // The class added to the checkbox (see the CSS file)
129
+ className: 'onoffswitch-checkbox'
130
+ };
131
+
132
+ OnOff.prototype = {
133
+ constructor: OnOff,
134
+
135
+ /**
136
+ * @returns {OnOff} Returns the instance
137
+ */
138
+ instance: function() {
139
+ return this;
140
+ },
141
+
142
+ /**
143
+ * Wrap the checkbox and add the label element
144
+ */
145
+ wrap: function() {
146
+ var elem = this.elem;
147
+ var $elem = this.$elem;
148
+ var options = this.options;
149
+
150
+ // Get or create elem wrapper
151
+ var $con = $elem.parent('.onoffswitch');
152
+ if (!$con.length) {
153
+ $elem.wrap('<div class="onoffswitch"></div>');
154
+ $con = $elem.parent()
155
+ .addClass(elem.className.replace(options.className, ''));
156
+ }
157
+ this.$con = $con;
158
+
159
+ // Get or create label
160
+ var $label = $elem.next('label[for="' + elem.id + '"]');
161
+ if (!$label.length) {
162
+ $label = $('<label/>')
163
+ .attr('for', elem.id)
164
+ .insertAfter(elem);
165
+ }
166
+ this.$label = $label.addClass('onoffswitch-label');
167
+
168
+ // Inner
169
+ var $inner = $label.find('.onoffswitch-inner');
170
+ if (!$inner.length) {
171
+ $inner = $('<span/>')
172
+ .addClass('onoffswitch-inner')
173
+ .prependTo($label);
174
+ }
175
+ this.$inner = $inner;
176
+
177
+ // Switch
178
+ var $switch = $label.find('.onoffswitch-switch');
179
+ if (!$switch.length) {
180
+ $switch = $('<span/>')
181
+ .addClass('onoffswitch-switch')
182
+ .appendTo($label);
183
+ }
184
+ this.$switch = $switch;
185
+ },
186
+
187
+ /**
188
+ * Handles the move event on the switch
189
+ */
190
+ _handleMove: function(e) {
191
+ if (this.disabled) return;
192
+ this.moved = true;
193
+ this.lastX = e.pageX;
194
+ var right = Math.max(Math.min(this.startX - this.lastX, this.maxRight), 0);
195
+ this.$switch.css('right', right);
196
+ this.$inner.css('marginLeft', -(right / this.maxRight) * 100 + '%');
197
+ },
198
+
199
+ /**
200
+ * Bind the move and end events to the document
201
+ */
202
+ _startMove: function(e) {
203
+ // Prevent default to avoid touch event collision
204
+ e.preventDefault();
205
+ var moveType, endType;
206
+ if (e.type === 'pointerdown') {
207
+ moveType = 'pointermove';
208
+ endType = 'pointerup';
209
+ } else if (e.type === 'touchstart') {
210
+ moveType = 'touchmove';
211
+ endType = 'touchend';
212
+ } else {
213
+ moveType = 'mousemove';
214
+ endType = 'mouseup';
215
+ }
216
+ var elem = this.elem;
217
+ var $elem = this.$elem;
218
+ var ns = this.options.namespace;
219
+ // Disable transitions
220
+ var $handle = this.$switch;
221
+ var handle = $handle[0];
222
+ var $t = this.$inner.add($handle).css('transition', 'none');
223
+
224
+ // Starting values
225
+ this.maxRight = this.$con.width() - $handle.width() -
226
+ $.css(handle, 'margin-left', true) -
227
+ $.css(handle, 'margin-right', true) -
228
+ $.css(handle, 'border-left-width', true) -
229
+ $.css(handle, 'border-right-width', true);
230
+ var startChecked = elem.checked;
231
+ this.moved = false;
232
+ this.startX = e.pageX + (startChecked ? 0 : this.maxRight);
233
+
234
+ // Bind document events
235
+ var self = this;
236
+ var $doc = this.$doc
237
+ .on(moveType + ns, $.proxy(this._handleMove, this))
238
+ .on(endType + ns, function() {
239
+ // Reenable transition
240
+ $t.css('transition', '');
241
+ $doc.off(ns);
242
+
243
+ setTimeout(function() {
244
+ // If there was a move
245
+ // ensure the proper checked value
246
+ if (self.moved) {
247
+ var checked = self.lastX > (self.startX - self.maxRight / 2);
248
+ if (elem.checked !== checked) {
249
+ elem.checked = checked;
250
+ // Trigger change in case it wasn't already fired
251
+ $elem.trigger('change');
252
+ }
253
+ }
254
+ // Normalize CSS and animate
255
+ self.$switch.css('right', '');
256
+ self.$inner.css('marginLeft', '');
257
+ });
258
+ });
259
+ },
260
+
261
+ /**
262
+ * Binds all necessary events
263
+ */
264
+ _bind: function() {
265
+ this._unbind();
266
+ this.$switch.on(
267
+ $.pointertouch.down,
268
+ $.proxy(this._startMove, this)
269
+ );
270
+ },
271
+
272
+ /**
273
+ * Enable or re-enable the onoff instance
274
+ */
275
+ enable: function() {
276
+ // Ensures the correct HTML before binding
277
+ this.wrap();
278
+ this._bind();
279
+ this.disabled = false;
280
+ },
281
+
282
+ /**
283
+ * Unbind all events
284
+ */
285
+ _unbind: function() {
286
+ this.$doc.add(this.$switch).off(this.options.namespace);
287
+ },
288
+
289
+ /**
290
+ * Disable onoff
291
+ * Removes all added HTML
292
+ */
293
+ disable: function() {
294
+ this.disabled = true;
295
+ this._unbind();
296
+ },
297
+
298
+ /**
299
+ * Removes all onoffswitch HTML and leaves the checkbox
300
+ * Also disables this instance
301
+ */
302
+ unwrap: function() {
303
+ // Destroys this OnOff
304
+ this.disable();
305
+ this.$label.remove();
306
+ this.$elem.unwrap().removeClass(this.options.className);
307
+ },
308
+
309
+ /**
310
+ * @returns {Boolean} Returns whether the current onoff instance is disabled
311
+ */
312
+ isDisabled: function() {
313
+ return this.disabled;
314
+ },
315
+
316
+ /**
317
+ * Destroy the onoff instance
318
+ */
319
+ destroy: function() {
320
+ this.disable();
321
+ $.removeData(this.elem, OnOff.datakey);
322
+ },
323
+
324
+ /**
325
+ * Get/set option on an existing instance
326
+ * @returns {Array|undefined} If getting, returns an array of
327
+ * all values on each instance for a given key. If setting,
328
+ * continue chaining by returning undefined.
329
+ */
330
+ option: function(key, value) {
331
+ var newOpts;
332
+ var options = this.options;
333
+ if (!key) {
334
+ // Avoids returning direct reference
335
+ return $.extend({}, options);
336
+ }
337
+
338
+ if (typeof key === 'string') {
339
+ if (arguments.length === 1) {
340
+ return options[ key ] !== undefined ?
341
+ options[ key ] :
342
+ null;
343
+ }
344
+ newOpts = {};
345
+ newOpts[ key ] = value;
346
+ } else {
347
+ newOpts = key;
348
+ }
349
+
350
+ // Set options
351
+ $.each(newOpts, $.proxy(function(k, val) {
352
+ switch(k) {
353
+ case 'namespace':
354
+ this._unbind();
355
+ break;
356
+ case 'className':
357
+ this.$elem.removeClass(options.className);
358
+ }
359
+ options[ k ] = val;
360
+ switch(k) {
361
+ case 'namespace':
362
+ this._bind();
363
+ break;
364
+ case 'className':
365
+ this.$elem.addClass(val);
366
+ }
367
+ }, this));
368
+ }
369
+ };
370
+
371
+ /**
372
+ * Extend jQuery
373
+ * @param {Object|String} options - The name of a method to call
374
+ * on the prototype or an object literal of options
375
+ * @returns {jQuery|Mixed} jQuery instance for regular chaining or
376
+ * the return value(s) of a onoff method call
377
+ */
378
+ $.fn.onoff = function(options) {
379
+ var instance, args, m, ret;
380
+
381
+ // Call methods widget-style
382
+ if (typeof options === 'string') {
383
+ ret = [];
384
+ args = slice.call(arguments, 1);
385
+ this.each(function() {
386
+ instance = $.data(this, OnOff.datakey);
387
+
388
+ if (!instance) {
389
+ ret.push(undefined);
390
+
391
+ // Ignore methods beginning with `_`
392
+ } else if (options.charAt(0) !== '_' &&
393
+ typeof (m = instance[ options ]) === 'function' &&
394
+ // If nothing is returned, do not add to return values
395
+ (m = m.apply(instance, args)) !== undefined) {
396
+
397
+ ret.push(m);
398
+ }
399
+ });
400
+
401
+ // Return an array of values for the jQuery instances
402
+ // Or the value itself if there is only one
403
+ // Or keep chaining
404
+ return ret.length ?
405
+ (ret.length === 1 ? ret[0] : ret) :
406
+ this;
407
+ }
408
+
409
+ return this.each(function() { new OnOff(this, options); });
410
+ };
411
+
412
+ return ($.OnOff = OnOff);
413
+ }));