sibu 0.2.5 → 0.2.6

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 (36) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/images/sibu/empty.png +0 -0
  3. data/app/assets/javascripts/cropper/cropper.js +3695 -0
  4. data/app/assets/javascripts/sibu/sibu.js.erb +25 -0
  5. data/app/assets/stylesheets/cropper/cropper.css +305 -0
  6. data/app/assets/stylesheets/sibu/sibu.css +1 -0
  7. data/app/controllers/sibu/application_controller.rb +4 -0
  8. data/app/controllers/sibu/images_controller.rb +7 -11
  9. data/app/controllers/sibu/pages_controller.rb +14 -4
  10. data/app/controllers/sibu/sites_controller.rb +13 -3
  11. data/app/helpers/sibu/application_helper.rb +4 -0
  12. data/app/helpers/sibu/pages_helper.rb +1 -9
  13. data/app/helpers/sibu/sites_helper.rb +8 -4
  14. data/app/models/concerns/sibu/image_uploader.rb +2 -2
  15. data/app/models/concerns/sibu/user_concern.rb +9 -0
  16. data/app/models/sibu/image.rb +12 -4
  17. data/app/models/sibu/page.rb +12 -1
  18. data/app/models/sibu/site.rb +19 -2
  19. data/app/views/sibu/images/_edit_form.html.erb +4 -4
  20. data/app/views/sibu/images/_form.html.erb +2 -2
  21. data/app/views/sibu/images/index.html.erb +3 -3
  22. data/app/views/sibu/images/new.html.erb +4 -0
  23. data/app/views/sibu/pages/_form.html.erb +0 -7
  24. data/app/views/sibu/pages/_media_edit_panel.html.erb +24 -12
  25. data/app/views/sibu/pages/edit_element.js.erb +54 -39
  26. data/app/views/sibu/pages/index.html.erb +1 -0
  27. data/app/views/sibu/pages/update_element.js.erb +1 -1
  28. data/app/views/sibu/sites/_form.html.erb +35 -13
  29. data/app/views/sibu/sites/edit.html.erb +1 -0
  30. data/app/views/sibu/sites/index.html.erb +4 -1
  31. data/config/routes.rb +4 -2
  32. data/db/migrate/20180321144021_move_images_to_user_level.rb +7 -0
  33. data/db/migrate/20180321170310_add_version_to_sibu_sites.rb +7 -0
  34. data/lib/sibu/version.rb +1 -1
  35. metadata +8 -3
  36. data/app/views/sibu/pages/_custom_styles.html.erb +0 -23
