spina-slider 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 90963772c9f8e02e26e926f550b0dd915e4a64a03e3d5a86667a00a789e3fa25
4
+ data.tar.gz: 9699388e52241b09d561e40784472141a02751e73a5c8264e72598f3b4f3239b
5
+ SHA512:
6
+ metadata.gz: 72472cf4e7d1d313c16dfdf75898319ed8faa6a04b3455246d9f17983b208ba9b6366870689a597872646ae2dbcdbe1f4e7e2d5b82d9221d188564ec774215b2
7
+ data.tar.gz: f82702036555456864875c019e59c20908dfb324cb8af4abf641284a72743314eeb5fc6a9491405ce0a4cad1eff96f05e4e1468f91f393091dc32c00a3587649
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/bundle/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at michael@actionvfx.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in spina-slider.gemspec
6
+ gemspec
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ spina-slider (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.17)
16
+ rake (~> 10.0)
17
+ spina-slider!
18
+
19
+ BUNDLED WITH
20
+ 1.17.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Michael Wilmouth
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Spina::Slider
2
+
3
+ This gem adds a [TwentyTwenty](https://zurb.com/playground/twentytwenty) before/after image slider to text
4
+ fields for [SpinaCMS](https://github.com/SpinaCMS/Spina).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'spina-slider', git: 'git://github.com/mawilmouth/spina-slider.git'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ ## Usage
19
+
20
+ Add these lines to your application.html.erb
21
+
22
+ ```
23
+ <%= javascript_include_tag 'slider'%>
24
+ <%= stylesheet_link_tag 'twentytwenty-no-compass'%>
25
+ ```
26
+
27
+ Add these lines to /app/assets/config/manifest.js
28
+
29
+ ```
30
+ //= link twentytwenty-no-compass.css
31
+ //= link slider.css
32
+ //= link slider.js
33
+ ```
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
38
+
39
+ ## Code of Conduct
40
+
41
+ Everyone interacting in the Spina::Slider project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mawilmouth/spina-slider/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,850 @@
1
+ class TrixAttachmentSlider {
2
+ photoAssignFirst(e, url) {
3
+ currentEditor = this;
4
+ var current_preview = $('.btn-selected').closest('.button-group').siblings('.slider-preview');
5
+ current_preview.find('.img.first').attr('src', url);
6
+ current_preview.siblings('.slider-markup').find('.img.first').attr('src', url);
7
+ current_preview.trigger('preview-slider');
8
+ var listItem = current_preview.siblings('.slider-todo').find('[data-slider-list=top]');
9
+ if (!listItem.hasClass('done')) listItem.addClass('done');
10
+ $('.btn-selected').removeClass('btn-selected');
11
+ }
12
+ photoAssignSecond(e, url) {
13
+ currentEditor = this;
14
+ var current_preview = $('.btn-selected').closest('.button-group').siblings('.slider-preview');
15
+ current_preview.find('.img.second').attr('src', url);
16
+ current_preview.siblings('.slider-markup').find('.img.second').attr('src', url);
17
+ current_preview.trigger('preview-slider');
18
+ var listItem = current_preview.siblings('.slider-todo').find('[data-slider-list=bottom]');
19
+ if (!listItem.hasClass('done')) listItem.toggleClass('done');
20
+ $('.btn-selected').removeClass('btn-selected');
21
+ }
22
+ previewSlider(e) {
23
+ $(this).find('.img-slider').twentytwenty();
24
+ }
25
+ addSlider(e) {
26
+ var slider_name = $(this).closest('.button-group').siblings('input.slider-name');
27
+ var name = slider_name.val();
28
+ if (name == '') {
29
+ alert('Please name the slider before inserting...');
30
+ } else {
31
+ var slider_markup = $(this).closest('.button-group').siblings('.slider-markup');
32
+ slider_markup.find('.slider-show').text(name);
33
+ var markup = slider_markup.html();
34
+ var attachment = new Trix.Attachment({content: markup, contentType: 'image-slider'});
35
+ currentEditor.editor.insertAttachment(attachment);
36
+ slider_markup.find('.img').attr('src', '#');
37
+ slider_markup.siblings('.slider-preview').find('.img').attr('src', '#');
38
+ slider_name.val('');
39
+ }
40
+ }
41
+ clearList(e) {
42
+ var topListItem = $(this).closest('.button-group').siblings('.slider-todo').find('[data-slider-list=top]');
43
+ var bottomListItem = topListItem.siblings('[data-slider-list=bottom]');
44
+ if (topListItem.hasClass('done')) topListItem.removeClass('done');
45
+ if (bottomListItem.hasClass('done')) bottomListItem.removeClass('done');
46
+ }
47
+ }
48
+
49
+ const customTrixTools = {
50
+ sliderBtn: '<button class="trix-button trix-button--text image-slider"' +
51
+ 'data-trix-action="image-slider" tabindex="-1" title="Before/After Image Slider"' +
52
+ 'type="button"><|></button>',
53
+
54
+ sliderDialog: '<div class="trix-dialogs" data-trix-dialogs=true>' +
55
+ '<div class="trix-dialog trix-dialog--image-slider"' +
56
+ 'data-trix-dialog-attribute="image-slider" data-trix-dialog="image-slider">' +
57
+ '<div class="trix-dialog__link-fields input-fields slider-fields">' +
58
+ '<ul class="slider-todo">' +
59
+ '<li data-slider-list="top">Select Top Image</li>' +
60
+ '<li data-slider-list="bottom">Select Bottom Image</li>' +
61
+ '</ul>' +
62
+ '<div class="slider-preview">' +
63
+ '<div class="img-slider">' +
64
+ '<img class="img first" src="#" alt="Top Photo" style="width:100%;"/>' +
65
+ '<img class="img second" src="#" alt="Bottom Photo" style="width:100%;height:100%"/>' +
66
+ '</div>' +
67
+ '</div>' +
68
+ '<div class="slider-markup">' +
69
+ '<span class="slider-show"></span>' +
70
+ '<div class="img-slider">' +
71
+ '<img class="img first" src="#" style="width:100%;"/>' +
72
+ '<img class="img second" src="#" style="width:100%;height:100%"/>' +
73
+ '</div>' +
74
+ '</div>' +
75
+ '<input type="text" class="slider-name" placeholder="Name this slider">' +
76
+ '<div class="button-group">' +
77
+ '<div class="first-slider-file button">Top Image</div>' +
78
+ '<div class="second-slider-file button">Bottom Image</div>' +
79
+ '<div class="add-slider button">Add to content</div>' +
80
+ '</div>' +
81
+ '</div>' +
82
+ '</div>' +
83
+ '</div>'
84
+ };
85
+
86
+ var sliderController = new TrixAttachmentSlider;
87
+ var currentEditor;
88
+
89
+
90
+ /*
91
+
92
+
93
+ jquery.event.move START
94
+
95
+
96
+ */
97
+
98
+ (function(fn) {
99
+ if (typeof define === 'function' && define.amd) {
100
+ define([], fn);
101
+ } else if ((typeof module !== "undefined" && module !== null) && module.exports) {
102
+ module.exports = fn;
103
+ } else {
104
+ fn();
105
+ }
106
+ })(function(){
107
+ var assign = Object.assign || window.jQuery && jQuery.extend;
108
+
109
+ // Number of pixels a pressed pointer travels before movestart
110
+ // event is fired.
111
+ var threshold = 8;
112
+
113
+ // Shim for requestAnimationFrame, falling back to timer. See:
114
+ // see http://paulirish.com/2011/requestanimationframe-for-smart-animating/
115
+ var requestFrame = (function(){
116
+ return (
117
+ window.requestAnimationFrame ||
118
+ window.webkitRequestAnimationFrame ||
119
+ window.mozRequestAnimationFrame ||
120
+ window.oRequestAnimationFrame ||
121
+ window.msRequestAnimationFrame ||
122
+ function(fn, element){
123
+ return window.setTimeout(function(){
124
+ fn();
125
+ }, 25);
126
+ }
127
+ );
128
+ })();
129
+
130
+ // Shim for customEvent
131
+ // see https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent#Polyfill
132
+ (function () {
133
+ if ( typeof window.CustomEvent === "function" ) return false;
134
+ function CustomEvent ( event, params ) {
135
+ params = params || { bubbles: false, cancelable: false, detail: undefined };
136
+ var evt = document.createEvent( 'CustomEvent' );
137
+ evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
138
+ return evt;
139
+ }
140
+
141
+ CustomEvent.prototype = window.Event.prototype;
142
+ window.CustomEvent = CustomEvent;
143
+ })();
144
+
145
+ var ignoreTags = {
146
+ textarea: true,
147
+ input: true,
148
+ select: true,
149
+ button: true
150
+ };
151
+
152
+ var mouseevents = {
153
+ move: 'mousemove',
154
+ cancel: 'mouseup dragstart',
155
+ end: 'mouseup'
156
+ };
157
+
158
+ var touchevents = {
159
+ move: 'touchmove',
160
+ cancel: 'touchend',
161
+ end: 'touchend'
162
+ };
163
+
164
+ var rspaces = /\s+/;
165
+
166
+
167
+ // DOM Events
168
+
169
+ var eventOptions = { bubbles: true, cancelable: true };
170
+
171
+ var eventsSymbol = typeof Symbol === "function" ? Symbol('events') : {};
172
+
173
+ function createEvent(type) {
174
+ return new CustomEvent(type, eventOptions);
175
+ }
176
+
177
+ function getEvents(node) {
178
+ return node[eventsSymbol] || (node[eventsSymbol] = {});
179
+ }
180
+
181
+ function on(node, types, fn, data, selector) {
182
+ types = types.split(rspaces);
183
+
184
+ var events = getEvents(node);
185
+ var i = types.length;
186
+ var handlers, type;
187
+
188
+ function handler(e) { fn(e, data); }
189
+
190
+ while (i--) {
191
+ type = types[i];
192
+ handlers = events[type] || (events[type] = []);
193
+ handlers.push([fn, handler]);
194
+ node.addEventListener(type, handler);
195
+ }
196
+ }
197
+
198
+ function off(node, types, fn, selector) {
199
+ types = types.split(rspaces);
200
+
201
+ var events = getEvents(node);
202
+ var i = types.length;
203
+ var type, handlers, k;
204
+
205
+ if (!events) { return; }
206
+
207
+ while (i--) {
208
+ type = types[i];
209
+ handlers = events[type];
210
+ if (!handlers) { continue; }
211
+ k = handlers.length;
212
+ while (k--) {
213
+ if (handlers[k][0] === fn) {
214
+ node.removeEventListener(type, handlers[k][1]);
215
+ handlers.splice(k, 1);
216
+ }
217
+ }
218
+ }
219
+ }
220
+
221
+ function trigger(node, type, properties) {
222
+ // Don't cache events. It prevents you from triggering an event of a
223
+ // given type from inside the handler of another event of that type.
224
+ var event = createEvent(type);
225
+ if (properties) { assign(event, properties); }
226
+ node.dispatchEvent(event);
227
+ }
228
+
229
+
230
+ // Constructors
231
+
232
+ function Timer(fn){
233
+ var callback = fn,
234
+ active = false,
235
+ running = false;
236
+
237
+ function trigger(time) {
238
+ if (active){
239
+ callback();
240
+ requestFrame(trigger);
241
+ running = true;
242
+ active = false;
243
+ }
244
+ else {
245
+ running = false;
246
+ }
247
+ }
248
+
249
+ this.kick = function(fn) {
250
+ active = true;
251
+ if (!running) { trigger(); }
252
+ };
253
+
254
+ this.end = function(fn) {
255
+ var cb = callback;
256
+
257
+ if (!fn) { return; }
258
+
259
+ // If the timer is not running, simply call the end callback.
260
+ if (!running) {
261
+ fn();
262
+ }
263
+ // If the timer is running, and has been kicked lately, then
264
+ // queue up the current callback and the end callback, otherwise
265
+ // just the end callback.
266
+ else {
267
+ callback = active ?
268
+ function(){ cb(); fn(); } :
269
+ fn ;
270
+
271
+ active = true;
272
+ }
273
+ };
274
+ }
275
+
276
+
277
+ // Functions
278
+
279
+ function noop() {}
280
+
281
+ function preventDefault(e) {
282
+ e.preventDefault();
283
+ }
284
+
285
+ function isIgnoreTag(e) {
286
+ return !!ignoreTags[e.target.tagName.toLowerCase()];
287
+ }
288
+
289
+ function isPrimaryButton(e) {
290
+ // Ignore mousedowns on any button other than the left (or primary)
291
+ // mouse button, or when a modifier key is pressed.
292
+ return (e.which === 1 && !e.ctrlKey && !e.altKey);
293
+ }
294
+
295
+ function identifiedTouch(touchList, id) {
296
+ var i, l;
297
+
298
+ if (touchList.identifiedTouch) {
299
+ return touchList.identifiedTouch(id);
300
+ }
301
+
302
+ // touchList.identifiedTouch() does not exist in
303
+ // webkit yet… we must do the search ourselves...
304
+
305
+ i = -1;
306
+ l = touchList.length;
307
+
308
+ while (++i < l) {
309
+ if (touchList[i].identifier === id) {
310
+ return touchList[i];
311
+ }
312
+ }
313
+ }
314
+
315
+ function changedTouch(e, data) {
316
+ var touch = identifiedTouch(e.changedTouches, data.identifier);
317
+
318
+ // This isn't the touch you're looking for.
319
+ if (!touch) { return; }
320
+
321
+ // Chrome Android (at least) includes touches that have not
322
+ // changed in e.changedTouches. That's a bit annoying. Check
323
+ // that this touch has changed.
324
+ if (touch.pageX === data.pageX && touch.pageY === data.pageY) { return; }
325
+
326
+ return touch;
327
+ }
328
+
329
+
330
+ // Handlers that decide when the first movestart is triggered
331
+
332
+ function mousedown(e){
333
+ // Ignore non-primary buttons
334
+ if (!isPrimaryButton(e)) { return; }
335
+
336
+ // Ignore form and interactive elements
337
+ if (isIgnoreTag(e)) { return; }
338
+
339
+ on(document, mouseevents.move, mousemove, e);
340
+ on(document, mouseevents.cancel, mouseend, e);
341
+ }
342
+
343
+ function mousemove(e, data){
344
+ checkThreshold(e, data, e, removeMouse);
345
+ }
346
+
347
+ function mouseend(e, data) {
348
+ removeMouse();
349
+ }
350
+
351
+ function removeMouse() {
352
+ off(document, mouseevents.move, mousemove);
353
+ off(document, mouseevents.cancel, mouseend);
354
+ }
355
+
356
+ function touchstart(e) {
357
+ // Don't get in the way of interaction with form elements
358
+ if (ignoreTags[e.target.tagName.toLowerCase()]) { return; }
359
+
360
+ var touch = e.changedTouches[0];
361
+
362
+ // iOS live updates the touch objects whereas Android gives us copies.
363
+ // That means we can't trust the touchstart object to stay the same,
364
+ // so we must copy the data. This object acts as a template for
365
+ // movestart, move and moveend event objects.
366
+ var data = {
367
+ target: touch.target,
368
+ pageX: touch.pageX,
369
+ pageY: touch.pageY,
370
+ identifier: touch.identifier,
371
+
372
+ // The only way to make handlers individually unbindable is by
373
+ // making them unique.
374
+ touchmove: function(e, data) { touchmove(e, data); },
375
+ touchend: function(e, data) { touchend(e, data); }
376
+ };
377
+
378
+ on(document, touchevents.move, data.touchmove, data);
379
+ on(document, touchevents.cancel, data.touchend, data);
380
+ }
381
+
382
+ function touchmove(e, data) {
383
+ var touch = changedTouch(e, data);
384
+ if (!touch) { return; }
385
+ checkThreshold(e, data, touch, removeTouch);
386
+ }
387
+
388
+ function touchend(e, data) {
389
+ var touch = identifiedTouch(e.changedTouches, data.identifier);
390
+ if (!touch) { return; }
391
+ removeTouch(data);
392
+ }
393
+
394
+ function removeTouch(data) {
395
+ off(document, touchevents.move, data.touchmove);
396
+ off(document, touchevents.cancel, data.touchend);
397
+ }
398
+
399
+ function checkThreshold(e, data, touch, fn) {
400
+ var distX = touch.pageX - data.pageX;
401
+ var distY = touch.pageY - data.pageY;
402
+
403
+ // Do nothing if the threshold has not been crossed.
404
+ if ((distX * distX) + (distY * distY) < (threshold * threshold)) { return; }
405
+
406
+ triggerStart(e, data, touch, distX, distY, fn);
407
+ }
408
+
409
+ function triggerStart(e, data, touch, distX, distY, fn) {
410
+ var touches = e.targetTouches;
411
+ var time = e.timeStamp - data.timeStamp;
412
+
413
+ // Create a movestart object with some special properties that
414
+ // are passed only to the movestart handlers.
415
+ var template = {
416
+ altKey: e.altKey,
417
+ ctrlKey: e.ctrlKey,
418
+ shiftKey: e.shiftKey,
419
+ startX: data.pageX,
420
+ startY: data.pageY,
421
+ distX: distX,
422
+ distY: distY,
423
+ deltaX: distX,
424
+ deltaY: distY,
425
+ pageX: touch.pageX,
426
+ pageY: touch.pageY,
427
+ velocityX: distX / time,
428
+ velocityY: distY / time,
429
+ identifier: data.identifier,
430
+ targetTouches: touches,
431
+ finger: touches ? touches.length : 1,
432
+ enableMove: function() {
433
+ this.moveEnabled = true;
434
+ this.enableMove = noop;
435
+ e.preventDefault();
436
+ }
437
+ };
438
+
439
+ // Trigger the movestart event.
440
+ trigger(data.target, 'movestart', template);
441
+
442
+ // Unbind handlers that tracked the touch or mouse up till now.
443
+ fn(data);
444
+ }
445
+
446
+
447
+ // Handlers that control what happens following a movestart
448
+
449
+ function activeMousemove(e, data) {
450
+ var timer = data.timer;
451
+
452
+ data.touch = e;
453
+ data.timeStamp = e.timeStamp;
454
+ timer.kick();
455
+ }
456
+
457
+ function activeMouseend(e, data) {
458
+ var target = data.target;
459
+ var event = data.event;
460
+ var timer = data.timer;
461
+
462
+ removeActiveMouse();
463
+
464
+ endEvent(target, event, timer, function() {
465
+ // Unbind the click suppressor, waiting until after mouseup
466
+ // has been handled.
467
+ setTimeout(function(){
468
+ off(target, 'click', preventDefault);
469
+ }, 0);
470
+ });
471
+ }
472
+
473
+ function removeActiveMouse() {
474
+ off(document, mouseevents.move, activeMousemove);
475
+ off(document, mouseevents.end, activeMouseend);
476
+ }
477
+
478
+ function activeTouchmove(e, data) {
479
+ var event = data.event;
480
+ var timer = data.timer;
481
+ var touch = changedTouch(e, event);
482
+
483
+ if (!touch) { return; }
484
+
485
+ // Stop the interface from gesturing
486
+ e.preventDefault();
487
+
488
+ event.targetTouches = e.targetTouches;
489
+ data.touch = touch;
490
+ data.timeStamp = e.timeStamp;
491
+
492
+ timer.kick();
493
+ }
494
+
495
+ function activeTouchend(e, data) {
496
+ var target = data.target;
497
+ var event = data.event;
498
+ var timer = data.timer;
499
+ var touch = identifiedTouch(e.changedTouches, event.identifier);
500
+
501
+ // This isn't the touch you're looking for.
502
+ if (!touch) { return; }
503
+
504
+ removeActiveTouch(data);
505
+ endEvent(target, event, timer);
506
+ }
507
+
508
+ function removeActiveTouch(data) {
509
+ off(document, touchevents.move, data.activeTouchmove);
510
+ off(document, touchevents.end, data.activeTouchend);
511
+ }
512
+
513
+
514
+ // Logic for triggering move and moveend events
515
+
516
+ function updateEvent(event, touch, timeStamp) {
517
+ var time = timeStamp - event.timeStamp;
518
+
519
+ event.distX = touch.pageX - event.startX;
520
+ event.distY = touch.pageY - event.startY;
521
+ event.deltaX = touch.pageX - event.pageX;
522
+ event.deltaY = touch.pageY - event.pageY;
523
+
524
+ // Average the velocity of the last few events using a decay
525
+ // curve to even out spurious jumps in values.
526
+ event.velocityX = 0.3 * event.velocityX + 0.7 * event.deltaX / time;
527
+ event.velocityY = 0.3 * event.velocityY + 0.7 * event.deltaY / time;
528
+ event.pageX = touch.pageX;
529
+ event.pageY = touch.pageY;
530
+ }
531
+
532
+ function endEvent(target, event, timer, fn) {
533
+ timer.end(function(){
534
+ trigger(target, 'moveend', event);
535
+ return fn && fn();
536
+ });
537
+ }
538
+
539
+
540
+ // Set up the DOM
541
+
542
+ function movestart(e) {
543
+ if (e.defaultPrevented) { return; }
544
+ if (!e.moveEnabled) { return; }
545
+
546
+ var event = {
547
+ startX: e.startX,
548
+ startY: e.startY,
549
+ pageX: e.pageX,
550
+ pageY: e.pageY,
551
+ distX: e.distX,
552
+ distY: e.distY,
553
+ deltaX: e.deltaX,
554
+ deltaY: e.deltaY,
555
+ velocityX: e.velocityX,
556
+ velocityY: e.velocityY,
557
+ identifier: e.identifier,
558
+ targetTouches: e.targetTouches,
559
+ finger: e.finger
560
+ };
561
+
562
+ var data = {
563
+ target: e.target,
564
+ event: event,
565
+ timer: new Timer(update),
566
+ touch: undefined,
567
+ timeStamp: e.timeStamp
568
+ };
569
+
570
+ function update(time) {
571
+ updateEvent(event, data.touch, data.timeStamp);
572
+ trigger(data.target, 'move', event);
573
+ }
574
+
575
+ if (e.identifier === undefined) {
576
+ // We're dealing with a mouse event.
577
+ // Stop clicks from propagating during a move
578
+ on(e.target, 'click', preventDefault);
579
+ on(document, mouseevents.move, activeMousemove, data);
580
+ on(document, mouseevents.end, activeMouseend, data);
581
+ }
582
+ else {
583
+ // In order to unbind correct handlers they have to be unique
584
+ data.activeTouchmove = function(e, data) { activeTouchmove(e, data); };
585
+ data.activeTouchend = function(e, data) { activeTouchend(e, data); };
586
+
587
+ // We're dealing with a touch.
588
+ on(document, touchevents.move, data.activeTouchmove, data);
589
+ on(document, touchevents.end, data.activeTouchend, data);
590
+ }
591
+ }
592
+
593
+ on(document, 'mousedown', mousedown);
594
+ on(document, 'touchstart', touchstart);
595
+ on(document, 'movestart', movestart);
596
+
597
+
598
+ // jQuery special events
599
+ //
600
+ // jQuery event objects are copies of DOM event objects. They need
601
+ // a little help copying the move properties across.
602
+
603
+ if (!window.jQuery) { return; }
604
+
605
+ var properties = ("startX startY pageX pageY distX distY deltaX deltaY velocityX velocityY").split(' ');
606
+
607
+ function enableMove1(e) { e.enableMove(); }
608
+ function enableMove2(e) { e.enableMove(); }
609
+ function enableMove3(e) { e.enableMove(); }
610
+
611
+ function add(handleObj) {
612
+ var handler = handleObj.handler;
613
+
614
+ handleObj.handler = function(e) {
615
+ // Copy move properties across from originalEvent
616
+ var i = properties.length;
617
+ var property;
618
+
619
+ while(i--) {
620
+ property = properties[i];
621
+ e[property] = e.originalEvent[property];
622
+ }
623
+
624
+ handler.apply(this, arguments);
625
+ };
626
+ }
627
+
628
+ jQuery.event.special.movestart = {
629
+ setup: function() {
630
+ // Movestart must be enabled to allow other move events
631
+ on(this, 'movestart', enableMove1);
632
+
633
+ // Do listen to DOM events
634
+ return false;
635
+ },
636
+
637
+ teardown: function() {
638
+ off(this, 'movestart', enableMove1);
639
+ return false;
640
+ },
641
+
642
+ add: add
643
+ };
644
+
645
+ jQuery.event.special.move = {
646
+ setup: function() {
647
+ on(this, 'movestart', enableMove2);
648
+ return false;
649
+ },
650
+
651
+ teardown: function() {
652
+ off(this, 'movestart', enableMove2);
653
+ return false;
654
+ },
655
+
656
+ add: add
657
+ };
658
+
659
+ jQuery.event.special.moveend = {
660
+ setup: function() {
661
+ on(this, 'movestart', enableMove3);
662
+ return false;
663
+ },
664
+
665
+ teardown: function() {
666
+ off(this, 'movestart', enableMove3);
667
+ return false;
668
+ },
669
+
670
+ add: add
671
+ };
672
+ });
673
+
674
+
675
+ /*
676
+
677
+
678
+ jquery.event.move END
679
+
680
+
681
+ */
682
+
683
+
684
+
685
+
686
+ /*
687
+
688
+
689
+ jquery.twentytwenty START
690
+
691
+
692
+ */
693
+ (function($){
694
+
695
+ $.fn.twentytwenty = function(options) {
696
+ var options = $.extend({
697
+ default_offset_pct: 0.5,
698
+ orientation: 'horizontal',
699
+ before_label: 'Before',
700
+ after_label: 'After',
701
+ no_overlay: false,
702
+ move_slider_on_hover: false,
703
+ move_with_handle_only: true,
704
+ click_to_move: false
705
+ }, options);
706
+
707
+ return this.each(function() {
708
+
709
+ var sliderPct = options.default_offset_pct;
710
+ var container = $(this);
711
+ var sliderOrientation = options.orientation;
712
+ var beforeDirection = (sliderOrientation === 'vertical') ? 'down' : 'left';
713
+ var afterDirection = (sliderOrientation === 'vertical') ? 'up' : 'right';
714
+
715
+
716
+ container.wrap("<div class='twentytwenty-wrapper twentytwenty-" + sliderOrientation + "'></div>");
717
+ if(!options.no_overlay) {
718
+ container.append("<div class='twentytwenty-overlay'></div>");
719
+ var overlay = container.find(".twentytwenty-overlay");
720
+ overlay.append("<div class='twentytwenty-before-label' data-content='"+options.before_label+"'></div>");
721
+ overlay.append("<div class='twentytwenty-after-label' data-content='"+options.after_label+"'></div>");
722
+ }
723
+ var beforeImg = container.find("img:first");
724
+ var afterImg = container.find("img:last");
725
+ container.append("<div class='twentytwenty-handle'></div>");
726
+ var slider = container.find(".twentytwenty-handle");
727
+ slider.append("<span class='twentytwenty-" + beforeDirection + "-arrow'></span>");
728
+ slider.append("<span class='twentytwenty-" + afterDirection + "-arrow'></span>");
729
+ container.addClass("twentytwenty-container");
730
+ beforeImg.addClass("twentytwenty-before");
731
+ afterImg.addClass("twentytwenty-after");
732
+
733
+ var calcOffset = function(dimensionPct) {
734
+ var w = beforeImg.width();
735
+ var h = beforeImg.height();
736
+ return {
737
+ w: w+"px",
738
+ h: h+"px",
739
+ cw: (dimensionPct*w)+"px",
740
+ ch: (dimensionPct*h)+"px"
741
+ };
742
+ };
743
+
744
+ var adjustContainer = function(offset) {
745
+ if (sliderOrientation === 'vertical') {
746
+ beforeImg.css("clip", "rect(0,"+offset.w+","+offset.ch+",0)");
747
+ afterImg.css("clip", "rect("+offset.ch+","+offset.w+","+offset.h+",0)");
748
+ }
749
+ else {
750
+ beforeImg.css("clip", "rect(0,"+offset.cw+","+offset.h+",0)");
751
+ afterImg.css("clip", "rect(0,"+offset.w+","+offset.h+","+offset.cw+")");
752
+ }
753
+ container.css("height", offset.h);
754
+ };
755
+
756
+ var adjustSlider = function(pct) {
757
+ var offset = calcOffset(pct);
758
+ slider.css((sliderOrientation==="vertical") ? "top" : "left", (sliderOrientation==="vertical") ? offset.ch : offset.cw);
759
+ adjustContainer(offset);
760
+ };
761
+
762
+ // Return the number specified or the min/max number if it outside the range given.
763
+ var minMaxNumber = function(num, min, max) {
764
+ return Math.max(min, Math.min(max, num));
765
+ };
766
+
767
+ // Calculate the slider percentage based on the position.
768
+ var getSliderPercentage = function(positionX, positionY) {
769
+ var sliderPercentage = (sliderOrientation === 'vertical') ?
770
+ (positionY-offsetY)/imgHeight :
771
+ (positionX-offsetX)/imgWidth;
772
+
773
+ return minMaxNumber(sliderPercentage, 0, 1);
774
+ };
775
+
776
+
777
+ $(window).on("resize.twentytwenty", function(e) {
778
+ adjustSlider(sliderPct);
779
+ });
780
+
781
+ var offsetX = 0;
782
+ var offsetY = 0;
783
+ var imgWidth = 0;
784
+ var imgHeight = 0;
785
+ var onMoveStart = function(e) {
786
+ if (((e.distX > e.distY && e.distX < -e.distY) || (e.distX < e.distY && e.distX > -e.distY)) && sliderOrientation !== 'vertical') {
787
+ e.preventDefault();
788
+ }
789
+ else if (((e.distX < e.distY && e.distX < -e.distY) || (e.distX > e.distY && e.distX > -e.distY)) && sliderOrientation === 'vertical') {
790
+ e.preventDefault();
791
+ }
792
+ container.addClass("active");
793
+ offsetX = container.offset().left;
794
+ offsetY = container.offset().top;
795
+ imgWidth = beforeImg.width();
796
+ imgHeight = beforeImg.height();
797
+ };
798
+ var onMove = function(e) {
799
+ if (container.hasClass("active")) {
800
+ sliderPct = getSliderPercentage(e.pageX, e.pageY);
801
+ adjustSlider(sliderPct);
802
+ }
803
+ };
804
+ var onMoveEnd = function() {
805
+ container.removeClass("active");
806
+ };
807
+
808
+ var moveTarget = options.move_with_handle_only ? slider : container;
809
+ moveTarget.on("movestart",onMoveStart);
810
+ moveTarget.on("move",onMove);
811
+ moveTarget.on("moveend",onMoveEnd);
812
+
813
+ if (options.move_slider_on_hover) {
814
+ container.on("mouseenter", onMoveStart);
815
+ container.on("mousemove", onMove);
816
+ container.on("mouseleave", onMoveEnd);
817
+ }
818
+
819
+ slider.on("touchmove", function(e) {
820
+ e.preventDefault();
821
+ });
822
+
823
+ container.find("img").on("mousedown", function(event) {
824
+ event.preventDefault();
825
+ });
826
+
827
+ if (options.click_to_move) {
828
+ container.on('click', function(e) {
829
+ offsetX = container.offset().left;
830
+ offsetY = container.offset().top;
831
+ imgWidth = beforeImg.width();
832
+ imgHeight = beforeImg.height();
833
+
834
+ sliderPct = getSliderPercentage(e.pageX, e.pageY);
835
+ adjustSlider(sliderPct);
836
+ });
837
+ }
838
+
839
+ $(window).trigger("resize.twentytwenty");
840
+ });
841
+ };
842
+
843
+ })(jQuery);
844
+ /*
845
+
846
+
847
+ jquery.twentytwenty END
848
+
849
+
850
+ */