Skip to content

Commit

Permalink
refactor: simplify window reference
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyuanchen committed Oct 18, 2017
1 parent fa03501 commit 4da21d1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 23 deletions.
9 changes: 4 additions & 5 deletions src/js/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import global from './global';

export const WINDOW = typeof window !== 'undefined' ? window : {};
export const NAMESPACE = 'cropper';

// Actions
Expand Down Expand Up @@ -42,9 +41,9 @@ export const EVENT_CROP_START = 'cropstart';
export const EVENT_DBLCLICK = 'dblclick';
export const EVENT_ERROR = 'error';
export const EVENT_LOAD = 'load';
export const EVENT_POINTER_DOWN = global.PointerEvent ? 'pointerdown' : 'touchstart mousedown';
export const EVENT_POINTER_MOVE = global.PointerEvent ? 'pointermove' : 'touchmove mousemove';
export const EVENT_POINTER_UP = global.PointerEvent ? ' pointerup pointercancel' : 'touchend touchcancel mouseup';
export const EVENT_POINTER_DOWN = WINDOW.PointerEvent ? 'pointerdown' : 'touchstart mousedown';
export const EVENT_POINTER_MOVE = WINDOW.PointerEvent ? 'pointermove' : 'touchmove mousemove';
export const EVENT_POINTER_UP = WINDOW.PointerEvent ? ' pointerup pointercancel' : 'touchend touchcancel mouseup';
export const EVENT_READY = 'ready';
export const EVENT_RESIZE = 'resize';
export const EVENT_WHEEL = 'wheel mousewheel DOMMouseScroll';
Expand Down
10 changes: 5 additions & 5 deletions src/js/cropper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import global from './global';
import DEFAULTS from './defaults';
import TEMPLATE from './template';
import render from './render';
Expand All @@ -23,6 +22,7 @@ import {
REGEXP_DATA_URL,
REGEXP_DATA_URL_JPEG,
REGEXP_TAG_NAME,
WINDOW,
} from './constants';
import {
addClass,
Expand All @@ -45,7 +45,7 @@ import {
setData,
} from './utilities';

const AnotherCropper = global.Cropper;
const AnotherCropper = WINDOW.Cropper;

class Cropper {
/**
Expand Down Expand Up @@ -102,7 +102,7 @@ class Cropper {

// e.g.: "http://example.com/img/picture.jpg"
url = element.src;
} else if (tagName === 'canvas' && global.HTMLCanvasElement) {
} else if (tagName === 'canvas' && window.HTMLCanvasElement) {
url = element.toDataURL();
}

Expand All @@ -119,7 +119,7 @@ class Cropper {

const { element, options } = this;

if (!options.checkOrientation || !global.ArrayBuffer) {
if (!options.checkOrientation || !window.ArrayBuffer) {
this.clone();
return;
}
Expand Down Expand Up @@ -402,7 +402,7 @@ class Cropper {
* @returns {Cropper} The cropper class.
*/
static noConflict() {
global.Cropper = AnotherCropper;
window.Cropper = AnotherCropper;
return Cropper;
}

Expand Down
5 changes: 2 additions & 3 deletions src/js/events.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import global from './global';
import {
EVENT_CROP,
EVENT_CROP_END,
Expand Down Expand Up @@ -57,7 +56,7 @@ export default {
addListener(document, EVENT_POINTER_UP, (this.onCropEnd = proxy(this.cropEnd, this)));

if (options.responsive) {
addListener(global, EVENT_RESIZE, (this.onResize = proxy(this.resize, this)));
addListener(window, EVENT_RESIZE, (this.onResize = proxy(this.resize, this)));
}
},

Expand Down Expand Up @@ -98,7 +97,7 @@ export default {
removeListener(document, EVENT_POINTER_UP, this.onCropEnd);

if (options.responsive) {
removeListener(global, EVENT_RESIZE, this.onResize);
removeListener(window, EVENT_RESIZE, this.onResize);
}
},
};
1 change: 0 additions & 1 deletion src/js/global.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/js/methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import global from './global';
import {
CLASS_CROP,
CLASS_DISABLED,
Expand Down Expand Up @@ -639,7 +638,7 @@ export default {
* @returns {HTMLCanvasElement} - The result canvas.
*/
getCroppedCanvas(options = {}) {
if (!this.ready || !global.HTMLCanvasElement) {
if (!this.ready || !window.HTMLCanvasElement) {
return null;
}

Expand Down
16 changes: 9 additions & 7 deletions src/js/utilities.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import global from './global';
import {
WINDOW,
} from './constants';

/**
* Check if the given value is not a number.
*/
export const isNaN = Number.isNaN || global.isNaN;
export const isNaN = Number.isNaN || WINDOW.isNaN;

/**
* Check if the given value is a number.
Expand Down Expand Up @@ -442,10 +444,10 @@ export function getOffset(element) {

return {
left: box.left + (
(global.scrollX || (doc && doc.scrollLeft) || 0) - ((doc && doc.clientLeft) || 0)
(window.scrollX || (doc && doc.scrollLeft) || 0) - ((doc && doc.clientLeft) || 0)
),
top: box.top + (
(global.scrollY || (doc && doc.scrollTop) || 0) - ((doc && doc.clientTop) || 0)
(window.scrollY || (doc && doc.scrollTop) || 0) - ((doc && doc.clientTop) || 0)
),
};
}
Expand All @@ -460,7 +462,7 @@ export function empty(element) {
}
}

const { location } = global;
const { location } = WINDOW;
const REGEXP_ORIGINS = /^(https?:)\/\/([^:/?#]+):?(\d*)/i;

/**
Expand Down Expand Up @@ -533,7 +535,7 @@ export function getTransforms({
};
}

const { navigator } = global;
const { navigator } = WINDOW;
const IS_SAFARI_OR_UIWEBVIEW = navigator && /(Macintosh|iPhone|iPod|iPad).*AppleWebKit/i.test(navigator.userAgent);

/**
Expand Down Expand Up @@ -637,7 +639,7 @@ export function getPointersCenter(pointers) {
/**
* Check if the given value is a finite number.
*/
export const isFinite = Number.isFinite || global.isFinite;
export const isFinite = Number.isFinite || WINDOW.isFinite;

/**
* Get the max sizes in a rectangle under the given aspect ratio.
Expand Down

0 comments on commit 4da21d1

Please sign in to comment.