@@ -0,0 +1,3695 @@
1
+ /*!
2
+ * Cropper.js v1.3.3
3
+ * https://github.com/fengyuanchen/cropperjs
4
+ *
5
+ * Copyright (c) 2015-2018 Chen Fengyuan
6
+ * Released under the MIT license
7
+ *
8
+ * Date: 2018-03-18T03:19:54.147Z
9
+ */
10
+
11
+ (function (global, factory) {
12
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
13
+ typeof define === 'function' && define.amd ? define(factory) :
14
+ (global.Cropper = factory());
15
+ }(this, (function () { 'use strict';
16
+
17
+ var IN_BROWSER = typeof window !== 'undefined';
18
+ var WINDOW = IN_BROWSER ? window : {};
19
+ var NAMESPACE = 'cropper';
20
+
21
+ // Actions
22
+ var ACTION_ALL = 'all';
23
+ var ACTION_CROP = 'crop';
24
+ var ACTION_MOVE = 'move';
25
+ var ACTION_ZOOM = 'zoom';
26
+ var ACTION_EAST = 'e';
27
+ var ACTION_WEST = 'w';
28
+ var ACTION_SOUTH = 's';
29
+ var ACTION_NORTH = 'n';
30
+ var ACTION_NORTH_EAST = 'ne';
31
+ var ACTION_NORTH_WEST = 'nw';
32
+ var ACTION_SOUTH_EAST = 'se';
33
+ var ACTION_SOUTH_WEST = 'sw';
34
+
35
+ // Classes
36
+ var CLASS_CROP = NAMESPACE + '-crop';
37
+ var CLASS_DISABLED = NAMESPACE + '-disabled';
38
+ var CLASS_HIDDEN = NAMESPACE + '-hidden';
39
+ var CLASS_HIDE = NAMESPACE + '-hide';
40
+ var CLASS_INVISIBLE = NAMESPACE + '-invisible';
41
+ var CLASS_MODAL = NAMESPACE + '-modal';
42
+ var CLASS_MOVE = NAMESPACE + '-move';
43
+
44
+ // Data keys
45
+ var DATA_ACTION = 'action';
46
+ var DATA_PREVIEW = 'preview';
47
+
48
+ // Drag modes
49
+ var DRAG_MODE_CROP = 'crop';
50
+ var DRAG_MODE_MOVE = 'move';
51
+ var DRAG_MODE_NONE = 'none';
52
+
53
+ // Events
54
+ var EVENT_CROP = 'crop';
55
+ var EVENT_CROP_END = 'cropend';
56
+ var EVENT_CROP_MOVE = 'cropmove';
57
+ var EVENT_CROP_START = 'cropstart';
58
+ var EVENT_DBLCLICK = 'dblclick';
59
+ var EVENT_LOAD = 'load';
60
+ var EVENT_POINTER_DOWN = WINDOW.PointerEvent ? 'pointerdown' : 'touchstart mousedown';
61
+ var EVENT_POINTER_MOVE = WINDOW.PointerEvent ? 'pointermove' : 'touchmove mousemove';
62
+ var EVENT_POINTER_UP = WINDOW.PointerEvent ? 'pointerup pointercancel' : 'touchend touchcancel mouseup';
63
+ var EVENT_READY = 'ready';
64
+ var EVENT_RESIZE = 'resize';
65
+ var EVENT_WHEEL = 'wheel mousewheel DOMMouseScroll';
66
+ var EVENT_ZOOM = 'zoom';
67
+
68
+ // RegExps
69
+ var REGEXP_ACTIONS = /^(?:e|w|s|n|se|sw|ne|nw|all|crop|move|zoom)$/;
70
+ var REGEXP_DATA_URL = /^data:/;
71
+ var REGEXP_DATA_URL_JPEG = /^data:image\/jpeg;base64,/;
72
+ var REGEXP_TAG_NAME = /^(?:img|canvas)$/i;
73
+
74
+ var DEFAULTS = {
75
+ // Define the view mode of the cropper
76
+ viewMode: 0, // 0, 1, 2, 3
77
+
78
+ // Define the dragging mode of the cropper
79
+ dragMode: DRAG_MODE_CROP, // 'crop', 'move' or 'none'
80
+
81
+ // Define the aspect ratio of the crop box
82
+ aspectRatio: NaN,
83
+
84
+ // An object with the previous cropping result data
85
+ data: null,
86
+
87
+ // A selector for adding extra containers to preview
88
+ preview: '',
89
+
90
+ // Re-render the cropper when resize the window
91
+ responsive: true,
92
+
93
+ // Restore the cropped area after resize the window
94
+ restore: true,
95
+
96
+ // Check if the current image is a cross-origin image
97
+ checkCrossOrigin: true,
98
+
99
+ // Check the current image's Exif Orientation information
100
+ checkOrientation: true,
101
+
102
+ // Show the black modal
103
+ modal: true,
104
+
105
+ // Show the dashed lines for guiding
106
+ guides: true,
107
+
108
+ // Show the center indicator for guiding
109
+ center: true,
110
+
111
+ // Show the white modal to highlight the crop box
112
+ highlight: true,
113
+
114
+ // Show the grid background
115
+ background: true,
116
+
117
+ // Enable to crop the image automatically when initialize
118
+ autoCrop: true,
119
+
120
+ // Define the percentage of automatic cropping area when initializes
121
+ autoCropArea: 0.8,
122
+
123
+ // Enable to move the image
124
+ movable: true,
125
+
126
+ // Enable to rotate the image
127
+ rotatable: true,
128
+
129
+ // Enable to scale the image
130
+ scalable: true,
131
+
132
+ // Enable to zoom the image
133
+ zoomable: true,
134
+
135
+ // Enable to zoom the image by dragging touch
136
+ zoomOnTouch: true,
137
+
138
+ // Enable to zoom the image by wheeling mouse
139
+ zoomOnWheel: true,
140
+
141
+ // Define zoom ratio when zoom the image by wheeling mouse
142
+ wheelZoomRatio: 0.1,
143
+
144
+ // Enable to move the crop box
145
+ cropBoxMovable: true,
146
+
147
+ // Enable to resize the crop box
148
+ cropBoxResizable: true,
149
+
150
+ // Toggle drag mode between "crop" and "move" when click twice on the cropper
151
+ toggleDragModeOnDblclick: true,
152
+
153
+ // Size limitation
154
+ minCanvasWidth: 0,
155
+ minCanvasHeight: 0,
156
+ minCropBoxWidth: 0,
157
+ minCropBoxHeight: 0,
158
+ minContainerWidth: 200,
159
+ minContainerHeight: 100,
160
+
161
+ // Shortcuts of events
162
+ ready: null,
163
+ cropstart: null,
164
+ cropmove: null,
165
+ cropend: null,
166
+ crop: null,
167
+ zoom: null
168
+ };
169
+
170
+ var TEMPLATE = '<div class="cropper-container" touch-action="none">' + '<div class="cropper-wrap-box">' + '<div class="cropper-canvas"></div>' + '</div>' + '<div class="cropper-drag-box"></div>' + '<div class="cropper-crop-box">' + '<span class="cropper-view-box"></span>' + '<span class="cropper-dashed dashed-h"></span>' + '<span class="cropper-dashed dashed-v"></span>' + '<span class="cropper-center"></span>' + '<span class="cropper-face"></span>' + '<span class="cropper-line line-e" data-action="e"></span>' + '<span class="cropper-line line-n" data-action="n"></span>' + '<span class="cropper-line line-w" data-action="w"></span>' + '<span class="cropper-line line-s" data-action="s"></span>' + '<span class="cropper-point point-e" data-action="e"></span>' + '<span class="cropper-point point-n" data-action="n"></span>' + '<span class="cropper-point point-w" data-action="w"></span>' + '<span class="cropper-point point-s" data-action="s"></span>' + '<span class="cropper-point point-ne" data-action="ne"></span>' + '<span class="cropper-point point-nw" data-action="nw"></span>' + '<span class="cropper-point point-sw" data-action="sw"></span>' + '<span class="cropper-point point-se" data-action="se"></span>' + '</div>' + '</div>';
171
+
172
+ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
173
+ return typeof obj;
174
+ } : function (obj) {
175
+ return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
176
+ };
177
+
178
+ var classCallCheck = function (instance, Constructor) {
179
+ if (!(instance instanceof Constructor)) {
180
+ throw new TypeError("Cannot call a class as a function");
181
+ }
182
+ };
183
+
184
+ var createClass = function () {
185
+ function defineProperties(target, props) {
186
+ for (var i = 0; i < props.length; i++) {
187
+ var descriptor = props[i];
188
+ descriptor.enumerable = descriptor.enumerable || false;
189
+ descriptor.configurable = true;
190
+ if ("value" in descriptor) descriptor.writable = true;
191
+ Object.defineProperty(target, descriptor.key, descriptor);
192
+ }
193
+ }
194
+
195
+ return function (Constructor, protoProps, staticProps) {
196
+ if (protoProps) defineProperties(Constructor.prototype, protoProps);
197
+ if (staticProps) defineProperties(Constructor, staticProps);
198
+ return Constructor;
199
+ };
200
+ }();
201
+
202
+ var toConsumableArray = function (arr) {
203
+ if (Array.isArray(arr)) {
204
+ for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
205
+
206
+ return arr2;
207
+ } else {
208
+ return Array.from(arr);
209
+ }
210
+ };
211
+
212
+ /**
213
+ * Check if the given value is not a number.
214
+ */
215
+ var isNaN = Number.isNaN || WINDOW.isNaN;
216
+
217
+ /**
218
+ * Check if the given value is a number.
219
+ * @param {*} value - The value to check.
220
+ * @returns {boolean} Returns `true` if the given value is a number, else `false`.
221
+ */
222
+ function isNumber(value) {
223
+ return typeof value === 'number' && !isNaN(value);
224
+ }
225
+
226
+ /**
227
+ * Check if the given value is undefined.
228
+ * @param {*} value - The value to check.
229
+ * @returns {boolean} Returns `true` if the given value is undefined, else `false`.
230
+ */
231
+ function isUndefined(value) {
232
+ return typeof value === 'undefined';
233
+ }
234
+
235
+ /**
236
+ * Check if the given value is an object.
237
+ * @param {*} value - The value to check.
238
+ * @returns {boolean} Returns `true` if the given value is an object, else `false`.
239
+ */
240
+ function isObject(value) {
241
+ return (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && value !== null;
242
+ }
243
+
244
+ var hasOwnProperty = Object.prototype.hasOwnProperty;
245
+
246
+ /**
247
+ * Check if the given value is a plain object.
248
+ * @param {*} value - The value to check.
249
+ * @returns {boolean} Returns `true` if the given value is a plain object, else `false`.
250
+ */
251
+
252
+ function isPlainObject(value) {
253
+ if (!isObject(value)) {
254
+ return false;
255
+ }
256
+
257
+ try {
258
+ var _constructor = value.constructor;
259
+ var prototype = _constructor.prototype;
260
+
261
+
262
+ return _constructor && prototype && hasOwnProperty.call(prototype, 'isPrototypeOf');
263
+ } catch (e) {
264
+ return false;
265
+ }
266
+ }
267
+
268
+ /**
269
+ * Check if the given value is a function.
270
+ * @param {*} value - The value to check.
271
+ * @returns {boolean} Returns `true` if the given value is a function, else `false`.
272
+ */
273
+ function isFunction(value) {
274
+ return typeof value === 'function';
275
+ }
276
+
277
+ /**
278
+ * Iterate the given data.
279
+ * @param {*} data - The data to iterate.
280
+ * @param {Function} callback - The process function for each element.
281
+ * @returns {*} The original data.
282
+ */
283
+ function forEach(data, callback) {
284
+ if (data && isFunction(callback)) {
285
+ if (Array.isArray(data) || isNumber(data.length) /* array-like */) {
286
+ var length = data.length;
287
+
288
+ var i = void 0;
289
+
290
+ for (i = 0; i < length; i += 1) {
291
+ if (callback.call(data, data[i], i, data) === false) {
292
+ break;
293
+ }
294
+ }
295
+ } else if (isObject(data)) {
296
+ Object.keys(data).forEach(function (key) {
297
+ callback.call(data, data[key], key, data);
298
+ });
299
+ }
300
+ }
301
+
302
+ return data;
303
+ }
304
+
305
+ /**
306
+ * Extend the given object.
307
+ * @param {*} obj - The object to be extended.
308
+ * @param {*} args - The rest objects which will be merged to the first object.
309
+ * @returns {Object} The extended object.
310
+ */
311
+ var assign = Object.assign || function assign(obj) {
312
+ for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
313
+ args[_key - 1] = arguments[_key];
314
+ }
315
+
316
+ if (isObject(obj) && args.length > 0) {
317
+ args.forEach(function (arg) {
318
+ if (isObject(arg)) {
319
+ Object.keys(arg).forEach(function (key) {
320
+ obj[key] = arg[key];
321
+ });
322
+ }
323
+ });
324
+ }
325
+
326
+ return obj;
327
+ };
328
+
329
+ var REGEXP_DECIMALS = /\.\d*(?:0|9){12}\d*$/i;
330
+
331
+ /**
332
+ * Normalize decimal number.
333
+ * Check out {@link http://0.30000000000000004.com/ }
334
+ * @param {number} value - The value to normalize.
335
+ * @param {number} [times=100000000000] - The times for normalizing.
336
+ * @returns {number} Returns the normalized number.
337
+ */
338
+ function normalizeDecimalNumber(value) {
339
+ var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100000000000;
340
+
341
+ return REGEXP_DECIMALS.test(value) ? Math.round(value * times) / times : value;
342
+ }
343
+
344
+ var REGEXP_SUFFIX = /^(?:width|height|left|top|marginLeft|marginTop)$/;
345
+
346
+ /**
347
+ * Apply styles to the given element.
348
+ * @param {Element} element - The target element.
349
+ * @param {Object} styles - The styles for applying.
350
+ */
351
+ function setStyle(element, styles) {
352
+ var style = element.style;
353
+
354
+
355
+ forEach(styles, function (value, property) {
356
+ if (REGEXP_SUFFIX.test(property) && isNumber(value)) {
357
+ value += 'px';
358
+ }
359
+
360
+ style[property] = value;
361
+ });
362
+ }
363
+
364
+ /**
365
+ * Check if the given element has a special class.
366
+ * @param {Element} element - The element to check.
367
+ * @param {string} value - The class to search.
368
+ * @returns {boolean} Returns `true` if the special class was found.
369
+ */
370
+ function hasClass(element, value) {
371
+ return element.classList ? element.classList.contains(value) : element.className.indexOf(value) > -1;
372
+ }
373
+
374
+ /**
375
+ * Add classes to the given element.
376
+ * @param {Element} element - The target element.
377
+ * @param {string} value - The classes to be added.
378
+ */
379
+ function addClass(element, value) {
380
+ if (!value) {
381
+ return;
382
+ }
383
+
384
+ if (isNumber(element.length)) {
385
+ forEach(element, function (elem) {
386
+ addClass(elem, value);
387
+ });
388
+ return;
389
+ }
390
+
391
+ if (element.classList) {
392
+ element.classList.add(value);
393
+ return;
394
+ }
395
+
396
+ var className = element.className.trim();
397
+
398
+ if (!className) {
399
+ element.className = value;
400
+ } else if (className.indexOf(value) < 0) {
401
+ element.className = className + ' ' + value;
402
+ }
403
+ }
404
+
405
+ /**
406
+ * Remove classes from the given element.
407
+ * @param {Element} element - The target element.
408
+ * @param {string} value - The classes to be removed.
409
+ */
410
+ function removeClass(element, value) {
411
+ if (!value) {
412
+ return;
413
+ }
414
+
415
+ if (isNumber(element.length)) {
416
+ forEach(element, function (elem) {
417
+ removeClass(elem, value);
418
+ });
419
+ return;
420
+ }
421
+
422
+ if (element.classList) {
423
+ element.classList.remove(value);
424
+ return;
425
+ }
426
+
427
+ if (element.className.indexOf(value) >= 0) {
428
+ element.className = element.className.replace(value, '');
429
+ }
430
+ }
431
+
432
+ /**
433
+ * Add or remove classes from the given element.
434
+ * @param {Element} element - The target element.
435
+ * @param {string} value - The classes to be toggled.
436
+ * @param {boolean} added - Add only.
437
+ */
438
+ function toggleClass(element, value, added) {
439
+ if (!value) {
440
+ return;
441
+ }
442
+
443
+ if (isNumber(element.length)) {
444
+ forEach(element, function (elem) {
445
+ toggleClass(elem, value, added);
446
+ });
447
+ return;
448
+ }
449
+
450
+ // IE10-11 doesn't support the second parameter of `classList.toggle`
451
+ if (added) {
452
+ addClass(element, value);
453
+ } else {
454
+ removeClass(element, value);
455
+ }
456
+ }
457
+
458
+ var REGEXP_HYPHENATE = /([a-z\d])([A-Z])/g;
459
+
460
+ /**
461
+ * Transform the given string from camelCase to kebab-case
462
+ * @param {string} value - The value to transform.
463
+ * @returns {string} The transformed value.
464
+ */
465
+ function hyphenate(value) {
466
+ return value.replace(REGEXP_HYPHENATE, '$1-$2').toLowerCase();
467
+ }
468
+
469
+ /**
470
+ * Get data from the given element.
471
+ * @param {Element} element - The target element.
472
+ * @param {string} name - The data key to get.
473
+ * @returns {string} The data value.
474
+ */
475
+ function getData(element, name) {
476
+ if (isObject(element[name])) {
477
+ return element[name];
478
+ } else if (element.dataset) {
479
+ return element.dataset[name];
480
+ }
481
+
482
+ return element.getAttribute('data-' + hyphenate(name));
483
+ }
484
+
485
+ /**
486
+ * Set data to the given element.
487
+ * @param {Element} element - The target element.
488
+ * @param {string} name - The data key to set.
489
+ * @param {string} data - The data value.
490
+ */
491
+ function setData(element, name, data) {
492
+ if (isObject(data)) {
493
+ element[name] = data;
494
+ } else if (element.dataset) {
495
+ element.dataset[name] = data;
496
+ } else {
497
+ element.setAttribute('data-' + hyphenate(name), data);
498
+ }
499
+ }
500
+
501
+ /**
502
+ * Remove data from the given element.
503
+ * @param {Element} element - The target element.
504
+ * @param {string} name - The data key to remove.
505
+ */
506
+ function removeData(element, name) {
507
+ if (isObject(element[name])) {
508
+ try {
509
+ delete element[name];
510
+ } catch (e) {
511
+ element[name] = undefined;
512
+ }
513
+ } else if (element.dataset) {
514
+ // #128 Safari not allows to delete dataset property
515
+ try {
516
+ delete element.dataset[name];
517
+ } catch (e) {
518
+ element.dataset[name] = undefined;
519
+ }
520
+ } else {
521
+ element.removeAttribute('data-' + hyphenate(name));
522
+ }
523
+ }
524
+
525
+ var REGEXP_SPACES = /\s\s*/;
526
+ var onceSupported = function () {
527
+ var supported = false;
528
+
529
+ if (IN_BROWSER) {
530
+ var once = false;
531
+ var listener = function listener() {};
532
+ var options = Object.defineProperty({}, 'once', {
533
+ get: function get$$1() {
534
+ supported = true;
535
+ return once;
536
+ },
537
+
538
+
539
+ /**
540
+ * This setter can fix a `TypeError` in strict mode
541
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Getter_only}
542
+ * @param {boolean} value - The value to set
543
+ */
544
+ set: function set$$1(value) {
545
+ once = value;
546
+ }
547
+ });
548
+
549
+ WINDOW.addEventListener('test', listener, options);
550
+ WINDOW.removeEventListener('test', listener, options);
551
+ }
552
+
553
+ return supported;
554
+ }();
555
+
556
+ /**
557
+ * Remove event listener from the target element.
558
+ * @param {Element} element - The event target.
559
+ * @param {string} type - The event type(s).
560
+ * @param {Function} listener - The event listener.
561
+ * @param {Object} options - The event options.
562
+ */
563
+ function removeListener(element, type, listener) {
564
+ var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
565
+
566
+ var handler = listener;
567
+
568
+ type.trim().split(REGEXP_SPACES).forEach(function (event) {
569
+ if (!onceSupported) {
570
+ var listeners = element.listeners;
571
+
572
+
573
+ if (listeners && listeners[event] && listeners[event][listener]) {
574
+ handler = listeners[event][listener];
575
+ delete listeners[event][listener];
576
+
577
+ if (Object.keys(listeners[event]).length === 0) {
578
+ delete listeners[event];
579
+ }
580
+
581
+ if (Object.keys(listeners).length === 0) {
582
+ delete element.listeners;
583
+ }
584
+ }
585
+ }
586
+
587
+ element.removeEventListener(event, handler, options);
588
+ });
589
+ }
590
+
591
+ /**
592
+ * Add event listener to the target element.
593
+ * @param {Element} element - The event target.
594
+ * @param {string} type - The event type(s).
595
+ * @param {Function} listener - The event listener.
596
+ * @param {Object} options - The event options.
597
+ */
598
+ function addListener(element, type, listener) {
599
+ var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
600
+
601
+ var _handler = listener;
602
+
603
+ type.trim().split(REGEXP_SPACES).forEach(function (event) {
604
+ if (options.once && !onceSupported) {
605
+ var _element$listeners = element.listeners,
606
+ listeners = _element$listeners === undefined ? {} : _element$listeners;
607
+
608
+
609
+ _handler = function handler() {
610
+ for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
611
+ args[_key2] = arguments[_key2];
612
+ }
613
+
614
+ delete listeners[event][listener];
615
+ element.removeEventListener(event, _handler, options);
616
+ listener.apply(element, args);
617
+ };
618
+
619
+ if (!listeners[event]) {
620
+ listeners[event] = {};
621
+ }
622
+
623
+ if (listeners[event][listener]) {
624
+ element.removeEventListener(event, listeners[event][listener], options);
625
+ }
626
+
627
+ listeners[event][listener] = _handler;
628
+ element.listeners = listeners;
629
+ }
630
+
631
+ element.addEventListener(event, _handler, options);
632
+ });
633
+ }
634
+
635
+ /**
636
+ * Dispatch event on the target element.
637
+ * @param {Element} element - The event target.
638
+ * @param {string} type - The event type(s).
639
+ * @param {Object} data - The additional event data.
640
+ * @returns {boolean} Indicate if the event is default prevented or not.
641
+ */
642
+ function dispatchEvent(element, type, data) {
643
+ var event = void 0;
644
+
645
+ // Event and CustomEvent on IE9-11 are global objects, not constructors
646
+ if (isFunction(Event) && isFunction(CustomEvent)) {
647
+ event = new CustomEvent(type, {
648
+ detail: data,
649
+ bubbles: true,
650
+ cancelable: true
651
+ });
652
+ } else {
653
+ event = document.createEvent('CustomEvent');
654
+ event.initCustomEvent(type, true, true, data);
655
+ }
656
+
657
+ return element.dispatchEvent(event);
658
+ }
659
+
660
+ /**
661
+ * Get the offset base on the document.
662
+ * @param {Element} element - The target element.
663
+ * @returns {Object} The offset data.
664
+ */
665
+ function getOffset(element) {
666
+ var box = element.getBoundingClientRect();
667
+
668
+ return {
669
+ left: box.left + (window.pageXOffset - document.documentElement.clientLeft),
670
+ top: box.top + (window.pageYOffset - document.documentElement.clientTop)
671
+ };
672
+ }
673
+
674
+ var location = WINDOW.location;
675
+
676
+ var REGEXP_ORIGINS = /^(https?:)\/\/([^:/?#]+):?(\d*)/i;
677
+
678
+ /**
679
+ * Check if the given URL is a cross origin URL.
680
+ * @param {string} url - The target URL.
681
+ * @returns {boolean} Returns `true` if the given URL is a cross origin URL, else `false`.
682
+ */
683
+ function isCrossOriginURL(url) {
684
+ var parts = url.match(REGEXP_ORIGINS);
685
+
686
+ return parts && (parts[1] !== location.protocol || parts[2] !== location.hostname || parts[3] !== location.port);
687
+ }
688
+
689
+ /**
690
+ * Add timestamp to the given URL.
691
+ * @param {string} url - The target URL.
692
+ * @returns {string} The result URL.
693
+ */
694
+ function addTimestamp(url) {
695
+ var timestamp = 'timestamp=' + new Date().getTime();
696
+
697
+ return url + (url.indexOf('?') === -1 ? '?' : '&') + timestamp;
698
+ }
699
+
700
+ /**
701
+ * Get transforms base on the given object.
702
+ * @param {Object} obj - The target object.
703
+ * @returns {string} A string contains transform values.
704
+ */
705
+ function getTransforms(_ref) {
706
+ var rotate = _ref.rotate,
707
+ scaleX = _ref.scaleX,
708
+ scaleY = _ref.scaleY,
709
+ translateX = _ref.translateX,
710
+ translateY = _ref.translateY;
711
+
712
+ var values = [];
713
+
714
+ if (isNumber(translateX) && translateX !== 0) {
715
+ values.push('translateX(' + translateX + 'px)');
716
+ }
717
+
718
+ if (isNumber(translateY) && translateY !== 0) {
719
+ values.push('translateY(' + translateY + 'px)');
720
+ }
721
+
722
+ // Rotate should come first before scale to match orientation transform
723
+ if (isNumber(rotate) && rotate !== 0) {
724
+ values.push('rotate(' + rotate + 'deg)');
725
+ }
726
+
727
+ if (isNumber(scaleX) && scaleX !== 1) {
728
+ values.push('scaleX(' + scaleX + ')');
729
+ }
730
+
731
+ if (isNumber(scaleY) && scaleY !== 1) {
732
+ values.push('scaleY(' + scaleY + ')');
733
+ }
734
+
735
+ var transform = values.length ? values.join(' ') : 'none';
736
+
737
+ return {
738
+ WebkitTransform: transform,
739
+ msTransform: transform,
740
+ transform: transform
741
+ };
742
+ }
743
+
744
+ /**
745
+ * Get the max ratio of a group of pointers.
746
+ * @param {string} pointers - The target pointers.
747
+ * @returns {number} The result ratio.
748
+ */
749
+ function getMaxZoomRatio(pointers) {
750
+ var pointers2 = assign({}, pointers);
751
+ var ratios = [];
752
+
753
+ forEach(pointers, function (pointer, pointerId) {
754
+ delete pointers2[pointerId];
755
+
756
+ forEach(pointers2, function (pointer2) {
757
+ var x1 = Math.abs(pointer.startX - pointer2.startX);
758
+ var y1 = Math.abs(pointer.startY - pointer2.startY);
759
+ var x2 = Math.abs(pointer.endX - pointer2.endX);
760
+ var y2 = Math.abs(pointer.endY - pointer2.endY);
761
+ var z1 = Math.sqrt(x1 * x1 + y1 * y1);
762
+ var z2 = Math.sqrt(x2 * x2 + y2 * y2);
763
+ var ratio = (z2 - z1) / z1;
764
+
765
+ ratios.push(ratio);
766
+ });
767
+ });
768
+
769
+ ratios.sort(function (a, b) {
770
+ return Math.abs(a) < Math.abs(b);
771
+ });
772
+
773
+ return ratios[0];
774
+ }
775
+
776
+ /**
777
+ * Get a pointer from an event object.
778
+ * @param {Object} event - The target event object.
779
+ * @param {boolean} endOnly - Indicates if only returns the end point coordinate or not.
780
+ * @returns {Object} The result pointer contains start and/or end point coordinates.
781
+ */
782
+ function getPointer(_ref2, endOnly) {
783
+ var pageX = _ref2.pageX,
784
+ pageY = _ref2.pageY;
785
+
786
+ var end = {
787
+ endX: pageX,
788
+ endY: pageY
789
+ };
790
+
791
+ return endOnly ? end : assign({
792
+ startX: pageX,
793
+ startY: pageY
794
+ }, end);
795
+ }
796
+
797
+ /**
798
+ * Get the center point coordinate of a group of pointers.
799
+ * @param {Object} pointers - The target pointers.
800
+ * @returns {Object} The center point coordinate.
801
+ */
802
+ function getPointersCenter(pointers) {
803
+ var pageX = 0;
804
+ var pageY = 0;
805
+ var count = 0;
806
+
807
+ forEach(pointers, function (_ref3) {
808
+ var startX = _ref3.startX,
809
+ startY = _ref3.startY;
810
+
811
+ pageX += startX;
812
+ pageY += startY;
813
+ count += 1;
814
+ });
815
+
816
+ pageX /= count;
817
+ pageY /= count;
818
+
819
+ return {
820
+ pageX: pageX,
821
+ pageY: pageY
822
+ };
823
+ }
824
+
825
+ /**
826
+ * Check if the given value is a finite number.
827
+ */
828
+ var isFinite = Number.isFinite || WINDOW.isFinite;
829
+
830
+ /**
831
+ * Get the max sizes in a rectangle under the given aspect ratio.
832
+ * @param {Object} data - The original sizes.
833
+ * @param {string} [type='contain'] - The adjust type.
834
+ * @returns {Object} The result sizes.
835
+ */
836
+ function getAdjustedSizes(_ref4) // or 'cover'
837
+ {
838
+ var aspectRatio = _ref4.aspectRatio,
839
+ height = _ref4.height,
840
+ width = _ref4.width;
841
+ var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'contain';
842
+
843
+ var isValidNumber = function isValidNumber(value) {
844
+ return isFinite(value) && value > 0;
845
+ };
846
+
847
+ if (isValidNumber(width) && isValidNumber(height)) {
848
+ var adjustedWidth = height * aspectRatio;
849
+
850
+ if (type === 'contain' && adjustedWidth > width || type === 'cover' && adjustedWidth < width) {
851
+ height = width / aspectRatio;
852
+ } else {
853
+ width = height * aspectRatio;
854
+ }
855
+ } else if (isValidNumber(width)) {
856
+ height = width / aspectRatio;
857
+ } else if (isValidNumber(height)) {
858
+ width = height * aspectRatio;
859
+ }
860
+
861
+ return {
862
+ width: width,
863
+ height: height
864
+ };
865
+ }
866
+
867
+ /**
868
+ * Get the new sizes of a rectangle after rotated.
869
+ * @param {Object} data - The original sizes.
870
+ * @returns {Object} The result sizes.
871
+ */
872
+ function getRotatedSizes(_ref5) {
873
+ var width = _ref5.width,
874
+ height = _ref5.height,
875
+ degree = _ref5.degree;
876
+
877
+ degree = Math.abs(degree) % 180;
878
+
879
+ if (degree === 90) {
880
+ return {
881
+ width: height,
882
+ height: width
883
+ };
884
+ }
885
+
886
+ var arc = degree % 90 * Math.PI / 180;
887
+ var sinArc = Math.sin(arc);
888
+ var cosArc = Math.cos(arc);
889
+ var newWidth = width * cosArc + height * sinArc;
890
+ var newHeight = width * sinArc + height * cosArc;
891
+
892
+ return degree > 90 ? {
893
+ width: newHeight,
894
+ height: newWidth
895
+ } : {
896
+ width: newWidth,
897
+ height: newHeight
898
+ };
899
+ }
900
+
901
+ /**
902
+ * Get a canvas which drew the given image.
903
+ * @param {HTMLImageElement} image - The image for drawing.
904
+ * @param {Object} imageData - The image data.
905
+ * @param {Object} canvasData - The canvas data.
906
+ * @param {Object} options - The options.
907
+ * @returns {HTMLCanvasElement} The result canvas.
908
+ */
909
+ function getSourceCanvas(image, _ref6, _ref7, _ref8) {
910
+ var imageNaturalWidth = _ref6.naturalWidth,
911
+ imageNaturalHeight = _ref6.naturalHeight,
912
+ _ref6$rotate = _ref6.rotate,
913
+ rotate = _ref6$rotate === undefined ? 0 : _ref6$rotate,
914
+ _ref6$scaleX = _ref6.scaleX,
915
+ scaleX = _ref6$scaleX === undefined ? 1 : _ref6$scaleX,
916
+ _ref6$scaleY = _ref6.scaleY,
917
+ scaleY = _ref6$scaleY === undefined ? 1 : _ref6$scaleY;
918
+ var aspectRatio = _ref7.aspectRatio,
919
+ naturalWidth = _ref7.naturalWidth,
920
+ naturalHeight = _ref7.naturalHeight;
921
+ var _ref8$fillColor = _ref8.fillColor,
922
+ fillColor = _ref8$fillColor === undefined ? 'transparent' : _ref8$fillColor,
923
+ _ref8$imageSmoothingE = _ref8.imageSmoothingEnabled,
924
+ imageSmoothingEnabled = _ref8$imageSmoothingE === undefined ? true : _ref8$imageSmoothingE,
925
+ _ref8$imageSmoothingQ = _ref8.imageSmoothingQuality,
926
+ imageSmoothingQuality = _ref8$imageSmoothingQ === undefined ? 'low' : _ref8$imageSmoothingQ,
927
+ _ref8$maxWidth = _ref8.maxWidth,
928
+ maxWidth = _ref8$maxWidth === undefined ? Infinity : _ref8$maxWidth,
929
+ _ref8$maxHeight = _ref8.maxHeight,
930
+ maxHeight = _ref8$maxHeight === undefined ? Infinity : _ref8$maxHeight,
931
+ _ref8$minWidth = _ref8.minWidth,
932
+ minWidth = _ref8$minWidth === undefined ? 0 : _ref8$minWidth,
933
+ _ref8$minHeight = _ref8.minHeight,
934
+ minHeight = _ref8$minHeight === undefined ? 0 : _ref8$minHeight;
935
+
936
+ var canvas = document.createElement('canvas');
937
+ var context = canvas.getContext('2d');
938
+ var maxSizes = getAdjustedSizes({
939
+ aspectRatio: aspectRatio,
940
+ width: maxWidth,
941
+ height: maxHeight
942
+ });
943
+ var minSizes = getAdjustedSizes({
944
+ aspectRatio: aspectRatio,
945
+ width: minWidth,
946
+ height: minHeight
947
+ }, 'cover');
948
+ var width = Math.min(maxSizes.width, Math.max(minSizes.width, naturalWidth));
949
+ var height = Math.min(maxSizes.height, Math.max(minSizes.height, naturalHeight));
950
+
951
+ // Note: should always use image's natural sizes for drawing as
952
+ // imageData.naturalWidth === canvasData.naturalHeight when rotate % 180 === 90
953
+ var destWidth = Math.min(maxSizes.width, Math.max(minSizes.width, imageNaturalWidth));
954
+ var destHeight = Math.min(maxSizes.height, Math.max(minSizes.height, imageNaturalHeight));
955
+ var params = [-destWidth / 2, -destHeight / 2, destWidth, destHeight];
956
+
957
+ canvas.width = normalizeDecimalNumber(width);
958
+ canvas.height = normalizeDecimalNumber(height);
959
+ context.fillStyle = fillColor;
960
+ context.fillRect(0, 0, width, height);
961
+ context.save();
962
+ context.translate(width / 2, height / 2);
963
+ context.rotate(rotate * Math.PI / 180);
964
+ context.scale(scaleX, scaleY);
965
+ context.imageSmoothingEnabled = imageSmoothingEnabled;
966
+ context.imageSmoothingQuality = imageSmoothingQuality;
967
+ context.drawImage.apply(context, [image].concat(toConsumableArray(params.map(function (param) {
968
+ return Math.floor(normalizeDecimalNumber(param));
969
+ }))));
970
+ context.restore();
971
+ return canvas;
972
+ }
973
+
974
+ var fromCharCode = String.fromCharCode;
975
+
976
+ /**
977
+ * Get string from char code in data view.
978
+ * @param {DataView} dataView - The data view for read.
979
+ * @param {number} start - The start index.
980
+ * @param {number} length - The read length.
981
+ * @returns {string} The read result.
982
+ */
983
+
984
+ function getStringFromCharCode(dataView, start, length) {
985
+ var str = '';
986
+ var i = void 0;
987
+
988
+ length += start;
989
+
990
+ for (i = start; i < length; i += 1) {
991
+ str += fromCharCode(dataView.getUint8(i));
992
+ }
993
+
994
+ return str;
995
+ }
996
+
997
+ var REGEXP_DATA_URL_HEAD = /^data:.*,/;
998
+
999
+ /**
1000
+ * Transform Data URL to array buffer.
1001
+ * @param {string} dataURL - The Data URL to transform.
1002
+ * @returns {ArrayBuffer} The result array buffer.
1003
+ */
1004
+ function dataURLToArrayBuffer(dataURL) {
1005
+ var base64 = dataURL.replace(REGEXP_DATA_URL_HEAD, '');
1006
+ var binary = atob(base64);
1007
+ var arrayBuffer = new ArrayBuffer(binary.length);
1008
+ var uint8 = new Uint8Array(arrayBuffer);
1009
+
1010
+ forEach(uint8, function (value, i) {
1011
+ uint8[i] = binary.charCodeAt(i);
1012
+ });
1013
+
1014
+ return arrayBuffer;
1015
+ }
1016
+
1017
+ /**
1018
+ * Transform array buffer to Data URL.
1019
+ * @param {ArrayBuffer} arrayBuffer - The array buffer to transform.
1020
+ * @param {string} mimeType - The mime type of the Data URL.
1021
+ * @returns {string} The result Data URL.
1022
+ */
1023
+ function arrayBufferToDataURL(arrayBuffer, mimeType) {
1024
+ var uint8 = new Uint8Array(arrayBuffer);
1025
+ var data = '';
1026
+
1027
+ // TypedArray.prototype.forEach is not supported in some browsers.
1028
+ forEach(uint8, function (value) {
1029
+ data += fromCharCode(value);
1030
+ });
1031
+
1032
+ return 'data:' + mimeType + ';base64,' + btoa(data);
1033
+ }
1034
+
1035
+ /**
1036
+ * Get orientation value from given array buffer.
1037
+ * @param {ArrayBuffer} arrayBuffer - The array buffer to read.
1038
+ * @returns {number} The read orientation value.
1039
+ */
1040
+ function getOrientation(arrayBuffer) {
1041
+ var dataView = new DataView(arrayBuffer);
1042
+ var orientation = void 0;
1043
+ var littleEndian = void 0;
1044
+ var app1Start = void 0;
1045
+ var ifdStart = void 0;
1046
+
1047
+ // Only handle JPEG image (start by 0xFFD8)
1048
+ if (dataView.getUint8(0) === 0xFF && dataView.getUint8(1) === 0xD8) {
1049
+ var length = dataView.byteLength;
1050
+ var offset = 2;
1051
+
1052
+ while (offset < length) {
1053
+ if (dataView.getUint8(offset) === 0xFF && dataView.getUint8(offset + 1) === 0xE1) {
1054
+ app1Start = offset;
1055
+ break;
1056
+ }
1057
+
1058
+ offset += 1;
1059
+ }
1060
+ }
1061
+
1062
+ if (app1Start) {
1063
+ var exifIDCode = app1Start + 4;
1064
+ var tiffOffset = app1Start + 10;
1065
+
1066
+ if (getStringFromCharCode(dataView, exifIDCode, 4) === 'Exif') {
1067
+ var endianness = dataView.getUint16(tiffOffset);
1068
+
1069
+ littleEndian = endianness === 0x4949;
1070
+
1071
+ if (littleEndian || endianness === 0x4D4D /* bigEndian */) {
1072
+ if (dataView.getUint16(tiffOffset + 2, littleEndian) === 0x002A) {
1073
+ var firstIFDOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1074
+
1075
+ if (firstIFDOffset >= 0x00000008) {
1076
+ ifdStart = tiffOffset + firstIFDOffset;
1077
+ }
1078
+ }
1079
+ }
1080
+ }
1081
+ }
1082
+
1083
+ if (ifdStart) {
1084
+ var _length = dataView.getUint16(ifdStart, littleEndian);
1085
+ var _offset = void 0;
1086
+ var i = void 0;
1087
+
1088
+ for (i = 0; i < _length; i += 1) {
1089
+ _offset = ifdStart + i * 12 + 2;
1090
+
1091
+ if (dataView.getUint16(_offset, littleEndian) === 0x0112 /* Orientation */) {
1092
+ // 8 is the offset of the current tag's value
1093
+ _offset += 8;
1094
+
1095
+ // Get the original orientation value
1096
+ orientation = dataView.getUint16(_offset, littleEndian);
1097
+
1098
+ // Override the orientation with its default value
1099
+ dataView.setUint16(_offset, 1, littleEndian);
1100
+ break;
1101
+ }
1102
+ }
1103
+ }
1104
+
1105
+ return orientation;
1106
+ }
1107
+
1108
+ /**
1109
+ * Parse Exif Orientation value.
1110
+ * @param {number} orientation - The orientation to parse.
1111
+ * @returns {Object} The parsed result.
1112
+ */
1113
+ function parseOrientation(orientation) {
1114
+ var rotate = 0;
1115
+ var scaleX = 1;
1116
+ var scaleY = 1;
1117
+
1118
+ switch (orientation) {
1119
+ // Flip horizontal
1120
+ case 2:
1121
+ scaleX = -1;
1122
+ break;
1123
+
1124
+ // Rotate left 180°
1125
+ case 3:
1126
+ rotate = -180;
1127
+ break;
1128
+
1129
+ // Flip vertical
1130
+ case 4:
1131
+ scaleY = -1;
1132
+ break;
1133
+
1134
+ // Flip vertical and rotate right 90°
1135
+ case 5:
1136
+ rotate = 90;
1137
+ scaleY = -1;
1138
+ break;
1139
+
1140
+ // Rotate right 90°
1141
+ case 6:
1142
+ rotate = 90;
1143
+ break;
1144
+
1145
+ // Flip horizontal and rotate right 90°
1146
+ case 7:
1147
+ rotate = 90;
1148
+ scaleX = -1;
1149
+ break;
1150
+
1151
+ // Rotate left 90°
1152
+ case 8:
1153
+ rotate = -90;
1154
+ break;
1155
+
1156
+ default:
1157
+ }
1158
+
1159
+ return {
1160
+ rotate: rotate,
1161
+ scaleX: scaleX,
1162
+ scaleY: scaleY
1163
+ };
1164
+ }
1165
+
1166
+ var render = {
1167
+ render: function render() {
1168
+ this.initContainer();
1169
+ this.initCanvas();
1170
+ this.initCropBox();
1171
+ this.renderCanvas();
1172
+
1173
+ if (this.cropped) {
1174
+ this.renderCropBox();
1175
+ }
1176
+ },
1177
+ initContainer: function initContainer() {
1178
+ var element = this.element,
1179
+ options = this.options,
1180
+ container = this.container,
1181
+ cropper = this.cropper;
1182
+
1183
+
1184
+ addClass(cropper, CLASS_HIDDEN);
1185
+ removeClass(element, CLASS_HIDDEN);
1186
+
1187
+ var containerData = {
1188
+ width: Math.max(container.offsetWidth, Number(options.minContainerWidth) || 200),
1189
+ height: Math.max(container.offsetHeight, Number(options.minContainerHeight) || 100)
1190
+ };
1191
+
1192
+ this.containerData = containerData;
1193
+
1194
+ setStyle(cropper, {
1195
+ width: containerData.width,
1196
+ height: containerData.height
1197
+ });
1198
+
1199
+ addClass(element, CLASS_HIDDEN);
1200
+ removeClass(cropper, CLASS_HIDDEN);
1201
+ },
1202
+
1203
+
1204
+ // Canvas (image wrapper)
1205
+ initCanvas: function initCanvas() {
1206
+ var containerData = this.containerData,
1207
+ imageData = this.imageData;
1208
+ var viewMode = this.options.viewMode;
1209
+
1210
+ var rotated = Math.abs(imageData.rotate) % 180 === 90;
1211
+ var naturalWidth = rotated ? imageData.naturalHeight : imageData.naturalWidth;
1212
+ var naturalHeight = rotated ? imageData.naturalWidth : imageData.naturalHeight;
1213
+ var aspectRatio = naturalWidth / naturalHeight;
1214
+ var canvasWidth = containerData.width;
1215
+ var canvasHeight = containerData.height;
1216
+
1217
+ if (containerData.height * aspectRatio > containerData.width) {
1218
+ if (viewMode === 3) {
1219
+ canvasWidth = containerData.height * aspectRatio;
1220
+ } else {
1221
+ canvasHeight = containerData.width / aspectRatio;
1222
+ }
1223
+ } else if (viewMode === 3) {
1224
+ canvasHeight = containerData.width / aspectRatio;
1225
+ } else {
1226
+ canvasWidth = containerData.height * aspectRatio;
1227
+ }
1228
+
1229
+ var canvasData = {
1230
+ aspectRatio: aspectRatio,
1231
+ naturalWidth: naturalWidth,
1232
+ naturalHeight: naturalHeight,
1233
+ width: canvasWidth,
1234
+ height: canvasHeight
1235
+ };
1236
+
1237
+ canvasData.left = (containerData.width - canvasWidth) / 2;
1238
+ canvasData.top = (containerData.height - canvasHeight) / 2;
1239
+ canvasData.oldLeft = canvasData.left;
1240
+ canvasData.oldTop = canvasData.top;
1241
+
1242
+ this.canvasData = canvasData;
1243
+ this.limited = viewMode === 1 || viewMode === 2;
1244
+ this.limitCanvas(true, true);
1245
+ this.initialImageData = assign({}, imageData);
1246
+ this.initialCanvasData = assign({}, canvasData);
1247
+ },
1248
+ limitCanvas: function limitCanvas(sizeLimited, positionLimited) {
1249
+ var options = this.options,
1250
+ containerData = this.containerData,
1251
+ canvasData = this.canvasData,
1252
+ cropBoxData = this.cropBoxData;
1253
+ var viewMode = options.viewMode;
1254
+ var aspectRatio = canvasData.aspectRatio;
1255
+
1256
+ var cropped = this.cropped && cropBoxData;
1257
+
1258
+ if (sizeLimited) {
1259
+ var minCanvasWidth = Number(options.minCanvasWidth) || 0;
1260
+ var minCanvasHeight = Number(options.minCanvasHeight) || 0;
1261
+
1262
+ if (viewMode > 1) {
1263
+ minCanvasWidth = Math.max(minCanvasWidth, containerData.width);
1264
+ minCanvasHeight = Math.max(minCanvasHeight, containerData.height);
1265
+
1266
+ if (viewMode === 3) {
1267
+ if (minCanvasHeight * aspectRatio > minCanvasWidth) {
1268
+ minCanvasWidth = minCanvasHeight * aspectRatio;
1269
+ } else {
1270
+ minCanvasHeight = minCanvasWidth / aspectRatio;
1271
+ }
1272
+ }
1273
+ } else if (viewMode > 0) {
1274
+ if (minCanvasWidth) {
1275
+ minCanvasWidth = Math.max(minCanvasWidth, cropped ? cropBoxData.width : 0);
1276
+ } else if (minCanvasHeight) {
1277
+ minCanvasHeight = Math.max(minCanvasHeight, cropped ? cropBoxData.height : 0);
1278
+ } else if (cropped) {
1279
+ minCanvasWidth = cropBoxData.width;
1280
+ minCanvasHeight = cropBoxData.height;
1281
+
1282
+ if (minCanvasHeight * aspectRatio > minCanvasWidth) {
1283
+ minCanvasWidth = minCanvasHeight * aspectRatio;
1284
+ } else {
1285
+ minCanvasHeight = minCanvasWidth / aspectRatio;
1286
+ }
1287
+ }
1288
+ }
1289
+
1290
+ var _getAdjustedSizes = getAdjustedSizes({
1291
+ aspectRatio: aspectRatio,
1292
+ width: minCanvasWidth,
1293
+ height: minCanvasHeight
1294
+ });
1295
+
1296
+ minCanvasWidth = _getAdjustedSizes.width;
1297
+ minCanvasHeight = _getAdjustedSizes.height;
1298
+
1299
+
1300
+ canvasData.minWidth = minCanvasWidth;
1301
+ canvasData.minHeight = minCanvasHeight;
1302
+ canvasData.maxWidth = Infinity;
1303
+ canvasData.maxHeight = Infinity;
1304
+ }
1305
+
1306
+ if (positionLimited) {
1307
+ if (viewMode) {
1308
+ var newCanvasLeft = containerData.width - canvasData.width;
1309
+ var newCanvasTop = containerData.height - canvasData.height;
1310
+
1311
+ canvasData.minLeft = Math.min(0, newCanvasLeft);
1312
+ canvasData.minTop = Math.min(0, newCanvasTop);
1313
+ canvasData.maxLeft = Math.max(0, newCanvasLeft);
1314
+ canvasData.maxTop = Math.max(0, newCanvasTop);
1315
+
1316
+ if (cropped && this.limited) {
1317
+ canvasData.minLeft = Math.min(cropBoxData.left, cropBoxData.left + (cropBoxData.width - canvasData.width));
1318
+ canvasData.minTop = Math.min(cropBoxData.top, cropBoxData.top + (cropBoxData.height - canvasData.height));
1319
+ canvasData.maxLeft = cropBoxData.left;
1320
+ canvasData.maxTop = cropBoxData.top;
1321
+
1322
+ if (viewMode === 2) {
1323
+ if (canvasData.width >= containerData.width) {
1324
+ canvasData.minLeft = Math.min(0, newCanvasLeft);
1325
+ canvasData.maxLeft = Math.max(0, newCanvasLeft);
1326
+ }
1327
+
1328
+ if (canvasData.height >= containerData.height) {
1329
+ canvasData.minTop = Math.min(0, newCanvasTop);
1330
+ canvasData.maxTop = Math.max(0, newCanvasTop);
1331
+ }
1332
+ }
1333
+ }
1334
+ } else {
1335
+ canvasData.minLeft = -canvasData.width;
1336
+ canvasData.minTop = -canvasData.height;
1337
+ canvasData.maxLeft = containerData.width;
1338
+ canvasData.maxTop = containerData.height;
1339
+ }
1340
+ }
1341
+ },
1342
+ renderCanvas: function renderCanvas(changed, transformed) {
1343
+ var canvasData = this.canvasData,
1344
+ imageData = this.imageData;
1345
+
1346
+
1347
+ if (transformed) {
1348
+ var _getRotatedSizes = getRotatedSizes({
1349
+ width: imageData.naturalWidth * Math.abs(imageData.scaleX || 1),
1350
+ height: imageData.naturalHeight * Math.abs(imageData.scaleY || 1),
1351
+ degree: imageData.rotate || 0
1352
+ }),
1353
+ naturalWidth = _getRotatedSizes.width,
1354
+ naturalHeight = _getRotatedSizes.height;
1355
+
1356
+ var width = canvasData.width * (naturalWidth / canvasData.naturalWidth);
1357
+ var height = canvasData.height * (naturalHeight / canvasData.naturalHeight);
1358
+
1359
+ canvasData.left -= (width - canvasData.width) / 2;
1360
+ canvasData.top -= (height - canvasData.height) / 2;
1361
+ canvasData.width = width;
1362
+ canvasData.height = height;
1363
+ canvasData.aspectRatio = naturalWidth / naturalHeight;
1364
+ canvasData.naturalWidth = naturalWidth;
1365
+ canvasData.naturalHeight = naturalHeight;
1366
+ this.limitCanvas(true, false);
1367
+ }
1368
+
1369
+ if (canvasData.width > canvasData.maxWidth || canvasData.width < canvasData.minWidth) {
1370
+ canvasData.left = canvasData.oldLeft;
1371
+ }
1372
+
1373
+ if (canvasData.height > canvasData.maxHeight || canvasData.height < canvasData.minHeight) {
1374
+ canvasData.top = canvasData.oldTop;
1375
+ }
1376
+
1377
+ canvasData.width = Math.min(Math.max(canvasData.width, canvasData.minWidth), canvasData.maxWidth);
1378
+ canvasData.height = Math.min(Math.max(canvasData.height, canvasData.minHeight), canvasData.maxHeight);
1379
+
1380
+ this.limitCanvas(false, true);
1381
+
1382
+ canvasData.left = Math.min(Math.max(canvasData.left, canvasData.minLeft), canvasData.maxLeft);
1383
+ canvasData.top = Math.min(Math.max(canvasData.top, canvasData.minTop), canvasData.maxTop);
1384
+ canvasData.oldLeft = canvasData.left;
1385
+ canvasData.oldTop = canvasData.top;
1386
+
1387
+ setStyle(this.canvas, assign({
1388
+ width: canvasData.width,
1389
+ height: canvasData.height
1390
+ }, getTransforms({
1391
+ translateX: canvasData.left,
1392
+ translateY: canvasData.top
1393
+ })));
1394
+
1395
+ this.renderImage(changed);
1396
+
1397
+ if (this.cropped && this.limited) {
1398
+ this.limitCropBox(true, true);
1399
+ }
1400
+ },
1401
+ renderImage: function renderImage(changed) {
1402
+ var canvasData = this.canvasData,
1403
+ imageData = this.imageData;
1404
+
1405
+ var width = imageData.naturalWidth * (canvasData.width / canvasData.naturalWidth);
1406
+ var height = imageData.naturalHeight * (canvasData.height / canvasData.naturalHeight);
1407
+
1408
+ assign(imageData, {
1409
+ width: width,
1410
+ height: height,
1411
+ left: (canvasData.width - width) / 2,
1412
+ top: (canvasData.height - height) / 2
1413
+ });
1414
+ setStyle(this.image, assign({
1415
+ width: imageData.width,
1416
+ height: imageData.height
1417
+ }, getTransforms(assign({
1418
+ translateX: imageData.left,
1419
+ translateY: imageData.top
1420
+ }, imageData))));
1421
+
1422
+ if (changed) {
1423
+ this.output();
1424
+ }
1425
+ },
1426
+ initCropBox: function initCropBox() {
1427
+ var options = this.options,
1428
+ canvasData = this.canvasData;
1429
+ var aspectRatio = options.aspectRatio;
1430
+
1431
+ var autoCropArea = Number(options.autoCropArea) || 0.8;
1432
+ var cropBoxData = {
1433
+ width: canvasData.width,
1434
+ height: canvasData.height
1435
+ };
1436
+
1437
+ if (aspectRatio) {
1438
+ if (canvasData.height * aspectRatio > canvasData.width) {
1439
+ cropBoxData.height = cropBoxData.width / aspectRatio;
1440
+ } else {
1441
+ cropBoxData.width = cropBoxData.height * aspectRatio;
1442
+ }
1443
+ }
1444
+
1445
+ this.cropBoxData = cropBoxData;
1446
+ this.limitCropBox(true, true);
1447
+
1448
+ // Initialize auto crop area
1449
+ cropBoxData.width = Math.min(Math.max(cropBoxData.width, cropBoxData.minWidth), cropBoxData.maxWidth);
1450
+ cropBoxData.height = Math.min(Math.max(cropBoxData.height, cropBoxData.minHeight), cropBoxData.maxHeight);
1451
+
1452
+ // The width/height of auto crop area must large than "minWidth/Height"
1453
+ cropBoxData.width = Math.max(cropBoxData.minWidth, cropBoxData.width * autoCropArea);
1454
+ cropBoxData.height = Math.max(cropBoxData.minHeight, cropBoxData.height * autoCropArea);
1455
+ cropBoxData.left = canvasData.left + (canvasData.width - cropBoxData.width) / 2;
1456
+ cropBoxData.top = canvasData.top + (canvasData.height - cropBoxData.height) / 2;
1457
+ cropBoxData.oldLeft = cropBoxData.left;
1458
+ cropBoxData.oldTop = cropBoxData.top;
1459
+
1460
+ this.initialCropBoxData = assign({}, cropBoxData);
1461
+ },
1462
+ limitCropBox: function limitCropBox(sizeLimited, positionLimited) {
1463
+ var options = this.options,
1464
+ containerData = this.containerData,
1465
+ canvasData = this.canvasData,
1466
+ cropBoxData = this.cropBoxData,
1467
+ limited = this.limited;
1468
+ var aspectRatio = options.aspectRatio;
1469
+
1470
+
1471
+ if (sizeLimited) {
1472
+ var minCropBoxWidth = Number(options.minCropBoxWidth) || 0;
1473
+ var minCropBoxHeight = Number(options.minCropBoxHeight) || 0;
1474
+ var maxCropBoxWidth = Math.min(containerData.width, limited ? canvasData.width : containerData.width);
1475
+ var maxCropBoxHeight = Math.min(containerData.height, limited ? canvasData.height : containerData.height);
1476
+
1477
+ // The min/maxCropBoxWidth/Height must be less than container's width/height
1478
+ minCropBoxWidth = Math.min(minCropBoxWidth, containerData.width);
1479
+ minCropBoxHeight = Math.min(minCropBoxHeight, containerData.height);
1480
+
1481
+ if (aspectRatio) {
1482
+ if (minCropBoxWidth && minCropBoxHeight) {
1483
+ if (minCropBoxHeight * aspectRatio > minCropBoxWidth) {
1484
+ minCropBoxHeight = minCropBoxWidth / aspectRatio;
1485
+ } else {
1486
+ minCropBoxWidth = minCropBoxHeight * aspectRatio;
1487
+ }
1488
+ } else if (minCropBoxWidth) {
1489
+ minCropBoxHeight = minCropBoxWidth / aspectRatio;
1490
+ } else if (minCropBoxHeight) {
1491
+ minCropBoxWidth = minCropBoxHeight * aspectRatio;
1492
+ }
1493
+
1494
+ if (maxCropBoxHeight * aspectRatio > maxCropBoxWidth) {
1495
+ maxCropBoxHeight = maxCropBoxWidth / aspectRatio;
1496
+ } else {
1497
+ maxCropBoxWidth = maxCropBoxHeight * aspectRatio;
1498
+ }
1499
+ }
1500
+
1501
+ // The minWidth/Height must be less than maxWidth/Height
1502
+ cropBoxData.minWidth = Math.min(minCropBoxWidth, maxCropBoxWidth);
1503
+ cropBoxData.minHeight = Math.min(minCropBoxHeight, maxCropBoxHeight);
1504
+ cropBoxData.maxWidth = maxCropBoxWidth;
1505
+ cropBoxData.maxHeight = maxCropBoxHeight;
1506
+ }
1507
+
1508
+ if (positionLimited) {
1509
+ if (limited) {
1510
+ cropBoxData.minLeft = Math.max(0, canvasData.left);
1511
+ cropBoxData.minTop = Math.max(0, canvasData.top);
1512
+ cropBoxData.maxLeft = Math.min(containerData.width, canvasData.left + canvasData.width) - cropBoxData.width;
1513
+ cropBoxData.maxTop = Math.min(containerData.height, canvasData.top + canvasData.height) - cropBoxData.height;
1514
+ } else {
1515
+ cropBoxData.minLeft = 0;
1516
+ cropBoxData.minTop = 0;
1517
+ cropBoxData.maxLeft = containerData.width - cropBoxData.width;
1518
+ cropBoxData.maxTop = containerData.height - cropBoxData.height;
1519
+ }
1520
+ }
1521
+ },
1522
+ renderCropBox: function renderCropBox() {
1523
+ var options = this.options,
1524
+ containerData = this.containerData,
1525
+ cropBoxData = this.cropBoxData;
1526
+
1527
+
1528
+ if (cropBoxData.width > cropBoxData.maxWidth || cropBoxData.width < cropBoxData.minWidth) {
1529
+ cropBoxData.left = cropBoxData.oldLeft;
1530
+ }
1531
+
1532
+ if (cropBoxData.height > cropBoxData.maxHeight || cropBoxData.height < cropBoxData.minHeight) {
1533
+ cropBoxData.top = cropBoxData.oldTop;
1534
+ }
1535
+
1536
+ cropBoxData.width = Math.min(Math.max(cropBoxData.width, cropBoxData.minWidth), cropBoxData.maxWidth);
1537
+ cropBoxData.height = Math.min(Math.max(cropBoxData.height, cropBoxData.minHeight), cropBoxData.maxHeight);
1538
+
1539
+ this.limitCropBox(false, true);
1540
+
1541
+ cropBoxData.left = Math.min(Math.max(cropBoxData.left, cropBoxData.minLeft), cropBoxData.maxLeft);
1542
+ cropBoxData.top = Math.min(Math.max(cropBoxData.top, cropBoxData.minTop), cropBoxData.maxTop);
1543
+ cropBoxData.oldLeft = cropBoxData.left;
1544
+ cropBoxData.oldTop = cropBoxData.top;
1545
+
1546
+ if (options.movable && options.cropBoxMovable) {
1547
+ // Turn to move the canvas when the crop box is equal to the container
1548
+ setData(this.face, DATA_ACTION, cropBoxData.width >= containerData.width && cropBoxData.height >= containerData.height ? ACTION_MOVE : ACTION_ALL);
1549
+ }
1550
+
1551
+ setStyle(this.cropBox, assign({
1552
+ width: cropBoxData.width,
1553
+ height: cropBoxData.height
1554
+ }, getTransforms({
1555
+ translateX: cropBoxData.left,
1556
+ translateY: cropBoxData.top
1557
+ })));
1558
+
1559
+ if (this.cropped && this.limited) {
1560
+ this.limitCanvas(true, true);
1561
+ }
1562
+
1563
+ if (!this.disabled) {
1564
+ this.output();
1565
+ }
1566
+ },
1567
+ output: function output() {
1568
+ this.preview();
1569
+ dispatchEvent(this.element, EVENT_CROP, this.getData());
1570
+ }
1571
+ };
1572
+
1573
+ var preview = {
1574
+ initPreview: function initPreview() {
1575
+ var crossOrigin = this.crossOrigin;
1576
+ var preview = this.options.preview;
1577
+
1578
+ var url = crossOrigin ? this.crossOriginUrl : this.url;
1579
+ var image = document.createElement('img');
1580
+
1581
+ if (crossOrigin) {
1582
+ image.crossOrigin = crossOrigin;
1583
+ }
1584
+
1585
+ image.src = url;
1586
+ this.viewBox.appendChild(image);
1587
+ this.viewBoxImage = image;
1588
+
1589
+ if (!preview) {
1590
+ return;
1591
+ }
1592
+
1593
+ var previews = preview;
1594
+
1595
+ if (typeof preview === 'string') {
1596
+ previews = this.element.ownerDocument.querySelectorAll(preview);
1597
+ } else if (preview.querySelector) {
1598
+ previews = [preview];
1599
+ }
1600
+
1601
+ this.previews = previews;
1602
+
1603
+ forEach(previews, function (el) {
1604
+ var img = document.createElement('img');
1605
+
1606
+ // Save the original size for recover
1607
+ setData(el, DATA_PREVIEW, {
1608
+ width: el.offsetWidth,
1609
+ height: el.offsetHeight,
1610
+ html: el.innerHTML
1611
+ });
1612
+
1613
+ if (crossOrigin) {
1614
+ img.crossOrigin = crossOrigin;
1615
+ }
1616
+
1617
+ img.src = url;
1618
+
1619
+ /**
1620
+ * Override img element styles
1621
+ * Add `display:block` to avoid margin top issue
1622
+ * Add `height:auto` to override `height` attribute on IE8
1623
+ * (Occur only when margin-top <= -height)
1624
+ */
1625
+ img.style.cssText = 'display:block;' + 'width:100%;' + 'height:auto;' + 'min-width:0!important;' + 'min-height:0!important;' + 'max-width:none!important;' + 'max-height:none!important;' + 'image-orientation:0deg!important;"';
1626
+
1627
+ el.innerHTML = '';
1628
+ el.appendChild(img);
1629
+ });
1630
+ },
1631
+ resetPreview: function resetPreview() {
1632
+ forEach(this.previews, function (element) {
1633
+ var data = getData(element, DATA_PREVIEW);
1634
+
1635
+ setStyle(element, {
1636
+ width: data.width,
1637
+ height: data.height
1638
+ });
1639
+
1640
+ element.innerHTML = data.html;
1641
+ removeData(element, DATA_PREVIEW);
1642
+ });
1643
+ },
1644
+ preview: function preview() {
1645
+ var imageData = this.imageData,
1646
+ canvasData = this.canvasData,
1647
+ cropBoxData = this.cropBoxData;
1648
+ var cropBoxWidth = cropBoxData.width,
1649
+ cropBoxHeight = cropBoxData.height;
1650
+ var width = imageData.width,
1651
+ height = imageData.height;
1652
+
1653
+ var left = cropBoxData.left - canvasData.left - imageData.left;
1654
+ var top = cropBoxData.top - canvasData.top - imageData.top;
1655
+
1656
+ if (!this.cropped || this.disabled) {
1657
+ return;
1658
+ }
1659
+
1660
+ setStyle(this.viewBoxImage, assign({
1661
+ width: width,
1662
+ height: height
1663
+ }, getTransforms(assign({
1664
+ translateX: -left,
1665
+ translateY: -top
1666
+ }, imageData))));
1667
+
1668
+ forEach(this.previews, function (element) {
1669
+ var data = getData(element, DATA_PREVIEW);
1670
+ var originalWidth = data.width;
1671
+ var originalHeight = data.height;
1672
+ var newWidth = originalWidth;
1673
+ var newHeight = originalHeight;
1674
+ var ratio = 1;
1675
+
1676
+ if (cropBoxWidth) {
1677
+ ratio = originalWidth / cropBoxWidth;
1678
+ newHeight = cropBoxHeight * ratio;
1679
+ }
1680
+
1681
+ if (cropBoxHeight && newHeight > originalHeight) {
1682
+ ratio = originalHeight / cropBoxHeight;
1683
+ newWidth = cropBoxWidth * ratio;
1684
+ newHeight = originalHeight;
1685
+ }
1686
+
1687
+ setStyle(element, {
1688
+ width: newWidth,
1689
+ height: newHeight
1690
+ });
1691
+
1692
+ setStyle(element.getElementsByTagName('img')[0], assign({
1693
+ width: width * ratio,
1694
+ height: height * ratio
1695
+ }, getTransforms(assign({
1696
+ translateX: -left * ratio,
1697
+ translateY: -top * ratio
1698
+ }, imageData))));
1699
+ });
1700
+ }
1701
+ };
1702
+
1703
+ var events = {
1704
+ bind: function bind() {
1705
+ var element = this.element,
1706
+ options = this.options,
1707
+ cropper = this.cropper;
1708
+
1709
+
1710
+ if (isFunction(options.cropstart)) {
1711
+ addListener(element, EVENT_CROP_START, options.cropstart);
1712
+ }
1713
+
1714
+ if (isFunction(options.cropmove)) {
1715
+ addListener(element, EVENT_CROP_MOVE, options.cropmove);
1716
+ }
1717
+
1718
+ if (isFunction(options.cropend)) {
1719
+ addListener(element, EVENT_CROP_END, options.cropend);
1720
+ }
1721
+
1722
+ if (isFunction(options.crop)) {
1723
+ addListener(element, EVENT_CROP, options.crop);
1724
+ }
1725
+
1726
+ if (isFunction(options.zoom)) {
1727
+ addListener(element, EVENT_ZOOM, options.zoom);
1728
+ }
1729
+
1730
+ addListener(cropper, EVENT_POINTER_DOWN, this.onCropStart = this.cropStart.bind(this));
1731
+
1732
+ if (options.zoomable && options.zoomOnWheel) {
1733
+ addListener(cropper, EVENT_WHEEL, this.onWheel = this.wheel.bind(this));
1734
+ }
1735
+
1736
+ if (options.toggleDragModeOnDblclick) {
1737
+ addListener(cropper, EVENT_DBLCLICK, this.onDblclick = this.dblclick.bind(this));
1738
+ }
1739
+
1740
+ addListener(element.ownerDocument, EVENT_POINTER_MOVE, this.onCropMove = this.cropMove.bind(this));
1741
+ addListener(element.ownerDocument, EVENT_POINTER_UP, this.onCropEnd = this.cropEnd.bind(this));
1742
+
1743
+ if (options.responsive) {
1744
+ addListener(window, EVENT_RESIZE, this.onResize = this.resize.bind(this));
1745
+ }
1746
+ },
1747
+ unbind: function unbind() {
1748
+ var element = this.element,
1749
+ options = this.options,
1750
+ cropper = this.cropper;
1751
+
1752
+
1753
+ if (isFunction(options.cropstart)) {
1754
+ removeListener(element, EVENT_CROP_START, options.cropstart);
1755
+ }
1756
+
1757
+ if (isFunction(options.cropmove)) {
1758
+ removeListener(element, EVENT_CROP_MOVE, options.cropmove);
1759
+ }
1760
+
1761
+ if (isFunction(options.cropend)) {
1762
+ removeListener(element, EVENT_CROP_END, options.cropend);
1763
+ }
1764
+
1765
+ if (isFunction(options.crop)) {
1766
+ removeListener(element, EVENT_CROP, options.crop);
1767
+ }
1768
+
1769
+ if (isFunction(options.zoom)) {
1770
+ removeListener(element, EVENT_ZOOM, options.zoom);
1771
+ }
1772
+
1773
+ removeListener(cropper, EVENT_POINTER_DOWN, this.onCropStart);
1774
+
1775
+ if (options.zoomable && options.zoomOnWheel) {
1776
+ removeListener(cropper, EVENT_WHEEL, this.onWheel);
1777
+ }
1778
+
1779
+ if (options.toggleDragModeOnDblclick) {
1780
+ removeListener(cropper, EVENT_DBLCLICK, this.onDblclick);
1781
+ }
1782
+
1783
+ removeListener(element.ownerDocument, EVENT_POINTER_MOVE, this.onCropMove);
1784
+ removeListener(element.ownerDocument, EVENT_POINTER_UP, this.onCropEnd);
1785
+
1786
+ if (options.responsive) {
1787
+ removeListener(window, EVENT_RESIZE, this.onResize);
1788
+ }
1789
+ }
1790
+ };
1791
+
1792
+ var handlers = {
1793
+ resize: function resize() {
1794
+ var options = this.options,
1795
+ container = this.container,
1796
+ containerData = this.containerData;
1797
+
1798
+ var minContainerWidth = Number(options.minContainerWidth) || 200;
1799
+ var minContainerHeight = Number(options.minContainerHeight) || 100;
1800
+
1801
+ if (this.disabled || containerData.width <= minContainerWidth || containerData.height <= minContainerHeight) {
1802
+ return;
1803
+ }
1804
+
1805
+ var ratio = container.offsetWidth / containerData.width;
1806
+
1807
+ // Resize when width changed or height changed
1808
+ if (ratio !== 1 || container.offsetHeight !== containerData.height) {
1809
+ var canvasData = void 0;
1810
+ var cropBoxData = void 0;
1811
+
1812
+ if (options.restore) {
1813
+ canvasData = this.getCanvasData();
1814
+ cropBoxData = this.getCropBoxData();
1815
+ }
1816
+
1817
+ this.render();
1818
+
1819
+ if (options.restore) {
1820
+ this.setCanvasData(forEach(canvasData, function (n, i) {
1821
+ canvasData[i] = n * ratio;
1822
+ }));
1823
+ this.setCropBoxData(forEach(cropBoxData, function (n, i) {
1824
+ cropBoxData[i] = n * ratio;
1825
+ }));
1826
+ }
1827
+ }
1828
+ },
1829
+ dblclick: function dblclick() {
1830
+ if (this.disabled || this.options.dragMode === DRAG_MODE_NONE) {
1831
+ return;
1832
+ }
1833
+
1834
+ this.setDragMode(hasClass(this.dragBox, CLASS_CROP) ? DRAG_MODE_MOVE : DRAG_MODE_CROP);
1835
+ },
1836
+ wheel: function wheel(e) {
1837
+ var _this = this;
1838
+
1839
+ var ratio = Number(this.options.wheelZoomRatio) || 0.1;
1840
+ var delta = 1;
1841
+
1842
+ if (this.disabled) {
1843
+ return;
1844
+ }
1845
+
1846
+ e.preventDefault();
1847
+
1848
+ // Limit wheel speed to prevent zoom too fast (#21)
1849
+ if (this.wheeling) {
1850
+ return;
1851
+ }
1852
+
1853
+ this.wheeling = true;
1854
+
1855
+ setTimeout(function () {
1856
+ _this.wheeling = false;
1857
+ }, 50);
1858
+
1859
+ if (e.deltaY) {
1860
+ delta = e.deltaY > 0 ? 1 : -1;
1861
+ } else if (e.wheelDelta) {
1862
+ delta = -e.wheelDelta / 120;
1863
+ } else if (e.detail) {
1864
+ delta = e.detail > 0 ? 1 : -1;
1865
+ }
1866
+
1867
+ this.zoom(-delta * ratio, e);
1868
+ },
1869
+ cropStart: function cropStart(e) {
1870
+ if (this.disabled) {
1871
+ return;
1872
+ }
1873
+
1874
+ var options = this.options,
1875
+ pointers = this.pointers;
1876
+
1877
+ var action = void 0;
1878
+
1879
+ if (e.changedTouches) {
1880
+ // Handle touch event
1881
+ forEach(e.changedTouches, function (touch) {
1882
+ pointers[touch.identifier] = getPointer(touch);
1883
+ });
1884
+ } else {
1885
+ // Handle mouse event and pointer event
1886
+ pointers[e.pointerId || 0] = getPointer(e);
1887
+ }
1888
+
1889
+ if (Object.keys(pointers).length > 1 && options.zoomable && options.zoomOnTouch) {
1890
+ action = ACTION_ZOOM;
1891
+ } else {
1892
+ action = getData(e.target, DATA_ACTION);
1893
+ }
1894
+
1895
+ if (!REGEXP_ACTIONS.test(action)) {
1896
+ return;
1897
+ }
1898
+
1899
+ if (dispatchEvent(this.element, EVENT_CROP_START, {
1900
+ originalEvent: e,
1901
+ action: action
1902
+ }) === false) {
1903
+ return;
1904
+ }
1905
+
1906
+ e.preventDefault();
1907
+
1908
+ this.action = action;
1909
+ this.cropping = false;
1910
+
1911
+ if (action === ACTION_CROP) {
1912
+ this.cropping = true;
1913
+ addClass(this.dragBox, CLASS_MODAL);
1914
+ }
1915
+ },
1916
+ cropMove: function cropMove(e) {
1917
+ var action = this.action;
1918
+
1919
+
1920
+ if (this.disabled || !action) {
1921
+ return;
1922
+ }
1923
+
1924
+ var pointers = this.pointers;
1925
+
1926
+
1927
+ e.preventDefault();
1928
+
1929
+ if (dispatchEvent(this.element, EVENT_CROP_MOVE, {
1930
+ originalEvent: e,
1931
+ action: action
1932
+ }) === false) {
1933
+ return;
1934
+ }
1935
+
1936
+ if (e.changedTouches) {
1937
+ forEach(e.changedTouches, function (touch) {
1938
+ assign(pointers[touch.identifier], getPointer(touch, true));
1939
+ });
1940
+ } else {
1941
+ assign(pointers[e.pointerId || 0], getPointer(e, true));
1942
+ }
1943
+
1944
+ this.change(e);
1945
+ },
1946
+ cropEnd: function cropEnd(e) {
1947
+ if (this.disabled) {
1948
+ return;
1949
+ }
1950
+
1951
+ var action = this.action,
1952
+ pointers = this.pointers;
1953
+
1954
+
1955
+ if (e.changedTouches) {
1956
+ forEach(e.changedTouches, function (touch) {
1957
+ delete pointers[touch.identifier];
1958
+ });
1959
+ } else {
1960
+ delete pointers[e.pointerId || 0];
1961
+ }
1962
+
1963
+ if (!action) {
1964
+ return;
1965
+ }
1966
+
1967
+ e.preventDefault();
1968
+
1969
+ if (!Object.keys(pointers).length) {
1970
+ this.action = '';
1971
+ }
1972
+
1973
+ if (this.cropping) {
1974
+ this.cropping = false;
1975
+ toggleClass(this.dragBox, CLASS_MODAL, this.cropped && this.options.modal);
1976
+ }
1977
+
1978
+ dispatchEvent(this.element, EVENT_CROP_END, {
1979
+ originalEvent: e,
1980
+ action: action
1981
+ });
1982
+ }
1983
+ };
1984
+
1985
+ var change = {
1986
+ change: function change(e) {
1987
+ var options = this.options,
1988
+ canvasData = this.canvasData,
1989
+ containerData = this.containerData,
1990
+ cropBoxData = this.cropBoxData,
1991
+ pointers = this.pointers;
1992
+ var action = this.action;
1993
+ var aspectRatio = options.aspectRatio;
1994
+ var left = cropBoxData.left,
1995
+ top = cropBoxData.top,
1996
+ width = cropBoxData.width,
1997
+ height = cropBoxData.height;
1998
+
1999
+ var right = left + width;
2000
+ var bottom = top + height;
2001
+ var minLeft = 0;
2002
+ var minTop = 0;
2003
+ var maxWidth = containerData.width;
2004
+ var maxHeight = containerData.height;
2005
+ var renderable = true;
2006
+ var offset = void 0;
2007
+
2008
+ // Locking aspect ratio in "free mode" by holding shift key
2009
+ if (!aspectRatio && e.shiftKey) {
2010
+ aspectRatio = width && height ? width / height : 1;
2011
+ }
2012
+
2013
+ if (this.limited) {
2014
+ minLeft = cropBoxData.minLeft;
2015
+ minTop = cropBoxData.minTop;
2016
+
2017
+ maxWidth = minLeft + Math.min(containerData.width, canvasData.width, canvasData.left + canvasData.width);
2018
+ maxHeight = minTop + Math.min(containerData.height, canvasData.height, canvasData.top + canvasData.height);
2019
+ }
2020
+
2021
+ var pointer = pointers[Object.keys(pointers)[0]];
2022
+ var range = {
2023
+ x: pointer.endX - pointer.startX,
2024
+ y: pointer.endY - pointer.startY
2025
+ };
2026
+ var check = function check(side) {
2027
+ switch (side) {
2028
+ case ACTION_EAST:
2029
+ if (right + range.x > maxWidth) {
2030
+ range.x = maxWidth - right;
2031
+ }
2032
+
2033
+ break;
2034
+
2035
+ case ACTION_WEST:
2036
+ if (left + range.x < minLeft) {
2037
+ range.x = minLeft - left;
2038
+ }
2039
+
2040
+ break;
2041
+
2042
+ case ACTION_NORTH:
2043
+ if (top + range.y < minTop) {
2044
+ range.y = minTop - top;
2045
+ }
2046
+
2047
+ break;
2048
+
2049
+ case ACTION_SOUTH:
2050
+ if (bottom + range.y > maxHeight) {
2051
+ range.y = maxHeight - bottom;
2052
+ }
2053
+
2054
+ break;
2055
+
2056
+ default:
2057
+ }
2058
+ };
2059
+
2060
+ switch (action) {
2061
+ // Move crop box
2062
+ case ACTION_ALL:
2063
+ left += range.x;
2064
+ top += range.y;
2065
+ break;
2066
+
2067
+ // Resize crop box
2068
+ case ACTION_EAST:
2069
+ if (range.x >= 0 && (right >= maxWidth || aspectRatio && (top <= minTop || bottom >= maxHeight))) {
2070
+ renderable = false;
2071
+ break;
2072
+ }
2073
+
2074
+ check(ACTION_EAST);
2075
+ width += range.x;
2076
+
2077
+ if (aspectRatio) {
2078
+ height = width / aspectRatio;
2079
+ top -= range.x / aspectRatio / 2;
2080
+ }
2081
+
2082
+ if (width < 0) {
2083
+ action = ACTION_WEST;
2084
+ width = 0;
2085
+ }
2086
+
2087
+ break;
2088
+
2089
+ case ACTION_NORTH:
2090
+ if (range.y <= 0 && (top <= minTop || aspectRatio && (left <= minLeft || right >= maxWidth))) {
2091
+ renderable = false;
2092
+ break;
2093
+ }
2094
+
2095
+ check(ACTION_NORTH);
2096
+ height -= range.y;
2097
+ top += range.y;
2098
+
2099
+ if (aspectRatio) {
2100
+ width = height * aspectRatio;
2101
+ left += range.y * aspectRatio / 2;
2102
+ }
2103
+
2104
+ if (height < 0) {
2105
+ action = ACTION_SOUTH;
2106
+ height = 0;
2107
+ }
2108
+
2109
+ break;
2110
+
2111
+ case ACTION_WEST:
2112
+ if (range.x <= 0 && (left <= minLeft || aspectRatio && (top <= minTop || bottom >= maxHeight))) {
2113
+ renderable = false;
2114
+ break;
2115
+ }
2116
+
2117
+ check(ACTION_WEST);
2118
+ width -= range.x;
2119
+ left += range.x;
2120
+
2121
+ if (aspectRatio) {
2122
+ height = width / aspectRatio;
2123
+ top += range.x / aspectRatio / 2;
2124
+ }
2125
+
2126
+ if (width < 0) {
2127
+ action = ACTION_EAST;
2128
+ width = 0;
2129
+ }
2130
+
2131
+ break;
2132
+
2133
+ case ACTION_SOUTH:
2134
+ if (range.y >= 0 && (bottom >= maxHeight || aspectRatio && (left <= minLeft || right >= maxWidth))) {
2135
+ renderable = false;
2136
+ break;
2137
+ }
2138
+
2139
+ check(ACTION_SOUTH);
2140
+ height += range.y;
2141
+
2142
+ if (aspectRatio) {
2143
+ width = height * aspectRatio;
2144
+ left -= range.y * aspectRatio / 2;
2145
+ }
2146
+
2147
+ if (height < 0) {
2148
+ action = ACTION_NORTH;
2149
+ height = 0;
2150
+ }
2151
+
2152
+ break;
2153
+
2154
+ case ACTION_NORTH_EAST:
2155
+ if (aspectRatio) {
2156
+ if (range.y <= 0 && (top <= minTop || right >= maxWidth)) {
2157
+ renderable = false;
2158
+ break;
2159
+ }
2160
+
2161
+ check(ACTION_NORTH);
2162
+ height -= range.y;
2163
+ top += range.y;
2164
+ width = height * aspectRatio;
2165
+ } else {
2166
+ check(ACTION_NORTH);
2167
+ check(ACTION_EAST);
2168
+
2169
+ if (range.x >= 0) {
2170
+ if (right < maxWidth) {
2171
+ width += range.x;
2172
+ } else if (range.y <= 0 && top <= minTop) {
2173
+ renderable = false;
2174
+ }
2175
+ } else {
2176
+ width += range.x;
2177
+ }
2178
+
2179
+ if (range.y <= 0) {
2180
+ if (top > minTop) {
2181
+ height -= range.y;
2182
+ top += range.y;
2183
+ }
2184
+ } else {
2185
+ height -= range.y;
2186
+ top += range.y;
2187
+ }
2188
+ }
2189
+
2190
+ if (width < 0 && height < 0) {
2191
+ action = ACTION_SOUTH_WEST;
2192
+ height = 0;
2193
+ width = 0;
2194
+ } else if (width < 0) {
2195
+ action = ACTION_NORTH_WEST;
2196
+ width = 0;
2197
+ } else if (height < 0) {
2198
+ action = ACTION_SOUTH_EAST;
2199
+ height = 0;
2200
+ }
2201
+
2202
+ break;
2203
+
2204
+ case ACTION_NORTH_WEST:
2205
+ if (aspectRatio) {
2206
+ if (range.y <= 0 && (top <= minTop || left <= minLeft)) {
2207
+ renderable = false;
2208
+ break;
2209
+ }
2210
+
2211
+ check(ACTION_NORTH);
2212
+ height -= range.y;
2213
+ top += range.y;
2214
+ width = height * aspectRatio;
2215
+ left += range.y * aspectRatio;
2216
+ } else {
2217
+ check(ACTION_NORTH);
2218
+ check(ACTION_WEST);
2219
+
2220
+ if (range.x <= 0) {
2221
+ if (left > minLeft) {
2222
+ width -= range.x;
2223
+ left += range.x;
2224
+ } else if (range.y <= 0 && top <= minTop) {
2225
+ renderable = false;
2226
+ }
2227
+ } else {
2228
+ width -= range.x;
2229
+ left += range.x;
2230
+ }
2231
+
2232
+ if (range.y <= 0) {
2233
+ if (top > minTop) {
2234
+ height -= range.y;
2235
+ top += range.y;
2236
+ }
2237
+ } else {
2238
+ height -= range.y;
2239
+ top += range.y;
2240
+ }
2241
+ }
2242
+
2243
+ if (width < 0 && height < 0) {
2244
+ action = ACTION_SOUTH_EAST;
2245
+ height = 0;
2246
+ width = 0;
2247
+ } else if (width < 0) {
2248
+ action = ACTION_NORTH_EAST;
2249
+ width = 0;
2250
+ } else if (height < 0) {
2251
+ action = ACTION_SOUTH_WEST;
2252
+ height = 0;
2253
+ }
2254
+
2255
+ break;
2256
+
2257
+ case ACTION_SOUTH_WEST:
2258
+ if (aspectRatio) {
2259
+ if (range.x <= 0 && (left <= minLeft || bottom >= maxHeight)) {
2260
+ renderable = false;
2261
+ break;
2262
+ }
2263
+
2264
+ check(ACTION_WEST);
2265
+ width -= range.x;
2266
+ left += range.x;
2267
+ height = width / aspectRatio;
2268
+ } else {
2269
+ check(ACTION_SOUTH);
2270
+ check(ACTION_WEST);
2271
+
2272
+ if (range.x <= 0) {
2273
+ if (left > minLeft) {
2274
+ width -= range.x;
2275
+ left += range.x;
2276
+ } else if (range.y >= 0 && bottom >= maxHeight) {
2277
+ renderable = false;
2278
+ }
2279
+ } else {
2280
+ width -= range.x;
2281
+ left += range.x;
2282
+ }
2283
+
2284
+ if (range.y >= 0) {
2285
+ if (bottom < maxHeight) {
2286
+ height += range.y;
2287
+ }
2288
+ } else {
2289
+ height += range.y;
2290
+ }
2291
+ }
2292
+
2293
+ if (width < 0 && height < 0) {
2294
+ action = ACTION_NORTH_EAST;
2295
+ height = 0;
2296
+ width = 0;
2297
+ } else if (width < 0) {
2298
+ action = ACTION_SOUTH_EAST;
2299
+ width = 0;
2300
+ } else if (height < 0) {
2301
+ action = ACTION_NORTH_WEST;
2302
+ height = 0;
2303
+ }
2304
+
2305
+ break;
2306
+
2307
+ case ACTION_SOUTH_EAST:
2308
+ if (aspectRatio) {
2309
+ if (range.x >= 0 && (right >= maxWidth || bottom >= maxHeight)) {
2310
+ renderable = false;
2311
+ break;
2312
+ }
2313
+
2314
+ check(ACTION_EAST);
2315
+ width += range.x;
2316
+ height = width / aspectRatio;
2317
+ } else {
2318
+ check(ACTION_SOUTH);
2319
+ check(ACTION_EAST);
2320
+
2321
+ if (range.x >= 0) {
2322
+ if (right < maxWidth) {
2323
+ width += range.x;
2324
+ } else if (range.y >= 0 && bottom >= maxHeight) {
2325
+ renderable = false;
2326
+ }
2327
+ } else {
2328
+ width += range.x;
2329
+ }
2330
+
2331
+ if (range.y >= 0) {
2332
+ if (bottom < maxHeight) {
2333
+ height += range.y;
2334
+ }
2335
+ } else {
2336
+ height += range.y;
2337
+ }
2338
+ }
2339
+
2340
+ if (width < 0 && height < 0) {
2341
+ action = ACTION_NORTH_WEST;
2342
+ height = 0;
2343
+ width = 0;
2344
+ } else if (width < 0) {
2345
+ action = ACTION_SOUTH_WEST;
2346
+ width = 0;
2347
+ } else if (height < 0) {
2348
+ action = ACTION_NORTH_EAST;
2349
+ height = 0;
2350
+ }
2351
+
2352
+ break;
2353
+
2354
+ // Move canvas
2355
+ case ACTION_MOVE:
2356
+ this.move(range.x, range.y);
2357
+ renderable = false;
2358
+ break;
2359
+
2360
+ // Zoom canvas
2361
+ case ACTION_ZOOM:
2362
+ this.zoom(getMaxZoomRatio(pointers), e);
2363
+ renderable = false;
2364
+ break;
2365
+
2366
+ // Create crop box
2367
+ case ACTION_CROP:
2368
+ if (!range.x || !range.y) {
2369
+ renderable = false;
2370
+ break;
2371
+ }
2372
+
2373
+ offset = getOffset(this.cropper);
2374
+ left = pointer.startX - offset.left;
2375
+ top = pointer.startY - offset.top;
2376
+ width = cropBoxData.minWidth;
2377
+ height = cropBoxData.minHeight;
2378
+
2379
+ if (range.x > 0) {
2380
+ action = range.y > 0 ? ACTION_SOUTH_EAST : ACTION_NORTH_EAST;
2381
+ } else if (range.x < 0) {
2382
+ left -= width;
2383
+ action = range.y > 0 ? ACTION_SOUTH_WEST : ACTION_NORTH_WEST;
2384
+ }
2385
+
2386
+ if (range.y < 0) {
2387
+ top -= height;
2388
+ }
2389
+
2390
+ // Show the crop box if is hidden
2391
+ if (!this.cropped) {
2392
+ removeClass(this.cropBox, CLASS_HIDDEN);
2393
+ this.cropped = true;
2394
+
2395
+ if (this.limited) {
2396
+ this.limitCropBox(true, true);
2397
+ }
2398
+ }
2399
+
2400
+ break;
2401
+
2402
+ default:
2403
+ }
2404
+
2405
+ if (renderable) {
2406
+ cropBoxData.width = width;
2407
+ cropBoxData.height = height;
2408
+ cropBoxData.left = left;
2409
+ cropBoxData.top = top;
2410
+ this.action = action;
2411
+ this.renderCropBox();
2412
+ }
2413
+
2414
+ // Override
2415
+ forEach(pointers, function (p) {
2416
+ p.startX = p.endX;
2417
+ p.startY = p.endY;
2418
+ });
2419
+ }
2420
+ };
2421
+
2422
+ var methods = {
2423
+ // Show the crop box manually
2424
+ crop: function crop() {
2425
+ if (this.ready && !this.cropped && !this.disabled) {
2426
+ this.cropped = true;
2427
+ this.limitCropBox(true, true);
2428
+
2429
+ if (this.options.modal) {
2430
+ addClass(this.dragBox, CLASS_MODAL);
2431
+ }
2432
+
2433
+ removeClass(this.cropBox, CLASS_HIDDEN);
2434
+ this.setCropBoxData(this.initialCropBoxData);
2435
+ }
2436
+
2437
+ return this;
2438
+ },
2439
+
2440
+
2441
+ // Reset the image and crop box to their initial states
2442
+ reset: function reset() {
2443
+ if (this.ready && !this.disabled) {
2444
+ this.imageData = assign({}, this.initialImageData);
2445
+ this.canvasData = assign({}, this.initialCanvasData);
2446
+ this.cropBoxData = assign({}, this.initialCropBoxData);
2447
+ this.renderCanvas();
2448
+
2449
+ if (this.cropped) {
2450
+ this.renderCropBox();
2451
+ }
2452
+ }
2453
+
2454
+ return this;
2455
+ },
2456
+
2457
+
2458
+ // Clear the crop box
2459
+ clear: function clear() {
2460
+ if (this.cropped && !this.disabled) {
2461
+ assign(this.cropBoxData, {
2462
+ left: 0,
2463
+ top: 0,
2464
+ width: 0,
2465
+ height: 0
2466
+ });
2467
+
2468
+ this.cropped = false;
2469
+ this.renderCropBox();
2470
+ this.limitCanvas(true, true);
2471
+
2472
+ // Render canvas after crop box rendered
2473
+ this.renderCanvas();
2474
+ removeClass(this.dragBox, CLASS_MODAL);
2475
+ addClass(this.cropBox, CLASS_HIDDEN);
2476
+ }
2477
+
2478
+ return this;
2479
+ },
2480
+
2481
+
2482
+ /**
2483
+ * Replace the image's src and rebuild the cropper
2484
+ * @param {string} url - The new URL.
2485
+ * @param {boolean} [hasSameSize] - Indicate if the new image has the same size as the old one.
2486
+ * @returns {Cropper} this
2487
+ */
2488
+ replace: function replace(url) {
2489
+ var hasSameSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
2490
+
2491
+ if (!this.disabled && url) {
2492
+ if (this.isImg) {
2493
+ this.element.src = url;
2494
+ }
2495
+
2496
+ if (hasSameSize) {
2497
+ this.url = url;
2498
+ this.image.src = url;
2499
+
2500
+ if (this.ready) {
2501
+ this.viewBoxImage.src = url;
2502
+
2503
+ forEach(this.previews, function (element) {
2504
+ element.getElementsByTagName('img')[0].src = url;
2505
+ });
2506
+ }
2507
+ } else {
2508
+ if (this.isImg) {
2509
+ this.replaced = true;
2510
+ }
2511
+
2512
+ this.options.data = null;
2513
+ this.uncreate();
2514
+ this.load(url);
2515
+ }
2516
+ }
2517
+
2518
+ return this;
2519
+ },
2520
+
2521
+
2522
+ // Enable (unfreeze) the cropper
2523
+ enable: function enable() {
2524
+ if (this.ready && this.disabled) {
2525
+ this.disabled = false;
2526
+ removeClass(this.cropper, CLASS_DISABLED);
2527
+ }
2528
+
2529
+ return this;
2530
+ },
2531
+
2532
+
2533
+ // Disable (freeze) the cropper
2534
+ disable: function disable() {
2535
+ if (this.ready && !this.disabled) {
2536
+ this.disabled = true;
2537
+ addClass(this.cropper, CLASS_DISABLED);
2538
+ }
2539
+
2540
+ return this;
2541
+ },
2542
+
2543
+
2544
+ /**
2545
+ * Destroy the cropper and remove the instance from the image
2546
+ * @returns {Cropper} this
2547
+ */
2548
+ destroy: function destroy() {
2549
+ var element = this.element;
2550
+
2551
+
2552
+ if (!getData(element, NAMESPACE)) {
2553
+ return this;
2554
+ }
2555
+
2556
+ if (this.isImg && this.replaced) {
2557
+ element.src = this.originalUrl;
2558
+ }
2559
+
2560
+ this.uncreate();
2561
+ removeData(element, NAMESPACE);
2562
+
2563
+ return this;
2564
+ },
2565
+
2566
+
2567
+ /**
2568
+ * Move the canvas with relative offsets
2569
+ * @param {number} offsetX - The relative offset distance on the x-axis.
2570
+ * @param {number} [offsetY=offsetX] - The relative offset distance on the y-axis.
2571
+ * @returns {Cropper} this
2572
+ */
2573
+ move: function move(offsetX) {
2574
+ var offsetY = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : offsetX;
2575
+ var _canvasData = this.canvasData,
2576
+ left = _canvasData.left,
2577
+ top = _canvasData.top;
2578
+
2579
+
2580
+ return this.moveTo(isUndefined(offsetX) ? offsetX : left + Number(offsetX), isUndefined(offsetY) ? offsetY : top + Number(offsetY));
2581
+ },
2582
+
2583
+
2584
+ /**
2585
+ * Move the canvas to an absolute point
2586
+ * @param {number} x - The x-axis coordinate.
2587
+ * @param {number} [y=x] - The y-axis coordinate.
2588
+ * @returns {Cropper} this
2589
+ */
2590
+ moveTo: function moveTo(x) {
2591
+ var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : x;
2592
+ var canvasData = this.canvasData;
2593
+
2594
+ var changed = false;
2595
+
2596
+ x = Number(x);
2597
+ y = Number(y);
2598
+
2599
+ if (this.ready && !this.disabled && this.options.movable) {
2600
+ if (isNumber(x)) {
2601
+ canvasData.left = x;
2602
+ changed = true;
2603
+ }
2604
+
2605
+ if (isNumber(y)) {
2606
+ canvasData.top = y;
2607
+ changed = true;
2608
+ }
2609
+
2610
+ if (changed) {
2611
+ this.renderCanvas(true);
2612
+ }
2613
+ }
2614
+
2615
+ return this;
2616
+ },
2617
+
2618
+
2619
+ /**
2620
+ * Zoom the canvas with a relative ratio
2621
+ * @param {number} ratio - The target ratio.
2622
+ * @param {Event} _originalEvent - The original event if any.
2623
+ * @returns {Cropper} this
2624
+ */
2625
+ zoom: function zoom(ratio, _originalEvent) {
2626
+ var canvasData = this.canvasData;
2627
+
2628
+
2629
+ ratio = Number(ratio);
2630
+
2631
+ if (ratio < 0) {
2632
+ ratio = 1 / (1 - ratio);
2633
+ } else {
2634
+ ratio = 1 + ratio;
2635
+ }
2636
+
2637
+ return this.zoomTo(canvasData.width * ratio / canvasData.naturalWidth, null, _originalEvent);
2638
+ },
2639
+
2640
+
2641
+ /**
2642
+ * Zoom the canvas to an absolute ratio
2643
+ * @param {number} ratio - The target ratio.
2644
+ * @param {Object} pivot - The zoom pivot point coordinate.
2645
+ * @param {Event} _originalEvent - The original event if any.
2646
+ * @returns {Cropper} this
2647
+ */
2648
+ zoomTo: function zoomTo(ratio, pivot, _originalEvent) {
2649
+ var options = this.options,
2650
+ canvasData = this.canvasData;
2651
+ var width = canvasData.width,
2652
+ height = canvasData.height,
2653
+ naturalWidth = canvasData.naturalWidth,
2654
+ naturalHeight = canvasData.naturalHeight;
2655
+
2656
+
2657
+ ratio = Number(ratio);
2658
+
2659
+ if (ratio >= 0 && this.ready && !this.disabled && options.zoomable) {
2660
+ var newWidth = naturalWidth * ratio;
2661
+ var newHeight = naturalHeight * ratio;
2662
+
2663
+ if (dispatchEvent(this.element, EVENT_ZOOM, {
2664
+ originalEvent: _originalEvent,
2665
+ oldRatio: width / naturalWidth,
2666
+ ratio: newWidth / naturalWidth
2667
+ }) === false) {
2668
+ return this;
2669
+ }
2670
+
2671
+ if (_originalEvent) {
2672
+ var pointers = this.pointers;
2673
+
2674
+ var offset = getOffset(this.cropper);
2675
+ var center = pointers && Object.keys(pointers).length ? getPointersCenter(pointers) : {
2676
+ pageX: _originalEvent.pageX,
2677
+ pageY: _originalEvent.pageY
2678
+ };
2679
+
2680
+ // Zoom from the triggering point of the event
2681
+ canvasData.left -= (newWidth - width) * ((center.pageX - offset.left - canvasData.left) / width);
2682
+ canvasData.top -= (newHeight - height) * ((center.pageY - offset.top - canvasData.top) / height);
2683
+ } else if (isPlainObject(pivot) && isNumber(pivot.x) && isNumber(pivot.y)) {
2684
+ canvasData.left -= (newWidth - width) * ((pivot.x - canvasData.left) / width);
2685
+ canvasData.top -= (newHeight - height) * ((pivot.y - canvasData.top) / height);
2686
+ } else {
2687
+ // Zoom from the center of the canvas
2688
+ canvasData.left -= (newWidth - width) / 2;
2689
+ canvasData.top -= (newHeight - height) / 2;
2690
+ }
2691
+
2692
+ canvasData.width = newWidth;
2693
+ canvasData.height = newHeight;
2694
+ this.renderCanvas(true);
2695
+ }
2696
+
2697
+ return this;
2698
+ },
2699
+
2700
+
2701
+ /**
2702
+ * Rotate the canvas with a relative degree
2703
+ * @param {number} degree - The rotate degree.
2704
+ * @returns {Cropper} this
2705
+ */
2706
+ rotate: function rotate(degree) {
2707
+ return this.rotateTo((this.imageData.rotate || 0) + Number(degree));
2708
+ },
2709
+
2710
+
2711
+ /**
2712
+ * Rotate the canvas to an absolute degree
2713
+ * @param {number} degree - The rotate degree.
2714
+ * @returns {Cropper} this
2715
+ */
2716
+ rotateTo: function rotateTo(degree) {
2717
+ degree = Number(degree);
2718
+
2719
+ if (isNumber(degree) && this.ready && !this.disabled && this.options.rotatable) {
2720
+ this.imageData.rotate = degree % 360;
2721
+ this.renderCanvas(true, true);
2722
+ }
2723
+
2724
+ return this;
2725
+ },
2726
+
2727
+
2728
+ /**
2729
+ * Scale the image on the x-axis.
2730
+ * @param {number} scaleX - The scale ratio on the x-axis.
2731
+ * @returns {Cropper} this
2732
+ */
2733
+ scaleX: function scaleX(_scaleX) {
2734
+ var scaleY = this.imageData.scaleY;
2735
+
2736
+
2737
+ return this.scale(_scaleX, isNumber(scaleY) ? scaleY : 1);
2738
+ },
2739
+
2740
+
2741
+ /**
2742
+ * Scale the image on the y-axis.
2743
+ * @param {number} scaleY - The scale ratio on the y-axis.
2744
+ * @returns {Cropper} this
2745
+ */
2746
+ scaleY: function scaleY(_scaleY) {
2747
+ var scaleX = this.imageData.scaleX;
2748
+
2749
+
2750
+ return this.scale(isNumber(scaleX) ? scaleX : 1, _scaleY);
2751
+ },
2752
+
2753
+
2754
+ /**
2755
+ * Scale the image
2756
+ * @param {number} scaleX - The scale ratio on the x-axis.
2757
+ * @param {number} [scaleY=scaleX] - The scale ratio on the y-axis.
2758
+ * @returns {Cropper} this
2759
+ */
2760
+ scale: function scale(scaleX) {
2761
+ var scaleY = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : scaleX;
2762
+ var imageData = this.imageData;
2763
+
2764
+ var transformed = false;
2765
+
2766
+ scaleX = Number(scaleX);
2767
+ scaleY = Number(scaleY);
2768
+
2769
+ if (this.ready && !this.disabled && this.options.scalable) {
2770
+ if (isNumber(scaleX)) {
2771
+ imageData.scaleX = scaleX;
2772
+ transformed = true;
2773
+ }
2774
+
2775
+ if (isNumber(scaleY)) {
2776
+ imageData.scaleY = scaleY;
2777
+ transformed = true;
2778
+ }
2779
+
2780
+ if (transformed) {
2781
+ this.renderCanvas(true, true);
2782
+ }
2783
+ }
2784
+
2785
+ return this;
2786
+ },
2787
+
2788
+
2789
+ /**
2790
+ * Get the cropped area position and size data (base on the original image)
2791
+ * @param {boolean} [rounded=false] - Indicate if round the data values or not.
2792
+ * @returns {Object} The result cropped data.
2793
+ */
2794
+ getData: function getData$$1() {
2795
+ var rounded = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
2796
+ var options = this.options,
2797
+ imageData = this.imageData,
2798
+ canvasData = this.canvasData,
2799
+ cropBoxData = this.cropBoxData;
2800
+
2801
+ var data = void 0;
2802
+
2803
+ if (this.ready && this.cropped) {
2804
+ data = {
2805
+ x: cropBoxData.left - canvasData.left,
2806
+ y: cropBoxData.top - canvasData.top,
2807
+ width: cropBoxData.width,
2808
+ height: cropBoxData.height
2809
+ };
2810
+
2811
+ var ratio = imageData.width / imageData.naturalWidth;
2812
+
2813
+ forEach(data, function (n, i) {
2814
+ n /= ratio;
2815
+ data[i] = rounded ? Math.round(n) : n;
2816
+ });
2817
+ } else {
2818
+ data = {
2819
+ x: 0,
2820
+ y: 0,
2821
+ width: 0,
2822
+ height: 0
2823
+ };
2824
+ }
2825
+
2826
+ if (options.rotatable) {
2827
+ data.rotate = imageData.rotate || 0;
2828
+ }
2829
+
2830
+ if (options.scalable) {
2831
+ data.scaleX = imageData.scaleX || 1;
2832
+ data.scaleY = imageData.scaleY || 1;
2833
+ }
2834
+
2835
+ return data;
2836
+ },
2837
+
2838
+
2839
+ /**
2840
+ * Set the cropped area position and size with new data
2841
+ * @param {Object} data - The new data.
2842
+ * @returns {Cropper} this
2843
+ */
2844
+ setData: function setData$$1(data) {
2845
+ var options = this.options,
2846
+ imageData = this.imageData,
2847
+ canvasData = this.canvasData;
2848
+
2849
+ var cropBoxData = {};
2850
+
2851
+ if (this.ready && !this.disabled && isPlainObject(data)) {
2852
+ var transformed = false;
2853
+
2854
+ if (options.rotatable) {
2855
+ if (isNumber(data.rotate) && data.rotate !== imageData.rotate) {
2856
+ imageData.rotate = data.rotate;
2857
+ transformed = true;
2858
+ }
2859
+ }
2860
+
2861
+ if (options.scalable) {
2862
+ if (isNumber(data.scaleX) && data.scaleX !== imageData.scaleX) {
2863
+ imageData.scaleX = data.scaleX;
2864
+ transformed = true;
2865
+ }
2866
+
2867
+ if (isNumber(data.scaleY) && data.scaleY !== imageData.scaleY) {
2868
+ imageData.scaleY = data.scaleY;
2869
+ transformed = true;
2870
+ }
2871
+ }
2872
+
2873
+ if (transformed) {
2874
+ this.renderCanvas(true, true);
2875
+ }
2876
+
2877
+ var ratio = imageData.width / imageData.naturalWidth;
2878
+
2879
+ if (isNumber(data.x)) {
2880
+ cropBoxData.left = data.x * ratio + canvasData.left;
2881
+ }
2882
+
2883
+ if (isNumber(data.y)) {
2884
+ cropBoxData.top = data.y * ratio + canvasData.top;
2885
+ }
2886
+
2887
+ if (isNumber(data.width)) {
2888
+ cropBoxData.width = data.width * ratio;
2889
+ }
2890
+
2891
+ if (isNumber(data.height)) {
2892
+ cropBoxData.height = data.height * ratio;
2893
+ }
2894
+
2895
+ this.setCropBoxData(cropBoxData);
2896
+ }
2897
+
2898
+ return this;
2899
+ },
2900
+
2901
+
2902
+ /**
2903
+ * Get the container size data.
2904
+ * @returns {Object} The result container data.
2905
+ */
2906
+ getContainerData: function getContainerData() {
2907
+ return this.ready ? assign({}, this.containerData) : {};
2908
+ },
2909
+
2910
+
2911
+ /**
2912
+ * Get the image position and size data.
2913
+ * @returns {Object} The result image data.
2914
+ */
2915
+ getImageData: function getImageData() {
2916
+ return this.sized ? assign({}, this.imageData) : {};
2917
+ },
2918
+
2919
+
2920
+ /**
2921
+ * Get the canvas position and size data.
2922
+ * @returns {Object} The result canvas data.
2923
+ */
2924
+ getCanvasData: function getCanvasData() {
2925
+ var canvasData = this.canvasData;
2926
+
2927
+ var data = {};
2928
+
2929
+ if (this.ready) {
2930
+ forEach(['left', 'top', 'width', 'height', 'naturalWidth', 'naturalHeight'], function (n) {
2931
+ data[n] = canvasData[n];
2932
+ });
2933
+ }
2934
+
2935
+ return data;
2936
+ },
2937
+
2938
+
2939
+ /**
2940
+ * Set the canvas position and size with new data.
2941
+ * @param {Object} data - The new canvas data.
2942
+ * @returns {Cropper} this
2943
+ */
2944
+ setCanvasData: function setCanvasData(data) {
2945
+ var canvasData = this.canvasData;
2946
+ var aspectRatio = canvasData.aspectRatio;
2947
+
2948
+
2949
+ if (this.ready && !this.disabled && isPlainObject(data)) {
2950
+ if (isNumber(data.left)) {
2951
+ canvasData.left = data.left;
2952
+ }
2953
+
2954
+ if (isNumber(data.top)) {
2955
+ canvasData.top = data.top;
2956
+ }
2957
+
2958
+ if (isNumber(data.width)) {
2959
+ canvasData.width = data.width;
2960
+ canvasData.height = data.width / aspectRatio;
2961
+ } else if (isNumber(data.height)) {
2962
+ canvasData.height = data.height;
2963
+ canvasData.width = data.height * aspectRatio;
2964
+ }
2965
+
2966
+ this.renderCanvas(true);
2967
+ }
2968
+
2969
+ return this;
2970
+ },
2971
+
2972
+
2973
+ /**
2974
+ * Get the crop box position and size data.
2975
+ * @returns {Object} The result crop box data.
2976
+ */
2977
+ getCropBoxData: function getCropBoxData() {
2978
+ var cropBoxData = this.cropBoxData;
2979
+
2980
+ var data = void 0;
2981
+
2982
+ if (this.ready && this.cropped) {
2983
+ data = {
2984
+ left: cropBoxData.left,
2985
+ top: cropBoxData.top,
2986
+ width: cropBoxData.width,
2987
+ height: cropBoxData.height
2988
+ };
2989
+ }
2990
+
2991
+ return data || {};
2992
+ },
2993
+
2994
+
2995
+ /**
2996
+ * Set the crop box position and size with new data.
2997
+ * @param {Object} data - The new crop box data.
2998
+ * @returns {Cropper} this
2999
+ */
3000
+ setCropBoxData: function setCropBoxData(data) {
3001
+ var cropBoxData = this.cropBoxData;
3002
+ var aspectRatio = this.options.aspectRatio;
3003
+
3004
+ var widthChanged = void 0;
3005
+ var heightChanged = void 0;
3006
+
3007
+ if (this.ready && this.cropped && !this.disabled && isPlainObject(data)) {
3008
+ if (isNumber(data.left)) {
3009
+ cropBoxData.left = data.left;
3010
+ }
3011
+
3012
+ if (isNumber(data.top)) {
3013
+ cropBoxData.top = data.top;
3014
+ }
3015
+
3016
+ if (isNumber(data.width) && data.width !== cropBoxData.width) {
3017
+ widthChanged = true;
3018
+ cropBoxData.width = data.width;
3019
+ }
3020
+
3021
+ if (isNumber(data.height) && data.height !== cropBoxData.height) {
3022
+ heightChanged = true;
3023
+ cropBoxData.height = data.height;
3024
+ }
3025
+
3026
+ if (aspectRatio) {
3027
+ if (widthChanged) {
3028
+ cropBoxData.height = cropBoxData.width / aspectRatio;
3029
+ } else if (heightChanged) {
3030
+ cropBoxData.width = cropBoxData.height * aspectRatio;
3031
+ }
3032
+ }
3033
+
3034
+ this.renderCropBox();
3035
+ }
3036
+
3037
+ return this;
3038
+ },
3039
+
3040
+
3041
+ /**
3042
+ * Get a canvas drawn the cropped image.
3043
+ * @param {Object} [options={}] - The config options.
3044
+ * @returns {HTMLCanvasElement} - The result canvas.
3045
+ */
3046
+ getCroppedCanvas: function getCroppedCanvas() {
3047
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
3048
+
3049
+ if (!this.ready || !window.HTMLCanvasElement) {
3050
+ return null;
3051
+ }
3052
+
3053
+ var canvasData = this.canvasData;
3054
+
3055
+ var source = getSourceCanvas(this.image, this.imageData, canvasData, options);
3056
+
3057
+ // Returns the source canvas if it is not cropped.
3058
+ if (!this.cropped) {
3059
+ return source;
3060
+ }
3061
+
3062
+ var _getData = this.getData(),
3063
+ initialX = _getData.x,
3064
+ initialY = _getData.y,
3065
+ initialWidth = _getData.width,
3066
+ initialHeight = _getData.height;
3067
+
3068
+ var ratio = source.width / Math.floor(canvasData.naturalWidth);
3069
+
3070
+ if (ratio !== 1) {
3071
+ initialX *= ratio;
3072
+ initialY *= ratio;
3073
+ initialWidth *= ratio;
3074
+ initialHeight *= ratio;
3075
+ }
3076
+
3077
+ var aspectRatio = initialWidth / initialHeight;
3078
+ var maxSizes = getAdjustedSizes({
3079
+ aspectRatio: aspectRatio,
3080
+ width: options.maxWidth || Infinity,
3081
+ height: options.maxHeight || Infinity
3082
+ });
3083
+ var minSizes = getAdjustedSizes({
3084
+ aspectRatio: aspectRatio,
3085
+ width: options.minWidth || 0,
3086
+ height: options.minHeight || 0
3087
+ }, 'cover');
3088
+
3089
+ var _getAdjustedSizes = getAdjustedSizes({
3090
+ aspectRatio: aspectRatio,
3091
+ width: options.width || (ratio !== 1 ? source.width : initialWidth),
3092
+ height: options.height || (ratio !== 1 ? source.height : initialHeight)
3093
+ }),
3094
+ width = _getAdjustedSizes.width,
3095
+ height = _getAdjustedSizes.height;
3096
+
3097
+ width = Math.min(maxSizes.width, Math.max(minSizes.width, width));
3098
+ height = Math.min(maxSizes.height, Math.max(minSizes.height, height));
3099
+
3100
+ var canvas = document.createElement('canvas');
3101
+ var context = canvas.getContext('2d');
3102
+
3103
+ canvas.width = normalizeDecimalNumber(width);
3104
+ canvas.height = normalizeDecimalNumber(height);
3105
+
3106
+ context.fillStyle = options.fillColor || 'transparent';
3107
+ context.fillRect(0, 0, width, height);
3108
+
3109
+ var _options$imageSmoothi = options.imageSmoothingEnabled,
3110
+ imageSmoothingEnabled = _options$imageSmoothi === undefined ? true : _options$imageSmoothi,
3111
+ imageSmoothingQuality = options.imageSmoothingQuality;
3112
+
3113
+
3114
+ context.imageSmoothingEnabled = imageSmoothingEnabled;
3115
+
3116
+ if (imageSmoothingQuality) {
3117
+ context.imageSmoothingQuality = imageSmoothingQuality;
3118
+ }
3119
+
3120
+ // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D.drawImage
3121
+ var sourceWidth = source.width;
3122
+ var sourceHeight = source.height;
3123
+
3124
+ // Source canvas parameters
3125
+ var srcX = initialX;
3126
+ var srcY = initialY;
3127
+ var srcWidth = void 0;
3128
+ var srcHeight = void 0;
3129
+
3130
+ // Destination canvas parameters
3131
+ var dstX = void 0;
3132
+ var dstY = void 0;
3133
+ var dstWidth = void 0;
3134
+ var dstHeight = void 0;
3135
+
3136
+ if (srcX <= -initialWidth || srcX > sourceWidth) {
3137
+ srcX = 0;
3138
+ srcWidth = 0;
3139
+ dstX = 0;
3140
+ dstWidth = 0;
3141
+ } else if (srcX <= 0) {
3142
+ dstX = -srcX;
3143
+ srcX = 0;
3144
+ srcWidth = Math.min(sourceWidth, initialWidth + srcX);
3145
+ dstWidth = srcWidth;
3146
+ } else if (srcX <= sourceWidth) {
3147
+ dstX = 0;
3148
+ srcWidth = Math.min(initialWidth, sourceWidth - srcX);
3149
+ dstWidth = srcWidth;
3150
+ }
3151
+
3152
+ if (srcWidth <= 0 || srcY <= -initialHeight || srcY > sourceHeight) {
3153
+ srcY = 0;
3154
+ srcHeight = 0;
3155
+ dstY = 0;
3156
+ dstHeight = 0;
3157
+ } else if (srcY <= 0) {
3158
+ dstY = -srcY;
3159
+ srcY = 0;
3160
+ srcHeight = Math.min(sourceHeight, initialHeight + srcY);
3161
+ dstHeight = srcHeight;
3162
+ } else if (srcY <= sourceHeight) {
3163
+ dstY = 0;
3164
+ srcHeight = Math.min(initialHeight, sourceHeight - srcY);
3165
+ dstHeight = srcHeight;
3166
+ }
3167
+
3168
+ // All the numerical parameters should be integer for `drawImage`
3169
+ // https://github.com/fengyuanchen/cropper/issues/476
3170
+ var params = [srcX, srcY, srcWidth, srcHeight];
3171
+
3172
+ // Avoid "IndexSizeError"
3173
+ if (dstWidth > 0 && dstHeight > 0) {
3174
+ var scale = width / initialWidth;
3175
+
3176
+ params.push(dstX * scale, dstY * scale, dstWidth * scale, dstHeight * scale);
3177
+ }
3178
+
3179
+ context.drawImage.apply(context, [source].concat(toConsumableArray(params.map(function (param) {
3180
+ return Math.floor(normalizeDecimalNumber(param));
3181
+ }))));
3182
+
3183
+ return canvas;
3184
+ },
3185
+
3186
+
3187
+ /**
3188
+ * Change the aspect ratio of the crop box.
3189
+ * @param {number} aspectRatio - The new aspect ratio.
3190
+ * @returns {Cropper} this
3191
+ */
3192
+ setAspectRatio: function setAspectRatio(aspectRatio) {
3193
+ var options = this.options;
3194
+
3195
+
3196
+ if (!this.disabled && !isUndefined(aspectRatio)) {
3197
+ // 0 -> NaN
3198
+ options.aspectRatio = Math.max(0, aspectRatio) || NaN;
3199
+
3200
+ if (this.ready) {
3201
+ this.initCropBox();
3202
+
3203
+ if (this.cropped) {
3204
+ this.renderCropBox();
3205
+ }
3206
+ }
3207
+ }
3208
+
3209
+ return this;
3210
+ },
3211
+
3212
+
3213
+ /**
3214
+ * Change the drag mode.
3215
+ * @param {string} mode - The new drag mode.
3216
+ * @returns {Cropper} this
3217
+ */
3218
+ setDragMode: function setDragMode(mode) {
3219
+ var options = this.options,
3220
+ dragBox = this.dragBox,
3221
+ face = this.face;
3222
+
3223
+
3224
+ if (this.ready && !this.disabled) {
3225
+ var croppable = mode === DRAG_MODE_CROP;
3226
+ var movable = options.movable && mode === DRAG_MODE_MOVE;
3227
+
3228
+ mode = croppable || movable ? mode : DRAG_MODE_NONE;
3229
+
3230
+ options.dragMode = mode;
3231
+ setData(dragBox, DATA_ACTION, mode);
3232
+ toggleClass(dragBox, CLASS_CROP, croppable);
3233
+ toggleClass(dragBox, CLASS_MOVE, movable);
3234
+
3235
+ if (!options.cropBoxMovable) {
3236
+ // Sync drag mode to crop box when it is not movable
3237
+ setData(face, DATA_ACTION, mode);
3238
+ toggleClass(face, CLASS_CROP, croppable);
3239
+ toggleClass(face, CLASS_MOVE, movable);
3240
+ }
3241
+ }
3242
+
3243
+ return this;
3244
+ }
3245
+ };
3246
+
3247
+ var AnotherCropper = WINDOW.Cropper;
3248
+
3249
+ var Cropper = function () {
3250
+ /**
3251
+ * Create a new Cropper.
3252
+ * @param {Element} element - The target element for cropping.
3253
+ * @param {Object} [options={}] - The configuration options.
3254
+ */
3255
+ function Cropper(element) {
3256
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3257
+ classCallCheck(this, Cropper);
3258
+
3259
+ if (!element || !REGEXP_TAG_NAME.test(element.tagName)) {
3260
+ throw new Error('The first argument is required and must be an <img> or <canvas> element.');
3261
+ }
3262
+
3263
+ this.element = element;
3264
+ this.options = assign({}, DEFAULTS, isPlainObject(options) && options);
3265
+ this.cropped = false;
3266
+ this.disabled = false;
3267
+ this.pointers = {};
3268
+ this.ready = false;
3269
+ this.reloading = false;
3270
+ this.replaced = false;
3271
+ this.sized = false;
3272
+ this.sizing = false;
3273
+ this.init();
3274
+ }
3275
+
3276
+ createClass(Cropper, [{
3277
+ key: 'init',
3278
+ value: function init() {
3279
+ var element = this.element;
3280
+
3281
+ var tagName = element.tagName.toLowerCase();
3282
+ var url = void 0;
3283
+
3284
+ if (getData(element, NAMESPACE)) {
3285
+ return;
3286
+ }
3287
+
3288
+ setData(element, NAMESPACE, this);
3289
+
3290
+ if (tagName === 'img') {
3291
+ this.isImg = true;
3292
+
3293
+ // e.g.: "img/picture.jpg"
3294
+ url = element.getAttribute('src') || '';
3295
+ this.originalUrl = url;
3296
+
3297
+ // Stop when it's a blank image
3298
+ if (!url) {
3299
+ return;
3300
+ }
3301
+
3302
+ // e.g.: "http://example.com/img/picture.jpg"
3303
+ url = element.src;
3304
+ } else if (tagName === 'canvas' && window.HTMLCanvasElement) {
3305
+ url = element.toDataURL();
3306
+ }
3307
+
3308
+ this.load(url);
3309
+ }
3310
+ }, {
3311
+ key: 'load',
3312
+ value: function load(url) {
3313
+ var _this = this;
3314
+
3315
+ if (!url) {
3316
+ return;
3317
+ }
3318
+
3319
+ this.url = url;
3320
+ this.imageData = {};
3321
+
3322
+ var element = this.element,
3323
+ options = this.options;
3324
+
3325
+
3326
+ if (!options.checkOrientation || !window.ArrayBuffer) {
3327
+ this.clone();
3328
+ return;
3329
+ }
3330
+
3331
+ // XMLHttpRequest disallows to open a Data URL in some browsers like IE11 and Safari
3332
+ if (REGEXP_DATA_URL.test(url)) {
3333
+ if (REGEXP_DATA_URL_JPEG.test(url)) {
3334
+ this.read(dataURLToArrayBuffer(url));
3335
+ } else {
3336
+ this.clone();
3337
+ }
3338
+
3339
+ return;
3340
+ }
3341
+
3342
+ var xhr = new XMLHttpRequest();
3343
+
3344
+ this.reloading = true;
3345
+ this.xhr = xhr;
3346
+
3347
+ var done = function done() {
3348
+ _this.reloading = false;
3349
+ _this.xhr = null;
3350
+ };
3351
+
3352
+ xhr.ontimeout = done;
3353
+ xhr.onabort = done;
3354
+ xhr.onerror = function () {
3355
+ done();
3356
+ _this.clone();
3357
+ };
3358
+
3359
+ xhr.onload = function () {
3360
+ done();
3361
+ _this.read(xhr.response);
3362
+ };
3363
+
3364
+ // Bust cache when there is a "crossOrigin" property
3365
+ if (options.checkCrossOrigin && isCrossOriginURL(url) && element.crossOrigin) {
3366
+ url = addTimestamp(url);
3367
+ }
3368
+
3369
+ xhr.open('get', url);
3370
+ xhr.responseType = 'arraybuffer';
3371
+ xhr.withCredentials = element.crossOrigin === 'use-credentials';
3372
+ xhr.send();
3373
+ }
3374
+ }, {
3375
+ key: 'read',
3376
+ value: function read(arrayBuffer) {
3377
+ var options = this.options,
3378
+ imageData = this.imageData;
3379
+
3380
+ var orientation = getOrientation(arrayBuffer);
3381
+ var rotate = 0;
3382
+ var scaleX = 1;
3383
+ var scaleY = 1;
3384
+
3385
+ if (orientation > 1) {
3386
+ this.url = arrayBufferToDataURL(arrayBuffer, 'image/jpeg');
3387
+
3388
+ var _parseOrientation = parseOrientation(orientation);
3389
+
3390
+ rotate = _parseOrientation.rotate;
3391
+ scaleX = _parseOrientation.scaleX;
3392
+ scaleY = _parseOrientation.scaleY;
3393
+ }
3394
+
3395
+ if (options.rotatable) {
3396
+ imageData.rotate = rotate;
3397
+ }
3398
+
3399
+ if (options.scalable) {
3400
+ imageData.scaleX = scaleX;
3401
+ imageData.scaleY = scaleY;
3402
+ }
3403
+
3404
+ this.clone();
3405
+ }
3406
+ }, {
3407
+ key: 'clone',
3408
+ value: function clone() {
3409
+ var element = this.element,
3410
+ url = this.url;
3411
+
3412
+ var crossOrigin = void 0;
3413
+ var crossOriginUrl = void 0;
3414
+
3415
+ if (this.options.checkCrossOrigin && isCrossOriginURL(url)) {
3416
+ crossOrigin = element.crossOrigin;
3417
+
3418
+
3419
+ if (crossOrigin) {
3420
+ crossOriginUrl = url;
3421
+ } else {
3422
+ crossOrigin = 'anonymous';
3423
+
3424
+ // Bust cache when there is not a "crossOrigin" property
3425
+ crossOriginUrl = addTimestamp(url);
3426
+ }
3427
+ }
3428
+
3429
+ this.crossOrigin = crossOrigin;
3430
+ this.crossOriginUrl = crossOriginUrl;
3431
+
3432
+ var image = document.createElement('img');
3433
+
3434
+ if (crossOrigin) {
3435
+ image.crossOrigin = crossOrigin;
3436
+ }
3437
+
3438
+ image.src = crossOriginUrl || url;
3439
+
3440
+ var start = this.start.bind(this);
3441
+ var stop = this.stop.bind(this);
3442
+
3443
+ this.image = image;
3444
+ this.onStart = start;
3445
+ this.onStop = stop;
3446
+
3447
+ if (this.isImg) {
3448
+ if (element.complete) {
3449
+ // start asynchronously to keep `this.cropper` is accessible in `ready` event handler.
3450
+ this.timeout = setTimeout(start, 0);
3451
+ } else {
3452
+ addListener(element, EVENT_LOAD, start, {
3453
+ once: true
3454
+ });
3455
+ }
3456
+ } else {
3457
+ image.onload = start;
3458
+ image.onerror = stop;
3459
+ addClass(image, CLASS_HIDE);
3460
+ element.parentNode.insertBefore(image, element.nextSibling);
3461
+ }
3462
+ }
3463
+ }, {
3464
+ key: 'start',
3465
+ value: function start(event) {
3466
+ var _this2 = this;
3467
+
3468
+ var image = this.isImg ? this.element : this.image;
3469
+
3470
+ if (event) {
3471
+ image.onload = null;
3472
+ image.onerror = null;
3473
+ }
3474
+
3475
+ this.sizing = true;
3476
+
3477
+ var IS_SAFARI = WINDOW.navigator && /(Macintosh|iPhone|iPod|iPad).*AppleWebKit/i.test(WINDOW.navigator.userAgent);
3478
+ var done = function done(naturalWidth, naturalHeight) {
3479
+ assign(_this2.imageData, {
3480
+ naturalWidth: naturalWidth,
3481
+ naturalHeight: naturalHeight,
3482
+ aspectRatio: naturalWidth / naturalHeight
3483
+ });
3484
+ _this2.sizing = false;
3485
+ _this2.sized = true;
3486
+ _this2.build();
3487
+ };
3488
+
3489
+ // Modern browsers (except Safari)
3490
+ if (image.naturalWidth && !IS_SAFARI) {
3491
+ done(image.naturalWidth, image.naturalHeight);
3492
+ return;
3493
+ }
3494
+
3495
+ var sizingImage = document.createElement('img');
3496
+ var body = document.body || document.documentElement;
3497
+
3498
+ this.sizingImage = sizingImage;
3499
+
3500
+ sizingImage.onload = function () {
3501
+ done(sizingImage.width, sizingImage.height);
3502
+
3503
+ if (!IS_SAFARI) {
3504
+ body.removeChild(sizingImage);
3505
+ }
3506
+ };
3507
+
3508
+ sizingImage.src = image.src;
3509
+
3510
+ // iOS Safari will convert the image automatically
3511
+ // with its orientation once append it into DOM (#279)
3512
+ if (!IS_SAFARI) {
3513
+ sizingImage.style.cssText = 'left:0;' + 'max-height:none!important;' + 'max-width:none!important;' + 'min-height:0!important;' + 'min-width:0!important;' + 'opacity:0;' + 'position:absolute;' + 'top:0;' + 'z-index:-1;';
3514
+ body.appendChild(sizingImage);
3515
+ }
3516
+ }
3517
+ }, {
3518
+ key: 'stop',
3519
+ value: function stop() {
3520
+ var image = this.image;
3521
+
3522
+
3523
+ image.onload = null;
3524
+ image.onerror = null;
3525
+ image.parentNode.removeChild(image);
3526
+ this.image = null;
3527
+ }
3528
+ }, {
3529
+ key: 'build',
3530
+ value: function build() {
3531
+ if (!this.sized || this.ready) {
3532
+ return;
3533
+ }
3534
+
3535
+ var element = this.element,
3536
+ options = this.options,
3537
+ image = this.image;
3538
+
3539
+ // Create cropper elements
3540
+
3541
+ var container = element.parentNode;
3542
+ var template = document.createElement('div');
3543
+
3544
+ template.innerHTML = TEMPLATE;
3545
+
3546
+ var cropper = template.querySelector('.' + NAMESPACE + '-container');
3547
+ var canvas = cropper.querySelector('.' + NAMESPACE + '-canvas');
3548
+ var dragBox = cropper.querySelector('.' + NAMESPACE + '-drag-box');
3549
+ var cropBox = cropper.querySelector('.' + NAMESPACE + '-crop-box');
3550
+ var face = cropBox.querySelector('.' + NAMESPACE + '-face');
3551
+
3552
+ this.container = container;
3553
+ this.cropper = cropper;
3554
+ this.canvas = canvas;
3555
+ this.dragBox = dragBox;
3556
+ this.cropBox = cropBox;
3557
+ this.viewBox = cropper.querySelector('.' + NAMESPACE + '-view-box');
3558
+ this.face = face;
3559
+
3560
+ canvas.appendChild(image);
3561
+
3562
+ // Hide the original image
3563
+ addClass(element, CLASS_HIDDEN);
3564
+
3565
+ // Inserts the cropper after to the current image
3566
+ container.insertBefore(cropper, element.nextSibling);
3567
+
3568
+ // Show the image if is hidden
3569
+ if (!this.isImg) {
3570
+ removeClass(image, CLASS_HIDE);
3571
+ }
3572
+
3573
+ this.initPreview();
3574
+ this.bind();
3575
+
3576
+ options.aspectRatio = Math.max(0, options.aspectRatio) || NaN;
3577
+ options.viewMode = Math.max(0, Math.min(3, Math.round(options.viewMode))) || 0;
3578
+
3579
+ addClass(cropBox, CLASS_HIDDEN);
3580
+
3581
+ if (!options.guides) {
3582
+ addClass(cropBox.getElementsByClassName(NAMESPACE + '-dashed'), CLASS_HIDDEN);
3583
+ }
3584
+
3585
+ if (!options.center) {
3586
+ addClass(cropBox.getElementsByClassName(NAMESPACE + '-center'), CLASS_HIDDEN);
3587
+ }
3588
+
3589
+ if (options.background) {
3590
+ addClass(cropper, NAMESPACE + '-bg');
3591
+ }
3592
+
3593
+ if (!options.highlight) {
3594
+ addClass(face, CLASS_INVISIBLE);
3595
+ }
3596
+
3597
+ if (options.cropBoxMovable) {
3598
+ addClass(face, CLASS_MOVE);
3599
+ setData(face, DATA_ACTION, ACTION_ALL);
3600
+ }
3601
+
3602
+ if (!options.cropBoxResizable) {
3603
+ addClass(cropBox.getElementsByClassName(NAMESPACE + '-line'), CLASS_HIDDEN);
3604
+ addClass(cropBox.getElementsByClassName(NAMESPACE + '-point'), CLASS_HIDDEN);
3605
+ }
3606
+
3607
+ this.render();
3608
+ this.ready = true;
3609
+ this.setDragMode(options.dragMode);
3610
+
3611
+ if (options.autoCrop) {
3612
+ this.crop();
3613
+ }
3614
+
3615
+ this.setData(options.data);
3616
+
3617
+ if (isFunction(options.ready)) {
3618
+ addListener(element, EVENT_READY, options.ready, {
3619
+ once: true
3620
+ });
3621
+ }
3622
+
3623
+ dispatchEvent(element, EVENT_READY);
3624
+ }
3625
+ }, {
3626
+ key: 'unbuild',
3627
+ value: function unbuild() {
3628
+ if (!this.ready) {
3629
+ return;
3630
+ }
3631
+
3632
+ this.ready = false;
3633
+ this.unbind();
3634
+ this.resetPreview();
3635
+ this.cropper.parentNode.removeChild(this.cropper);
3636
+ removeClass(this.element, CLASS_HIDDEN);
3637
+ }
3638
+ }, {
3639
+ key: 'uncreate',
3640
+ value: function uncreate() {
3641
+ var element = this.element;
3642
+
3643
+
3644
+ if (this.ready) {
3645
+ this.unbuild();
3646
+ this.ready = false;
3647
+ this.cropped = false;
3648
+ } else if (this.sizing) {
3649
+ this.sizingImage.onload = null;
3650
+ this.sizing = false;
3651
+ this.sized = false;
3652
+ } else if (this.reloading) {
3653
+ this.xhr.abort();
3654
+ } else if (this.isImg) {
3655
+ if (element.complete) {
3656
+ clearTimeout(this.timeout);
3657
+ } else {
3658
+ removeListener(element, EVENT_LOAD, this.onStart);
3659
+ }
3660
+ } else if (this.image) {
3661
+ this.stop();
3662
+ }
3663
+ }
3664
+
3665
+ /**
3666
+ * Get the no conflict cropper class.
3667
+ * @returns {Cropper} The cropper class.
3668
+ */
3669
+
3670
+ }], [{
3671
+ key: 'noConflict',
3672
+ value: function noConflict() {
3673
+ window.Cropper = AnotherCropper;
3674
+ return Cropper;
3675
+ }
3676
+
3677
+ /**
3678
+ * Change the default options.
3679
+ * @param {Object} options - The new default options.
3680
+ */
3681
+
3682
+ }, {
3683
+ key: 'setDefaults',
3684
+ value: function setDefaults(options) {
3685
+ assign(DEFAULTS, isPlainObject(options) && options);
3686
+ }
3687
+ }]);
3688
+ return Cropper;
3689
+ }();
3690
+
3691
+ assign(Cropper.prototype, render, preview, events, handlers, change, methods);
3692
+
3693
+ return Cropper;
3694
+
3695
+ })));