Skip to content

Commit

Permalink
Set up handler hand off + class toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
tristen committed Mar 27, 2015
1 parent 8955428 commit 643a7b5
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 82 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.js]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
31 changes: 31 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"rules": {
"space-after-function-name": 2,
"space-before-blocks": 2,
"space-after-keywords": 2,
"space-unary-ops": 2,
"no-use-before-define": [2, "nofunc"],
"camelcase": 0,
"comma-style": 2,
"eqeqeq": 0,
"new-cap": 2,
"no-multi-spaces": 2,
"brace-style": 2,
"no-underscore-dangle": [0],
"no-self-compare": 2,
"no-void": 2,
"wrap-iife": 2,
"no-eq-null": 2,
"quotes": [0],
"curly": 0,
"dot-notation": [0],
"no-shadow": 0
},
"env": {
"node": true,
"browser": true
},
"globals": {
"mapboxgl": true
}
}
3 changes: 3 additions & 0 deletions dist/mapboxgl.draw.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@
.mapboxgl-ctrl-draw-btn.circle:before { content:'\e602'; }
.mapboxgl-ctrl-draw-btn.square:before { content:'\e603'; }
.mapboxgl-ctrl-draw-btn.line:before { content:'\e604'; }

.mapboxgl-ctrl-draw-btn.active,
.mapboxgl-ctrl-draw-btn.active:hover { background-color:rgba(0,0,0,0.10); }
183 changes: 141 additions & 42 deletions dist/mapboxgl.draw.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @class mapbox.Draw
*
* @param {Object} options
* @param {String} [options.position=topright] A string indicating the control's position on the map. Options are `topright`, `topleft`, `bottomright`, `bottomleft`
* @param {String} [options.position=top-right] A string indicating the control's position on the map. Options are `topright`, `topleft`, `bottomright`, `bottomleft`
* @returns {Draw} `this`
* @example
* var map = new mapboxgl.Map({
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"url": "git://github.com/mapbox/gl-draw.git"
},
"keywords": [
"gl",
"draw",
"webgl",
"mapbox",
"draw",
"drawing"
Expand Down
21 changes: 0 additions & 21 deletions src/dom.js

This file was deleted.

75 changes: 59 additions & 16 deletions src/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

var extend = require('xtend');
var Control = require('./control');
var DOM = require('./dom');
var util = require('./util');
var DOM = util.DOM;

// Control handlers
var Shape = require('./handlers/shape');
var Line = require('./handlers/line');
var Circle = require('./handlers/circle');
var Square = require('./handlers/square');
var Marker = require('./handlers/marker');

module.exports = Draw;

Expand All @@ -13,29 +20,65 @@ function Draw(options) {

Draw.prototype = extend(Control, {
options: {
position: 'top-left'
position: 'top-left',
controls: {
marker: true,
line: true,
shape: true,
square: true,
circle: true
}
},

onAdd: function(map) {
var className = 'mapboxgl-ctrl';
var container = this._container = DOM.create('div', className + '-group', map.getContainer());
this._shapeButton = this._createButton('mapboxgl-ctrl-draw-btn shape', 'Shape tool', this._drawShape.bind(map));
this._lineButton = this._createButton('mapboxgl-ctrl-draw-btn line', 'Line tool', this._drawLine.bind(map));
this._circleButton = this._createButton('mapboxgl-ctrl-draw-btn circle', 'Circle tool', this._drawCircle.bind(map));
this._squareButton = this._createButton('mapboxgl-ctrl-draw-btn square', 'Rectangle tool', this._drawSquare.bind(map));
this._markerButton = this._createButton('mapboxgl-ctrl-draw-btn marker', 'Marker tool', this._drawMarker.bind(map));
var controlClass = this._controlClass = 'mapboxgl-ctrl-draw-btn';
var container = this._container = DOM.create('div', 'mapboxgl-ctrl-group', map.getContainer());
var controls = this.options.controls;

if (controls.shape) this._createButton(controlClass + ' shape', 'Shape tool', this._drawShape.bind(map));
if (controls.line) this._createButton(controlClass + ' line', 'Line tool', this._drawLine.bind(map));
if (controls.circle) this._createButton(controlClass + ' circle', 'Circle tool', this._drawCircle.bind(map));
if (controls.square) this._createButton(controlClass + ' square', 'Rectangle tool', this._drawSquare.bind(map));
if (controls.marker) this._createButton(controlClass + ' marker', 'Marker tool', this._drawMarker.bind(map));
return container;
},

_drawShape: function(map) {},
_drawLine: function(map) {},
_drawCircle: function(map) {},
_drawSquare: function(map) {},
_drawMarker: function(map) {},
_drawShape: function() {
new Shape(this);
},

_drawLine: function() {
new Line(this);
},

_drawCircle: function() {
new Circle(this);
},

_drawSquare: function() {
new Square(this);
},

_drawMarker: function() {
new Marker(this);
},

_createButton: function(className, title, fn) {
var a = DOM.create('button', className, this._container, {title: title});
a.addEventListener('click', function() { fn(); });
var a = DOM.create('button', className, this._container, {
title: title
});

var controlClass = this._controlClass;
a.addEventListener('click', function(e) {
e.preventDefault();

if (!this.classList.contains('active')) {
DOM.removeClass(document.querySelectorAll('.' + controlClass), 'active');
this.classList.add('active');
fn();
}
});

return a;
}
});
7 changes: 7 additions & 0 deletions src/handlers/circle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = Circle;

function Circle(map) {
console.log(map);
}
7 changes: 7 additions & 0 deletions src/handlers/line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = Line;

function Line(map) {
console.log(map);
}
7 changes: 7 additions & 0 deletions src/handlers/marker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = Marker;

function Marker(map) {
console.log(map);
}
7 changes: 7 additions & 0 deletions src/handlers/shape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = Shape;

function Shape(map) {
console.log(map);
}
7 changes: 7 additions & 0 deletions src/handlers/square.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = Square;

function Square(map) {
console.log(map);
}
34 changes: 34 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,39 @@ module.exports = {
obj.options[i] = options[i];
}
return obj.options;
},

DOM: {
/* Builds DOM elements
*
* @param {String} tag Element name
* @param {String} [className]
* @param {Object} [container] DOM element to append to
* @param {Object} [attrbutes] Object containing attributes to apply to an
* element. Attribute name corresponds to the key.
* @returns {el} The dom element
*/
create: function(tag, className, container, attributes) {
var el = document.createElement(tag);
if (className) el.className = className;
if (attributes) {
for (var key in attributes) {
el.setAttribute(key, attributes[key]);
}
}
if (container) container.appendChild(el);
return el;
},

/* Removes classes from an array of DOM elements
*
* @param {HTMLElement} elements
* @param {String} klass
*/
removeClass: function(elements, klass) {
Array.prototype.forEach.call(elements, function(el) {
el.classList.remove(klass);
});
}
}
};

0 comments on commit 643a7b5

Please sign in to comment.