refinerycms-core 4.0.3 → 4.1.0

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