Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better checks for external modules. #696

Merged
merged 5 commits into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/d3/d3Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,11 @@ registerRenderer('d3', d3Renderer);
* @returns {boolean} true if available.
*/
d3Renderer.supported = function () {
return !!__webpack_modules__[require.resolveWeak('d3')]; // eslint-disable-line
if (!__webpack_modules__[require.resolveWeak('d3')]) { // eslint-disable-line
return false;
}
var d3 = require('d3');
return d3 !== undefined;
};

/**
Expand Down
40 changes: 21 additions & 19 deletions src/mapInteractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,33 +751,35 @@ var mapInteractor = function (args) {
(usedInputs.pan || usedInputs.rotate) &&
__webpack_modules__[require.resolveWeak('hammerjs')]) { // eslint-disable-line
var Hammer = require('hammerjs');
var recog = [],
touchEvents = ['hammer.input'];
if (usedInputs.rotate) {
recog.push([Hammer.Rotate, {enable: true}]);
touchEvents = touchEvents.concat(['rotatestart', 'rotateend', 'rotatemove']);
}
if (usedInputs.pan) {
recog.push([Hammer.Pan, {direction: Hammer.DIRECTION_ALL}]);
touchEvents = touchEvents.concat(['panstart', 'panend', 'panmove']);
if (Hammer !== undefined) {
var recog = [],
touchEvents = ['hammer.input'];
if (usedInputs.rotate) {
recog.push([Hammer.Rotate, {enable: true}]);
touchEvents = touchEvents.concat(['rotatestart', 'rotateend', 'rotatemove']);
}
if (usedInputs.pan) {
recog.push([Hammer.Pan, {direction: Hammer.DIRECTION_ALL}]);
touchEvents = touchEvents.concat(['panstart', 'panend', 'panmove']);
}
var hammerParams = {recognizers: recog, preventDefault: true};
m_touchHandler = {
manager: new Hammer.Manager($node[0], hammerParams),
touchSupport: m_this.hasTouchSupport(),
lastTime: 0
};
touchEvents.forEach(function (touchEvent) {
m_touchHandler.manager.on(touchEvent, m_this._handleTouch);
});
}
var hammerParams = {recognizers: recog, preventDefault: true};
m_touchHandler = {
manager: new Hammer.Manager($node[0], hammerParams),
touchSupport: m_this.hasTouchSupport(),
lastTime: 0
};
touchEvents.forEach(function (touchEvent) {
m_touchHandler.manager.on(touchEvent, m_this._handleTouch);
});
}

return m_this;
};

////////////////////////////////////////////////////////////////////////////
/**
* Disiconnects events to a map. If the map is not set, then this does
* Disconnects events to a map. If the map is not set, then this does
* nothing.
* @returns {geo.mapInteractor}
*/
Expand Down
35 changes: 35 additions & 0 deletions tests/cases/mapInteractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1819,3 +1819,38 @@ describe('mapInteractor', function () {
expect(map.info.zoom).toBe(2);
});
});
describe('Optional Dependencies', function () {
var $ = require('jquery');
var geo = require('../test-utils').geo;

beforeEach(function () {
// create a new div
$('<div id="mapNode1" class="mapNode testNode"></div>')
.css({width: '800px', height: '600px'}).appendTo('body');
});

afterEach(function () {
// delete the div and clean up lingering event handlers
$('.testNode').remove();
$(document).off('.geojs');
});

it('test missing Hammer library', function () {
var old = __webpack_modules__[require.resolveWeak('hammerjs')]; // eslint-disable-line
__webpack_modules__[require.resolveWeak('hammerjs')] = null; // eslint-disable-line
var map = geo.map({node: '#mapNode1'}),
interactor = map.interactor();

expect(interactor.hasTouchSupport()).toBe(true);
expect(map.center().x).toBeCloseTo(0);
// We shouldn't process pan touch events
interactor.simulateEvent(
'panstart', {touch: true, center: {x: 20, y: 20}});
interactor.simulateEvent(
'panmove', {touch: true, center: {x: 30, y: 20}});
interactor.simulateEvent(
'panend', {touch: true, center: {x: 40, y: 20}});
expect(map.center().x).toBeCloseTo(0);
__webpack_modules__[require.resolveWeak('hammerjs')] = old; // eslint-disable-line
});
});
6 changes: 6 additions & 0 deletions tests/cases/renderers.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ describe('renderers', function () {

expect(geo.checkRenderer('d3')).toBe('d3');

var oldd3 = __webpack_modules__[require.resolveWeak('d3')]; // eslint-disable-line
__webpack_modules__[require.resolveWeak('d3')] = null; // eslint-disable-line
expect(geo.checkRenderer('d3')).toBe(null);
__webpack_modules__[require.resolveWeak('d3')] = oldd3; // eslint-disable-line
expect(geo.checkRenderer('d3')).toBe('d3');

mockVGLRenderer(false);
expect(geo.checkRenderer('vgl')).toBe(null);
restoreVGLRenderer();
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = {
},
externals: {
d3: 'd3',
hammerjs: 'hammerjs'
hammerjs: 'Hammer'
},
plugins: [
define_plugin,
Expand Down