code stringlengths 2 1.05M |
|---|
/** @namespace */
var geo = {};
window.geo = geo;
geo.renderers = {};
geo.features = {};
geo.fileReaders = {};
geo.rendererLayerAdjustments = {};
//////////////////////////////////////////////////////////////////////////////
/**
* Convenient function to define JS inheritance
*/
//////////////////////////////////////////////////////////////////////////////
geo.inherit = function (C, P) {
'use strict';
var F = inherit.func();
F.prototype = P.prototype;
C.prototype = new F();
C.prototype.constructor = C;
};
geo.inherit.func = function () {
'use strict';
return function () {};
};
// Should get rid of this at some point.
window.inherit = geo.inherit;
//////////////////////////////////////////////////////////////////////////////
/**
* This is a helper method for generating new-style subclasses as an
* alternative to the older `inherit` classes. Note: these classes
* intentionally don't support constructors for the moment. We may
* consider alternate semantics such as ES6 classes or stampit
* (https://github.com/stampit-org/stampit) as an alternative to handling
* private variables.
*
* @param {object?} props Instance methods and properties to add/override
* @returns {object} The inherited object
*/
//////////////////////////////////////////////////////////////////////////////
geo.extend = function (props) {
'use strict';
var child = Object.create(this.prototype);
$.extend(child.prototype, props || {});
return child;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Register a new file reader type
*/
//////////////////////////////////////////////////////////////////////////////
geo.registerFileReader = function (name, func) {
'use strict';
if (geo.fileReaders === undefined) {
geo.fileReaders = {};
}
geo.fileReaders[name] = func;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new file reader
*/
//////////////////////////////////////////////////////////////////////////////
geo.createFileReader = function (name, opts) {
'use strict';
if (geo.fileReaders.hasOwnProperty(name)) {
return geo.fileReaders[name](opts);
}
return null;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Register a new renderer type
*/
//////////////////////////////////////////////////////////////////////////////
geo.registerRenderer = function (name, func) {
'use strict';
if (geo.renderers === undefined) {
geo.renderers = {};
}
geo.renderers[name] = func;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create new instance of the renderer
*/
//////////////////////////////////////////////////////////////////////////////
geo.createRenderer = function (name, layer, canvas, options) {
'use strict';
if (geo.renderers.hasOwnProperty(name)) {
var ren = geo.renderers[name](
{layer: layer, canvas: canvas, options: options}
);
ren._init();
return ren;
}
return null;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Check if the named renderer is supported. If not, display a warning and get
* the name of a fallback renderer. Ideally, we would pass a list of desired
* features, and, if the renderer is unavailable, this would choose a fallback
* that would support those features.
*
* @params {string|null} name name of the desired renderer
* @params {boolean} noFallack if true, don't recommend a fallback
* @return {string|null|false} the name of the renderer that should be used
* of false if no valid renderer can be determined.
*/
//////////////////////////////////////////////////////////////////////////////
geo.checkRenderer = function (name, noFallback) {
'use strict';
if (name === null) {
return name;
}
if (geo.renderers.hasOwnProperty(name)) {
var ren = geo.renderers[name];
if (!ren.supported || ren.supported()) {
return name;
}
if (!ren.fallback || noFallback) {
return false;
}
var fallback = geo.checkRenderer(ren.fallback(), true);
if (fallback !== false) {
console.warn(name + ' renderer is unavailable, using ' + fallback +
' renderer instead');
}
return fallback;
}
return false;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Register a new feature type
*/
//////////////////////////////////////////////////////////////////////////////
geo.registerFeature = function (category, name, func) {
'use strict';
if (geo.features === undefined) {
geo.features = {};
}
if (!(category in geo.features)) {
geo.features[category] = {};
}
// TODO Add warning if the name already exists
geo.features[category][name] = func;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create new instance of the renderer
*/
//////////////////////////////////////////////////////////////////////////////
geo.createFeature = function (name, layer, renderer, arg) {
'use strict';
var category = renderer.api(),
options = {'layer': layer, 'renderer': renderer};
if (category in geo.features && name in geo.features[category]) {
if (arg !== undefined) {
$.extend(true, options, arg);
}
var feature = geo.features[category][name](options);
layer.gcs = function () {
return layer.map().gcs();
};
return feature;
}
return null;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Register a layer adjustment.
*/
//////////////////////////////////////////////////////////////////////////////
geo.registerLayerAdjustment = function (category, name, func) {
'use strict';
if (geo.rendererLayerAdjustments === undefined) {
geo.rendererLayerAdjustments = {};
}
if (!(category in geo.rendererLayerAdjustments)) {
geo.rendererLayerAdjustments[category] = {};
}
// TODO Add warning if the name already exists
geo.rendererLayerAdjustments[category][name] = func;
};
//////////////////////////////////////////////////////////////////////////////
/**
* If a layer needs to be adjusted based on the renderer, call the function
* that adjusts it.
*
* @param {string} name Name of the layer.
* @param {object} layer Instantiated layer object.
*/
//////////////////////////////////////////////////////////////////////////////
geo.adjustLayerForRenderer = function (name, layer) {
'use strict';
var rendererName = layer.rendererName();
if (rendererName) {
if (geo.rendererLayerAdjustments &&
geo.rendererLayerAdjustments[rendererName] &&
geo.rendererLayerAdjustments[rendererName][name]) {
geo.rendererLayerAdjustments[rendererName][name].apply(layer);
}
}
};
//////////////////////////////////////////////////////////////////////////////
/**
* Register a new layer type
*/
//////////////////////////////////////////////////////////////////////////////
geo.registerLayer = function (name, func) {
'use strict';
if (geo.layers === undefined) {
geo.layers = {};
}
geo.layers[name] = func;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create new instance of the layer
*/
//////////////////////////////////////////////////////////////////////////////
geo.createLayer = function (name, map, arg) {
'use strict';
/// Default renderer is vgl
var options = {'map': map, 'renderer': 'vgl'},
layer = null;
if (name in geo.layers) {
if (arg !== undefined) {
$.extend(true, options, arg);
}
layer = geo.layers[name](options);
layer._init();
return layer;
} else {
return null;
}
};
//////////////////////////////////////////////////////////////////////////////
/**
* Register a new widget type
*/
//////////////////////////////////////////////////////////////////////////////
geo.registerWidget = function (category, name, func) {
'use strict';
if (geo.widgets === undefined) {
geo.widgets = {};
}
if (!(category in geo.widgets)) {
geo.widgets[category] = {};
}
// TODO Add warning if the name already exists
geo.widgets[category][name] = func;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create new instance of the widget
*/
//////////////////////////////////////////////////////////////////////////////
geo.createWidget = function (name, layer, arg) {
'use strict';
var options = {
layer: layer
};
if (name in geo.widgets.dom) {
if (arg !== undefined) {
$.extend(true, options, arg);
}
return geo.widgets.dom[name](options);
}
throw new Error('Cannot create unknown widget ' + name);
};
// Add a polyfill for window.requestAnimationFrame.
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function (func) {
'use strict';
window.setTimeout(func, 15);
};
}
// Add a polyfill for Math.log2
if (!Math.log2) {
Math.log2 = function () {
'use strict';
return Math.log.apply(Math, arguments) / Math.LN2;
};
}
// Add a polyfill for Math.sinh
Math.sinh = Math.sinh || function (x) {
'use strict';
var y = Math.exp(x);
return (y - 1 / y) / 2;
};
/*global geo*/
geo.version = '0.8.0';
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module unless amdModuleId is set
define('vgl', [], function () {
return (root['vgl'] = factory());
});
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
root['vgl'] = factory();
}
}(this, function () {
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl: true, ogs: true, inherit*/
/*exported vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
if (typeof ogs === 'undefined') {
var ogs = {};
}
//////////////////////////////////////////////////////////////////////////////
/**
* Create namespace for the given name
*
* @param ns_string
* @returns {*|{}}
*/
//////////////////////////////////////////////////////////////////////////////
ogs.namespace = function (ns_string) {
'use strict';
var parts = ns_string.split('.'), parent = ogs, i;
// strip redundant leading global
if (parts[0] === 'ogs') {
parts = parts.slice(1);
}
for (i = 0; i < parts.length; i += 1) {
// create a property if it doesn't exist
if (typeof parent[parts[i]] === 'undefined') {
parent[parts[i]] = {};
}
parent = parent[parts[i]];
}
return parent;
};
/** vgl namespace */
var vgl = ogs.namespace('gl');
//////////////////////////////////////////////////////////////////////////////
/**
* Convenient function to define JS inheritance
*
* @param C
* @param P
*/
//////////////////////////////////////////////////////////////////////////////
function inherit(C, P) {
'use strict';
var F = function () {
};
F.prototype = P.prototype;
C.prototype = new F();
C.uber = P.prototype;
C.prototype.constructor = C;
}
//////////////////////////////////////////////////////////////////////////////
/**
* Convenient function to get size of an object
*
* @param obj
* @returns {number} *
*/
//////////////////////////////////////////////////////////////////////////////
Object.size = function (obj) {
'use strict';
var size = 0, key = null;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
size += 1;
}
}
return size;
};
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Wrap GL enums. Currently to get values of the enums we need to create
* or access the context.
*
* Using enums from here:
* https://github.com/toji/dart-gl-enums/blob/master/lib/gl_enums.dart
*
* @class
*/
//////////////////////////////////////////////////////////////////////////////
vgl.GL = {
ACTIVE_ATTRIBUTES : 0x8B89,
ACTIVE_TEXTURE : 0x84E0,
ACTIVE_UNIFORMS : 0x8B86,
ALIASED_LINE_WIDTH_RANGE : 0x846E,
ALIASED_POINT_SIZE_RANGE : 0x846D,
ALPHA : 0x1906,
ALPHA_BITS : 0x0D55,
ALWAYS : 0x0207,
ARRAY_BUFFER : 0x8892,
ARRAY_BUFFER_BINDING : 0x8894,
ATTACHED_SHADERS : 0x8B85,
BACK : 0x0405,
BLEND : 0x0BE2,
BLEND_COLOR : 0x8005,
BLEND_DST_ALPHA : 0x80CA,
BLEND_DST_RGB : 0x80C8,
BLEND_EQUATION : 0x8009,
BLEND_EQUATION_ALPHA : 0x883D,
BLEND_EQUATION_RGB : 0x8009,
BLEND_SRC_ALPHA : 0x80CB,
BLEND_SRC_RGB : 0x80C9,
BLUE_BITS : 0x0D54,
BOOL : 0x8B56,
BOOL_VEC2 : 0x8B57,
BOOL_VEC3 : 0x8B58,
BOOL_VEC4 : 0x8B59,
BROWSER_DEFAULT_WEBGL : 0x9244,
BUFFER_SIZE : 0x8764,
BUFFER_USAGE : 0x8765,
BYTE : 0x1400,
CCW : 0x0901,
CLAMP_TO_EDGE : 0x812F,
COLOR_ATTACHMENT0 : 0x8CE0,
COLOR_BUFFER_BIT : 0x00004000,
COLOR_CLEAR_VALUE : 0x0C22,
COLOR_WRITEMASK : 0x0C23,
COMPILE_STATUS : 0x8B81,
COMPRESSED_TEXTURE_FORMATS : 0x86A3,
CONSTANT_ALPHA : 0x8003,
CONSTANT_COLOR : 0x8001,
CONTEXT_LOST_WEBGL : 0x9242,
CULL_FACE : 0x0B44,
CULL_FACE_MODE : 0x0B45,
CURRENT_PROGRAM : 0x8B8D,
CURRENT_VERTEX_ATTRIB : 0x8626,
CW : 0x0900,
DECR : 0x1E03,
DECR_WRAP : 0x8508,
DELETE_STATUS : 0x8B80,
DEPTH_ATTACHMENT : 0x8D00,
DEPTH_BITS : 0x0D56,
DEPTH_BUFFER_BIT : 0x00000100,
DEPTH_CLEAR_VALUE : 0x0B73,
DEPTH_COMPONENT : 0x1902,
DEPTH_COMPONENT16 : 0x81A5,
DEPTH_FUNC : 0x0B74,
DEPTH_RANGE : 0x0B70,
DEPTH_STENCIL : 0x84F9,
DEPTH_STENCIL_ATTACHMENT : 0x821A,
DEPTH_TEST : 0x0B71,
DEPTH_WRITEMASK : 0x0B72,
DITHER : 0x0BD0,
DONT_CARE : 0x1100,
DST_ALPHA : 0x0304,
DST_COLOR : 0x0306,
DYNAMIC_DRAW : 0x88E8,
ELEMENT_ARRAY_BUFFER : 0x8893,
ELEMENT_ARRAY_BUFFER_BINDING : 0x8895,
EQUAL : 0x0202,
FASTEST : 0x1101,
FLOAT : 0x1406,
FLOAT_MAT2 : 0x8B5A,
FLOAT_MAT3 : 0x8B5B,
FLOAT_MAT4 : 0x8B5C,
FLOAT_VEC2 : 0x8B50,
FLOAT_VEC3 : 0x8B51,
FLOAT_VEC4 : 0x8B52,
FRAGMENT_SHADER : 0x8B30,
FRAMEBUFFER : 0x8D40,
FRAMEBUFFER_ATTACHMENT_OBJECT_NAME : 0x8CD1,
FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE : 0x8CD0,
FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE : 0x8CD3,
FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL : 0x8CD2,
FRAMEBUFFER_BINDING : 0x8CA6,
FRAMEBUFFER_COMPLETE : 0x8CD5,
FRAMEBUFFER_INCOMPLETE_ATTACHMENT : 0x8CD6,
FRAMEBUFFER_INCOMPLETE_DIMENSIONS : 0x8CD9,
FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT : 0x8CD7,
FRAMEBUFFER_UNSUPPORTED : 0x8CDD,
FRONT : 0x0404,
FRONT_AND_BACK : 0x0408,
FRONT_FACE : 0x0B46,
FUNC_ADD : 0x8006,
FUNC_REVERSE_SUBTRACT : 0x800B,
FUNC_SUBTRACT : 0x800A,
GENERATE_MIPMAP_HINT : 0x8192,
GEQUAL : 0x0206,
GREATER : 0x0204,
GREEN_BITS : 0x0D53,
HIGH_FLOAT : 0x8DF2,
HIGH_INT : 0x8DF5,
INCR : 0x1E02,
INCR_WRAP : 0x8507,
INT : 0x1404,
INT_VEC2 : 0x8B53,
INT_VEC3 : 0x8B54,
INT_VEC4 : 0x8B55,
INVALID_ENUM : 0x0500,
INVALID_FRAMEBUFFER_OPERATION : 0x0506,
INVALID_OPERATION : 0x0502,
INVALID_VALUE : 0x0501,
INVERT : 0x150A,
KEEP : 0x1E00,
LEQUAL : 0x0203,
LESS : 0x0201,
LINEAR : 0x2601,
LINEAR_MIPMAP_LINEAR : 0x2703,
LINEAR_MIPMAP_NEAREST : 0x2701,
LINES : 0x0001,
LINE_LOOP : 0x0002,
LINE_STRIP : 0x0003,
LINE_WIDTH : 0x0B21,
LINK_STATUS : 0x8B82,
LOW_FLOAT : 0x8DF0,
LOW_INT : 0x8DF3,
LUMINANCE : 0x1909,
LUMINANCE_ALPHA : 0x190A,
MAX_COMBINED_TEXTURE_IMAGE_UNITS : 0x8B4D,
MAX_CUBE_MAP_TEXTURE_SIZE : 0x851C,
MAX_FRAGMENT_UNIFORM_VECTORS : 0x8DFD,
MAX_RENDERBUFFER_SIZE : 0x84E8,
MAX_TEXTURE_IMAGE_UNITS : 0x8872,
MAX_TEXTURE_SIZE : 0x0D33,
MAX_VARYING_VECTORS : 0x8DFC,
MAX_VERTEX_ATTRIBS : 0x8869,
MAX_VERTEX_TEXTURE_IMAGE_UNITS : 0x8B4C,
MAX_VERTEX_UNIFORM_VECTORS : 0x8DFB,
MAX_VIEWPORT_DIMS : 0x0D3A,
MEDIUM_FLOAT : 0x8DF1,
MEDIUM_INT : 0x8DF4,
MIRRORED_REPEAT : 0x8370,
NEAREST : 0x2600,
NEAREST_MIPMAP_LINEAR : 0x2702,
NEAREST_MIPMAP_NEAREST : 0x2700,
NEVER : 0x0200,
NICEST : 0x1102,
NONE : 0,
NOTEQUAL : 0x0205,
NO_ERROR : 0,
ONE : 1,
ONE_MINUS_CONSTANT_ALPHA : 0x8004,
ONE_MINUS_CONSTANT_COLOR : 0x8002,
ONE_MINUS_DST_ALPHA : 0x0305,
ONE_MINUS_DST_COLOR : 0x0307,
ONE_MINUS_SRC_ALPHA : 0x0303,
ONE_MINUS_SRC_COLOR : 0x0301,
OUT_OF_MEMORY : 0x0505,
PACK_ALIGNMENT : 0x0D05,
POINTS : 0x0000,
POLYGON_OFFSET_FACTOR : 0x8038,
POLYGON_OFFSET_FILL : 0x8037,
POLYGON_OFFSET_UNITS : 0x2A00,
RED_BITS : 0x0D52,
RENDERBUFFER : 0x8D41,
RENDERBUFFER_ALPHA_SIZE : 0x8D53,
RENDERBUFFER_BINDING : 0x8CA7,
RENDERBUFFER_BLUE_SIZE : 0x8D52,
RENDERBUFFER_DEPTH_SIZE : 0x8D54,
RENDERBUFFER_GREEN_SIZE : 0x8D51,
RENDERBUFFER_HEIGHT : 0x8D43,
RENDERBUFFER_INTERNAL_FORMAT : 0x8D44,
RENDERBUFFER_RED_SIZE : 0x8D50,
RENDERBUFFER_STENCIL_SIZE : 0x8D55,
RENDERBUFFER_WIDTH : 0x8D42,
RENDERER : 0x1F01,
REPEAT : 0x2901,
REPLACE : 0x1E01,
RGB : 0x1907,
RGB565 : 0x8D62,
RGB5_A1 : 0x8057,
RGBA : 0x1908,
RGBA4 : 0x8056,
SAMPLER_2D : 0x8B5E,
SAMPLER_CUBE : 0x8B60,
SAMPLES : 0x80A9,
SAMPLE_ALPHA_TO_COVERAGE : 0x809E,
SAMPLE_BUFFERS : 0x80A8,
SAMPLE_COVERAGE : 0x80A0,
SAMPLE_COVERAGE_INVERT : 0x80AB,
SAMPLE_COVERAGE_VALUE : 0x80AA,
SCISSOR_BOX : 0x0C10,
SCISSOR_TEST : 0x0C11,
SHADER_TYPE : 0x8B4F,
SHADING_LANGUAGE_VERSION : 0x8B8C,
SHORT : 0x1402,
SRC_ALPHA : 0x0302,
SRC_ALPHA_SATURATE : 0x0308,
SRC_COLOR : 0x0300,
STATIC_DRAW : 0x88E4,
STENCIL_ATTACHMENT : 0x8D20,
STENCIL_BACK_FAIL : 0x8801,
STENCIL_BACK_FUNC : 0x8800,
STENCIL_BACK_PASS_DEPTH_FAIL : 0x8802,
STENCIL_BACK_PASS_DEPTH_PASS : 0x8803,
STENCIL_BACK_REF : 0x8CA3,
STENCIL_BACK_VALUE_MASK : 0x8CA4,
STENCIL_BACK_WRITEMASK : 0x8CA5,
STENCIL_BITS : 0x0D57,
STENCIL_BUFFER_BIT : 0x00000400,
STENCIL_CLEAR_VALUE : 0x0B91,
STENCIL_FAIL : 0x0B94,
STENCIL_FUNC : 0x0B92,
STENCIL_INDEX : 0x1901,
STENCIL_INDEX8 : 0x8D48,
STENCIL_PASS_DEPTH_FAIL : 0x0B95,
STENCIL_PASS_DEPTH_PASS : 0x0B96,
STENCIL_REF : 0x0B97,
STENCIL_TEST : 0x0B90,
STENCIL_VALUE_MASK : 0x0B93,
STENCIL_WRITEMASK : 0x0B98,
STREAM_DRAW : 0x88E0,
SUBPIXEL_BITS : 0x0D50,
TEXTURE : 0x1702,
TEXTURE0 : 0x84C0,
TEXTURE1 : 0x84C1,
TEXTURE10 : 0x84CA,
TEXTURE11 : 0x84CB,
TEXTURE12 : 0x84CC,
TEXTURE13 : 0x84CD,
TEXTURE14 : 0x84CE,
TEXTURE15 : 0x84CF,
TEXTURE16 : 0x84D0,
TEXTURE17 : 0x84D1,
TEXTURE18 : 0x84D2,
TEXTURE19 : 0x84D3,
TEXTURE2 : 0x84C2,
TEXTURE20 : 0x84D4,
TEXTURE21 : 0x84D5,
TEXTURE22 : 0x84D6,
TEXTURE23 : 0x84D7,
TEXTURE24 : 0x84D8,
TEXTURE25 : 0x84D9,
TEXTURE26 : 0x84DA,
TEXTURE27 : 0x84DB,
TEXTURE28 : 0x84DC,
TEXTURE29 : 0x84DD,
TEXTURE3 : 0x84C3,
TEXTURE30 : 0x84DE,
TEXTURE31 : 0x84DF,
TEXTURE4 : 0x84C4,
TEXTURE5 : 0x84C5,
TEXTURE6 : 0x84C6,
TEXTURE7 : 0x84C7,
TEXTURE8 : 0x84C8,
TEXTURE9 : 0x84C9,
TEXTURE_2D : 0x0DE1,
TEXTURE_BINDING_2D : 0x8069,
TEXTURE_BINDING_CUBE_MAP : 0x8514,
TEXTURE_CUBE_MAP : 0x8513,
TEXTURE_CUBE_MAP_NEGATIVE_X : 0x8516,
TEXTURE_CUBE_MAP_NEGATIVE_Y : 0x8518,
TEXTURE_CUBE_MAP_NEGATIVE_Z : 0x851A,
TEXTURE_CUBE_MAP_POSITIVE_X : 0x8515,
TEXTURE_CUBE_MAP_POSITIVE_Y : 0x8517,
TEXTURE_CUBE_MAP_POSITIVE_Z : 0x8519,
TEXTURE_MAG_FILTER : 0x2800,
TEXTURE_MIN_FILTER : 0x2801,
TEXTURE_WRAP_S : 0x2802,
TEXTURE_WRAP_T : 0x2803,
TRIANGLES : 0x0004,
TRIANGLE_FAN : 0x0006,
TRIANGLE_STRIP : 0x0005,
UNPACK_ALIGNMENT : 0x0CF5,
UNPACK_COLORSPACE_CONVERSION_WEBGL : 0x9243,
UNPACK_FLIP_Y_WEBGL : 0x9240,
UNPACK_PREMULTIPLY_ALPHA_WEBGL : 0x9241,
UNSIGNED_BYTE : 0x1401,
UNSIGNED_INT : 0x1405,
UNSIGNED_SHORT : 0x1403,
UNSIGNED_SHORT_4_4_4_4 : 0x8033,
UNSIGNED_SHORT_5_5_5_1 : 0x8034,
UNSIGNED_SHORT_5_6_5 : 0x8363,
VALIDATE_STATUS : 0x8B83,
VENDOR : 0x1F00,
VERSION : 0x1F02,
VERTEX_ATTRIB_ARRAY_BUFFER_BINDING : 0x889F,
VERTEX_ATTRIB_ARRAY_ENABLED : 0x8622,
VERTEX_ATTRIB_ARRAY_NORMALIZED : 0x886A,
VERTEX_ATTRIB_ARRAY_POINTER : 0x8645,
VERTEX_ATTRIB_ARRAY_SIZE : 0x8623,
VERTEX_ATTRIB_ARRAY_STRIDE : 0x8624,
VERTEX_ATTRIB_ARRAY_TYPE : 0x8625,
VERTEX_SHADER : 0x8B31,
VIEWPORT : 0x0BA2,
ZERO : 0
};
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class timestamp
*
* @class
* @returns {vgl.timestamp}
*/
//////////////////////////////////////////////////////////////////////////////
var m_globalModifiedTime = 0;
vgl.timestamp = function () {
'use strict';
if (!(this instanceof vgl.timestamp)) {
return new vgl.timestamp();
}
var m_modifiedTime = 0;
/////////////////////////////////////////////////////////////////////////////
/**
* Update modified time
*/
/////////////////////////////////////////////////////////////////////////////
this.modified = function () {
m_globalModifiedTime += 1;
m_modifiedTime = m_globalModifiedTime;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get modified time
*
* @returns {number}
*/
/////////////////////////////////////////////////////////////////////////////
this.getMTime = function () {
return m_modifiedTime;
};
};
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class object
*
* @class
* @returns {vgl.object}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.object = function () {
'use strict';
if (!(this instanceof vgl.object)) {
return new vgl.object();
}
/** @private */
var m_modifiedTime = vgl.timestamp();
m_modifiedTime.modified();
////////////////////////////////////////////////////////////////////////////
/**
* Mark the object modified
*/
////////////////////////////////////////////////////////////////////////////
this.modified = function () {
m_modifiedTime.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Return modified time of the object
*
* @returns {*}
*/
////////////////////////////////////////////////////////////////////////////
this.getMTime = function () {
return m_modifiedTime.getMTime();
};
return this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class event
*
* @class event
* @returns {vgl.event}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.event = function () {
'use strict';
if (!(this instanceof vgl.event)) {
return new vgl.event();
}
vgl.object.call(this);
return this;
};
inherit(vgl.event, vgl.object);
//////////////////////////////////////////////////////////////////////////////
/**
* types
*/
//////////////////////////////////////////////////////////////////////////////
vgl.event.keyPress = 'vgl.event.keyPress';
vgl.event.mousePress = 'vgl.event.mousePress';
vgl.event.mouseRelease = 'vgl.event.mouseRelease';
vgl.event.contextMenu = 'vgl.event.contextMenu';
vgl.event.configure = 'vgl.event.configure';
vgl.event.enable = 'vgl.event.enable';
vgl.event.mouseWheel = 'vgl.event.mouseWheel';
vgl.event.keyRelease = 'vgl.event.keyRelease';
vgl.event.middleButtonPress = 'vgl.event.middleButtonPress';
vgl.event.startInteraction = 'vgl.event.startInteraction';
vgl.event.enter = 'vgl.event.enter';
vgl.event.rightButtonPress = 'vgl.event.rightButtonPress';
vgl.event.middleButtonRelease = 'vgl.event.middleButtonRelease';
vgl.event.char = 'vgl.event.char';
vgl.event.disable = 'vgl.event.disable';
vgl.event.endInteraction = 'vgl.event.endInteraction';
vgl.event.mouseMove = 'vgl.event.mouseMove';
vgl.event.mouseOut = 'vgl.event.mouseOut';
vgl.event.expose = 'vgl.event.expose';
vgl.event.timer = 'vgl.event.timer';
vgl.event.leftButtonPress = 'vgl.event.leftButtonPress';
vgl.event.leave = 'vgl.event.leave';
vgl.event.rightButtonRelease = 'vgl.event.rightButtonRelease';
vgl.event.leftButtonRelease = 'vgl.event.leftButtonRelease';
vgl.event.click = 'vgl.event.click';
vgl.event.dblClick = 'vgl.event.dblClick';
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class boundingObject
*
* @class
* @return {vgl.boundingObject}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.boundingObject = function () {
'use strict';
if (!(this instanceof vgl.boundingObject)) {
return new vgl.boundingObject();
}
vgl.object.call(this);
/** @private */
var m_bounds = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
m_computeBoundsTimestamp = vgl.timestamp(),
m_boundsDirtyTimestamp = vgl.timestamp();
m_computeBoundsTimestamp.modified();
m_boundsDirtyTimestamp.modified();
////////////////////////////////////////////////////////////////////////////
/**
* Get current bounds of the object
*/
////////////////////////////////////////////////////////////////////////////
this.bounds = function () {
return m_bounds;
};
////////////////////////////////////////////////////////////////////////////
/**
* Check if bounds are valid
*/
////////////////////////////////////////////////////////////////////////////
this.hasValidBounds = function (bounds) {
if (bounds[0] === Number.MAX_VALUE ||
bounds[1] === -Number.MAX_VALUE ||
bounds[2] === Number.MAX_VALUE ||
bounds[3] === -Number.MAX_VALUE ||
bounds[4] === Number.MAX_VALUE ||
bounds[5] === -Number.MAX_VALUE) {
return false;
}
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set current bounds of the object
*/
////////////////////////////////////////////////////////////////////////////
this.setBounds = function (minX, maxX, minY, maxY, minZ, maxZ) {
if (!this.hasValidBounds([minX, maxX, minY, maxY, minZ, maxZ])) {
return;
}
m_bounds[0] = minX;
m_bounds[1] = maxX;
m_bounds[2] = minY;
m_bounds[3] = maxY;
m_bounds[4] = minZ;
m_bounds[5] = maxZ;
this.modified();
m_computeBoundsTimestamp.modified();
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Reset bounds to default values
*/
////////////////////////////////////////////////////////////////////////////
this.resetBounds = function () {
m_bounds[0] = Number.MAX_VALUE;
m_bounds[1] = -Number.MAX_VALUE;
m_bounds[2] = Number.MAX_VALUE;
m_bounds[3] = -Number.MAX_VALUE;
m_bounds[4] = Number.MAX_VALUE;
m_bounds[5] = -Number.MAX_VALUE;
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute bounds of the object
*
* Should be implemented by the concrete class
*/
////////////////////////////////////////////////////////////////////////////
this.computeBounds = function () {
};
////////////////////////////////////////////////////////////////////////////
/**
* Return bounds computation modification time
*
* @returns {vgl.timestamp}
*/
////////////////////////////////////////////////////////////////////////////
this.computeBoundsTimestamp = function () {
return m_computeBoundsTimestamp;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return bounds dirty timestamp
*
* @returns {vgl.timestamp}
*/
////////////////////////////////////////////////////////////////////////////
this.boundsDirtyTimestamp = function () {
return m_boundsDirtyTimestamp;
};
this.resetBounds();
return this;
};
vgl.boundingObject.ReferenceFrame = {
'Relative' : 0,
'Absolute' : 1
};
inherit(vgl.boundingObject, vgl.object);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class node
*
* @class
* @returns {vgl.node}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.node = function () {
'use strict';
if (!(this instanceof vgl.node)) {
return new vgl.node();
}
vgl.boundingObject.call(this);
/** @private */
var m_parent = null,
m_material = null,
m_visible = true,
m_overlay = false;
////////////////////////////////////////////////////////////////////////////
/**
* Accept visitor for scene traversal
*/
////////////////////////////////////////////////////////////////////////////
this.accept = function (visitor) {
visitor.visit(this);
};
////////////////////////////////////////////////////////////////////////////
/**
* Return active material used by the node
*/
////////////////////////////////////////////////////////////////////////////
this.material = function () {
return m_material;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set material to be used the node
*
* @param material
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.setMaterial = function (material) {
if (material !== m_material) {
m_material = material;
this.modified();
return true;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Check if the node is visible or node
*
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.visible = function () {
return m_visible;
};
////////////////////////////////////////////////////////////////////////////
/**
* Turn ON/OFF visibility of the node
*
* @param flag
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.setVisible = function (flag) {
if (flag !== m_visible) {
m_visible = flag;
this.modified();
return true;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return current parent of the node
*
* @returns {null}
*/
////////////////////////////////////////////////////////////////////////////
this.parent = function () {
return m_parent;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set parent of the node
*
* @param parent
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.setParent = function (parent) {
if (parent !== m_parent) {
if (m_parent !== null) {
m_parent.removeChild(this);
}
m_parent = parent;
this.modified();
return true;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Check if the node is an overlay node
*
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.overlay = function () {
return m_overlay;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set if the node is an overlay node or not
*
* @param flag
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.setOverlay = function (flag) {
if (m_overlay !== flag) {
m_overlay = flag;
this.modified();
return true;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/*
* Traverse parent and their parent and so on
*/
////////////////////////////////////////////////////////////////////////////
this.ascend = function (visitor) {
visitor = visitor; /* unused parameter */
};
////////////////////////////////////////////////////////////////////////////
/**
* Traverse children
*/
////////////////////////////////////////////////////////////////////////////
this.traverse = function (visitor) {
visitor = visitor; /* unused parameter */
};
////////////////////////////////////////////////////////////////////////////
/**
* Mark that the bounds are modified
*
*/
////////////////////////////////////////////////////////////////////////////
this.boundsModified = function () {
// @todo Implement this
this.boundsDirtyTimestamp().modified();
if (m_parent !== null) {
m_parent.boundsModified();
}
};
return this;
};
inherit(vgl.node, vgl.boundingObject);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class groupNode
*
* @class
* @returns {vgl.groupNode}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.groupNode = function () {
'use strict';
if (!(this instanceof vgl.groupNode)) {
return new vgl.groupNode();
}
vgl.node.call(this);
var m_children = [];
// Reference to base class methods
this.b_setVisible = this.setVisible;
////////////////////////////////////////////////////////////////////////////
/**
* Turn on / off visibility
*
* @param flag
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.setVisible = function (flag) {
var i;
if (this.b_setVisible(flag) !== true) {
return false;
}
for (i = 0; i < m_children.length; i += 1) {
m_children[i].setVisible(flag);
}
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Make the incoming node as child of the group node
*
* @param childNode
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.addChild = function (childNode) {
if (childNode instanceof vgl.node) {
if (m_children.indexOf(childNode) === -1) {
childNode.setParent(this);
m_children.push(childNode);
this.boundsDirtyTimestamp().modified();
return true;
}
return false;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Remove parent-child relationship between the group and incoming node
*
* @param childNode
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.removeChild = function (childNode) {
if (childNode.parent() === this) {
var index = m_children.indexOf(childNode);
m_children.splice(index, 1);
this.boundsDirtyTimestamp().modified();
return true;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Remove parent-child relationship between child nodes and the group node
*/
////////////////////////////////////////////////////////////////////////////
this.removeChildren = function () {
var i;
for (i = 0; i < m_children.length; i += 1) {
this.removeChild(m_children[i]);
}
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Return children of this group node
*
* @returns {Array}
*/
////////////////////////////////////////////////////////////////////////////
this.children = function () {
return m_children;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return true if this group node has node as a child, false otherwise.
*
* @param node
* @returns {bool}
*/
////////////////////////////////////////////////////////////////////////////
this.hasChild = function (node) {
var i = 0, child = false;
for (i = 0; i < m_children.length; i += 1) {
if (m_children[i] === node) {
child = true;
break;
}
}
return child;
};
////////////////////////////////////////////////////////////////////////////
/**
* Accept a visitor and traverse the scene tree
*
* @param visitor
*/
////////////////////////////////////////////////////////////////////////////
this.accept = function (visitor) {
visitor.visit(this);
};
////////////////////////////////////////////////////////////////////////////
/**
* Traverse the scene
*
* @param visitor
*/
////////////////////////////////////////////////////////////////////////////
this.traverse = function (visitor) {
switch (visitor.type()) {
case visitor.UpdateVisitor:
this.traverseChildrenAndUpdateBounds(visitor);
break;
case visitor.CullVisitor:
this.traverseChildren(visitor);
break;
default:
break;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Traverse all of the children and update the bounds for each
*
* @param visitor
*/
////////////////////////////////////////////////////////////////////////////
this.traverseChildrenAndUpdateBounds = function (visitor) {
var i;
if (this.m_parent && this.boundsDirtyTimestamp().getMTime() >
this.computeBoundsTimestamp().getMTime()) {
// Flag parents bounds dirty.
this.m_parent.boundsDirtyTimestamp.modified();
}
this.computeBounds();
if (visitor.mode() === visitor.TraverseAllChildren) {
for (i = 0; i < m_children.length(); i += 1) {
m_children[i].accept(visitor);
this.updateBounds(m_children[i]);
}
}
this.computeBoundsTimestamp().modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Traverse children of the group node
*
* @param visitor
*/
////////////////////////////////////////////////////////////////////////////
this.traverseChildren = function (visitor) {
var i;
if (visitor.mode() === vgl.vesVisitor.TraverseAllChildren) {
for (i = 0; i < m_children.length(); i += 1) {
m_children[i].accept(visitor);
}
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute bounds for the group node
*/
////////////////////////////////////////////////////////////////////////////
this.computeBounds = function () {
var i = 0;
if (this.computeBoundsTimestamp().getMTime() >
this.boundsDirtyTimestamp().getMTime()) {
return;
}
for (i = 0; i < m_children.length; i += 1) {
this.updateBounds(m_children[i]);
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Update bounds for the group node
*
* This method is used internally to update bounds of the group node by
* traversing each of its child.
*
* @param child
*/
////////////////////////////////////////////////////////////////////////////
this.updateBounds = function (child) {
// FIXME: This check should not be required and possibly is incorrect
if (child.overlay()) {
return;
}
// Make sure that child bounds are upto date
child.computeBounds();
var bounds = this.bounds(),
childBounds = child.bounds(),
istep = 0,
jstep = 0,
i;
for (i = 0; i < 3; i += 1) {
istep = i * 2;
jstep = i * 2 + 1;
if (childBounds[istep] < bounds[istep]) {
bounds[istep] = childBounds[istep];
}
if (childBounds[jstep] > bounds[jstep]) {
bounds[jstep] = childBounds[jstep];
}
}
this.setBounds(bounds[0], bounds[1], bounds[2], bounds[3],
bounds[4], bounds[5]);
};
return this;
};
inherit(vgl.groupNode, vgl.node);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, vec3, mat4, inherit*/
//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class actor
*
* @class
* @returns {vgl.actor}
*/
////////////////////////////////////////////////////////////////////////////
vgl.actor = function () {
'use strict';
if (!(this instanceof vgl.actor)) {
return new vgl.actor();
}
vgl.node.call(this);
/** @private */
var m_this = this,
m_transformMatrix = mat4.create(),
m_referenceFrame = vgl.boundingObject.ReferenceFrame.Relative,
m_mapper = null;
////////////////////////////////////////////////////////////////////////////
/**
* Get transformation matrix used by the actor
*
* @returns {mat4}
*/
////////////////////////////////////////////////////////////////////////////
this.matrix = function () {
return m_transformMatrix;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set transformation matrix for the actor
*
* @param {mat4} 4X4 transformation matrix
*/
////////////////////////////////////////////////////////////////////////////
this.setMatrix = function (tmatrix) {
if (tmatrix !== m_transformMatrix) {
m_transformMatrix = tmatrix;
m_this.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get reference frame for the transformations
*
* @returns {String} Possible values are Absolute or Relative
*/
////////////////////////////////////////////////////////////////////////////
this.referenceFrame = function () {
return m_referenceFrame;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set reference frame for the transformations
*
* @param {vgl.boundingObject.ReferenceFrame}
* referenceFrame Possible values are (Absolute | Relative)
*/
////////////////////////////////////////////////////////////////////////////
this.setReferenceFrame = function (referenceFrame) {
if (referenceFrame !== m_referenceFrame) {
m_referenceFrame = referenceFrame;
m_this.modified();
return true;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return mapper where actor gets it behavior and data
*
* @returns {vgl.mapper}
*/
////////////////////////////////////////////////////////////////////////////
this.mapper = function () {
return m_mapper;
};
////////////////////////////////////////////////////////////////////////////
/**
* Connect an actor to its data source
*
* @param {vgl.mapper}
*/
////////////////////////////////////////////////////////////////////////////
this.setMapper = function (mapper) {
if (mapper !== m_mapper) {
m_mapper = mapper;
m_this.boundsModified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* @todo
*/
////////////////////////////////////////////////////////////////////////////
this.accept = function (visitor) {
visitor = visitor; /* ignore this parameter */
};
////////////////////////////////////////////////////////////////////////////
/**
* @todo
*/
////////////////////////////////////////////////////////////////////////////
this.ascend = function (visitor) {
visitor = visitor; /* ignore this parameter */
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute object space to world space matrix
* @todo
*/
////////////////////////////////////////////////////////////////////////////
this.computeLocalToWorldMatrix = function (matrix, visitor) {
matrix = matrix; /* ignore this parameter */
visitor = visitor; /* ignore this parameter */
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute world space to object space matrix
* @todo
*/
////////////////////////////////////////////////////////////////////////////
this.computeWorldToLocalMatrix = function (matrix, visitor) {
matrix = matrix; /* ignore this parameter */
visitor = visitor; /* ignore this parameter */
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute actor bounds
*/
////////////////////////////////////////////////////////////////////////////
this.computeBounds = function () {
if (m_mapper === null || m_mapper === undefined) {
m_this.resetBounds();
return;
}
var computeBoundsTimestamp = m_this.computeBoundsTimestamp(),
mapperBounds, minPt, maxPt, newBounds;
if (m_this.boundsDirtyTimestamp().getMTime() > computeBoundsTimestamp.getMTime() ||
m_mapper.boundsDirtyTimestamp().getMTime() > computeBoundsTimestamp.getMTime()) {
m_mapper.computeBounds();
mapperBounds = m_mapper.bounds();
minPt = [mapperBounds[0], mapperBounds[2], mapperBounds[4]];
maxPt = [mapperBounds[1], mapperBounds[3], mapperBounds[5]];
vec3.transformMat4(minPt, minPt, m_transformMatrix);
vec3.transformMat4(maxPt, maxPt, m_transformMatrix);
newBounds = [
minPt[0] > maxPt[0] ? maxPt[0] : minPt[0],
minPt[0] > maxPt[0] ? minPt[0] : maxPt[0],
minPt[1] > maxPt[1] ? maxPt[1] : minPt[1],
minPt[1] > maxPt[1] ? minPt[1] : maxPt[1],
minPt[2] > maxPt[2] ? maxPt[2] : minPt[2],
minPt[2] > maxPt[2] ? minPt[2] : maxPt[2]
];
m_this.setBounds(newBounds[0], newBounds[1],
newBounds[2], newBounds[3],
newBounds[4], newBounds[5]);
computeBoundsTimestamp.modified();
}
};
return m_this;
};
inherit(vgl.actor, vgl.node);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Freeze javascript object
*
* @param obj
*/
//////////////////////////////////////////////////////////////////////////////
vgl.freezeObject = function (obj) {
'use strict';
/**
* Freezes an object, using Object.freeze if available, otherwise returns
* the object unchanged. This function should be used in setup code to prevent
* errors from completely halting JavaScript execution in legacy browsers.
*
* @exports freezeObject
*/
var freezedObject = Object.freeze(obj);
if (typeof freezedObject === 'undefined') {
freezedObject = function (o) {
return o;
};
}
return freezedObject;
};
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Returns the first parameter if not undefined,
* otherwise the second parameter.
*
* @class
* @returns {vgl.defaultValue}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.defaultValue = function (a, b) {
'use strict';
if (typeof a !== 'undefined') {
return a;
}
return b;
};
vgl.defaultValue.EMPTY_OBJECT = vgl.freezeObject({});
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class graphicsObject
*
* @class
* @param type
* @returns {vgl.graphicsObject}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.graphicsObject = function (type) {
'use strict';
type = type; /* unused parameter */
if (!(this instanceof vgl.graphicsObject)) {
return new vgl.graphicsObject();
}
vgl.object.call(this);
var m_this = this;
////////////////////////////////////////////////////////////////////////////
/**
* Setup (initialize) the object
*
* @param renderState
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this._setup = function (renderState) {
renderState = renderState; /* unused parameter */
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Remove any resources acquired before deletion
*
* @param renderState
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this._cleanup = function (renderState) {
renderState = renderState; /* unused parameter */
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Bind and activate
*
* @param renderState
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.bind = function (renderState) {
renderState = renderState; /* unused parameter */
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Undo bind and deactivate
*
* @param renderState
* @returns {boolean}
*
* TODO: Change it to unbind (simple)
*/
////////////////////////////////////////////////////////////////////////////
this.undoBind = function (renderState) {
renderState = renderState; /* unused parameter */
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Render the object
*/
////////////////////////////////////////////////////////////////////////////
this.render = function (renderState) {
renderState = renderState; /* unused parameter */
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Remove the object and release its graphics resources
*/
////////////////////////////////////////////////////////////////////////////
this.remove = function (renderState) {
m_this._cleanup(renderState);
};
return m_this;
};
inherit(vgl.graphicsObject, vgl.object);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, Uint16Array*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of geojson reader
*
* This contains code that reads a geoJSON file and produces rendering
* primitives from it.
*
* @class
* @returns {vgl.geojsonReader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.geojsonReader = function () {
'use strict';
if (!(this instanceof vgl.geojsonReader)) {
return new vgl.geojsonReader();
}
////////////////////////////////////////////////////////////////////////////
/**
* Read scalars
*
* @param coordinates
* @param geom
* @param size_estimate
* @param idx
*/
////////////////////////////////////////////////////////////////////////////
this.readScalars = function (coordinates, geom, size_estimate, idx) {
var array = null,
s = null,
r = null,
g = null,
b = null;
if (this.m_scalarFormat === 'values' && coordinates.length === 4) {
s = coordinates[3];
array = geom.sourceData(vgl.vertexAttributeKeys.Scalar);
if (!array) {
array = new vgl.sourceDataSf();
if (this.m_scalarRange) {
array.setScalarRange(this.m_scalarRange[0], this.m_scalarRange[1]);
}
if (size_estimate !== undefined) {
//array.length = size_estimate; //no, slow on Safari
array.data().length = size_estimate;
}
geom.addSource(array);
}
if (size_estimate === undefined) {
array.pushBack(s);
} else {
array.insertAt(idx, s);
}
} else if (this.m_scalarFormat === 'rgb' && coordinates.length === 6) {
array = geom.sourceData(vgl.vertexAttributeKeys.Color);
if (!array) {
array = new vgl.sourceDataC3fv();
if (size_estimate !== undefined) {
array.length = size_estimate * 3;
}
geom.addSource(array);
}
r = coordinates[3];
g = coordinates[4];
b = coordinates[5];
if (size_estimate === undefined) {
array.pushBack([r, g, b]);
} else {
array.insertAt(idx, [r, g, b]);
}
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Read point data
*
* @param coordinates
* @returns {vgl.geometryData}
*/
////////////////////////////////////////////////////////////////////////////
this.readPoint = function (coordinates) {
var geom = new vgl.geometryData(),
vglpoints = new vgl.points(),
vglcoords = new vgl.sourceDataP3fv(),
indices = new Uint16Array(1),
x = null,
y = null,
z = null,
i = null;
geom.addSource(vglcoords);
for (i = 0; i < 1; i += 1) {
indices[i] = i;
x = coordinates[0];
y = coordinates[1];
z = 0.0;
if (coordinates.length > 2) {
z = coordinates[2];
}
//console.log('read ' + x + ',' + y + ',' + z);
vglcoords.pushBack([x, y, z]);
//attributes
this.readScalars(coordinates, geom);
}
vglpoints.setIndices(indices);
geom.addPrimitive(vglpoints);
geom.setName('aPoint');
return geom;
};
////////////////////////////////////////////////////////////////////////////
/**
* Read multipoint data
*
* @param coordinates
* @returns {vgl.geometryData}
*/
////////////////////////////////////////////////////////////////////////////
this.readMultiPoint = function (coordinates) {
var geom = new vgl.geometryData(),
vglpoints = new vgl.points(),
vglcoords = new vgl.sourceDataP3fv(),
indices = new Uint16Array(coordinates.length),
pntcnt = 0,
estpntcnt = coordinates.length,
x = null,
y = null,
z = null,
i;
//preallocate with size estimate
vglcoords.data().length = estpntcnt * 3; //x,y,z
for (i = 0; i < coordinates.length; i += 1) {
indices[i] = i;
x = coordinates[i][0];
y = coordinates[i][1];
z = 0.0;
if (coordinates[i].length > 2) {
z = coordinates[i][2];
}
//console.log('read ' + x + ',' + y + ',' + z);
vglcoords.insertAt(pntcnt, [x, y, z]);
//attributes
this.readScalars(coordinates[i], geom, estpntcnt, pntcnt);
pntcnt += 1;
}
vglpoints.setIndices(indices);
geom.addPrimitive(vglpoints);
geom.addSource(vglcoords);
geom.setName('manyPoints');
return geom;
};
////////////////////////////////////////////////////////////////////////////
/**
* Read line string data
*
* @param coordinates
* @returns {vgl.geometryData}
*/
////////////////////////////////////////////////////////////////////////////
this.readLineString = function (coordinates) {
var geom = new vgl.geometryData(),
vglline = new vgl.lineStrip(),
vglcoords = new vgl.sourceDataP3fv(),
indices = [],
i = null,
x = null,
y = null,
z = null;
vglline.setIndicesPerPrimitive(coordinates.length);
for (i = 0; i < coordinates.length; i += 1) {
indices.push(i);
x = coordinates[i][0];
y = coordinates[i][1];
z = 0.0;
if (coordinates[i].length > 2) {
z = coordinates[i][2];
}
//console.log('read ' + x + ',' + y + ',' + z);
vglcoords.pushBack([x, y, z]);
//attributes
this.readScalars(coordinates[i], geom);
}
vglline.setIndices(indices);
geom.addPrimitive(vglline);
geom.addSource(vglcoords);
geom.setName('aLineString');
return geom;
};
////////////////////////////////////////////////////////////////////////////
/**
* Read multi line string
*
* @param coordinates
* @returns {vgl.geometryData}
*/
////////////////////////////////////////////////////////////////////////////
this.readMultiLineString = function (coordinates) {
var geom = new vgl.geometryData(),
vglcoords = new vgl.sourceDataP3fv(),
pntcnt = 0,
//lines should be at least 2 verts long, underest OK
estpntcnt = coordinates.length * 2,
i = null,
j = null,
x = null,
y = null,
z = null,
indices = null,
vglline = null,
thisLineLength = null;
// Preallocate with size estimate
vglcoords.data().length = estpntcnt * 3; //x,y,z
for (j = 0; j < coordinates.length; j += 1) {
indices = [];
//console.log('getting line ' + j);
vglline = new vgl.lineStrip();
thisLineLength = coordinates[j].length;
vglline.setIndicesPerPrimitive(thisLineLength);
for (i = 0; i < thisLineLength; i += 1) {
indices.push(pntcnt);
x = coordinates[j][i][0];
y = coordinates[j][i][1];
z = 0.0;
if (coordinates[j][i].length > 2) {
z = coordinates[j][i][2];
}
//console.log('read ' + x + ',' + y + ',' + z);
vglcoords.insertAt(pntcnt, [x, y, z]);
//attributes
this.readScalars(coordinates[j][i], geom, estpntcnt * 2, pntcnt);
pntcnt += 1;
}
vglline.setIndices(indices);
geom.addPrimitive(vglline);
}
geom.setName('aMultiLineString');
geom.addSource(vglcoords);
return geom;
};
////////////////////////////////////////////////////////////////////////////
/**
* Read polygon data
*
* @param coordinates
* @returns {vgl.geometryData}
*/
////////////////////////////////////////////////////////////////////////////
this.readPolygon = function (coordinates) {
//TODO: ignoring holes given in coordinates[1...]
//TODO: ignoring convex
//TODO: implement ear clipping in VGL instead of this to handle both
var geom = new vgl.geometryData(),
vglcoords = new vgl.sourceDataP3fv(),
x = null,
y = null,
z = null,
thisPolyLength = coordinates[0].length,
vl = 1,
i = null,
indices = null,
vgltriangle = null;
for (i = 0; i < thisPolyLength; i += 1) {
x = coordinates[0][i][0];
y = coordinates[0][i][1];
z = 0.0;
if (coordinates[0][i].length > 2) {
z = coordinates[0][i][2];
}
//console.log('read ' + x + ',' + y + ',' + z);
vglcoords.pushBack([x, y, z]);
//attributes
this.readScalars(coordinates[0][i], geom);
if (i > 1) {
//console.log('Cutting new triangle 0,'+ vl+ ','+ i);
indices = new Uint16Array([0, vl, i]);
vgltriangle = new vgl.triangles();
vgltriangle.setIndices(indices);
geom.addPrimitive(vgltriangle);
vl = i;
}
}
geom.setName('POLY');
geom.addSource(vglcoords);
return geom;
};
////////////////////////////////////////////////////////////////////////////
/**
* Read multi polygon data
*
* @param coordinates
* @returns {vgl.geometryData}
*/
////////////////////////////////////////////////////////////////////////////
this.readMultiPolygon = function (coordinates) {
var geom = new vgl.geometryData(),
vglcoords = new vgl.sourceDataP3fv(),
ccount = 0,
numPolys = coordinates.length,
pntcnt = 0,
estpntcnt = numPolys * 3, // assume triangles, underest is fine
vgltriangle = new vgl.triangles(),
indexes = [],
i = null,
j = null,
x = null,
y = null,
z = null,
thisPolyLength = null,
vf = null,
vl = null,
flip = null,
flipped = false,
tcount = 0;
//var time1 = new Date().getTime()
//var a = 0;
//var b = 0;
//var c = 0;
//var d = 0;
//preallocate with size estimate
vglcoords.data().length = numPolys * 3; //x,y,z
for (j = 0; j < numPolys; j += 1) {
//console.log('getting poly ' + j);
thisPolyLength = coordinates[j][0].length;
vf = ccount;
vl = ccount + 1;
flip = [false, false, false];
for (i = 0; i < thisPolyLength; i += 1) {
//var timea = new Date().getTime()
x = coordinates[j][0][i][0];
y = coordinates[j][0][i][1];
z = 0.0;
if (coordinates[j][0][i].length > 2) {
z = coordinates[j][0][i][2];
}
flipped = false;
if (x > 180) {
flipped = true;
x = x - 360;
}
if (i === 0) {
flip[0] = flipped;
} else {
flip[1 + (i - 1) % 2] = flipped;
}
//var timeb = new Date().getTime();
//console.log('read ' + x + ',' + y + ',' + z);
vglcoords.insertAt(pntcnt, [x, y, z]);
//var timec = new Date().getTime();
//attributes
this.readScalars(coordinates[j][0][i], geom, estpntcnt, pntcnt);
pntcnt += 1;
//var timed = new Date().getTime()
if (i > 1) {
//if (vl < 50) {
//console.log('Cutting new triangle ' + tcount + ':' + vf + ',' +
// vl + ',' + ccount);
//console.log(indexes);
//}
if (flip[0] === flip[1] && flip[1] === flip[2]) {
//indexes = indexes.concat([vf,vl,ccount]); //no, very slow in Safari
indexes[tcount * 3 + 0] = vf;
indexes[tcount * 3 + 1] = vl;
indexes[tcount * 3 + 2] = ccount;
tcount += 1;
}
//else {
// //TODO: duplicate triangles that straddle boundary on either side
//}
vl = ccount;
}
ccount += 1;
//var timee = new Date().getTime()
//a = a + (timeb-timea)
//b = b + (timec-timeb)
//c = c + (timed-timec)
//d = d + (timee-timed)
}
}
vgltriangle.setIndices(indexes);
geom.addPrimitive(vgltriangle);
//console.log('NUMPOLYS ' + pntcnt);
//console.log('RMP: ', a, ',', b, ',', c, ',', d)
//var time2 = new Date().getTime()
geom.setName('aMultiPoly');
geom.addSource(vglcoords);
//var time3 = new Date().getTime()
//console.log('RMP: ', time2-time1, ',', time3-time2)
return geom;
};
////////////////////////////////////////////////////////////////////////////
/**
* @param object
* @returns {*}
*/
////////////////////////////////////////////////////////////////////////////
this.readGJObjectInt = function (object) {
if (!object.hasOwnProperty('type')) {
//console.log('uh oh, not a geojson object');
return null;
}
//look for properties type annotation
if (object.properties &&
object.properties.ScalarFormat &&
object.properties.ScalarFormat === 'values') {
this.m_scalarFormat = 'values';
if (object.properties.ScalarRange) {
this.m_scalarRange = object.properties.ScalarRange;
}
}
if (object.properties &&
object.properties.ScalarFormat &&
object.properties.ScalarFormat === 'rgb') {
this.m_scalarFormat = 'rgb';
}
//TODO: ignoring 'crs' and 'bbox' and misc meta data on all of these,
//best to handle as references into original probably
var ret,
type = object.type,
next = null,
nextset = null,
i = null;
switch (type) {
case 'Point':
//console.log('parsed Point');
ret = this.readPoint(object.coordinates);
break;
case 'MultiPoint':
//console.log('parsed MultiPoint');
ret = this.readMultiPoint(object.coordinates);
break;
case 'LineString':
//console.log('parsed LineString');
ret = this.readLineString(object.coordinates);
break;
case 'MultiLineString':
//console.log('parsed MultiLineString');
ret = this.readMultiLineString(object.coordinates);
break;
case 'Polygon':
//console.log('parsed Polygon');
ret = this.readPolygon(object.coordinates);
break;
case 'MultiPolygon':
//console.log('parsed MultiPolygon');
ret = this.readMultiPolygon(object.coordinates);
break;
case 'GeometryCollection':
//console.log('parsed GeometryCollection');
nextset = [];
for (i = 0; i < object.geometries.length; i += 1) {
next = this.readGJObject(object.geometries[i]);
nextset.push(next);
}
ret = nextset;
break;
case 'Feature':
//console.log('parsed Feature');
next = this.readGJObject(object.geometry);
ret = next;
break;
case 'FeatureCollection':
//console.log('parsed FeatureCollection');
nextset = [];
for (i = 0; i < object.features.length; i += 1) {
next = this.readGJObject(object.features[i]);
nextset.push(next);
}
ret = nextset;
break;
default:
console.log('Don\'t understand type ' + type);
ret = null;
break;
}
return ret;
};
////////////////////////////////////////////////////////////////////////////
/**
* @param object
* @returns {*}
*/
////////////////////////////////////////////////////////////////////////////
this.readGJObject = function (object) {
//var time1, time2;
var ret;
//time1 = new Date().getTime()
ret = this.readGJObjectInt(object);
//time2 = new Date().getTime()
//console.log('ELAPSED: ', time2-time1)
return ret;
};
/**
* Linearize geometries
*
* @param geoms
* @param geom
*/
this.linearizeGeoms = function (geoms, geom) {
var i = null;
if (Object.prototype.toString.call(geom) === '[object Array]') {
for (i = 0; i < geom.length; i += 1) {
this.linearizeGeoms(geoms, geom[i]);
}
} else {
geoms.push(geom);
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Read geometries from geojson object
*
* @param object
* @returns {Array}
*/
////////////////////////////////////////////////////////////////////////////
this.readGeomObject = function (object) {
var geom,
geoms = [];
geom = this.readGJObject(object);
this.linearizeGeoms(geoms, geom);
return geoms;
};
////////////////////////////////////////////////////////////////////////////
/**
* Given a buffer get rendering primitives
*
* @param buffer
* @returns {*}
*/
////////////////////////////////////////////////////////////////////////////
this.getPrimitives = function (buffer) {
//console.log('Parsing geoJSON');
if (!buffer) {
return [];
}
var obj = JSON.parse(buffer),
geom = this.readGJObject(obj),
geoms = [];
this.m_scalarFormat = 'none';
this.m_scalarRange = null;
this.linearizeGeoms(geoms, geom);
return { 'geoms': geoms,
'scalarFormat': this.m_scalarFormat,
'scalarRange': this.m_scalarRange };
};
return this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl*/
//////////////////////////////////////////////////////////////////////////////
vgl.data = function () {
'use strict';
if (!(this instanceof vgl.data)) {
return new vgl.data();
}
////////////////////////////////////////////////////////////////////////////
/**
* Return data type. Should be implemented by the derived class
*/
////////////////////////////////////////////////////////////////////////////
this.type = function () {
};
};
vgl.data.raster = 0;
vgl.data.point = 1;
vgl.data.lineString = 2;
vgl.data.polygon = 3;
vgl.data.geometry = 10;
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, Uint16Array, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class primitive
*
* @class
* @return {vgl.primitive}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.primitive = function () {
'use strict';
if (!(this instanceof vgl.primitive)) {
return new vgl.primitive();
}
/** @private */
var m_indicesPerPrimitive = 0,
m_primitiveType = 0,
m_indicesValueType = 0,
m_indices = null;
////////////////////////////////////////////////////////////////////////////
/**
* Get indices of the primitive
*
* @returns {null}
*/
////////////////////////////////////////////////////////////////////////////
this.indices = function () {
return m_indices;
};
////////////////////////////////////////////////////////////////////////////
/**
* Create indices array for the primitive
* @param type
*/
////////////////////////////////////////////////////////////////////////////
this.createIndices = function (type) {
type = type; /* unused parameters */
// TODO Check for the type
m_indices = new Uint16Array();
};
////////////////////////////////////////////////////////////////////////////
/**
* Return the number of indices
*/
////////////////////////////////////////////////////////////////////////////
this.numberOfIndices = function () {
return m_indices.length;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return size of indices in bytes
*/
////////////////////////////////////////////////////////////////////////////
this.sizeInBytes = function () {
return m_indices.length * Uint16Array.BYTES_PER_ELEMENT;
};
////////////////////////////////////////////////////////////////////////////
/*
* Return primitive type g
*/
////////////////////////////////////////////////////////////////////////////
this.primitiveType = function () {
return m_primitiveType;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set primitive type
*/
////////////////////////////////////////////////////////////////////////////
this.setPrimitiveType = function (type) {
m_primitiveType = type;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return count of indices that form a primitives
*/
////////////////////////////////////////////////////////////////////////////
this.indicesPerPrimitive = function () {
return m_indicesPerPrimitive;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set count of indices that form a primitive
*/
////////////////////////////////////////////////////////////////////////////
this.setIndicesPerPrimitive = function (count) {
m_indicesPerPrimitive = count;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return indices value type
*/
////////////////////////////////////////////////////////////////////////////
this.indicesValueType = function () {
return m_indicesValueType;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set indices value type
*/
////////////////////////////////////////////////////////////////////////////
this.setIndicesValueType = function (type) {
m_indicesValueType = type;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set indices from a array
*/
////////////////////////////////////////////////////////////////////////////
this.setIndices = function (indicesArray) {
// TODO Check for the type
m_indices = new Uint16Array(indicesArray);
};
return this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class triangleStrip
*
* @returns {vgl.triangleStrip}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.triangleStrip = function () {
'use strict';
if (!(this instanceof vgl.triangleStrip)) {
return new vgl.triangleStrip();
}
vgl.primitive.call(this);
this.setPrimitiveType(vgl.GL.TRIANGLE_STRIP);
this.setIndicesValueType(vgl.GL.UNSIGNED_SHORT);
this.setIndicesPerPrimitive(3);
return this;
};
inherit(vgl.triangleStrip, vgl.primitive);
////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class triangles
*
* @returns {vgl.triangles}
*/
////////////////////////////////////////////////////////////////////////////
vgl.triangles = function () {
'use strict';
if (!(this instanceof vgl.triangles)) {
return new vgl.triangles();
}
vgl.primitive.call(this);
this.setPrimitiveType(vgl.GL.TRIANGLES);
this.setIndicesValueType(vgl.GL.UNSIGNED_SHORT);
this.setIndicesPerPrimitive(3);
return this;
};
inherit(vgl.triangles, vgl.primitive);
//////////////////////////////////////////////////////////////////////////////
/**
* create a instance of lines primitive type
*
* @returns {vgl.lines}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.lines = function () {
'use strict';
if (!(this instanceof vgl.lines)) {
return new vgl.lines();
}
vgl.primitive.call(this);
this.setPrimitiveType(vgl.GL.LINES);
this.setIndicesValueType(vgl.GL.UNSIGNED_SHORT);
this.setIndicesPerPrimitive(2);
return this;
};
inherit(vgl.lines, vgl.primitive);
//////////////////////////////////////////////////////////////////////////////
/**
* create a instance of line strip primitive type
*
* @returns {vgl.lineStrip}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.lineStrip = function () {
'use strict';
if (!(this instanceof vgl.lineStrip)) {
return new vgl.lineStrip();
}
vgl.primitive.call(this);
this.setPrimitiveType(vgl.GL.LINE_STRIP);
this.setIndicesValueType(vgl.GL.UNSIGNED_SHORT);
this.setIndicesPerPrimitive(2);
return this;
};
inherit(vgl.lineStrip, vgl.primitive);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class points
*
* @returns {vgl.points}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.points = function () {
'use strict';
if (!(this instanceof vgl.points)) {
return new vgl.points();
}
vgl.primitive.call(this);
this.setPrimitiveType(vgl.GL.POINTS);
this.setIndicesValueType(vgl.GL.UNSIGNED_SHORT);
this.setIndicesPerPrimitive(1);
return this;
};
inherit(vgl.points, vgl.primitive);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class vertexDataP3f
*
* @returns {vgl.vertexDataP3f}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.vertexDataP3f = function () {
'use strict';
if (!(this instanceof vgl.vertexDataP3f)) {
return new vgl.vertexDataP3f();
}
/** @private */
this.m_position = [];
return this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class vertexDataP3N3f
*
* @class
* @returns {vgl.vertexDataP3N3f}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.vertexDataP3N3f = function () {
'use strict';
if (!(this instanceof vgl.vertexDataP3N3f)) {
return new vgl.vertexDataP3N3f();
}
this.m_position = [];
this.m_normal = [];
return this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class vertexDataP3T3f
*
* @class
* @returns {vgl.vertexDataP3T3f}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.vertexDataP3T3f = function () {
'use strict';
if (!(this instanceof vgl.vertexDataP3T3f)) {
return new vgl.vertexDataP3T3f();
}
this.m_position = [];
this.m_texCoordinate = [];
return this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class sourceData
* @class
* @returns {vgl.sourceData}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.sourceData = function (arg) {
'use strict';
if (!(this instanceof vgl.sourceData)) {
return new vgl.sourceData(arg);
}
arg = arg || {};
var m_attributesMap = {},
m_data = [],
m_name = arg.name || 'Source ' + new Date().toISOString(),
////////////////////////////////////////////////////////////////////////////
/**
* Attribute data for the source
*/
////////////////////////////////////////////////////////////////////////////
vglAttributeData = function () {
// Number of components per group
// Type of data type (GL_FLOAT etc)
this.m_numberOfComponents = 0;
// Size of data type
this.m_dataType = 0;
this.m_dataTypeSize = 0;
// Specifies whether fixed-point data values should be normalized
// (true) or converted directly as fixed-point values (false)
// when they are accessed.
this.m_normalized = false;
// Strides for each attribute.
this.m_stride = 0;
// Offset
this.m_offset = 0;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return raw data for this source
*
* @returns {Array or Float32Array}
*/
////////////////////////////////////////////////////////////////////////////
this.data = function () {
return m_data;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return raw data for this source
*
* @returns {Array or Float32Array}
*/
////////////////////////////////////////////////////////////////////////////
this.getData = function () {
return this.data();
};
////////////////////////////////////////////////////////////////////////////
/**
* If the raw data is not a Float32Array, convert it to one. Then, return
* raw data for this source
*
* @returns {Float32Array}
*/
////////////////////////////////////////////////////////////////////////////
this.dataToFloat32Array = function () {
if (!(m_data instanceof Float32Array)) {
m_data = new Float32Array(m_data);
}
return m_data;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set data for this source
*
*/
////////////////////////////////////////////////////////////////////////////
this.setData = function (data) {
if (!(data instanceof Array) && !(data instanceof Float32Array)) {
console.log('[error] Requires array');
return;
}
if (data instanceof Float32Array) {
m_data = data;
} else {
m_data = data.slice(0);
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Add new attribute data to the source
*/
////////////////////////////////////////////////////////////////////////////
this.addAttribute = function (key, dataType, sizeOfDataType, offset, stride,
noOfComponents, normalized) {
if (!m_attributesMap.hasOwnProperty(key)) {
/* jshint newcap: false */
//jscs:disable requireCapitalizedConstructors
var newAttr = new vglAttributeData();
//jscs:enable requireCapitalizedConstructors
/* jshint newcap: true */
newAttr.m_dataType = dataType;
newAttr.m_dataTypeSize = sizeOfDataType;
newAttr.m_offset = offset;
newAttr.m_stride = stride;
newAttr.m_numberOfComponents = noOfComponents;
newAttr.m_normalized = normalized;
m_attributesMap[key] = newAttr;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Return size of the source data
*/
////////////////////////////////////////////////////////////////////////////
this.sizeOfArray = function () {
return Object.size(m_data);
};
////////////////////////////////////////////////////////////////////////////
/**
* Return length of array
*/
////////////////////////////////////////////////////////////////////////////
this.lengthOfArray = function () {
return m_data.length;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return size of the source data in bytes
*/
////////////////////////////////////////////////////////////////////////////
/*
* TODO: code below is probably wrong.
* Example:
* format P3N3f
* m_data = [ 1, 2, 3, 4, 5, 6 ]; // contains one vertex,
* // one normal, m_data.length == 6
*
* The inner loop computes:
* sizeInBytes += 3 * 4; // for position
* sizeInBytes += 3 * 4; // for normal
*
* Then sizeInBytes *= 6; // m_data.length == 6
* which gives sizeInBytes == 144 bytes when it should have been 4*6 = 24
*/
this.sizeInBytes = function () {
var sizeInBytes = 0,
keys = this.keys(), i;
for (i = 0; i < keys.length(); i += 1) {
sizeInBytes += this.numberOfComponents(keys[i]) *
this.sizeOfAttributeDataType(keys[i]);
}
sizeInBytes *= this.sizeOfArray();
return sizeInBytes;
};
////////////////////////////////////////////////////////////////////////////
/**
* Check if there is attribute exists of a given key type
*/
////////////////////////////////////////////////////////////////////////////
this.hasKey = function (key) {
return m_attributesMap.hasOwnProperty(key);
};
////////////////////////////////////////////////////////////////////////////
/**
* Return keys of all attributes
*/
////////////////////////////////////////////////////////////////////////////
this.keys = function () {
return Object.keys(m_attributesMap);
};
////////////////////////////////////////////////////////////////////////////
/**
* Return number of attributes of source data
*/
////////////////////////////////////////////////////////////////////////////
this.numberOfAttributes = function () {
return Object.size(m_attributesMap);
};
////////////////////////////////////////////////////////////////////////////
/**
* Return number of components of the attribute data
*/
////////////////////////////////////////////////////////////////////////////
this.attributeNumberOfComponents = function (key) {
if (m_attributesMap.hasOwnProperty(key)) {
return m_attributesMap[key].m_numberOfComponents;
}
return 0;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return if the attribute data is normalized
*/
////////////////////////////////////////////////////////////////////////////
this.normalized = function (key) {
if (m_attributesMap.hasOwnProperty(key)) {
return m_attributesMap[key].m_normalized;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return size of the attribute data type
*/
////////////////////////////////////////////////////////////////////////////
this.sizeOfAttributeDataType = function (key) {
if (m_attributesMap.hasOwnProperty(key)) {
return m_attributesMap[key].m_dataTypeSize;
}
return 0;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return attribute data type
*/
////////////////////////////////////////////////////////////////////////////
this.attributeDataType = function (key) {
if (m_attributesMap.hasOwnProperty(key)) {
return m_attributesMap[key].m_dataType;
}
return undefined;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return attribute offset
*/
////////////////////////////////////////////////////////////////////////////
this.attributeOffset = function (key) {
if (m_attributesMap.hasOwnProperty(key)) {
return m_attributesMap[key].m_offset;
}
return 0;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return attribute stride
*/
////////////////////////////////////////////////////////////////////////////
this.attributeStride = function (key) {
if (m_attributesMap.hasOwnProperty(key)) {
return m_attributesMap[key].m_stride;
}
return 0;
};
////////////////////////////////////////////////////////////////////////////
/**
* Virtual function to insert new vertex data at the end
*/
////////////////////////////////////////////////////////////////////////////
this.pushBack = function (vertexData) {
vertexData = vertexData; /* unused parameter */
// Should be implemented by the base class
};
////////////////////////////////////////////////////////////////////////////
/**
* Insert new data block to the raw data
*/
////////////////////////////////////////////////////////////////////////////
this.insert = function (data) {
var i;
//m_data = m_data.concat(data); //no, slow on Safari
/* If we will are given a Float32Array and don't have any other data, use
* it directly. */
if (!m_data.length && data.length && data instanceof Float32Array) {
m_data = data;
return;
}
/* If our internal array is immutable and we will need to change it, create
* a regular mutable array from it. */
if (!m_data.slice && (m_data.length || !data.slice)) {
m_data = Array.prototype.slice.call(m_data);
}
if (!data.length) {
/* data is a singular value, so append it to our array */
m_data[m_data.length] = data;
} else {
/* We don't have any data currently, so it is faster to copy the data
* using slice. */
if (!m_data.length && data.slice) {
m_data = data.slice(0);
} else {
for (i = 0; i < data.length; i += 1) {
m_data[m_data.length] = data[i];
}
}
}
};
this.insertAt = function (index, data) {
var i;
if (!data.length) {
m_data[index] = data;
} else {
for (i = 0; i < data.length; i += 1) {
m_data[index * data.length + i] = data[i];
}
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Return name of the source data
*/
////////////////////////////////////////////////////////////////////////////
this.name = function () {
return m_name;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set name of the source data
*/
////////////////////////////////////////////////////////////////////////////
this.setName = function (name) {
m_name = name;
};
return this;
};
vgl.sourceDataAnyfv = function (size, key, arg) {
'use strict';
if (!(this instanceof vgl.sourceDataAnyfv)) {
return new vgl.sourceDataAnyfv(size, key, arg);
}
vgl.sourceData.call(this, arg);
this.addAttribute(key, vgl.GL.FLOAT,
4, 0, size * 4, size, false);
this.pushBack = function (value) {
this.insert(value);
};
return this;
};
inherit(vgl.sourceDataAnyfv, vgl.sourceData);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class sourceDataP3T3f
*
* @returns {vgl.sourceDataP3T3f}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.sourceDataP3T3f = function (arg) {
'use strict';
if (!(this instanceof vgl.sourceDataP3T3f)) {
return new vgl.sourceDataP3T3f(arg);
}
vgl.sourceData.call(this, arg);
this.addAttribute(vgl.vertexAttributeKeys.Position, vgl.GL.FLOAT, 4, 0, 6 * 4, 3,
false);
this.addAttribute(vgl.vertexAttributeKeys.TextureCoordinate, vgl.GL.FLOAT, 4, 12,
6 * 4, 3, false);
this.pushBack = function (value) {
this.insert(value.m_position);
this.insert(value.m_texCoordinate);
};
return this;
};
inherit(vgl.sourceDataP3T3f, vgl.sourceData);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class sourceDataP3N3f
*
* @returns {vgl.sourceDataP3N3f}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.sourceDataP3N3f = function (arg) {
'use strict';
if (!(this instanceof vgl.sourceDataP3N3f)) {
return new vgl.sourceDataP3N3f(arg);
}
vgl.sourceData.call(this, arg);
this.addAttribute(vgl.vertexAttributeKeys.Position, vgl.GL.FLOAT, 4, 0, 6 * 4, 3,
false);
this.addAttribute(vgl.vertexAttributeKeys.Normal, vgl.GL.FLOAT, 4, 12, 6 * 4, 3,
false);
this.pushBack = function (value) {
this.insert(value.m_position);
this.insert(value.m_normal);
};
return this;
};
inherit(vgl.sourceDataP3N3f, vgl.sourceData);
/////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class sourceDataP3fv
*
* @returns {vgl.sourceDataP3fv}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.sourceDataP3fv = function (arg) {
'use strict';
if (!(this instanceof vgl.sourceDataP3fv)) {
return new vgl.sourceDataP3fv(arg);
}
vgl.sourceData.call(this, arg);
this.addAttribute(vgl.vertexAttributeKeys.Position, vgl.GL.FLOAT, 4, 0, 3 * 4, 3,
false);
this.pushBack = function (value) {
this.insert(value);
};
return this;
};
inherit(vgl.sourceDataP3fv, vgl.sourceData);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class sourceDataT2fv
*
* @returns {vgl.sourceDataT2fv}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.sourceDataT2fv = function (arg) {
'use strict';
if (!(this instanceof vgl.sourceDataT2fv)) {
return new vgl.sourceDataT2fv(arg);
}
vgl.sourceData.call(this, arg);
this.addAttribute(vgl.vertexAttributeKeys.TextureCoordinate, vgl.GL.FLOAT, 4, 0,
2 * 4, 2, false);
this.pushBack = function (value) {
this.insert(value);
};
return this;
};
inherit(vgl.sourceDataT2fv, vgl.sourceData);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class sourceDataC3fv
*
* @returns {vgl.sourceDataC3fv}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.sourceDataC3fv = function (arg) {
'use strict';
if (!(this instanceof vgl.sourceDataC3fv)) {
return new vgl.sourceDataC3fv(arg);
}
vgl.sourceData.call(this, arg);
this.addAttribute(vgl.vertexAttributeKeys.Color, vgl.GL.FLOAT, 4, 0, 3 * 4, 3, false);
this.pushBack = function (value) {
this.insert(value);
};
return this;
};
inherit(vgl.sourceDataC3fv, vgl.sourceData);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class sourceDataSf meant to hold scalar float values
*
* @class
* @returns {vgl.sourceDataSf}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.sourceDataSf = function (arg) {
'use strict';
if (!(this instanceof vgl.sourceDataSf)) {
return new vgl.sourceDataSf(arg);
}
var m_min = null,
m_max = null,
m_fixedmin = null,
m_fixedmax = null;
vgl.sourceData.call(this, arg);
this.addAttribute(vgl.vertexAttributeKeys.Scalar, vgl.GL.FLOAT, 4, 0, 4, 1, false);
this.pushBack = function (value) {
if (m_max === null || value > m_max) {
m_max = value;
}
if (m_min === null || value < m_min) {
m_min = value;
}
//this.insert(value); //no, slow on Safari
this.data()[this.data().length] = value;
};
this.insertAt = function (index, value) {
if (m_max === null || value > m_max) {
m_max = value;
}
if (m_min === null || value < m_min) {
m_min = value;
}
//call superclass ??
//vgl.sourceData.insertAt.call(this, index, value);
this.data()[index] = value;
};
this.scalarRange = function () {
if (m_fixedmin === null || m_fixedmax === null) {
return [m_min, m_max];
}
return [m_fixedmin, m_fixedmax];
};
this.setScalarRange = function (min, max) {
m_fixedmin = min;
m_fixedmax = max;
};
return this;
};
inherit(vgl.sourceDataSf, vgl.sourceData);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class sourceDataDf meant to hold data float values
*
* This source array is the best way to pass a array of floats to the shader
* that has one entry for each of the vertices.
*
* @class
* @returns {vgl.sourceDataDf}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.sourceDataDf = function (arg) {
'use strict';
if (!(this instanceof vgl.sourceDataDf)) {
return new vgl.sourceDataDf(arg);
}
vgl.sourceData.call(this, arg);
this.addAttribute(vgl.vertexAttributeKeys.Scalar, vgl.GL.FLOAT,
4, 0, 4, 1, false);
this.pushBack = function (value) {
this.data()[this.data().length] = value;
};
this.insertAt = function (index, value) {
this.data()[index] = value;
};
return this;
};
inherit(vgl.sourceDataDf, vgl.sourceData);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class geometryData
*
* @class
* @returns {vgl.geometryData}
*/
/////////////////////////////////////////////////////////////////////////////
vgl.geometryData = function () {
'use strict';
if (!(this instanceof vgl.geometryData)) {
return new vgl.geometryData();
}
vgl.data.call(this);
/** @private */
var m_name = '',
m_primitives = [],
m_sources = [],
m_bounds = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
m_computeBoundsTimestamp = vgl.timestamp(),
m_boundsDirtyTimestamp = vgl.timestamp();
////////////////////////////////////////////////////////////////////////////
/**
* Return type
*/
////////////////////////////////////////////////////////////////////////////
this.type = function () {
return vgl.data.geometry;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return ID of the geometry data
*/
////////////////////////////////////////////////////////////////////////////
this.name = function () {
return m_name;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set name of the geometry data
*/
////////////////////////////////////////////////////////////////////////////
this.setName = function (name) {
m_name = name;
};
////////////////////////////////////////////////////////////////////////////
/**
* Add new source
*/
////////////////////////////////////////////////////////////////////////////
this.addSource = function (source, sourceName) {
// @todo Check if the incoming source has duplicate keys
if (sourceName !== undefined) {
source.setName(sourceName);
}
// NOTE This might not work on IE8 or lower
if (m_sources.indexOf(source) === -1) {
m_sources.push(source);
if (source.hasKey(vgl.vertexAttributeKeys.Position)) {
m_boundsDirtyTimestamp.modified();
}
return true;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return source for a given index. Returns 0 if not found.
*/
////////////////////////////////////////////////////////////////////////////
this.source = function (index) {
if (index < m_sources.length) {
return m_sources[index];
}
return 0;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return source with a specified name. Returns 0 if not found.
*/
////////////////////////////////////////////////////////////////////////////
this.sourceByName = function (sourceName) {
for (var i = 0; i < m_sources.length; i += 1) {
if (m_sources[i].name() === sourceName) {
return m_sources[i];
}
}
return 0;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return number of sources
*/
////////////////////////////////////////////////////////////////////////////
this.numberOfSources = function () {
return m_sources.length;
};
/**
* Return source data given a key
*/
this.sourceData = function (key) {
var i;
for (i = 0; i < m_sources.length; i += 1) {
if (m_sources[i].hasKey(key)) {
return m_sources[i];
}
}
return null;
};
////////////////////////////////////////////////////////////////////////////
/**
* Add new primitive
*/
////////////////////////////////////////////////////////////////////////////
this.addPrimitive = function (primitive) {
m_primitives.push(primitive);
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return primitive for a given index. Returns null if not found.
*/
////////////////////////////////////////////////////////////////////////////
this.primitive = function (index) {
if (index < m_primitives.length) {
return m_primitives[index];
}
return null;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return number of primitives
*/
////////////////////////////////////////////////////////////////////////////
this.numberOfPrimitives = function () {
return m_primitives.length;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return bounds [minX, maxX, minY, maxY, minZ, maxZ]
*/
////////////////////////////////////////////////////////////////////////////
this.bounds = function () {
if (m_boundsDirtyTimestamp.getMTime() > m_computeBoundsTimestamp.getMTime()) {
this.computeBounds();
}
return m_bounds;
};
////////////////////////////////////////////////////////////////////////////
/**
* Check if bounds are dirty or mark them as such.
*
* @param dirty: true to set bounds as dirty.
* Return true if bounds are dirty.
*/
////////////////////////////////////////////////////////////////////////////
this.boundsDirty = function (dirty) {
if (dirty) {
m_boundsDirtyTimestamp.modified();
}
return m_boundsDirtyTimestamp.getMTime() > m_computeBoundsTimestamp.getMTime();
};
////////////////////////////////////////////////////////////////////////////
/**
* Reset bounds
*/
////////////////////////////////////////////////////////////////////////////
this.resetBounds = function () {
m_bounds[0] = 0.0;
m_bounds[1] = 0.0;
m_bounds[2] = 0.0;
m_bounds[3] = 0.0;
m_bounds[4] = 0.0;
m_bounds[5] = 0.0;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set bounds
*/
////////////////////////////////////////////////////////////////////////////
this.setBounds = function (minX, maxX, minY, maxY, minZ, maxZ) {
m_bounds[0] = minX;
m_bounds[1] = maxX;
m_bounds[2] = minY;
m_bounds[3] = maxY;
m_bounds[4] = minZ;
m_bounds[5] = maxZ;
m_computeBoundsTimestamp.modified();
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute bounds
*/
////////////////////////////////////////////////////////////////////////////
this.computeBounds = function () {
if (m_boundsDirtyTimestamp.getMTime() > m_computeBoundsTimestamp.getMTime()) {
var attr = vgl.vertexAttributeKeys.Position,
sourceData = this.sourceData(attr),
data = sourceData.data(),
numberOfComponents = sourceData.attributeNumberOfComponents(attr),
stride = sourceData.attributeStride(attr),
offset = sourceData.attributeOffset(attr),
sizeOfDataType = sourceData.sizeOfAttributeDataType(attr),
count = data.length,
j, ib, jb, maxv, minv,
value = null,
vertexIndex;
// We advance by index, not by byte
stride /= sizeOfDataType;
offset /= sizeOfDataType;
this.resetBounds();
for (j = 0; j < numberOfComponents; j += 1) {
ib = j * 2;
jb = j * 2 + 1;
if (count) {
maxv = minv = m_bounds[jb] = data[offset + j];
} else {
maxv = minv = 0;
}
for (vertexIndex = offset + stride + j; vertexIndex < count;
vertexIndex += stride) {
value = data[vertexIndex];
if (value > maxv) {
maxv = value;
}
if (value < minv) {
minv = value;
}
}
m_bounds[ib] = minv; m_bounds[jb] = maxv;
}
m_computeBoundsTimestamp.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Returns the vertex closest to a given position
*/
////////////////////////////////////////////////////////////////////////////
this.findClosestVertex = function (point) {
var attr = vgl.vertexAttributeKeys.Position,
sourceData = this.sourceData(attr),
sizeOfDataType = sourceData.sizeOfAttributeDataType(attr),
numberOfComponents = sourceData.attributeNumberOfComponents(attr),
data = sourceData.data(),
stride = sourceData.attributeStride(attr) / sizeOfDataType,
offset = sourceData.attributeOffset(attr) / sizeOfDataType,
minDist = Number.MAX_VALUE,
minIndex = null,
vi, vPos, dx, dy, dz, dist, i;
// assume positions are always triplets
if (numberOfComponents !== 3) {
console.log('[warning] Find closest vertex assumes three' +
'component vertex ');
}
if (!point.z) {
point = {x: point.x, y: point.y, z: 0};
}
for (vi = offset, i = 0; vi < data.length; vi += stride, i += 1) {
vPos = [data[vi],
data[vi + 1],
data[vi + 2]];
dx = vPos[0] - point.x;
dy = vPos[1] - point.y;
dz = vPos[2] - point.z;
dist = Math.sqrt(dx * dx + dy * dy + dz * dz);
if (dist < minDist) {
minDist = dist;
minIndex = i;
}
}
return minIndex;
};
////////////////////////////////////////////////////////////////////////////
/**
* Returns the requested vertex position
*/
////////////////////////////////////////////////////////////////////////////
this.getPosition = function (index) {
var attr = vgl.vertexAttributeKeys.Position,
sourceData = this.sourceData(attr),
sizeOfDataType = sourceData.sizeOfAttributeDataType(attr),
numberOfComponents = sourceData.attributeNumberOfComponents(attr),
data = sourceData.data(),
stride = sourceData.attributeStride(attr) / sizeOfDataType,
offset = sourceData.attributeOffset(attr) / sizeOfDataType;
// assume positions are always triplets
if (numberOfComponents !== 3) {
console.log('[warning] getPosition assumes three component data');
}
return [data[offset + index * stride],
data[offset + index * stride + 1],
data[offset + index * stride + 2]];
};
////////////////////////////////////////////////////////////////////////////
/**
* Returns the scalar corresponding to a given vertex index
*/
////////////////////////////////////////////////////////////////////////////
this.getScalar = function (index) {
var attr = vgl.vertexAttributeKeys.Scalar,
sourceData = this.sourceData(attr),
numberOfComponents, sizeOfDataType, data, stride, offset;
if (!sourceData) {
return null;
}
numberOfComponents = sourceData.attributeNumberOfComponents(attr);
sizeOfDataType = sourceData.sizeOfAttributeDataType(attr);
data = sourceData.data();
stride = sourceData.attributeStride(attr) / sizeOfDataType;
offset = sourceData.attributeOffset(attr) / sizeOfDataType;
//console.log('index for scalar is ' + index);
//console.log('offset for scalar is ' + offset);
//console.log('stride for scalar is ' + stride);
//console.log('have ' + data.length + ' scalars');
if (index * stride + offset >= data.length) {
console.log('access out of bounds in getScalar');
}
return data[index * stride + offset];
};
return this;
};
inherit(vgl.geometryData, vgl.data);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, Float32Array, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class mapper
*
* @class
* @returns {vgl.mapper}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.mapper = function (arg) {
'use strict';
if (!(this instanceof vgl.mapper)) {
return new vgl.mapper(arg);
}
vgl.boundingObject.call(this);
/** @private */
arg = arg || {};
var m_dirty = true,
m_color = [0.0, 1.0, 1.0],
m_geomData = null,
m_buffers = [],
m_bufferVertexAttributeMap = {},
m_dynamicDraw = arg.dynamicDraw === undefined ? false : arg.dynamicDraw,
m_glCompileTimestamp = vgl.timestamp(),
m_context = null,
m_this = this;
////////////////////////////////////////////////////////////////////////////
/**
* Delete cached VBO if any
*/
////////////////////////////////////////////////////////////////////////////
this.deleteVertexBufferObjects = function (renderState) {
var i;
var context = m_context;
if (renderState) {
context = renderState.m_context;
}
if (context) {
for (i = 0; i < m_buffers.length; i += 1) {
context.deleteBuffer(m_buffers[i]);
}
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Create new VBO for all its geometryData sources and primitives
*
* @private
*/
////////////////////////////////////////////////////////////////////////////
function createVertexBufferObjects(renderState) {
if (m_geomData) {
if (renderState) {
m_context = renderState.m_context;
}
var numberOfSources = m_geomData.numberOfSources(),
i, j, k, bufferId = null, keys, ks, numberOfPrimitives, data;
for (i = 0; i < numberOfSources; i += 1) {
bufferId = m_context.createBuffer();
m_context.bindBuffer(vgl.GL.ARRAY_BUFFER, bufferId);
data = m_geomData.source(i).data();
if (!(data instanceof Float32Array)) {
data = new Float32Array(data);
}
m_context.bufferData(vgl.GL.ARRAY_BUFFER, data,
m_dynamicDraw ? vgl.GL.DYNAMIC_DRAW :
vgl.GL.STATIC_DRAW);
keys = m_geomData.source(i).keys();
ks = [];
for (j = 0; j < keys.length; j += 1) {
ks.push(keys[j]);
}
m_bufferVertexAttributeMap[i] = ks;
m_buffers[i] = bufferId;
}
numberOfPrimitives = m_geomData.numberOfPrimitives();
for (k = 0; k < numberOfPrimitives; k += 1) {
bufferId = m_context.createBuffer();
m_context.bindBuffer(vgl.GL.ARRAY_BUFFER, bufferId);
m_context.bufferData(vgl.GL.ARRAY_BUFFER,
m_geomData.primitive(k).indices(), vgl.GL.STATIC_DRAW);
m_buffers[i] = bufferId;
i += 1;
}
m_glCompileTimestamp.modified();
}
}
////////////////////////////////////////////////////////////////////////////
/**
* Clear cache related to buffers
*
* @private
*/
////////////////////////////////////////////////////////////////////////////
function cleanUpDrawObjects(renderState) {
renderState = renderState; /* avoid unused warning */
m_bufferVertexAttributeMap = {};
m_buffers = [];
}
////////////////////////////////////////////////////////////////////////////
/**
* Setup draw objects; Delete old ones and create new ones
*
* @private
*/
////////////////////////////////////////////////////////////////////////////
function setupDrawObjects(renderState) {
// Delete buffer objects from past if any.
m_this.deleteVertexBufferObjects(renderState);
// Clear any cache related to buffers
cleanUpDrawObjects(renderState);
// Now construct the new ones.
createVertexBufferObjects(renderState);
m_dirty = false;
}
////////////////////////////////////////////////////////////////////////////
/**
* Compute bounds of the data
*/
////////////////////////////////////////////////////////////////////////////
this.computeBounds = function () {
if (m_geomData === null || typeof m_geomData === 'undefined') {
this.resetBounds();
return;
}
var computeBoundsTimestamp = this.computeBoundsTimestamp(),
boundsDirtyTimestamp = this.boundsDirtyTimestamp(),
geomBounds = null;
if (boundsDirtyTimestamp.getMTime() > computeBoundsTimestamp.getMTime()) {
geomBounds = m_geomData.bounds();
this.setBounds(geomBounds[0], geomBounds[1], geomBounds[2],
geomBounds[3], geomBounds[4], geomBounds[5]) ;
computeBoundsTimestamp.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get solid color of the geometry
*/
////////////////////////////////////////////////////////////////////////////
this.color = function () {
return m_color;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set solid color of the geometry. Default is teal [1.0, 1.0, 1.0]
*
* @param r Red component of the color [0.0 - 1.0]
* @param g Green component of the color [0.0 - 1.0]
* @param b Blue component of the color [0.0 - 1.0]
*/
////////////////////////////////////////////////////////////////////////////
this.setColor = function (r, g, b) {
m_color[0] = r;
m_color[1] = g;
m_color[2] = b;
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Return stored geometry data if any
*/
////////////////////////////////////////////////////////////////////////////
this.geometryData = function () {
return m_geomData;
};
////////////////////////////////////////////////////////////////////////////
/**
* Connect mapper to its geometry data
*/
////////////////////////////////////////////////////////////////////////////
this.setGeometryData = function (geom) {
if (m_geomData !== geom) {
m_geomData = geom;
this.modified();
this.boundsDirtyTimestamp().modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Update the buffer used for a named source.
*
* @param {String} sourceName The name of the source to update.
* @param {Object[] or Float32Array} values The values to use for the source.
* If not specified, use the source's own buffer.
*/
////////////////////////////////////////////////////////////////////////////
this.updateSourceBuffer = function (sourceName, values, renderState) {
if (renderState) {
m_context = renderState.m_context;
}
if (!m_context) {
return false;
}
var bufferIndex = -1;
for (var i = 0; i < m_geomData.numberOfSources(); i += 1) {
if (m_geomData.source(i).name() === sourceName) {
bufferIndex = i;
break;
}
}
if (bufferIndex < 0 || bufferIndex >= m_buffers.length) {
return false;
}
if (!values) {
values = m_geomData.source(i).dataToFloat32Array();
}
m_context.bindBuffer(vgl.GL.ARRAY_BUFFER, m_buffers[bufferIndex]);
if (values instanceof Float32Array) {
m_context.bufferSubData(vgl.GL.ARRAY_BUFFER, 0, values);
} else {
m_context.bufferSubData(vgl.GL.ARRAY_BUFFER, 0,
new Float32Array(values));
}
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the buffer used for a named source. If the current buffer isn't a
* Float32Array, it is converted to one. This array can then be modified
* directly, after which updateSourceBuffer can be called to update the
* GL array.
*
* @param {String} sourceName The name of the source to update.
* @returns {Float32Array} An array used for this source.
*/
////////////////////////////////////////////////////////////////////////////
this.getSourceBuffer = function (sourceName) {
var source = m_geomData.sourceByName(sourceName);
if (!source) {
return new Float32Array();
}
return source.dataToFloat32Array();
};
////////////////////////////////////////////////////////////////////////////
/**
* Render the mapper
*/
////////////////////////////////////////////////////////////////////////////
this.render = function (renderState) {
if (this.getMTime() > m_glCompileTimestamp.getMTime() ||
renderState.m_contextChanged) {
setupDrawObjects(renderState);
}
m_context = renderState.m_context;
// Fixed vertex color
m_context.vertexAttrib3fv(vgl.vertexAttributeKeys.Color, this.color());
// TODO Use renderState
var bufferIndex = 0,
j = 0, i, noOfPrimitives = null, primitive = null;
for (i in m_bufferVertexAttributeMap) {
if (m_bufferVertexAttributeMap.hasOwnProperty(i)) {
m_context.bindBuffer(vgl.GL.ARRAY_BUFFER,
m_buffers[bufferIndex]);
for (j = 0; j < m_bufferVertexAttributeMap[i].length; j += 1) {
renderState.m_material
.bindVertexData(renderState, m_bufferVertexAttributeMap[i][j]);
}
bufferIndex += 1;
}
}
noOfPrimitives = m_geomData.numberOfPrimitives();
for (j = 0; j < noOfPrimitives; j += 1) {
m_context.bindBuffer(vgl.GL.ARRAY_BUFFER, m_buffers[bufferIndex]);
bufferIndex += 1;
primitive = m_geomData.primitive(j);
switch (primitive.primitiveType()) {
case vgl.GL.POINTS:
m_context.drawArrays(vgl.GL.POINTS, 0, primitive.numberOfIndices());
break;
case vgl.GL.LINES:
m_context.drawArrays(vgl.GL.LINES, 0, primitive.numberOfIndices());
break;
case vgl.GL.LINE_STRIP:
m_context.drawArrays(vgl.GL.LINE_STRIP, 0, primitive.numberOfIndices());
break;
case vgl.GL.TRIANGLES:
m_context.drawArrays(vgl.GL.TRIANGLES, 0, primitive.numberOfIndices());
break;
case vgl.GL.TRIANGLE_STRIP:
m_context.drawArrays(vgl.GL.TRIANGLE_STRIP, 0, primitive.numberOfIndices());
break;
}
m_context.bindBuffer (vgl.GL.ARRAY_BUFFER, null);
}
};
return this;
};
inherit(vgl.mapper, vgl.boundingObject);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
vgl.groupMapper = function () {
'use strict';
if (!(this instanceof vgl.groupMapper)) {
return new vgl.groupMapper();
}
vgl.mapper.call(this);
/** @private */
var m_createMappersTimestamp = vgl.timestamp(),
m_mappers = [],
m_geomDataArray = [];
////////////////////////////////////////////////////////////////////////////
/**
* Return stored geometry data if any
*
* @param index optional
*/
////////////////////////////////////////////////////////////////////////////
this.geometryData = function (index) {
if (index !== undefined && index < m_geomDataArray.length) {
return m_geomDataArray[index];
}
if (m_geomDataArray.length > 0) {
return m_geomDataArray[0];
}
return null;
};
////////////////////////////////////////////////////////////////////////////
/**
* Connect mapper to its geometry data
*
* @param geom {vgl.geomData}
*/
////////////////////////////////////////////////////////////////////////////
this.setGeometryData = function (geom) {
if (m_geomDataArray.length === 1) {
if (m_geomDataArray[0] === geom) {
return;
}
}
m_geomDataArray = [];
m_geomDataArray.push(geom);
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Return stored geometry data array if any
*/
////////////////////////////////////////////////////////////////////////////
this.geometryDataArray = function () {
return m_geomDataArray;
};
////////////////////////////////////////////////////////////////////////////
/**
* Connect mapper to its geometry data
*
* @param geoms {Array}
*/
////////////////////////////////////////////////////////////////////////////
this.setGeometryDataArray = function (geoms) {
if (geoms instanceof Array) {
if (m_geomDataArray !== geoms) {
m_geomDataArray = [];
m_geomDataArray = geoms;
this.modified();
return true;
}
} else {
console.log('[error] Requies array of geometry data');
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute bounds of the data
*/
////////////////////////////////////////////////////////////////////////////
this.computeBounds = function () {
if (m_geomDataArray === null ||
m_geomDataArray === undefined) {
this.resetBounds();
return;
}
var computeBoundsTimestamp = this.computeBoundsTimestamp(),
boundsDirtyTimestamp = this.boundsDirtyTimestamp(),
m_bounds = this.bounds(),
geomBounds = null,
i = null;
if (boundsDirtyTimestamp.getMTime() >
computeBoundsTimestamp.getMTime()) {
for (i = 0; i < m_geomDataArray.length; i += 1) {
geomBounds = m_geomDataArray[i].bounds();
if (m_bounds[0] > geomBounds[0]) {
m_bounds[0] = geomBounds[0];
}
if (m_bounds[1] < geomBounds[1]) {
m_bounds[1] = geomBounds[1];
}
if (m_bounds[2] > geomBounds[2]) {
m_bounds[2] = geomBounds[2];
}
if (m_bounds[3] < geomBounds[3]) {
m_bounds[3] = geomBounds[3];
}
if (m_bounds[4] > geomBounds[4]) {
m_bounds[4] = geomBounds[4];
}
if (m_bounds[5] < geomBounds[5]) {
m_bounds[5] = geomBounds[5];
}
}
this.modified();
computeBoundsTimestamp.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Render the mapper
*/
////////////////////////////////////////////////////////////////////////////
this.render = function (renderState) {
var i = null;
if (this.getMTime() > m_createMappersTimestamp.getMTime()) {
// NOTE Hoping that it will release the graphics resources
for (i = 0; i < m_geomDataArray.length; i += 1) {
m_mappers.push(vgl.mapper());
m_mappers[i].setGeometryData(m_geomDataArray[i]);
}
m_createMappersTimestamp.modified();
}
for (i = 0; i < m_mappers.length; i += 1) {
m_mappers[i].render(renderState);
}
};
return this;
};
inherit(vgl.groupMapper, vgl.mapper);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
vgl.materialAttributeType = {
'Undefined' : 0x0,
'ShaderProgram' : 0x1,
'Texture' : 0x2,
'Blend' : 0x3,
'Depth' : 0x4
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class materialAttribute
*
* @class
* @param type
* @returns {vgl.materialAttribute}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.materialAttribute = function (type) {
'use strict';
if (!(this instanceof vgl.materialAttribute)) {
return new vgl.materialAttribute();
}
vgl.graphicsObject.call(this);
/** @private */
var m_this = this,
m_type = type,
m_enabled = true;
////////////////////////////////////////////////////////////////////////////
/**
* Return tyep of the material attribute
*
* @returns {*}
*/
////////////////////////////////////////////////////////////////////////////
this.type = function () {
return m_type;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return if material attribute is enabled or not
*
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.enabled = function () {
return m_enabled;
};
////////////////////////////////////////////////////////////////////////////
/**
* Bind and activate vertex specific data
*
* @param renderState
* @param key
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.bindVertexData = function (renderState, key) {
renderState = renderState; /* unused parameter */
key = key /* unused parameter */;
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Undo bind and deactivate vertex specific data
*
* @param renderState
* @param key
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.undoBindVertexData = function (renderState, key) {
renderState = renderState; /* unused parameter */
key = key /* unused parameter */;
return false;
};
return m_this;
};
inherit(vgl.materialAttribute, vgl.graphicsObject);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of clas blendFunction
*
* @class
* @param source
* @param destination
* @returns {vgl.blendFunction}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.blendFunction = function (source, destination) {
'use strict';
if (!(this instanceof vgl.blendFunction)) {
return new vgl.blendFunction(source, destination);
}
/** @private */
var m_source = source,
m_destination = destination;
////////////////////////////////////////////////////////////////////////////
/**
* Apply blend function to the current state
*
* @param {vgl.renderState}
*/
////////////////////////////////////////////////////////////////////////////
this.apply = function (renderState) {
renderState.m_context.blendFuncSeparate(m_source, m_destination,
vgl.GL.ONE, vgl.GL.ONE_MINUS_SRC_ALPHA);
};
return this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class blend
*
* @returns {vgl.blend}
*/
////////////////////////////////////////////////////////////////////////////
vgl.blend = function () {
'use strict';
if (!(this instanceof vgl.blend)) {
return new vgl.blend();
}
vgl.materialAttribute.call(
this, vgl.materialAttributeType.Blend);
/** @private */
var m_wasEnabled = false,
m_blendFunction = vgl.blendFunction(vgl.GL.SRC_ALPHA,
vgl.GL.ONE_MINUS_SRC_ALPHA);
////////////////////////////////////////////////////////////////////////////
/**
* Bind blend attribute
*
* @param {vgl.renderState}
*/
////////////////////////////////////////////////////////////////////////////
this.bind = function (renderState) {
m_wasEnabled = renderState.m_context.isEnabled(vgl.GL.BLEND);
if (this.enabled()) {
renderState.m_context.enable(vgl.GL.BLEND);
m_blendFunction.apply(renderState);
} else {
renderState.m_context.disable(vgl.GL.BLEND);
}
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Undo bind blend attribute
*
* @param {vgl.renderState}
*/
////////////////////////////////////////////////////////////////////////////
this.undoBind = function (renderState) {
if (m_wasEnabled) {
renderState.m_context.enable(vgl.GL.BLEND);
} else {
renderState.m_context.disable(vgl.GL.BLEND);
}
return true;
};
return this;
};
inherit(vgl.blend, vgl.materialAttribute);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class material
*
* @class
* @returns {vgl.material}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.material = function () {
'use strict';
if (!(this instanceof vgl.material)) {
return new vgl.material();
}
vgl.graphicsObject.call(this);
// / Private member variables
var m_this = this,
m_shaderProgram = new vgl.shaderProgram(),
m_binNumber = 100,
m_textureAttributes = {},
m_attributes = {};
////////////////////////////////////////////////////////////////////////////
/**
* Return bin number for the material
*
* @default 100
* @returns {number}
*/
////////////////////////////////////////////////////////////////////////////
this.binNumber = function () {
return m_binNumber;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set bin number for the material
*
* @param binNo
*/
////////////////////////////////////////////////////////////////////////////
this.setBinNumber = function (binNo) {
m_binNumber = binNo;
m_this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Check if incoming attribute already exists in the material
*
* @param attr
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.exists = function (attr) {
if (attr.type() === vgl.materialAttribute.Texture) {
return m_textureAttributes.hasOwnProperty(attr);
}
return m_attributes.hasOwnProperty(attr);
};
////////////////////////////////////////////////////////////////////////////
/**
* Get uniform given a name
* @param name Uniform name
* @returns {vgl.uniform}
*/
////////////////////////////////////////////////////////////////////////////
this.uniform = function (name) {
if (m_shaderProgram) {
return m_shaderProgram.uniform(name);
}
return null;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get material attribute
* @param attr Attribute name
* @returns {vgl.materialAttribute}
*/
////////////////////////////////////////////////////////////////////////////
this.attribute = function (name) {
if (m_attributes.hasOwnProperty(name)) {
return m_attributes[name];
}
if (m_textureAttributes.hasOwnProperty(name)) {
return m_textureAttributes[name];
}
return null;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set a new attribute for the material
*
* This method replace any existing attribute except for textures as
* materials can have multiple textures.
*
* @param attr
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.setAttribute = function (attr) {
if (attr.type() === vgl.materialAttributeType.Texture &&
m_textureAttributes[attr.textureUnit()] !== attr) {
m_textureAttributes[attr.textureUnit()] = attr;
m_this.modified();
return true;
}
if (m_attributes[attr.type()] === attr) {
return false;
}
// Shader is a very special attribute
if (attr.type() === vgl.materialAttributeType.ShaderProgram) {
m_shaderProgram = attr;
}
m_attributes[attr.type()] = attr;
m_this.modified();
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Add a new attribute to the material.
*
* @param attr
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.addAttribute = function (attr) {
if (m_this.exists(attr)) {
return false;
}
if (attr.type() === vgl.materialAttributeType.Texture) {
// TODO Currently we don't check if we are replacing or not.
// It would be nice to have a flag for it.
m_textureAttributes[attr.textureUnit()] = attr;
m_this.modified();
return true;
}
// Shader is a very special attribute
if (attr.type() === vgl.materialAttributeType.ShaderProgram) {
m_shaderProgram = attr;
}
m_attributes[attr.type()] = attr;
m_this.modified();
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return shader program used by the material
*
* @returns {vgl.shaderProgram}
*/
////////////////////////////////////////////////////////////////////////////
this.shaderProgram = function () {
return m_shaderProgram;
};
////////////////////////////////////////////////////////////////////////////
/**
* Setup (initialize) the material attribute
*
* @param renderState
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this._setup = function (renderState) {
renderState = renderState; /* unused parameter */
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Remove any resources acquired before deletion
*
* @param renderState
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this._cleanup = function (renderState) {
for (var key in m_attributes) {
if (m_attributes.hasOwnProperty(key)) {
m_attributes[key]._cleanup(renderState);
}
}
for (key in m_textureAttributes) {
if (m_textureAttributes.hasOwnProperty(key)) {
m_textureAttributes[key]._cleanup(renderState);
}
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Bind and activate material states
*
* @param renderState
*/
////////////////////////////////////////////////////////////////////////////
this.bind = function (renderState) {
var key = null;
m_shaderProgram.bind(renderState);
for (key in m_attributes) {
if (m_attributes.hasOwnProperty(key)) {
if (m_attributes[key] !== m_shaderProgram) {
m_attributes[key].bind(renderState);
}
}
}
for (key in m_textureAttributes) {
if (m_textureAttributes.hasOwnProperty(key)) {
m_textureAttributes[key].bind(renderState);
}
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Undo-bind and de-activate material states
*
* @param renderState
*/
////////////////////////////////////////////////////////////////////////////
this.undoBind = function (renderState) {
var key = null;
for (key in m_attributes) {
if (m_attributes.hasOwnProperty(key)) {
m_attributes[key].undoBind(renderState);
}
}
for (key in m_textureAttributes) {
if (m_textureAttributes.hasOwnProperty(key)) {
m_textureAttributes[key].undoBind(renderState);
}
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Bind vertex data
*
* @param renderState
* @param key
*/
////////////////////////////////////////////////////////////////////////////
this.bindVertexData = function (renderState, key) {
var i = null;
for (i in m_attributes) {
if (m_attributes.hasOwnProperty(i)) {
m_attributes[i].bindVertexData(renderState, key);
}
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Undo bind vertex data
*
* @param renderState
* @param key
*/
////////////////////////////////////////////////////////////////////////////
this.undoBindVertexData = function (renderState, key) {
var i = null;
for (i in m_attributes) {
if (m_attributes.hasOwnProperty(i)) {
m_attributes.undoBindVertexData(renderState, key);
}
}
};
return m_this;
};
vgl.material.RenderBin = {
'Base' : 0,
'Default' : 100,
'Opaque' : 100,
'Transparent' : 1000,
'Overlay' : 10000
};
inherit(vgl.material, vgl.graphicsObject);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, vec2, vec3, vec4, mat4, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class renderState
*
* @returns {vgl.renderState}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.renderState = function () {
'use strict';
this.m_context = null;
this.m_modelViewMatrix = mat4.create();
this.m_normalMatrix = mat4.create();
this.m_projectionMatrix = null;
this.m_material = null;
this.m_mapper = null;
};
////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class renderer *
*
* @returns {vgl.renderer}
*/
////////////////////////////////////////////////////////////////////////////
vgl.renderer = function (arg) {
'use strict';
if (!(this instanceof vgl.renderer)) {
return new vgl.renderer(arg);
}
vgl.graphicsObject.call(this);
arg = arg || {};
/** @private */
var m_this = this;
m_this.m_renderWindow = null;
m_this.m_contextChanged = false;
m_this.m_sceneRoot = new vgl.groupNode();
m_this.m_camera = new vgl.camera(arg);
m_this.m_nearClippingPlaneTolerance = null;
m_this.m_x = 0;
m_this.m_y = 0;
m_this.m_width = 0;
m_this.m_height = 0;
m_this.m_resizable = true;
m_this.m_resetScene = true;
m_this.m_layer = 0;
m_this.m_renderPasses = null;
m_this.m_resetClippingRange = true;
m_this.m_depthBits = null;
m_this.m_camera.addChild(m_this.m_sceneRoot);
////////////////////////////////////////////////////////////////////////////
/**
* Get width of the renderer
*/
////////////////////////////////////////////////////////////////////////////
this.width = function () {
return m_this.m_width;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get height of the renderer
*/
////////////////////////////////////////////////////////////////////////////
this.height = function () {
return m_this.m_height;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get layer this renderer is associated with
*
* @return {Number}
*/
////////////////////////////////////////////////////////////////////////////
this.layer = function () {
return m_this.m_layer;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set the layer this renderer is associated with.
*
* @param layerNo
*/
////////////////////////////////////////////////////////////////////////////
this.setLayer = function (layerNo) {
m_this.m_layer = layerNo;
m_this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
*
*/
////////////////////////////////////////////////////////////////////////////
this.isResizable = function () {
return m_this.m_resizable;
};
////////////////////////////////////////////////////////////////////////////
/**
*
*/
////////////////////////////////////////////////////////////////////////////
this.setResizable = function (r) {
m_this.m_resizable = r;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return render window (owner) of the renderer
*/
////////////////////////////////////////////////////////////////////////////
this.renderWindow = function () {
return m_this.m_renderWindow;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set render window for the renderer
*/
////////////////////////////////////////////////////////////////////////////
this.setRenderWindow = function (renWin) {
if (m_this.m_renderWindow !== renWin) {
if (m_this.m_renderWindow) {
m_this.m_renderWindow.removeRenderer(this);
}
m_this.m_renderWindow = renWin;
m_this.m_contextChanged = true;
m_this.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get background color
*/
////////////////////////////////////////////////////////////////////////////
this.backgroundColor = function () {
return m_this.m_camera.clearColor();
};
////////////////////////////////////////////////////////////////////////////
/**
* Set background color of the renderer
*
* @param r
* @param g
* @param b
* @param a
*/
////////////////////////////////////////////////////////////////////////////
this.setBackgroundColor = function (r, g, b, a) {
m_this.m_camera.setClearColor(r, g, b, a);
m_this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Get scene root
*
* @returns {vgl.groupNode}
*/
////////////////////////////////////////////////////////////////////////////
this.sceneRoot = function () {
return m_this.m_sceneRoot;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get main camera of the renderer
*
* @returns {vgl.camera}
*/
////////////////////////////////////////////////////////////////////////////
this.camera = function () {
return m_this.m_camera;
};
////////////////////////////////////////////////////////////////////////////
/**
* Render the scene
*/
////////////////////////////////////////////////////////////////////////////
this.render = function () {
var i, renSt, children, actor = null, sortedActors = [],
mvMatrixInv = mat4.create(), clearColor = null;
renSt = new vgl.renderState();
renSt.m_renderer = m_this;
renSt.m_context = m_this.renderWindow().context();
if (!m_this.m_depthBits || m_this.m_contextChanged) {
m_this.m_depthBits = renSt.m_context.getParameter(vgl.GL.DEPTH_BITS);
}
renSt.m_contextChanged = m_this.m_contextChanged;
if (m_this.m_renderPasses) {
for (i = 0; i < m_this.m_renderPasses.length; i += 1) {
if (m_this.m_renderPasses[i].render(renSt)) {
// Stop the rendering if render pass returns false
console.log('returning');
m_this.m_renderPasses[i].remove(renSt);
return;
}
m_this.m_renderPasses[i].remove(renSt);
}
}
renSt.m_context.enable(vgl.GL.DEPTH_TEST);
renSt.m_context.depthFunc(vgl.GL.LEQUAL);
/*jshint bitwise: false */
if (m_this.m_camera.clearMask() & vgl.GL.COLOR_BUFFER_BIT) {
clearColor = m_this.m_camera.clearColor();
renSt.m_context.clearColor(clearColor[0], clearColor[1],
clearColor[2], clearColor[3]);
}
if (m_this.m_camera.clearMask() & vgl.GL.DEPTH_BUFFER_BIT) {
renSt.m_context.clearDepth(m_this.m_camera.clearDepth());
}
/*jshint bitwise: true */
renSt.m_context.clear(m_this.m_camera.clearMask());
// Set the viewport for this renderer
renSt.m_context.viewport(m_this.m_x, m_this.m_y,
m_this.m_width, m_this.m_height);
children = m_this.m_sceneRoot.children();
if (children.length > 0 && m_this.m_resetScene) {
m_this.resetCamera();
m_this.m_resetScene = false;
}
for (i = 0; i < children.length; i += 1) {
actor = children[i];
// Compute the bounds even if the actor is not visible
actor.computeBounds();
// If bin number is < 0, then don't even bother
// rendering the data
if (actor.visible() && actor.material().binNumber() >= 0) {
sortedActors.push([actor.material().binNumber(), actor]);
}
}
// Now perform sorting
sortedActors.sort(function (a, b) {return a[0] - b[0];});
for (i = 0; i < sortedActors.length; i += 1) {
actor = sortedActors[i][1];
if (actor.referenceFrame() ===
vgl.boundingObject.ReferenceFrame.Relative) {
var view = m_this.m_camera.viewMatrix();
/* If the view matrix is a plain array, keep it as such. This is
* intended to preserve precision, and will only be the case if the
* view matrix was created by delibrately setting it as an array. */
if (view instanceof Array) {
renSt.m_modelViewMatrix = new Array(16);
}
mat4.multiply(renSt.m_modelViewMatrix, view, actor.matrix());
renSt.m_projectionMatrix = m_this.m_camera.projectionMatrix();
renSt.m_modelViewAlignment = m_this.m_camera.viewAlignment();
} else {
renSt.m_modelViewMatrix = actor.matrix();
renSt.m_modelViewAlignment = null;
renSt.m_projectionMatrix = mat4.create();
mat4.ortho(renSt.m_projectionMatrix,
0, m_this.m_width, 0, m_this.m_height, -1, 1);
}
mat4.invert(mvMatrixInv, renSt.m_modelViewMatrix);
mat4.transpose(renSt.m_normalMatrix, mvMatrixInv);
renSt.m_material = actor.material();
renSt.m_mapper = actor.mapper();
// TODO Fix this shortcut
renSt.m_material.bind(renSt);
renSt.m_mapper.render(renSt);
renSt.m_material.undoBind(renSt);
}
renSt.m_context.finish();
m_this.m_contextChanged = false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Automatically set up the camera based on visible actors
*/
////////////////////////////////////////////////////////////////////////////
this.resetCamera = function () {
m_this.m_camera.computeBounds();
var vn = m_this.m_camera.directionOfProjection(),
visibleBounds = m_this.m_camera.bounds(),
center = [
(visibleBounds[0] + visibleBounds[1]) / 2.0,
(visibleBounds[2] + visibleBounds[3]) / 2.0,
(visibleBounds[4] + visibleBounds[5]) / 2.0
],
diagonals = [
visibleBounds[1] - visibleBounds[0],
visibleBounds[3] - visibleBounds[2],
visibleBounds[5] - visibleBounds[4]
],
radius = 0.0,
aspect = m_this.m_camera.viewAspect(),
angle = m_this.m_camera.viewAngle(),
distance = null,
vup = null;
if (diagonals[0] > diagonals[1]) {
if (diagonals[0] > diagonals[2]) {
radius = diagonals[0] / 2.0;
} else {
radius = diagonals[2] / 2.0;
}
} else {
if (diagonals[1] > diagonals[2]) {
radius = diagonals[1] / 2.0;
} else {
radius = diagonals[2] / 2.0;
}
}
// @todo Need to figure out what's happening here
if (aspect >= 1.0) {
angle = 2.0 * Math.atan(Math.tan(angle * 0.5) / aspect);
} else {
angle = 2.0 * Math.atan(Math.tan(angle * 0.5) * aspect);
}
distance = radius / Math.sin(angle * 0.5);
vup = m_this.m_camera.viewUpDirection();
if (Math.abs(vec3.dot(vup, vn)) > 0.999) {
m_this.m_camera.setViewUpDirection(-vup[2], vup[0], vup[1]);
}
m_this.m_camera.setFocalPoint(center[0], center[1], center[2]);
m_this.m_camera.setPosition(center[0] + distance * -vn[0],
center[1] + distance * -vn[1], center[2] + distance * -vn[2]);
m_this.resetCameraClippingRange(visibleBounds);
};
////////////////////////////////////////////////////////////////////////////
/**
* Check whether or not whether or not the bounds are valid
*/
////////////////////////////////////////////////////////////////////////////
this.hasValidBounds = function (bounds) {
if (bounds[0] === Number.MAX_VALUE ||
bounds[1] === -Number.MAX_VALUE ||
bounds[2] === Number.MAX_VALUE ||
bounds[3] === -Number.MAX_VALUE ||
bounds[4] === Number.MAX_VALUE ||
bounds[5] === -Number.MAX_VALUE) {
return false;
}
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Recalculate camera's clipping range
*/
////////////////////////////////////////////////////////////////////////////
this.resetCameraClippingRange = function (bounds) {
if (typeof bounds === 'undefined') {
m_this.m_camera.computeBounds();
bounds = m_this.m_camera.bounds();
}
if (!m_this.hasValidBounds(bounds)) {
return;
}
var vn = m_this.m_camera.viewPlaneNormal(),
position = m_this.m_camera.position(),
a = -vn[0],
b = -vn[1],
c = -vn[2],
d = -(a * position[0] + b * position[1] + c * position[2]),
range = vec2.create(),
dist = null,
i = null,
j = null,
k = null;
if (!m_this.m_resetClippingRange) {
return;
}
// Set the max near clipping plane and the min far clipping plane
range[0] = a * bounds[0] + b * bounds[2] + c * bounds[4] + d;
range[1] = 1e-18;
// Find the closest / farthest bounding box vertex
for (k = 0; k < 2; k += 1) {
for (j = 0; j < 2; j += 1) {
for (i = 0; i < 2; i += 1) {
dist = a * bounds[i] + b * bounds[2 + j] + c * bounds[4 + k] + d;
range[0] = (dist < range[0]) ? (dist) : (range[0]);
range[1] = (dist > range[1]) ? (dist) : (range[1]);
}
}
}
// Do not let the range behind the camera throw off the calculation.
if (range[0] < 0.0) {
range[0] = 0.0;
}
// Give ourselves a little breathing room
range[0] = 0.99 * range[0] - (range[1] - range[0]) * 0.5;
range[1] = 1.01 * range[1] + (range[1] - range[0]) * 0.5;
// Make sure near is not bigger than far
range[0] = (range[0] >= range[1]) ? (0.01 * range[1]) : (range[0]);
// Make sure near is at least some fraction of far - this prevents near
// from being behind the camera or too close in front. How close is too
// close depends on the resolution of the depth buffer.
if (!m_this.m_nearClippingPlaneTolerance) {
m_this.m_nearClippingPlaneTolerance = 0.01;
if (m_this.m_depthBits && m_this.m_depthBits > 16) {
m_this.m_nearClippingPlaneTolerance = 0.001;
}
}
// make sure the front clipping range is not too far from the far clippnig
// range, this is to make sure that the zbuffer resolution is effectively
// used.
if (range[0] < m_this.m_nearClippingPlaneTolerance * range[1]) {
range[0] = m_this.m_nearClippingPlaneTolerance * range[1];
}
m_this.m_camera.setClippingRange(range[0], range[1]);
};
////////////////////////////////////////////////////////////////////////////
/**
* Resize viewport given a width and height
*/
////////////////////////////////////////////////////////////////////////////
this.resize = function (width, height) {
if (!width || !height) {
return;
}
// @note: where do m_this.m_x and m_this.m_y come from?
m_this.positionAndResize(m_this.m_x, m_this.m_y, width, height);
};
////////////////////////////////////////////////////////////////////////////
/**
* Resize viewport given a position, width and height
*/
////////////////////////////////////////////////////////////////////////////
this.positionAndResize = function (x, y, width, height) {
var i;
// TODO move this code to camera
if (x < 0 || y < 0 || width <= 0 || height <= 0) {
console.log('[error] Invalid position and resize values',
x, y, width, height);
return;
}
//If we're allowing this renderer to resize ...
if (m_this.m_resizable) {
m_this.m_width = width;
m_this.m_height = height;
m_this.m_camera.setViewAspect(width / height);
m_this.m_camera.setParallelExtents({width: width, height: height});
m_this.modified();
}
if (m_this.m_renderPasses) {
for (i = 0; i < m_this.m_renderPasses.length; i += 1) {
m_this.m_renderPasses[i].resize(width, height);
m_this.m_renderPasses[i].renderer().positionAndResize(x, y, width, height);
}
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Add new actor to the collection
*
* @param actor
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.addActor = function (actor) {
if (actor instanceof vgl.actor) {
m_this.m_sceneRoot.addChild(actor);
m_this.modified();
return true;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return true if this renderer has this actor attached, false otherwise.
*
* @param actor
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.hasActor = function (actor) {
return m_this.m_sceneRoot.hasChild(actor);
};
////////////////////////////////////////////////////////////////////////////
/**
* Add an array of actors to the collection
*/
////////////////////////////////////////////////////////////////////////////
this.addActors = function (actors) {
var i = null;
if (actors instanceof Array) {
for (i = 0; i < actors.length; i += 1) {
m_this.m_sceneRoot.addChild(actors[i]);
}
m_this.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Remove the actor from the collection
*
* @param actor
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.removeActor = function (actor) {
if (m_this.m_sceneRoot.children().indexOf(actor) !== -1) {
/* When we remove an actor, free the VBOs of the mapper and mark the
* mapper as modified; it will reallocate VBOs as necessary. */
if (actor.mapper()) {
actor.mapper().deleteVertexBufferObjects();
actor.mapper().modified();
}
m_this.m_sceneRoot.removeChild(actor);
m_this.modified();
return true;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Remove actors from the collection
*
* @param actors
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.removeActors = function (actors) {
if (!(actors instanceof Array)) {
return false;
}
var i;
for (i = 0; i < actors.length; i += 1) {
m_this.m_sceneRoot.removeChild(actors[i]);
}
m_this.modified();
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Remove all actors for a renderer
*
* @returns {*}
*/
////////////////////////////////////////////////////////////////////////////
this.removeAllActors = function () {
return m_this.m_sceneRoot.removeChildren();
};
////////////////////////////////////////////////////////////////////////////
/**
* Transform a point in the world space to display space
*/
////////////////////////////////////////////////////////////////////////////
this.worldToDisplay = function (worldPt, viewMatrix, projectionMatrix, width,
height) {
var viewProjectionMatrix = mat4.create(),
winX = null,
winY = null,
winZ = null,
winW = null,
clipPt = null;
mat4.multiply(viewProjectionMatrix, projectionMatrix, viewMatrix);
// Transform world to clipping coordinates
clipPt = vec4.create();
vec4.transformMat4(clipPt, worldPt, viewProjectionMatrix);
if (clipPt[3] !== 0.0) {
clipPt[0] = clipPt[0] / clipPt[3];
clipPt[1] = clipPt[1] / clipPt[3];
clipPt[2] = clipPt[2] / clipPt[3];
clipPt[3] = 1.0;
}
winX = (((clipPt[0]) + 1) / 2.0) * width;
// We calculate -point3D.getY() because the screen Y axis is
// oriented top->down
winY = ((1 - clipPt[1]) / 2.0) * height;
winZ = clipPt[2];
winW = clipPt[3];
return vec4.fromValues(winX, winY, winZ, winW);
};
////////////////////////////////////////////////////////////////////////////
/**
* Transform a point in display space to world space
* @param displayPt
* @param viewMatrix
* @param projectionMatrix
* @param width
* @param height
* @returns {vec4}
*/
////////////////////////////////////////////////////////////////////////////
this.displayToWorld = function (displayPt, viewMatrix, projectionMatrix,
width, height) {
var x = (2.0 * displayPt[0] / width) - 1,
y = -(2.0 * displayPt[1] / height) + 1,
z = displayPt[2],
viewProjectionInverse = mat4.create(),
worldPt = null;
mat4.multiply(viewProjectionInverse, projectionMatrix, viewMatrix);
mat4.invert(viewProjectionInverse, viewProjectionInverse);
worldPt = vec4.fromValues(x, y, z, 1);
vec4.transformMat4(worldPt, worldPt, viewProjectionInverse);
if (worldPt[3] !== 0.0) {
worldPt[0] = worldPt[0] / worldPt[3];
worldPt[1] = worldPt[1] / worldPt[3];
worldPt[2] = worldPt[2] / worldPt[3];
worldPt[3] = 1.0;
}
return worldPt;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the focusDisplayPoint
* @returns {vec4}
*/
////////////////////////////////////////////////////////////////////////////
this.focusDisplayPoint = function () {
var focalPoint = m_this.m_camera.focalPoint(),
focusWorldPt = vec4.fromValues(
focalPoint[0], focalPoint[1], focalPoint[2], 1);
return m_this.worldToDisplay(
focusWorldPt, m_this.m_camera.viewMatrix(),
m_this.m_camera.projectionMatrix(), m_this.m_width, m_this.m_height);
};
////////////////////////////////////////////////////////////////////////////
/**
* Will the scene be reset.
* @returns {bool}
*/
////////////////////////////////////////////////////////////////////////////
this.resetScene = function () {
return m_this.m_resetScene;
};
////////////////////////////////////////////////////////////////////////////
/**
* If true the scene will be reset, otherwise the scene will not be
* automatically reset.
*
* @param reset
*/
////////////////////////////////////////////////////////////////////////////
this.setResetScene = function (reset) {
if (m_this.m_resetScene !== reset) {
m_this.m_resetScene = reset;
m_this.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Will the clipping range be reset
* @returns {bool}
*/
////////////////////////////////////////////////////////////////////////////
this.resetClippingRange = function () {
return m_this.m_resetClippingRange;
};
////////////////////////////////////////////////////////////////////////////
/**
* If true the camera clipping range will be reset, otherwise the scene will
* not be automatically reset.
*
* @param reset
*/
////////////////////////////////////////////////////////////////////////////
this.setResetClippingRange = function (reset) {
if (m_this.m_resetClippingRange !== reset) {
m_this.m_resetClippingRange = reset;
m_this.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
*
*/
////////////////////////////////////////////////////////////////////////////
this.addRenderPass = function (renPass) {
var i;
if (m_this.m_renderPasses) {
for (i = 0; i < m_this.m_renderPasses.length; i += 1) {
if (renPass === m_this.m_renderPasses[i]) {
return;
}
}
}
m_this.m_renderPasses = [];
m_this.m_renderPasses.push(renPass);
};
////////////////////////////////////////////////////////////////////////////
/**
*
*/
////////////////////////////////////////////////////////////////////////////
this.removeRenderPass = function (renPass) {
renPass = renPass; // TODO Implement this
};
////////////////////////////////////////////////////////////////////////////
/**
*
*/
////////////////////////////////////////////////////////////////////////////
this._cleanup = function (renderState) {
var children = m_this.m_sceneRoot.children();
for (var i = 0; i < children.length; i += 1) {
var actor = children[i];
actor.material()._cleanup(renderState);
actor.mapper()._cleanup(renderState);
}
m_this.m_sceneRoot.removeChildren();
};
return m_this;
};
inherit(vgl.renderer, vgl.graphicsObject);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, vec4, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class renderWindow
*
* @class
* @returns {vgl.renderWindow}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.renderWindow = function (canvas) {
'use strict';
if (!(this instanceof vgl.renderWindow)) {
return new vgl.renderWindow(canvas);
}
vgl.graphicsObject.call(this);
/** @private */
var m_this = this,
m_x = 0,
m_y = 0,
m_width = 400,
m_height = 400,
m_canvas = canvas,
m_activeRender = null,
m_renderers = [],
m_context = null;
////////////////////////////////////////////////////////////////////////////
/**
* Get size of the render window
*
* @returns {Array}
*/
////////////////////////////////////////////////////////////////////////////
this.windowSize = function () {
return [m_width, m_height];
};
////////////////////////////////////////////////////////////////////////////
/**
* Set size of the render window
*
* @param width
* @param height
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.setWindowSize = function (width, height) {
if (m_width !== width || m_height !== height) {
m_width = width;
m_height = height;
m_this.modified();
return true;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get window position (top left coordinates)
*
* @returns {Array}
*/
////////////////////////////////////////////////////////////////////////////
this.windowPosition = function () {
return [m_x, m_y];
};
////////////////////////////////////////////////////////////////////////////
/**
* Set window position (top left coordinates)
*
* @param x
* @param y
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.setWindowPosition = function (x, y) {
if ((m_x !== x) || (m_y !== y)) {
m_x = x;
m_y = y;
m_this.modified();
return true;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return all renderers contained in the render window
* @returns {Array}
*/
////////////////////////////////////////////////////////////////////////////
this.renderers = function () {
return m_renderers;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get active renderer of the the render window
*
* @returns vgl.renderer
*/
////////////////////////////////////////////////////////////////////////////
this.activeRenderer = function () {
return m_activeRender;
};
////////////////////////////////////////////////////////////////////////////
/**
* Add renderer to the render window
*
* @param ren
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.addRenderer = function (ren) {
if (m_this.hasRenderer(ren) === false) {
m_renderers.push(ren);
ren.setRenderWindow(m_this);
if (m_activeRender === null) {
m_activeRender = ren;
}
if (ren.layer() !== 0) {
ren.camera().setClearMask(vgl.GL.DepthBufferBit);
}
m_this.modified();
return true;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Remove renderer from the render window
*
* @param ren
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.removeRenderer = function (ren) {
var index = m_renderers.indexOf(ren);
if (index !== -1) {
if (m_activeRender === ren) {
m_activeRender = null;
}
m_renderers.splice(index, 1);
m_this.modified();
return true;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return a renderer at a given index
*
* @param index
* @returns {vgl.renderer}
*/
////////////////////////////////////////////////////////////////////////////
this.getRenderer = function (index) {
if (index < m_renderers.length) {
return m_renderers[index];
}
console.log('[WARNING] Out of index array');
return null;
};
////////////////////////////////////////////////////////////////////////////
/**
* Check if the renderer exists
*
* @param ren
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.hasRenderer = function (ren) {
var i;
for (i = 0; i < m_renderers.length; i += 1) {
if (ren === m_renderers[i]) {
return true;
}
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Resize window
*
* @param width
* @param height
*/
////////////////////////////////////////////////////////////////////////////
this.resize = function (width, height) {
m_this.positionAndResize(m_x, m_y, width, height);
m_this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Resize and reposition the window
*
* @param x
* @param y
* @param width
* @param height
*/
////////////////////////////////////////////////////////////////////////////
this.positionAndResize = function (x, y, width, height) {
m_x = x;
m_y = y;
m_width = width;
m_height = height;
var i;
for (i = 0; i < m_renderers.length; i += 1) {
m_renderers[i].positionAndResize(m_x, m_y, m_width, m_height);
}
m_this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Create the window
*
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this._setup = function (renderState) {
renderState = renderState; /* unused parameter */
m_context = null;
try {
// Try to grab the standard context. If it fails, fallback to
// experimental.
m_context = m_canvas.getContext('webgl') ||
m_canvas.getContext('experimental-webgl');
// Set width and height of renderers if not set already
var i;
for (i = 0; i < m_renderers.length; i += 1) {
if ((m_renderers[i].width() > m_width) ||
m_renderers[i].width() === 0 ||
(m_renderers[i].height() > m_height) ||
m_renderers[i].height() === 0) {
m_renderers[i].resize(m_x, m_y, m_width, m_height);
}
}
return true;
}
catch (e) {
}
// If we don't have a GL context, give up now
if (!m_context) {
console('[ERROR] Unable to initialize WebGL. Your browser may not support it.');
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return current GL context
*/
////////////////////////////////////////////////////////////////////////////
this.context = function () {
return m_context;
};
////////////////////////////////////////////////////////////////////////////
/**
* Delete this window and release any graphics resources
*/
////////////////////////////////////////////////////////////////////////////
this._cleanup = function (renderState) {
var i;
for (i = 0; i < m_renderers.length; i += 1) {
m_renderers[i]._cleanup(renderState);
}
vgl.clearCachedShaders(renderState ? renderState.m_context : null);
};
////////////////////////////////////////////////////////////////////////////
/**
* Render the scene
*/
////////////////////////////////////////////////////////////////////////////
this.render = function () {
var i;
m_renderers.sort(function (a, b) {return a.layer() - b.layer();});
for (i = 0; i < m_renderers.length; i += 1) {
m_renderers[i].render();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the focusDisplayPoint from the activeRenderer
* @returns {vec4}
*/
////////////////////////////////////////////////////////////////////////////
this.focusDisplayPoint = function () {
return m_activeRender.focusDisplayPoint();
};
////////////////////////////////////////////////////////////////////////////
/**
* Transform a point in display space to world space
* @param {Number} x
* @param {Number} y
* @param {vec4} focusDisplayPoint
* @returns {vec4}
*/
////////////////////////////////////////////////////////////////////////////
this.displayToWorld = function (x, y, focusDisplayPoint, ren) {
ren = ren === undefined ? ren = m_activeRender : ren;
var camera = ren.camera();
if (!focusDisplayPoint) {
focusDisplayPoint = ren.focusDisplayPoint();
}
return ren.displayToWorld(
vec4.fromValues(x, y, focusDisplayPoint[2], 1.0), camera.viewMatrix(),
camera.projectionMatrix(), m_width, m_height);
};
////////////////////////////////////////////////////////////////////////////
/**
* Transform a point in display space to world space
* @param {Number} x
* @param {Number} y
* @param {vec4} focusDisplayPoint
* @returns {vec4}
*/
////////////////////////////////////////////////////////////////////////////
this.worldToDisplay = function (x, y, z, ren) {
ren = ren === undefined ? ren = m_activeRender : ren;
var camera = ren.camera();
return ren.worldToDisplay(
vec4.fromValues(x, y, z, 1.0), camera.viewMatrix(),
camera.projectionMatrix(), m_width, m_height);
};
return m_this;
};
inherit(vgl.renderWindow, vgl.graphicsObject);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, vec3, vec4, mat4, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class camera
*
* @class
* @returns {vgl.camera}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.camera = function (arg) {
'use strict';
if (!(this instanceof vgl.camera)) {
return new vgl.camera(arg);
}
vgl.groupNode.call(this);
arg = arg || {};
/** @private */
var m_viewAngle = (Math.PI * 30) / 180.0,
m_position = vec4.fromValues(0.0, 0.0, 1.0, 1.0),
m_focalPoint = vec4.fromValues(0.0, 0.0, 0.0, 1.0),
m_centerOfRotation = vec3.fromValues(0.0, 0.0, 0.0),
m_viewUp = vec4.fromValues(0.0, 1.0, 0.0, 0.0),
m_rightDir = vec4.fromValues(1.0, 0.0, 0.0, 0.0),
m_near = 0.01,
m_far = 10000.0,
m_viewAspect = 1.0,
m_directionOfProjection = vec4.fromValues(0.0, 0.0, -1.0, 0.0),
m_viewPlaneNormal = vec4.fromValues(0.0, 0.0, 1.0, 0.0),
m_viewMatrix = mat4.create(),
m_projectionMatrix = mat4.create(),
m_computeModelViewMatrixTime = vgl.timestamp(),
m_computeProjectMatrixTime = vgl.timestamp(),
m_left = -1.0,
m_right = 1.0,
m_top = +1.0,
m_bottom = -1.0,
m_parallelExtents = {zoom: 1, tilesize: 256},
m_enableTranslation = true,
m_enableRotation = true,
m_enableScale = true,
m_enableParallelProjection = false,
m_clearColor = [0.0, 0.0, 0.0, 0.0],
m_clearDepth = 1.0,
/*jshint bitwise: false */
m_clearMask = vgl.GL.COLOR_BUFFER_BIT |
vgl.GL.DEPTH_BUFFER_BIT;
/*jshint bitwise: true */
if (arg.parallelProjection !== undefined) {
m_enableParallelProjection = arg.parallelProjection ? true : false;
}
////////////////////////////////////////////////////////////////////////////
/**
* Get view angle of the camera
*/
////////////////////////////////////////////////////////////////////////////
this.viewAngle = function () {
return m_viewAngle;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set view angle of the camera in degrees, which is converted to radians.
*/
////////////////////////////////////////////////////////////////////////////
this.setViewAngleDegrees = function (a) {
m_viewAngle = (Math.PI * a) / 180.0;
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Set view angle of the camera in degrees, which is converted to radians.
*/
////////////////////////////////////////////////////////////////////////////
this.setViewAngle = function (a) {
if (m_enableScale) {
m_viewAngle = a;
this.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get position of the camera
*/
////////////////////////////////////////////////////////////////////////////
this.position = function () {
return m_position;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set position of the camera
*/
////////////////////////////////////////////////////////////////////////////
this.setPosition = function (x, y, z) {
if (m_enableTranslation) {
m_position = vec4.fromValues(x, y, z, 1.0);
this.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get focal point of the camera
*/
////////////////////////////////////////////////////////////////////////////
this.focalPoint = function () {
return m_focalPoint;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set focal point of the camera
*/
////////////////////////////////////////////////////////////////////////////
this.setFocalPoint = function (x, y, z) {
if (m_enableRotation && m_enableTranslation) {
m_focalPoint = vec4.fromValues(x, y, z, 1.0);
this.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get view-up direction of camera
*/
////////////////////////////////////////////////////////////////////////////
this.viewUpDirection = function () {
return m_viewUp;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set view-up direction of the camera
*/
////////////////////////////////////////////////////////////////////////////
this.setViewUpDirection = function (x, y, z) {
m_viewUp = vec4.fromValues(x, y, z, 0);
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Get center of rotation for camera
*/
////////////////////////////////////////////////////////////////////////////
this.centerOfRotation = function () {
return m_centerOfRotation;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set center of rotation for camera
*/
////////////////////////////////////////////////////////////////////////////
this.setCenterOfRotation = function (centerOfRotation) {
m_centerOfRotation = centerOfRotation;
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Get clipping range of the camera
*/
////////////////////////////////////////////////////////////////////////////
this.clippingRange = function () {
return [m_near, m_far];
};
////////////////////////////////////////////////////////////////////////////
/**
* Set clipping range of the camera
*/
////////////////////////////////////////////////////////////////////////////
this.setClippingRange = function (near, far) {
m_near = near;
m_far = far;
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Get view aspect
*/
////////////////////////////////////////////////////////////////////////////
this.viewAspect = function () {
return m_viewAspect;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set view aspect
*/
////////////////////////////////////////////////////////////////////////////
this.setViewAspect = function (aspect) {
m_viewAspect = aspect;
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Return active mode for scaling (enabled / disabled)
*/
////////////////////////////////////////////////////////////////////////////
this.enableScale = function () {
return m_enableScale;
};
////////////////////////////////////////////////////////////////////////////
/**
* Enable/disable scaling
*/
////////////////////////////////////////////////////////////////////////////
this.setEnableScale = function (flag) {
if (flag !== m_enableScale) {
m_enableScale = flag;
this.modified();
return true;
}
return m_enableScale;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return active mode for rotation (enabled / disabled)
*/
////////////////////////////////////////////////////////////////////////////
this.enableRotation = function () {
return m_enableRotation;
};
////////////////////////////////////////////////////////////////////////////
/**
* Enable / disable rotation
*/
////////////////////////////////////////////////////////////////////////////
this.setEnableRotation = function (flag) {
if (flag !== m_enableRotation) {
m_enableRotation = flag;
this.modified();
return true;
}
return m_enableRotation;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return active mode for translation (enabled/disabled)
*/
////////////////////////////////////////////////////////////////////////////
this.enableTranslation = function () {
return m_enableTranslation;
};
////////////////////////////////////////////////////////////////////////////
/**
* Enable / disable translation
*/
////////////////////////////////////////////////////////////////////////////
this.setEnableTranslation = function (flag) {
if (flag !== m_enableTranslation) {
m_enableTranslation = flag;
this.modified();
return true;
}
return m_enableTranslation;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return if parallel projection is enabled
*/
////////////////////////////////////////////////////////////////////////////
this.isEnabledParallelProjection = function () {
return m_enableParallelProjection;
};
////////////////////////////////////////////////////////////////////////////
/**
* Enable / disable parallel projection
*/
////////////////////////////////////////////////////////////////////////////
this.enableParallelProjection = function (flag) {
if (flag !== m_enableParallelProjection) {
m_enableParallelProjection = flag;
this.modified();
return true;
}
return m_enableParallelProjection;
};
////////////////////////////////////////////////////////////////////////////
/**
* Enable / disable parallel projection
*/
////////////////////////////////////////////////////////////////////////////
this.setEnableParallelProjection = function (flag) {
return this.enableParallelProjection(flag);
};
////////////////////////////////////////////////////////////////////////////
/**
* Get parallel projection parameters
*/
////////////////////////////////////////////////////////////////////////////
this.parallelProjection = function () {
return {left: m_left, right: m_right, top: m_top, bottom: m_bottom};
};
////////////////////////////////////////////////////////////////////////////
/**
* Set parallel projection parameters
*/
////////////////////////////////////////////////////////////////////////////
this.setParallelProjection = function (left, right, top, bottom) {
m_left = left;
m_right = right;
m_top = top;
m_bottom = bottom;
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Get parallel projection extents parameters
*/
////////////////////////////////////////////////////////////////////////////
this.parallelExtents = function () {
return m_parallelExtents;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set parallel projection extents parameters
*/
////////////////////////////////////////////////////////////////////////////
this.setParallelExtents = function (extents) {
var prop = ['width', 'height', 'zoom', 'tilesize'], mod = false, i, key;
for (i = 0; i < prop.length; i += 1) {
key = prop[i];
if (extents[key] !== undefined &&
extents[key] !== m_parallelExtents[key]) {
m_parallelExtents[key] = extents[key];
mod = true;
}
}
if (mod && m_parallelExtents.width && m_parallelExtents.height &&
m_parallelExtents.zoom !== undefined && m_parallelExtents.tilesize) {
var unitsPerPixel = this.unitsPerPixel(m_parallelExtents.zoom,
m_parallelExtents.tilesize);
m_right = unitsPerPixel * m_parallelExtents.width / 2;
m_left = -m_right;
m_top = unitsPerPixel * m_parallelExtents.height / 2;
m_bottom = -m_top;
this.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute the units per pixel.
*
* @param zoom: tile zoom level.
* @param tilesize: number of pixels per tile (defaults to 256).
* @returns: unitsPerPixel.
*/
////////////////////////////////////////////////////////////////////////////
this.unitsPerPixel = function (zoom, tilesize) {
tilesize = tilesize || 256;
return 360.0 * Math.pow(2, -zoom) / tilesize;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return direction of projection
*/
////////////////////////////////////////////////////////////////////////////
this.directionOfProjection = function () {
this.computeDirectionOfProjection();
return m_directionOfProjection;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return view plane normal direction
*/
////////////////////////////////////////////////////////////////////////////
this.viewPlaneNormal = function () {
this.computeViewPlaneNormal();
return m_viewPlaneNormal;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return view-matrix for the camera This method does not compute the
* view-matrix for the camera. It is assumed that a call to computeViewMatrix
* has been made earlier.
*
* @returns {mat4}
*/
////////////////////////////////////////////////////////////////////////////
this.viewMatrix = function () {
return this.computeViewMatrix();
};
////////////////////////////////////////////////////////////////////////////
/**
* Set the view-matrix for the camera and mark that it is up to date so that
* it won't be recomputed unless something else changes.
*
* @param {mat4} view: new view matrix.
* @param {boolean} preserveType: if true, clone the input using slice. This
* can be used to ensure the array is a specific precision.
*/
////////////////////////////////////////////////////////////////////////////
this.setViewMatrix = function (view, preserveType) {
if (!preserveType) {
mat4.copy(m_viewMatrix, view);
} else {
m_viewMatrix = view.slice();
}
m_computeModelViewMatrixTime.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Return camera projection matrix This method does not compute the
* projection-matrix for the camera. It is assumed that a call to
* computeProjectionMatrix has been made earlier.
*
* @returns {mat4}
*/
////////////////////////////////////////////////////////////////////////////
this.projectionMatrix = function () {
return this.computeProjectionMatrix();
};
////////////////////////////////////////////////////////////////////////////
/**
* Set the projection-matrix for the camera and mark that it is up to date so
* that it won't be recomputed unless something else changes.
*
* @param {mat4} proj: new projection matrix.
*/
////////////////////////////////////////////////////////////////////////////
this.setProjectionMatrix = function (proj) {
mat4.copy(m_projectionMatrix, proj);
m_computeProjectMatrixTime.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Return clear mask used by this camera
*
* @returns {number}
*/
////////////////////////////////////////////////////////////////////////////
this.clearMask = function () {
return m_clearMask;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set clear mask for camera
*
* @param mask
*/
////////////////////////////////////////////////////////////////////////////
this.setClearMask = function (mask) {
m_clearMask = mask;
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Get clear color (background color) of the camera
*
* @returns {Array}
*/
////////////////////////////////////////////////////////////////////////////
this.clearColor = function () {
return m_clearColor;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set clear color (background color) for the camera
*
* @param color RGBA
*/
////////////////////////////////////////////////////////////////////////////
this.setClearColor = function (r, g, b, a) {
m_clearColor[0] = r;
m_clearColor[1] = g;
m_clearColor[2] = b;
m_clearColor[3] = a;
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
*
* @returns {{1.0: null}}
*/
////////////////////////////////////////////////////////////////////////////
this.clearDepth = function () {
return m_clearDepth;
};
////////////////////////////////////////////////////////////////////////////
/**
*
* @param depth
*/
////////////////////////////////////////////////////////////////////////////
this.setClearDepth = function (depth) {
m_clearDepth = depth;
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute direction of projection
*/
////////////////////////////////////////////////////////////////////////////
this.computeDirectionOfProjection = function () {
vec3.subtract(m_directionOfProjection, m_focalPoint, m_position);
vec3.normalize(m_directionOfProjection, m_directionOfProjection);
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute view plane normal
*/
////////////////////////////////////////////////////////////////////////////
this.computeViewPlaneNormal = function () {
m_viewPlaneNormal[0] = -m_directionOfProjection[0];
m_viewPlaneNormal[1] = -m_directionOfProjection[1];
m_viewPlaneNormal[2] = -m_directionOfProjection[2];
};
////////////////////////////////////////////////////////////////////////////
/**
* Move camera closer or further away from the scene
*/
////////////////////////////////////////////////////////////////////////////
this.zoom = function (d, dir) {
if (d === 0) {
return;
}
if (!m_enableTranslation) {
return;
}
d = d * vec3.distance(m_focalPoint, m_position);
if (!dir) {
dir = m_directionOfProjection;
m_position[0] = m_focalPoint[0] - d * dir[0];
m_position[1] = m_focalPoint[1] - d * dir[1];
m_position[2] = m_focalPoint[2] - d * dir[2];
} else {
m_position[0] = m_position[0] + d * dir[0];
m_position[1] = m_position[1] + d * dir[1];
m_position[2] = m_position[2] + d * dir[2];
}
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Move camera sideways
*/
////////////////////////////////////////////////////////////////////////////
this.pan = function (dx, dy, dz) {
if (!m_enableTranslation) {
return;
}
m_position[0] += dx;
m_position[1] += dy;
m_position[2] += dz;
m_focalPoint[0] += dx;
m_focalPoint[1] += dy;
m_focalPoint[2] += dz;
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute camera coordinate axes
*/
////////////////////////////////////////////////////////////////////////////
this.computeOrthogonalAxes = function () {
this.computeDirectionOfProjection();
vec3.cross(m_rightDir, m_directionOfProjection, m_viewUp);
vec3.normalize(m_rightDir, m_rightDir);
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Rotate camera around center of rotation
* @param dx Rotation around vertical axis in degrees
* @param dy Rotation around horizontal axis in degrees
*/
////////////////////////////////////////////////////////////////////////////
this.rotate = function (dx, dy) {
if (!m_enableRotation) {
return;
}
// Convert degrees into radians
dx = 0.5 * dx * (Math.PI / 180.0);
dy = 0.5 * dy * (Math.PI / 180.0);
var mat = mat4.create(),
inverseCenterOfRotation = new vec3.create();
mat4.identity(mat);
inverseCenterOfRotation[0] = -m_centerOfRotation[0];
inverseCenterOfRotation[1] = -m_centerOfRotation[1];
inverseCenterOfRotation[2] = -m_centerOfRotation[2];
mat4.translate(mat, mat, m_centerOfRotation);
mat4.rotate(mat, mat, dx, m_viewUp);
mat4.rotate(mat, mat, dy, m_rightDir);
mat4.translate(mat, mat, inverseCenterOfRotation);
vec4.transformMat4(m_position, m_position, mat);
vec4.transformMat4(m_focalPoint, m_focalPoint, mat);
// Update viewup vector
vec4.transformMat4(m_viewUp, m_viewUp, mat);
vec4.normalize(m_viewUp, m_viewUp);
// Update direction of projection
this.computeOrthogonalAxes();
// Mark modified
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute camera view matrix
*/
////////////////////////////////////////////////////////////////////////////
this.computeViewMatrix = function () {
if (m_computeModelViewMatrixTime.getMTime() < this.getMTime()) {
mat4.lookAt(m_viewMatrix, m_position, m_focalPoint, m_viewUp);
m_computeModelViewMatrixTime.modified();
}
return m_viewMatrix;
};
////////////////////////////////////////////////////////////////////////////
/**
* Check if the texture should be aligned to the screen. Alignment only
* occurs if the parallel extents contain width, height, and a
* close-to-integer zoom level, and if the units-per-pixel value has been
* computed. The camera must either be in parallel projection mode OR must
* have a perspective camera which is oriented along the z-axis without any
* rotation.
*
* @returns: either null if no alignment should occur, or an alignment object
* with the rounding value and offsets.
*/
////////////////////////////////////////////////////////////////////////////
this.viewAlignment = function () {
if (!m_enableParallelProjection) {
/* If we aren't in parallel projection mode, ensure that the projection
* matrix meets strict specifications */
var proj = this.projectionMatrix();
if (proj[1] || proj[2] || proj[3] || proj[4] || proj[6] || proj[7] ||
proj[8] || proj[9] || proj[12] || proj[13] || proj[15]) {
return null;
}
}
var unitsPerPixel = this.unitsPerPixel(m_parallelExtents.zoom,
m_parallelExtents.tilesize);
/* If we don't have screen dimensions, we can't know how to align pixels */
if (!m_parallelExtents.width || !m_parallelExtents.height ||
!unitsPerPixel) {
return null;
}
/* If we aren't at an integer zoom level, we shouldn't align pixels. If
* we are really close to an integer zoom level, that is good enough. */
if (parseFloat(m_parallelExtents.zoom.toFixed(6)) !==
parseFloat(m_parallelExtents.zoom.toFixed(0))) {
return null;
}
var align = {roundx: unitsPerPixel, roundy: unitsPerPixel, dx: 0, dy: 0};
/* If the screen is an odd number of pixels, shift the view center to the
* center of a pixel so that the pixels fit discretely across the screen.
* If an even number of pixels, align the view center between pixels for
* the same reason. */
if (m_parallelExtents.width % 2) {
align.dx = unitsPerPixel * 0.5;
}
if (m_parallelExtents.height % 2) {
align.dy = unitsPerPixel * 0.5;
}
return align;
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute camera projection matrix
*/
////////////////////////////////////////////////////////////////////////////
this.computeProjectionMatrix = function () {
if (m_computeProjectMatrixTime.getMTime() < this.getMTime()) {
if (!m_enableParallelProjection) {
mat4.perspective(m_projectionMatrix, m_viewAngle, m_viewAspect, m_near, m_far);
} else {
mat4.ortho(m_projectionMatrix,
m_left, m_right, m_bottom, m_top, m_near, m_far);
}
m_computeProjectMatrixTime.modified();
}
return m_projectionMatrix;
};
////////////////////////////////////////////////////////////////////////////
/**
* Convert a zoom level and window size to a camera height.
*/
////////////////////////////////////////////////////////////////////////////
this.zoomToHeight = function (zoom, width, height) {
return vgl.zoomToHeight(zoom, width, height, m_viewAngle);
};
this.computeDirectionOfProjection();
return this;
};
inherit(vgl.camera, vgl.groupNode);
////////////////////////////////////////////////////////////////////////////
/**
* Convert a zoom level and window size to a camera height.
*
* @param zoom: Zoom level, as used in OSM maps.
* @param width: width of the window.
* @param height: height of the window.
* @returns: perspective camera height.
*/
////////////////////////////////////////////////////////////////////////////
vgl.zoomToHeight = function (zoom, width, height, viewAngle) {
'use strict';
viewAngle = viewAngle || (30 * Math.PI / 180.0);
var newZ = 360 * Math.pow(2, -zoom);
newZ /= Math.tan(viewAngle / 2) * 2 * 256 / height;
return newZ;
};
////////////////////////////////////////////////////////////////////////////
/**
* Convert a camera height and window size to a zoom level.
*
* @param z: perspective camera height.
* @param width: width of the window.
* @param height: height of the window.
* @returns: zoom level.
*/
////////////////////////////////////////////////////////////////////////////
vgl.heightToZoom = function (z, width, height, viewAngle) {
'use strict';
viewAngle = viewAngle || (30 * Math.PI / 180.0);
z *= Math.tan(viewAngle / 2) * 2 * 256 / height;
var zoom = -Math.log2(z / 360);
return zoom;
};
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit, $*/
//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class interactorStyle
*
* @class vgl.interactorStyle
* interactorStyle is a base class for all interactor styles
* @returns {vgl.interactorStyle}
*/
////////////////////////////////////////////////////////////////////////////
vgl.interactorStyle = function () {
'use strict';
if (!(this instanceof vgl.interactorStyle)) {
return new vgl.interactorStyle();
}
vgl.object.call(this);
// Private member variables
var m_that = this,
m_viewer = null;
////////////////////////////////////////////////////////////////////////////
/**
* Return viewer referenced by the interactor style
*
* @returns {null}
*/
////////////////////////////////////////////////////////////////////////////
this.viewer = function () {
return m_viewer;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set viewer for the interactor style
*
* @param viewer
*/
////////////////////////////////////////////////////////////////////////////
this.setViewer = function (viewer) {
if (viewer !== m_viewer) {
m_viewer = viewer;
$(m_viewer).on(vgl.event.mousePress, m_that.handleMouseDown);
$(m_viewer).on(vgl.event.mouseRelease, m_that.handleMouseUp);
$(m_viewer).on(vgl.event.mouseMove, m_that.handleMouseMove);
$(m_viewer).on(vgl.event.mouseOut, m_that.handleMouseOut);
$(m_viewer).on(vgl.event.mouseWheel, m_that.handleMouseWheel);
$(m_viewer).on(vgl.event.keyPress, m_that.handleKeyPress);
$(m_viewer).on(vgl.event.mouseContextMenu, m_that.handleContextMenu);
$(m_viewer).on(vgl.event.click, m_that.handleClick);
$(m_viewer).on(vgl.event.dblClick, m_that.handleDoubleClick);
this.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse down event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleMouseDown = function (event) {
event = event; /* unused parameter */
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse up event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleMouseUp = function (event) {
event = event; /* unused parameter */
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse move event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleMouseMove = function (event) {
event = event; /* unused parameter */
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse move event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleMouseOut = function (event) {
event = event; /* unused parameter */
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse wheel event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleMouseWheel = function (event) {
event = event; /* unused parameter */
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle click event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleClick = function (event) {
event = event; /* unused parameter */
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle double click event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleDoubleClick = function (event) {
event = event; /* unused parameter */
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle key press event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleKeyPress = function (event) {
event = event; /* unused parameter */
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle context menu event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleContextMenu = function (event) {
event = event; /* unused parameter */
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Reset to default
*/
////////////////////////////////////////////////////////////////////////////
this.reset = function () {
return true;
};
return this;
};
inherit(vgl.interactorStyle, vgl.object);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, vec4, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of trackballInteractorStyle
*
* @class vgl.trackballInteractorStyle
* @returns {vgl.trackballInteractorStyle}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.trackballInteractorStyle = function () {
'use strict';
if (!(this instanceof vgl.trackballInteractorStyle)) {
return new vgl.trackballInteractorStyle();
}
vgl.interactorStyle.call(this);
var m_that = this,
m_leftMouseBtnDown = false,
m_rightMouseBtnDown = false,
m_midMouseBtnDown = false,
m_outsideCanvas,
m_currPos = {x: 0, y: 0},
m_lastPos = {x: 0, y: 0};
/////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse move event
*
* @param event
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.handleMouseMove = function (event) {
var width = m_that.viewer().renderWindow().windowSize()[0],
height = m_that.viewer().renderWindow().windowSize()[1],
ren = m_that.viewer().renderWindow().activeRenderer(),
cam = ren.camera(), coords = m_that.viewer().relMouseCoords(event),
fp, fdp, fwp, dp1, dp2, wp1, wp2, dx, dy, dz, m_zTrans;
m_outsideCanvas = false;
m_currPos = {x: 0, y: 0};
if ((coords.x < 0) || (coords.x > width)) {
m_currPos.x = 0;
m_outsideCanvas = true;
} else {
m_currPos.x = coords.x;
}
if ((coords.y < 0) || (coords.y > height)) {
m_currPos.y = 0;
m_outsideCanvas = true;
} else {
m_currPos.y = coords.y;
}
if (m_outsideCanvas === true) {
return;
}
fp = cam.focalPoint();
fwp = vec4.fromValues(fp[0], fp[1], fp[2], 1);
fdp = ren.worldToDisplay(fwp, cam.viewMatrix(),
cam.projectionMatrix(), width, height);
dp1 = vec4.fromValues(m_currPos.x, m_currPos.y, fdp[2], 1.0);
dp2 = vec4.fromValues(m_lastPos.x, m_lastPos.y, fdp[2], 1.0);
wp1 = ren.displayToWorld(dp1, cam.viewMatrix(), cam.projectionMatrix(),
width, height);
wp2 = ren.displayToWorld(dp2, cam.viewMatrix(), cam.projectionMatrix(),
width, height);
dx = wp1[0] - wp2[0];
dy = wp1[1] - wp2[1];
dz = wp1[2] - wp2[2];
if (m_midMouseBtnDown) {
cam.pan(-dx, -dy, -dz);
m_that.viewer().render();
}
if (m_leftMouseBtnDown) {
cam.rotate((m_lastPos.x - m_currPos.x),
(m_lastPos.y - m_currPos.y));
ren.resetCameraClippingRange();
m_that.viewer().render();
}
if (m_rightMouseBtnDown) {
/// 2.0 is the speed up factor
m_zTrans = 2.0 * (m_currPos.y - m_lastPos.y) / height;
// Calculate zoom scale here
if (m_zTrans > 0) {
cam.zoom(1 - Math.abs(m_zTrans));
} else {
cam.zoom(1 + Math.abs(m_zTrans));
}
ren.resetCameraClippingRange();
m_that.viewer().render();
}
m_lastPos.x = m_currPos.x;
m_lastPos.y = m_currPos.y;
return false;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse down event
*
* @param event
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.handleMouseDown = function (event) {
var coords;
if (event.button === 0) {
m_leftMouseBtnDown = true;
}
if (event.button === 1) {
m_midMouseBtnDown = true;
}
if (event.button === 2) {
m_rightMouseBtnDown = true;
}
coords = m_that.viewer().relMouseCoords(event);
if (coords.x < 0) {
m_lastPos.x = 0;
} else {
m_lastPos.x = coords.x;
}
if (coords.y < 0) {
m_lastPos.y = 0;
} else {
m_lastPos.y = coords.y;
}
return false;
};
// @note We never get mouse up from scroll bar: See the bug report here
// http://bugs.jquery.com/ticket/8184
/////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse up event
*
* @param event
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.handleMouseUp = function (event) {
if (event.button === 0) {
m_leftMouseBtnDown = false;
}
if (event.button === 1) {
m_midMouseBtnDown = false;
}
if (event.button === 2) {
m_rightMouseBtnDown = false;
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse wheel event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleMouseWheel = function (event) {
var ren = m_that.viewer().renderWindow().activeRenderer(),
cam = ren.camera();
// TODO Compute zoom factor intelligently
if (event.originalEvent.wheelDelta < 0) {
cam.zoom(0.9);
} else {
cam.zoom(1.1);
}
ren.resetCameraClippingRange();
m_that.viewer().render();
return true;
};
return this;
};
inherit(vgl.trackballInteractorStyle, vgl.interactorStyle);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, vec4, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of pvwInteractorStyle (for ParaViewWeb)
*
* @class vgl.pvwInteractorStyle
* @returns {vgl.pvwInteractorStyle}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.pvwInteractorStyle = function () {
'use strict';
if (!(this instanceof vgl.pvwInteractorStyle)) {
return new vgl.pvwInteractorStyle();
}
vgl.trackballInteractorStyle.call(this);
var m_that = this,
m_leftMouseButtonDown = false,
m_rightMouseButtonDown = false,
m_middleMouseButtonDown = false,
m_width,
m_height,
m_renderer,
m_camera,
m_outsideCanvas,
m_coords,
m_currentMousePos,
m_focalPoint,
m_focusWorldPt,
m_focusDisplayPt,
m_displayPt1,
m_displayPt2,
m_worldPt1,
m_worldPt2,
m_dx,
m_dy,
m_dz,
m_zTrans,
m_mouseLastPos = {
x: 0,
y: 0
};
function render() {
m_renderer.resetCameraClippingRange();
m_that.viewer().render();
}
/////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse move event
*
* @param event
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.handleMouseMove = function (event) {
var rens = [], i = null, secCameras = [], deltaxy = null;
m_width = m_that.viewer().renderWindow().windowSize()[0];
m_height = m_that.viewer().renderWindow().windowSize()[1];
m_renderer = m_that.viewer().renderWindow().activeRenderer();
m_camera = m_renderer.camera();
m_outsideCanvas = false;
m_coords = m_that.viewer().relMouseCoords(event);
m_currentMousePos = {
x: 0,
y: 0
};
// Get secondary cameras
rens = m_that.viewer().renderWindow().renderers();
for (i = 0; i < rens.length; i += 1) {
if (m_renderer !== rens[i]) {
secCameras.push(rens[i].camera());
}
}
if ((m_coords.x < 0) || (m_coords.x > m_width)) {
m_currentMousePos.x = 0;
m_outsideCanvas = true;
} else {
m_currentMousePos.x = m_coords.x;
}
if ((m_coords.y < 0) || (m_coords.y > m_height)) {
m_currentMousePos.y = 0;
m_outsideCanvas = true;
} else {
m_currentMousePos.y = m_coords.y;
}
if (m_outsideCanvas === true) {
return;
}
m_focalPoint = m_camera.focalPoint();
m_focusWorldPt = vec4.fromValues(m_focalPoint[0], m_focalPoint[1],
m_focalPoint[2], 1);
m_focusDisplayPt = m_renderer.worldToDisplay(m_focusWorldPt,
m_camera.viewMatrix(), m_camera.projectionMatrix(), m_width, m_height);
m_displayPt1 = vec4.fromValues(
m_currentMousePos.x, m_currentMousePos.y, m_focusDisplayPt[2], 1.0);
m_displayPt2 = vec4.fromValues(
m_mouseLastPos.x, m_mouseLastPos.y, m_focusDisplayPt[2], 1.0);
m_worldPt1 = m_renderer.displayToWorld(
m_displayPt1, m_camera.viewMatrix(), m_camera.projectionMatrix(),
m_width, m_height);
m_worldPt2 = m_renderer.displayToWorld(
m_displayPt2, m_camera.viewMatrix(), m_camera.projectionMatrix(),
m_width, m_height);
m_dx = m_worldPt1[0] - m_worldPt2[0];
m_dy = m_worldPt1[1] - m_worldPt2[1];
m_dz = m_worldPt1[2] - m_worldPt2[2];
if (m_middleMouseButtonDown) {
m_camera.pan(-m_dx, -m_dy, -m_dz);
render();
}
if (m_leftMouseButtonDown) {
deltaxy = [(m_mouseLastPos.x - m_currentMousePos.x),
(m_mouseLastPos.y - m_currentMousePos.y)];
m_camera.rotate(deltaxy[0], deltaxy[1]);
// Apply rotation to all other cameras
for (i = 0; i < secCameras.length; i += 1) {
secCameras[i].rotate(deltaxy[0], deltaxy[1]);
}
// Apply rotation to all other cameras
for (i = 0; i < rens.length; i += 1) {
rens[i].resetCameraClippingRange();
}
render();
}
if (m_rightMouseButtonDown) {
/// 2.0 is the speed up factor.
m_zTrans = 2.0 * (m_currentMousePos.y - m_mouseLastPos.y) / m_height;
// Calculate zoom scale here
if (m_zTrans > 0) {
m_camera.zoom(1 - Math.abs(m_zTrans));
} else {
m_camera.zoom(1 + Math.abs(m_zTrans));
}
render();
}
m_mouseLastPos.x = m_currentMousePos.x;
m_mouseLastPos.y = m_currentMousePos.y;
return false;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse down event
*
* @param event
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.handleMouseDown = function (event) {
if (event.button === 0) {
m_leftMouseButtonDown = true;
}
if (event.button === 1) {
m_middleMouseButtonDown = true;
}
if (event.button === 2) {
m_rightMouseButtonDown = true;
}
m_coords = m_that.viewer().relMouseCoords(event);
if (m_coords.x < 0) {
m_mouseLastPos.x = 0;
} else {
m_mouseLastPos.x = m_coords.x;
}
if (m_coords.y < 0) {
m_mouseLastPos.y = 0;
} else {
m_mouseLastPos.y = m_coords.y;
}
return false;
};
// @note We never get mouse up from scroll bar: See the bug report here
// http://bugs.jquery.com/ticket/8184
/////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse up event
*
* @param event
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.handleMouseUp = function (event) {
if (event.button === 0) {
m_leftMouseButtonDown = false;
}
if (event.button === 1) {
m_middleMouseButtonDown = false;
}
if (event.button === 2) {
m_rightMouseButtonDown = false;
}
return false;
};
return this;
};
inherit(vgl.pvwInteractorStyle, vgl.trackballInteractorStyle);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global window, vgl, inherit, $*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class viewer
*
* @param canvas
* @returns {vgl.viewer}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.viewer = function (canvas, options) {
'use strict';
if (!(this instanceof vgl.viewer)) {
return new vgl.viewer(canvas, options);
}
vgl.object.call(this);
var m_that = this,
m_canvas = canvas,
m_ready = true,
m_interactorStyle = null,
m_renderer = vgl.renderer(options),
m_renderWindow = vgl.renderWindow(m_canvas);
////////////////////////////////////////////////////////////////////////////
/**
* Get canvas of the viewer
*
* @returns {*}
*/
////////////////////////////////////////////////////////////////////////////
this.canvas = function () {
return m_canvas;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return render window of the viewer
*
* @returns {vgl.renderWindow}
*/
////////////////////////////////////////////////////////////////////////////
this.renderWindow = function () {
return m_renderWindow;
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize the viewer
*
* This is a must call or otherwise render context may not initialized
* properly.
*/
////////////////////////////////////////////////////////////////////////////
this.init = function () {
if (m_renderWindow !== null) {
m_renderWindow._setup();
} else {
console.log('[ERROR] No render window attached');
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Remove the viewer
*/
////////////////////////////////////////////////////////////////////////////
this.exit = function (renderState) {
if (m_renderWindow !== null) {
m_renderWindow._cleanup(renderState);
} else {
console.log('[ERROR] No render window attached');
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get interactor style of the viewer
*
* @returns {vgl.interactorStyle}
*/
////////////////////////////////////////////////////////////////////////////
this.interactorStyle = function () {
return m_interactorStyle;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set interactor style to be used by the viewer
*
* @param {vgl.interactorStyle} style
*/
////////////////////////////////////////////////////////////////////////////
this.setInteractorStyle = function (style) {
if (style !== m_interactorStyle) {
m_interactorStyle = style;
m_interactorStyle.setViewer(this);
this.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse down event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleMouseDown = function (event) {
if (m_ready === true) {
var fixedEvent = $.event.fix(event || window.event);
// Only prevent default action for right mouse button
if (event.button === 2) {
fixedEvent.preventDefault();
}
fixedEvent.state = 'down';
fixedEvent.type = vgl.event.mousePress;
$(m_that).trigger(fixedEvent);
}
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse up event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleMouseUp = function (event) {
if (m_ready === true) {
var fixedEvent = $.event.fix(event || window.event);
fixedEvent.preventDefault();
fixedEvent.state = 'up';
fixedEvent.type = vgl.event.mouseRelease;
$(m_that).trigger(fixedEvent);
}
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse move event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleMouseMove = function (event) {
if (m_ready === true) {
var fixedEvent = $.event.fix(event || window.event);
fixedEvent.preventDefault();
fixedEvent.type = vgl.event.mouseMove;
$(m_that).trigger(fixedEvent);
}
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse wheel scroll
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleMouseWheel = function (event) {
if (m_ready === true) {
var fixedEvent = $.event.fix(event || window.event);
fixedEvent.preventDefault();
fixedEvent.type = vgl.event.mouseWheel;
$(m_that).trigger(fixedEvent);
}
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse move event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleMouseOut = function (event) {
if (m_ready === true) {
var fixedEvent = $.event.fix(event || window.event);
fixedEvent.preventDefault();
fixedEvent.type = vgl.event.mouseOut;
$(m_that).trigger(fixedEvent);
}
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle key press event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleKeyPress = function (event) {
if (m_ready === true) {
var fixedEvent = $.event.fix(event || window.event);
fixedEvent.preventDefault();
fixedEvent.type = vgl.event.keyPress;
$(m_that).trigger(fixedEvent);
}
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle context menu event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleContextMenu = function (event) {
if (m_ready === true) {
var fixedEvent = $.event.fix(event || window.event);
fixedEvent.preventDefault();
fixedEvent.type = vgl.event.contextMenu;
$(m_that).trigger(fixedEvent);
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle click event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleClick = function (event) {
if (m_ready === true) {
var fixedEvent = $.event.fix(event || window.event);
fixedEvent.preventDefault();
fixedEvent.type = vgl.event.click;
$(m_that).trigger(fixedEvent);
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle double click event
*
* @param event
* @returns {boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.handleDoubleClick = function (event) {
if (m_ready === true) {
var fixedEvent = $.event.fix(event || window.event);
fixedEvent.preventDefault();
fixedEvent.type = vgl.event.dblClick;
$(m_that).trigger(fixedEvent);
}
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get mouse coodinates related to canvas
*
* @param event
* @returns {{x: number, y: number}}
*/
////////////////////////////////////////////////////////////////////////////
this.relMouseCoords = function (event) {
if (event.pageX === undefined || event.pageY === undefined) {
throw 'Missing attributes pageX and pageY on the event';
}
var totalOffsetX = 0,
totalOffsetY = 0,
canvasX = 0,
canvasY = 0,
currentElement = m_canvas;
do {
totalOffsetX += currentElement.offsetLeft - currentElement.scrollLeft;
totalOffsetY += currentElement.offsetTop - currentElement.scrollTop;
currentElement = currentElement.offsetParent;
} while (currentElement);
canvasX = event.pageX - totalOffsetX;
canvasY = event.pageY - totalOffsetY;
return {
x: canvasX,
y: canvasY
};
};
////////////////////////////////////////////////////////////////////////////
/**
* Render
*/
////////////////////////////////////////////////////////////////////////////
this.render = function () {
m_renderWindow.render();
};
////////////////////////////////////////////////////////////////////////////
/**
* Bind canvas mouse events to their default handlers
*/
////////////////////////////////////////////////////////////////////////////
this.bindEventHandlers = function () {
$(m_canvas).on('mousedown', this.handleMouseDown);
$(m_canvas).on('mouseup', this.handleMouseUp);
$(m_canvas).on('mousemove', this.handleMouseMove);
$(m_canvas).on('mousewheel', this.handleMouseWheel);
$(m_canvas).on('contextmenu', this.handleContextMenu);
};
////////////////////////////////////////////////////////////////////////////
/**
* Undo earlier binded handlers for canvas mouse events
*/
////////////////////////////////////////////////////////////////////////////
this.unbindEventHandlers = function () {
$(m_canvas).off('mousedown', this.handleMouseDown);
$(m_canvas).off('mouseup', this.handleMouseUp);
$(m_canvas).off('mousemove', this.handleMouseMove);
$(m_canvas).off('mousewheel', this.handleMouseWheel);
$(m_canvas).off('contextmenu', this.handleContextMenu);
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function () {
this.bindEventHandlers();
m_renderWindow.addRenderer(m_renderer);
};
this._init();
return this;
};
inherit(vgl.viewer, vgl.object);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class shader
*
* @param type
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.shader = function (type) {
'use strict';
if (!(this instanceof vgl.shader)) {
return new vgl.shader(type);
}
vgl.object.call(this);
var m_shaderContexts = [],
m_shaderType = type,
m_shaderSource = '';
/**
* A shader can be associated with multiple contexts. Each context needs to
* be compiled and attached separately. These are tracked in the
* m_shaderContexts array.
*
* @param renderState a renderState that includes a m_context value.
* @return an object with context, compileTimestamp, and, if compiled, a
* shaderHandle entry.
*/
this._getContextEntry = function (renderState) {
var context = renderState.m_context, i, entry;
for (i = 0; i < m_shaderContexts.length; i += 1) {
if (m_shaderContexts[i].context === context) {
return m_shaderContexts[i];
}
}
entry = {
context: context,
compileTimestamp: vgl.timestamp()
};
m_shaderContexts.push(entry);
return entry;
};
/**
* Remove the context from the list of tracked contexts. This allows the
* associated shader handle to be GCed. Does nothing if the context is not
* in the list of tracked contexts.
*
* @param renderState a renderState that includes a m_context value.
*/
this.removeContext = function (renderState) {
var context = renderState.m_context, i;
for (i = 0; i < m_shaderContexts.length; i += 1) {
if (m_shaderContexts[i].context === context) {
m_shaderContexts.splice(i, 1);
return;
}
}
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get shader handle
*/
/////////////////////////////////////////////////////////////////////////////
this.shaderHandle = function (renderState) {
var entry = this._getContextEntry(renderState);
return entry.shaderHandle;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get type of the shader
*
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.shaderType = function () {
return m_shaderType;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get shader source
*
* @returns {string}
*/
/////////////////////////////////////////////////////////////////////////////
this.shaderSource = function () {
return m_shaderSource;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set shader source
*
* @param {string} source
*/
/////////////////////////////////////////////////////////////////////////////
this.setShaderSource = function (source) {
m_shaderSource = source;
this.modified();
};
/////////////////////////////////////////////////////////////////////////////
/**
* Compile the shader
*
* @returns {null}
*/
/////////////////////////////////////////////////////////////////////////////
this.compile = function (renderState) {
var entry = this._getContextEntry(renderState);
if (this.getMTime() < entry.compileTimestamp.getMTime()) {
return entry.shaderHandle;
}
renderState.m_context.deleteShader(entry.shaderHandle);
entry.shaderHandle = renderState.m_context.createShader(m_shaderType);
renderState.m_context.shaderSource(entry.shaderHandle, m_shaderSource);
renderState.m_context.compileShader(entry.shaderHandle);
// See if it compiled successfully
if (!renderState.m_context.getShaderParameter(entry.shaderHandle,
vgl.GL.COMPILE_STATUS)) {
console.log('[ERROR] An error occurred compiling the shaders: ' +
renderState.m_context.getShaderInfoLog(entry.shaderHandle));
console.log(m_shaderSource);
renderState.m_context.deleteShader(entry.shaderHandle);
return null;
}
entry.compileTimestamp.modified();
return entry.shaderHandle;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Attach shader to the program
*
* @param programHandle
*/
/////////////////////////////////////////////////////////////////////////////
this.attachShader = function (renderState, programHandle) {
renderState.m_context.attachShader(
programHandle, this.shaderHandle(renderState));
};
};
inherit(vgl.shader, vgl.object);
/* We can use the same shader multiple times if it is identical. This caches
* the last N shaders and will reuse them when possible. The cache keeps the
* most recently requested shader at the front. If you are doing anything more
* to a shader then creating it and setting its source once, do not use this
* cache.
*/
(function () {
'use strict';
var m_shaderCache = [],
m_shaderCacheMaxSize = 10;
/////////////////////////////////////////////////////////////////////////////
/**
* Get a shader from the cache. Create a new shader if necessary using a
* specific source.
*
* @param type One of vgl.GL.*_SHADER
* @param context the GL context for the shader.
* @param {string} source the source code of the shader.
*/
/////////////////////////////////////////////////////////////////////////////
vgl.getCachedShader = function (type, context, source) {
for (var i = 0; i < m_shaderCache.length; i += 1) {
if (m_shaderCache[i].type === type &&
m_shaderCache[i].context === context &&
m_shaderCache[i].source === source) {
if (i) {
m_shaderCache.splice(0, 0, m_shaderCache.splice(i, 1)[0]);
}
return m_shaderCache[0].shader;
}
}
var shader = new vgl.shader(type);
shader.setShaderSource(source);
m_shaderCache.unshift({
type: type,
context: context,
source: source,
shader: shader
});
if (m_shaderCache.length >= m_shaderCacheMaxSize) {
m_shaderCache.splice(m_shaderCacheMaxSize,
m_shaderCache.length - m_shaderCacheMaxSize);
}
return shader;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Clear the shader cache.
*
* @param context the GL context to clear, or null for clear all.
*/
/////////////////////////////////////////////////////////////////////////////
vgl.clearCachedShaders = function (context) {
for (var i = m_shaderCache.length - 1; i >= 0; i -= 1) {
if (context === null || context === undefined ||
m_shaderCache[i].context === context) {
m_shaderCache.splice(i, 1);
}
}
};
})();
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit, $*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instace of class shaderProgram
*
* @class
* @returns {vgl.shaderProgram}
*/
//////////////////////////////////////////////////////////////////////////////
var getBaseUrl = (function () {
'use strict';
var baseUrl = '.';
var scripts = document.getElementsByTagName('script');
/* When run in certain environments, there may be no scripts loaded. For
* instance, jQuery's $.getScript won't add it to a script tag. */
if (scripts.length > 0) {
var index = scripts.length - 1;
var vglScript = scripts[index];
index = vglScript.src.lastIndexOf('/');
baseUrl = vglScript.src.substring(0, index);
}
return function () { return baseUrl; };
})();
vgl.shaderProgram = function () {
'use strict';
if (!(this instanceof vgl.shaderProgram)) {
return new vgl.shaderProgram();
}
vgl.materialAttribute.call(
this, vgl.materialAttributeType.ShaderProgram);
/** @private */
var m_this = this,
m_programHandle = 0,
m_compileTimestamp = vgl.timestamp(),
m_bindTimestamp = vgl.timestamp(),
m_shaders = [],
m_uniforms = [],
m_vertexAttributes = {},
m_uniformNameToLocation = {},
m_vertexAttributeNameToLocation = {};
/////////////////////////////////////////////////////////////////////////////
/**
* Create a particular shader type using GLSL shader strings from a file
*/
/////////////////////////////////////////////////////////////////////////////
this.loadFromFile = function (type, sourceUrl) {
var shader;
$.ajax({
url: sourceUrl,
type: 'GET',
async: false,
success: function (result) {
//console.log(result);
shader = vgl.shader(type);
shader.setShaderSource(result);
m_this.addShader(shader);
}
});
};
/////////////////////////////////////////////////////////////////////////////
/**
* Create a particular shader type using GLSL shader strings from a file
* relative to VGL load URL.
*/
/////////////////////////////////////////////////////////////////////////////
this.loadShader = function (type, file) {
this.loadFromFile(type, getBaseUrl() + '/shaders/' + file);
};
/////////////////////////////////////////////////////////////////////////////
/**
* Query uniform location in the program
*
* @param name
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.queryUniformLocation = function (renderState, name) {
return renderState.m_context.getUniformLocation(m_programHandle, name);
};
/////////////////////////////////////////////////////////////////////////////
/**
* Query attribute location in the program
*
* @param name
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.queryAttributeLocation = function (renderState, name) {
return renderState.m_context.getAttribLocation(m_programHandle, name);
};
/////////////////////////////////////////////////////////////////////////////
/**
* Add a new shader to the program
*
* @param shader
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.addShader = function (shader) {
if (m_shaders.indexOf(shader) > -1) {
return false;
}
var i;
for (i = 0; i < m_shaders.length; i += 1) {
if (m_shaders[i].shaderType() === shader.shaderType()) {
m_shaders.splice(m_shaders.indexOf(shader), 1);
}
}
m_shaders.push(shader);
m_this.modified();
return true;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Add a new uniform to the program
*
* @param uniform
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.addUniform = function (uniform) {
if (m_uniforms.indexOf(uniform) > -1) {
return false;
}
m_uniforms.push(uniform);
m_this.modified();
};
/////////////////////////////////////////////////////////////////////////////
/**
* Add a new vertex attribute to the program
*
* @param attr
* @param key
*/
/////////////////////////////////////////////////////////////////////////////
this.addVertexAttribute = function (attr, key) {
m_vertexAttributes[key] = attr;
m_this.modified();
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get uniform location
*
* This method does not perform any query into the program but relies on
* the fact that it depends on a call to queryUniformLocation earlier.
*
* @param name
* @returns {number}
*/
/////////////////////////////////////////////////////////////////////////////
this.uniformLocation = function (name) {
return m_uniformNameToLocation[name];
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get attribute location
*
* This method does not perform any query into the program but relies on the
* fact that it depends on a call to queryUniformLocation earlier.
*
* @param name
* @returns {number}
*/
/////////////////////////////////////////////////////////////////////////////
this.attributeLocation = function (name) {
return m_vertexAttributeNameToLocation[name];
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get uniform object using name as the key
*
* @param name
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.uniform = function (name) {
var i;
for (i = 0; i < m_uniforms.length; i += 1) {
if (m_uniforms[i].name() === name) {
return m_uniforms[i];
}
}
return null;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Update all uniforms
*
* This method should be used directly unless required
*/
/////////////////////////////////////////////////////////////////////////////
this.updateUniforms = function (renderState) {
var i;
for (i = 0; i < m_uniforms.length; i += 1) {
m_uniforms[i].callGL(renderState,
m_uniformNameToLocation[m_uniforms[i].name()]);
}
};
/////////////////////////////////////////////////////////////////////////////
/**
* Link shader program
*
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.link = function (renderState) {
renderState.m_context.linkProgram(m_programHandle);
// If creating the shader program failed, alert
if (!renderState.m_context.getProgramParameter(m_programHandle,
vgl.GL.LINK_STATUS)) {
console.log('[ERROR] Unable to initialize the shader program.');
return false;
}
return true;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Use the shader program
*/
/////////////////////////////////////////////////////////////////////////////
this.use = function (renderState) {
renderState.m_context.useProgram(m_programHandle);
};
/////////////////////////////////////////////////////////////////////////////
/**
* Peform any initialization required
*/
/////////////////////////////////////////////////////////////////////////////
this._setup = function (renderState) {
if (m_programHandle === 0) {
m_programHandle = renderState.m_context.createProgram();
}
};
/////////////////////////////////////////////////////////////////////////////
/**
* Peform any clean up required when the program gets deleted
*/
/////////////////////////////////////////////////////////////////////////////
this._cleanup = function (renderState) {
m_this.deleteVertexAndFragment(renderState);
m_this.deleteProgram(renderState);
};
/////////////////////////////////////////////////////////////////////////////
/**
* Delete the shader program
*/
/////////////////////////////////////////////////////////////////////////////
this.deleteProgram = function (renderState) {
renderState.m_context.deleteProgram(m_programHandle);
};
/////////////////////////////////////////////////////////////////////////////
/**
* Delete vertex and fragment shaders
*/
/////////////////////////////////////////////////////////////////////////////
this.deleteVertexAndFragment = function (renderState) {
var i;
for (i = 0; i < m_shaders.length; i += 1) {
renderState.m_context.detachShader(m_shaders[i].shaderHandle(renderState));
renderState.m_context.deleteShader(m_shaders[i].shaderHandle(renderState));
m_shaders[i].removeContext(renderState);
}
};
/////////////////////////////////////////////////////////////////////////////
/**
* Compile and link a shader
*/
/////////////////////////////////////////////////////////////////////////////
this.compileAndLink = function (renderState) {
var i;
if (m_compileTimestamp.getMTime() >= this.getMTime()) {
return;
}
m_this._setup(renderState);
// Compile shaders
for (i = 0; i < m_shaders.length; i += 1) {
m_shaders[i].compile(renderState);
m_shaders[i].attachShader(renderState, m_programHandle);
}
m_this.bindAttributes(renderState);
// link program
if (!m_this.link(renderState)) {
console.log('[ERROR] Failed to link Program');
m_this._cleanup(renderState);
}
m_compileTimestamp.modified();
};
/////////////////////////////////////////////////////////////////////////////
/**
* Bind the program with its shaders
*
* @param renderState
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.bind = function (renderState) {
var i = 0;
if (m_bindTimestamp.getMTime() < m_this.getMTime()) {
// Compile shaders
m_this.compileAndLink(renderState);
m_this.use(renderState);
m_this.bindUniforms(renderState);
m_bindTimestamp.modified();
} else {
m_this.use(renderState);
}
// Call update callback.
for (i = 0; i < m_uniforms.length; i += 1) {
m_uniforms[i].update(renderState, m_this);
}
// Now update values to GL.
m_this.updateUniforms(renderState);
};
/////////////////////////////////////////////////////////////////////////////
/**
* Undo binding of the shader program
*
* @param renderState
*/
/////////////////////////////////////////////////////////////////////////////
this.undoBind = function (renderState) {
// REF https://www.khronos.org/opengles/sdk/docs/man/xhtml/glUseProgram.xml
// If program is 0, then the current rendering state refers to an invalid
// program object, and the results of vertex and fragment shader execution
// due to any glDrawArrays or glDrawElements commands are undefined
renderState.m_context.useProgram(null);
};
/////////////////////////////////////////////////////////////////////////////
/**
* Bind vertex data
*
* @param renderState
* @param key
*/
/////////////////////////////////////////////////////////////////////////////
this.bindVertexData = function (renderState, key) {
if (m_vertexAttributes.hasOwnProperty(key)) {
m_vertexAttributes[key].bindVertexData(renderState, key);
}
};
/////////////////////////////////////////////////////////////////////////////
/**
* Undo bind vetex data
*
* @param renderState
* @param key
*/
/////////////////////////////////////////////////////////////////////////////
this.undoBindVertexData = function (renderState, key) {
if (m_vertexAttributes.hasOwnProperty(key)) {
m_vertexAttributes[key].undoBindVertexData(renderState, key);
}
};
/////////////////////////////////////////////////////////////////////////////
/**
* Bind uniforms
*/
/////////////////////////////////////////////////////////////////////////////
this.bindUniforms = function (renderState) {
var i;
for (i = 0; i < m_uniforms.length; i += 1) {
m_uniformNameToLocation[m_uniforms[i].name()] = this
.queryUniformLocation(renderState, m_uniforms[i].name());
}
};
/////////////////////////////////////////////////////////////////////////////
/**
* Bind vertex attributes
*/
/////////////////////////////////////////////////////////////////////////////
this.bindAttributes = function (renderState) {
var key, name;
for (key in m_vertexAttributes) {
if (m_vertexAttributes.hasOwnProperty(key)) {
name = m_vertexAttributes[key].name();
renderState.m_context.bindAttribLocation(m_programHandle, key, name);
m_vertexAttributeNameToLocation[name] = key;
}
}
};
return m_this;
};
inherit(vgl.shaderProgram, vgl.materialAttribute);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global Uint8Array, vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class texture
*
* @class
* @returns {vgl.texture}
*/
///////////////////////////////////////////////////////////////////////////////
vgl.texture = function () {
'use strict';
if (!(this instanceof vgl.texture)) {
return new vgl.texture();
}
vgl.materialAttribute.call(
this, vgl.materialAttributeType.Texture);
this.m_width = 0;
this.m_height = 0;
this.m_depth = 0;
this.m_textureHandle = null;
this.m_textureUnit = 0;
this.m_pixelFormat = vgl.GL.RGBA;
this.m_pixelDataType = vgl.GL.UNSIGNED_BYTE;
this.m_internalFormat = vgl.GL.RGBA;
this.m_nearestPixel = false;
this.m_image = null;
var m_setupTimestamp = vgl.timestamp(),
m_that = this;
function activateTextureUnit(renderState) {
switch (m_that.m_textureUnit) {
case 0:
renderState.m_context.activeTexture(vgl.GL.TEXTURE0);
break;
case 1:
renderState.m_context.activeTexture(vgl.GL.TEXTURE1);
break;
case 2:
renderState.m_context.activeTexture(vgl.GL.TEXTURE2);
break;
case 3:
renderState.m_context.activeTexture(vgl.GL.TEXTURE3);
break;
case 4:
renderState.m_context.activeTexture(vgl.GL.TEXTURE4);
break;
case 5:
renderState.m_context.activeTexture(vgl.GL.TEXTURE5);
break;
case 6:
renderState.m_context.activeTexture(vgl.GL.TEXTURE6);
break;
case 7:
renderState.m_context.activeTexture(vgl.GL.TEXTURE7);
break;
case 8:
renderState.m_context.activeTexture(vgl.GL.TEXTURE8);
break;
case 9:
renderState.m_context.activeTexture(vgl.GL.TEXTURE9);
break;
case 10:
renderState.m_context.activeTexture(vgl.GL.TEXTURE10);
break;
case 11:
renderState.m_context.activeTexture(vgl.GL.TEXTURE11);
break;
case 12:
renderState.m_context.activeTexture(vgl.GL.TEXTURE12);
break;
case 13:
renderState.m_context.activeTexture(vgl.GL.TEXTURE13);
break;
case 14:
renderState.m_context.activeTexture(vgl.GL.TEXTURE14);
break;
case 15:
renderState.m_context.activeTexture(vgl.GL.TEXTURE15);
break;
default:
throw '[error] Texture unit ' + m_that.m_textureUnit +
' is not supported';
}
}
/////////////////////////////////////////////////////////////////////////////
/**
* Create texture, update parameters, and bind data
*
* @param renderState
*/
/////////////////////////////////////////////////////////////////////////////
this.setup = function (renderState) {
// Activate the texture unit first
activateTextureUnit(renderState);
renderState.m_context.deleteTexture(this.m_textureHandle);
this.m_textureHandle = renderState.m_context.createTexture();
renderState.m_context.bindTexture(vgl.GL.TEXTURE_2D, this.m_textureHandle);
renderState.m_context.texParameteri(vgl.GL.TEXTURE_2D,
vgl.GL.TEXTURE_MIN_FILTER,
this.m_nearestPixel ? vgl.GL.NEAREST : vgl.GL.LINEAR);
renderState.m_context.texParameteri(vgl.GL.TEXTURE_2D,
vgl.GL.TEXTURE_MAG_FILTER,
this.m_nearestPixel ? vgl.GL.NEAREST : vgl.GL.LINEAR);
renderState.m_context.texParameteri(vgl.GL.TEXTURE_2D,
vgl.GL.TEXTURE_WRAP_S, vgl.GL.CLAMP_TO_EDGE);
renderState.m_context.texParameteri(vgl.GL.TEXTURE_2D,
vgl.GL.TEXTURE_WRAP_T, vgl.GL.CLAMP_TO_EDGE);
if (this.m_image !== null) {
renderState.m_context.pixelStorei(vgl.GL.UNPACK_ALIGNMENT, 1);
renderState.m_context.pixelStorei(vgl.GL.UNPACK_FLIP_Y_WEBGL, true);
this.updateDimensions();
this.computeInternalFormatUsingImage();
// console.log('m_internalFormat ' + this.m_internalFormat);
// console.log('m_pixelFormat ' + this.m_pixelFormat);
// console.log('m_pixelDataType ' + this.m_pixelDataType);
// FOR now support only 2D textures
renderState.m_context.texImage2D(vgl.GL.TEXTURE_2D, 0, this.m_internalFormat,
this.m_pixelFormat, this.m_pixelDataType, this.m_image);
} else {
renderState.m_context.texImage2D(vgl.GL.TEXTURE_2D, 0, this.m_internalFormat,
this.m_width, this.m_height, 0, this.m_pixelFormat, this.m_pixelDataType, null);
}
renderState.m_context.bindTexture(vgl.GL.TEXTURE_2D, null);
m_setupTimestamp.modified();
};
/////////////////////////////////////////////////////////////////////////////
/**
* Create texture and if already created use it
*
* @param renderState
*/
/////////////////////////////////////////////////////////////////////////////
this.bind = function (renderState) {
// TODO Call setup via material setup
if (this.getMTime() > m_setupTimestamp.getMTime()) {
this.setup(renderState);
}
activateTextureUnit(renderState);
renderState.m_context.bindTexture(vgl.GL.TEXTURE_2D, this.m_textureHandle);
};
/////////////////////////////////////////////////////////////////////////////
/**
* Turn off the use of this texture
*
* @param renderState
*/
/////////////////////////////////////////////////////////////////////////////
this.undoBind = function (renderState) {
renderState.m_context.bindTexture(vgl.GL.TEXTURE_2D, null);
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get image used by the texture
*
* @returns {vgl.image}
*/
/////////////////////////////////////////////////////////////////////////////
this.image = function () {
return this.m_image;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set image for the texture
*
* @param {vgl.image} image
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.setImage = function (image) {
if (image !== null) {
this.m_image = image;
this.updateDimensions();
this.modified();
return true;
}
return false;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get nearest pixel flag for the texture
*
* @returns boolean
*/
/////////////////////////////////////////////////////////////////////////////
this.nearestPixel = function () {
return this.m_nearestPixel;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set nearest pixel flag for the texture
*
* @param {boolean} nearest pixel flag
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.setNearestPixel = function (nearest) {
nearest = nearest ? true : false;
if (nearest !== this.m_nearestPixel) {
this.m_nearestPixel = nearest;
this.modified();
return true;
}
return false;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get texture unit of the texture
*
* @returns {number}
*/
/////////////////////////////////////////////////////////////////////////////
this.textureUnit = function () {
return this.m_textureUnit;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set texture unit of the texture. Default is 0.
*
* @param {number} unit
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.setTextureUnit = function (unit) {
if (this.m_textureUnit === unit) {
return false;
}
this.m_textureUnit = unit;
this.modified();
return true;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get width of the texture
*
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.width = function () {
return this.m_width;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set width of the texture
*
* @param {number} width
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.setWidth = function (width) {
if (m_that.m_width !== width) {
m_that.m_width = width;
m_that.modified();
return true;
}
return false;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get width of the texture
*
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.height = function () {
return m_that.m_height;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set height of the texture
*
* @param {number} height
* @returns {vgl.texture}
*/
/////////////////////////////////////////////////////////////////////////////
this.setHeight = function (height) {
if (m_that.m_height !== height) {
m_that.m_height = height;
m_that.modified();
return true;
}
return false;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get depth of the texture
*
* @returns {number}
*/
/////////////////////////////////////////////////////////////////////////////
this.depth = function () {
return this.m_depth;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set depth of the texture
*
* @param {number} depth
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.setDepth = function (depth) {
if (this.m_image === null) {
return false;
}
this.m_depth = depth;
this.modified();
return true;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get the texture handle (id) of the texture
*
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.textureHandle = function () {
return this.m_textureHandle;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get internal format of the texture
*
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.internalFormat = function () {
return this.m_internalFormat;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set internal format of the texture
*
* @param internalFormat
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.setInternalFormat = function (internalFormat) {
if (this.m_internalFormat !== internalFormat) {
this.m_internalFormat = internalFormat;
this.modified();
return true;
}
return false;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get pixel format of the texture
*
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.pixelFormat = function () {
return this.m_pixelFormat;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set pixel format of the texture
*
* @param pixelFormat
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.setPixelFormat = function (pixelFormat) {
if (this.m_image === null) {
return false;
}
this.m_pixelFormat = pixelFormat;
this.modified();
return true;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get pixel data type
*
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.pixelDataType = function () {
return this.m_pixelDataType;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set pixel data type
*
* @param pixelDataType
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.setPixelDataType = function (pixelDataType) {
if (this.m_image === null) {
return false;
}
this.m_pixelDataType = pixelDataType;
this.modified();
return true;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Compute internal format of the texture
*/
/////////////////////////////////////////////////////////////////////////////
this.computeInternalFormatUsingImage = function () {
// Currently image does not define internal format
// and hence it's pixel format is the only way to query
// information on how color has been stored.
// switch (this.m_image.pixelFormat()) {
// case vgl.GL.RGB:
// this.m_internalFormat = vgl.GL.RGB;
// break;
// case vgl.GL.RGBA:
// this.m_internalFormat = vgl.GL.RGBA;
// break;
// case vgl.GL.Luminance:
// this.m_internalFormat = vgl.GL.Luminance;
// break;
// case vgl.GL.LuminanceAlpha:
// this.m_internalFormat = vgl.GL.LuminanceAlpha;
// break;
// // Do nothing when image pixel format is none or undefined.
// default:
// break;
// };
// TODO Fix this
this.m_internalFormat = vgl.GL.RGBA;
this.m_pixelFormat = vgl.GL.RGBA;
this.m_pixelDataType = vgl.GL.UNSIGNED_BYTE;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Update texture dimensions
*/
/////////////////////////////////////////////////////////////////////////////
this.updateDimensions = function () {
if (this.m_image !== null) {
this.m_width = this.m_image.width;
this.m_height = this.m_image.height;
this.m_depth = 0; // Only 2D images are supported now
}
};
return this;
};
inherit(vgl.texture, vgl.materialAttribute);
///////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class lookupTable
*
* @class
* @returns {vgl.lookupTable}
*/
///////////////////////////////////////////////////////////////////////////////
vgl.lookupTable = function () {
'use strict';
if (!(this instanceof vgl.lookupTable)) {
return new vgl.lookupTable();
}
vgl.texture.call(this);
var m_setupTimestamp = vgl.timestamp(),
m_range = [0, 0];
this.m_colorTable = //paraview bwr colortable
[0.07514311, 0.468049805, 1, 1,
0.247872569, 0.498782363, 1, 1,
0.339526309, 0.528909511, 1, 1,
0.409505078, 0.558608486, 1, 1,
0.468487184, 0.588057293, 1, 1,
0.520796675, 0.617435078, 1, 1,
0.568724526, 0.646924167, 1, 1,
0.613686735, 0.676713218, 1, 1,
0.656658579, 0.707001303, 1, 1,
0.698372844, 0.738002964, 1, 1,
0.739424025, 0.769954435, 1, 1,
0.780330104, 0.803121429, 1, 1,
0.821573924, 0.837809045, 1, 1,
0.863634967, 0.874374691, 1, 1,
0.907017747, 0.913245283, 1, 1,
0.936129275, 0.938743558, 0.983038586, 1,
0.943467973, 0.943498599, 0.943398095, 1,
0.990146732, 0.928791426, 0.917447482, 1,
1, 0.88332677, 0.861943246, 1,
1, 0.833985467, 0.803839606, 1,
1, 0.788626485, 0.750707739, 1,
1, 0.746206642, 0.701389973, 1,
1, 0.70590052, 0.654994046, 1,
1, 0.667019783, 0.610806959, 1,
1, 0.6289553, 0.568237474, 1,
1, 0.591130233, 0.526775617, 1,
1, 0.552955184, 0.485962266, 1,
1, 0.513776083, 0.445364274, 1,
1, 0.472800903, 0.404551679, 1,
1, 0.428977855, 0.363073592, 1,
1, 0.380759558, 0.320428137, 1,
0.961891484, 0.313155629, 0.265499262, 1,
0.916482116, 0.236630659, 0.209939162, 1].map(
function (x) {return x * 255;});
/////////////////////////////////////////////////////////////////////////////
/**
* Create lookup table, initialize parameters, and bind data to it
*
* @param {vgl.renderState} renderState
*/
/////////////////////////////////////////////////////////////////////////////
this.setup = function (renderState) {
if (this.textureUnit() === 0) {
renderState.m_context.activeTexture(vgl.GL.TEXTURE0);
} else if (this.textureUnit() === 1) {
renderState.m_context.activeTexture(vgl.GL.TEXTURE1);
}
renderState.m_context.deleteTexture(this.m_textureHandle);
this.m_textureHandle = renderState.m_context.createTexture();
renderState.m_context.bindTexture(vgl.GL.TEXTURE_2D, this.m_textureHandle);
renderState.m_context.texParameteri(vgl.GL.TEXTURE_2D,
vgl.GL.TEXTURE_MIN_FILTER, vgl.GL.LINEAR);
renderState.m_context.texParameteri(vgl.GL.TEXTURE_2D,
vgl.GL.TEXTURE_MAG_FILTER, vgl.GL.LINEAR);
renderState.m_context.texParameteri(vgl.GL.TEXTURE_2D,
vgl.GL.TEXTURE_WRAP_S, vgl.GL.CLAMP_TO_EDGE);
renderState.m_context.texParameteri(vgl.GL.TEXTURE_2D,
vgl.GL.TEXTURE_WRAP_T, vgl.GL.CLAMP_TO_EDGE);
renderState.m_context.pixelStorei(vgl.GL.UNPACK_ALIGNMENT, 1);
this.m_width = this.m_colorTable.length / 4;
this.m_height = 1;
this.m_depth = 0;
renderState.m_context.texImage2D(vgl.GL.TEXTURE_2D,
0, vgl.GL.RGBA, this.m_width, this.m_height, this.m_depth,
vgl.GL.RGBA, vgl.GL.UNSIGNED_BYTE, new Uint8Array(this.m_colorTable));
renderState.m_context.bindTexture(vgl.GL.TEXTURE_2D, null);
m_setupTimestamp.modified();
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get color table used by the lookup table
*
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.colorTable = function () {
return this.m_colorTable;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set color table used by the lookup table
*
* @param colors
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.setColorTable = function (colors) {
if (this.m_colorTable === colors) {
return false;
}
this.m_colorTable = colors;
this.modified();
return true;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get scalar range
*
* @returns {Array}
*/
/////////////////////////////////////////////////////////////////////////////
this.range = function () {
return m_range;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set scalar range for the lookup table
*
* @param range
* @returns {boolean}
*/
/////////////////////////////////////////////////////////////////////////////
this.setRange = function (range) {
if (m_range === range) {
return false;
}
m_range = range;
this.modified();
return true;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Given a [min,max] range update the lookup table range
*
* @param range
*/
/////////////////////////////////////////////////////////////////////////////
this.updateRange = function (range) {
if (!(range instanceof Array)) {
console.log('[error] Invalid data type for range. Requires array [min,max]');
}
if (range[0] < m_range[0]) {
m_range[0] = range[0];
this.modified();
}
if (range[1] > m_range[1]) {
m_range[1] = range[1];
this.modified();
}
};
return this;
};
inherit(vgl.lookupTable, vgl.texture);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, mat4, inherit*/
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class uniform
*
* @param type
* @param name
* @returns {vgl.uniform} OpenGL uniform encapsulation
*/
///////////////////////////////////////////////////////////////////////////////
vgl.uniform = function (type, name) {
'use strict';
if (!(this instanceof vgl.uniform)) {
return new vgl.uniform();
}
this.getTypeNumberOfComponents = function (type) {
switch (type) {
case vgl.GL.FLOAT:
case vgl.GL.INT:
case vgl.GL.BOOL:
return 1;
case vgl.GL.FLOAT_VEC2:
case vgl.GL.INT_VEC2:
case vgl.GL.BOOL_VEC2:
return 2;
case vgl.GL.FLOAT_VEC3:
case vgl.GL.INT_VEC3:
case vgl.GL.BOOL_VEC3:
return 3;
case vgl.GL.FLOAT_VEC4:
case vgl.GL.INT_VEC4:
case vgl.GL.BOOL_VEC4:
return 4;
case vgl.GL.FLOAT_MAT3:
return 9;
case vgl.GL.FLOAT_MAT4:
return 16;
default:
return 0;
}
};
var m_type = type,
m_name = name,
m_dataArray = [];
m_dataArray.length = this.getTypeNumberOfComponents(m_type);
/////////////////////////////////////////////////////////////////////////////
/**
* Get name of the uniform
*
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.name = function () {
return m_name;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get type of the uniform
*
* @returns {*}
*/
/////////////////////////////////////////////////////////////////////////////
this.type = function () {
return m_type;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Get value of the uniform
*
* @returns {Array}
*/
/////////////////////////////////////////////////////////////////////////////
this.get = function () {
return m_dataArray;
};
/////////////////////////////////////////////////////////////////////////////
/**
* Set value of the uniform
*
* @param value
*/
/////////////////////////////////////////////////////////////////////////////
this.set = function (value) {
var i = 0;
if (m_dataArray.length === 16) {
for (i = 0; i < 16; i += 1) {
m_dataArray[i] = value[i];
}
} else if (m_dataArray.length === 9) {
for (i = 0; i < 9; i += 1) {
m_dataArray[i] = value[i];
}
} else if (m_dataArray.length === 4) {
for (i = 0; i < 4; i += 1) {
m_dataArray[i] = value[i];
}
} else if (m_dataArray.length === 3) {
for (i = 0; i < 3; i += 1) {
m_dataArray[i] = value[i];
}
} else if (m_dataArray.length === 2) {
for (i = 0; i < 2; i += 1) {
m_dataArray[i] = value[i];
}
} else {
m_dataArray[0] = value;
}
};
/////////////////////////////////////////////////////////////////////////////
/**
* Call GL and pass updated values to the current shader
*
* @param location
*/
/////////////////////////////////////////////////////////////////////////////
this.callGL = function (renderState, location) {
if (this.m_numberElements < 1) {
return;
}
switch (m_type) {
case vgl.GL.BOOL:
case vgl.GL.INT:
renderState.m_context.uniform1iv(location, m_dataArray);
break;
case vgl.GL.FLOAT:
renderState.m_context.uniform1fv(location, m_dataArray);
break;
case vgl.GL.FLOAT_VEC2:
renderState.m_context.uniform2fv(location, m_dataArray);
break;
case vgl.GL.FLOAT_VEC3:
renderState.m_context.uniform3fv(location, m_dataArray);
break;
case vgl.GL.FLOAT_VEC4:
renderState.m_context.uniform4fv(location, m_dataArray);
break;
case vgl.GL.FLOAT_MAT3:
renderState.m_context.uniformMatrix3fv(location, vgl.GL.FALSE, m_dataArray);
break;
case vgl.GL.FLOAT_MAT4:
renderState.m_context.uniformMatrix4fv(location, vgl.GL.FALSE, m_dataArray);
break;
default:
break;
}
};
/////////////////////////////////////////////////////////////////////////////
/**
* Virtual method to update the uniform
*
* Should be implemented by the derived class.
*
* @param renderState
* @param program
*/
/////////////////////////////////////////////////////////////////////////////
this.update = function (renderState, program) {
renderState = renderState; /* unused parameter */
program = program; /* unused parameter */
// Should be implemented by the derived class
};
return this;
};
///////////////////////////////////////////////////////////////////////////////
/**
* Create new instance of class modelViewUniform
*
* @param name
* @returns {vgl.modelViewUniform}
*/
///////////////////////////////////////////////////////////////////////////////
vgl.modelViewUniform = function (name) {
'use strict';
if (!(this instanceof vgl.modelViewUniform)) {
return new vgl.modelViewUniform(name);
}
if (name.length === 0) {
name = 'modelViewMatrix';
}
vgl.uniform.call(this, vgl.GL.FLOAT_MAT4, name);
this.set(mat4.create());
/////////////////////////////////////////////////////////////////////////////
/**
* Update the uniform given a render state and shader program
*
* @param {vgl.renderState} renderState
* @param {vgl.shaderProgram} program
*/
/////////////////////////////////////////////////////////////////////////////
this.update = function (renderState, program) {
program = program; /* unused parameter */
this.set(renderState.m_modelViewMatrix);
};
return this;
};
inherit(vgl.modelViewUniform, vgl.uniform);
///////////////////////////////////////////////////////////////////////////////
/**
* Create new instance of class modelViewOriginUniform.
*
* @param name
* @param {array} origin a triplet of floats.
* @returns {vgl.modelViewUniform}
*/
///////////////////////////////////////////////////////////////////////////////
vgl.modelViewOriginUniform = function (name, origin) {
'use strict';
if (!(this instanceof vgl.modelViewOriginUniform)) {
return new vgl.modelViewOriginUniform(name, origin);
}
if (name.length === 0) {
name = 'modelViewMatrix';
}
origin = origin || [0, 0, 0];
var m_origin = [origin[0], origin[1], origin[2] || 0];
vgl.uniform.call(this, vgl.GL.FLOAT_MAT4, name);
this.set(mat4.create());
/**
* Change the origin used by the uniform view matrix.
*
* @param {array} origin a triplet of floats.
*/
this.setOrigin = function (origin) {
origin = origin || [0, 0, 0];
m_origin = [origin[0], origin[1], origin[2] || 0];
};
/////////////////////////////////////////////////////////////////////////////
/**
* Update the uniform given a render state and shader program. This offsets
* the modelViewMatrix by the origin, and, if the model view should be
* aligned, aligns it appropriately. The alignment must be done after the
* origin offset to maintain precision.
*
* @param {vgl.renderState} renderState
* @param {vgl.shaderProgram} program
*/
/////////////////////////////////////////////////////////////////////////////
this.update = function (renderState, program) {
program = program; /* unused parameter */
var view = mat4.create();
mat4.translate(view, renderState.m_modelViewMatrix, m_origin);
if (renderState.m_modelViewAlignment) {
var align = renderState.m_modelViewAlignment;
/* view[12] and view[13] are the x and y offsets. align.round is the
* units-per-pixel, and align.dx and .dy are either 0 or half the size of
* a unit-per-pixel. The alignment guarantees that the texels are
* aligned with screen pixels. */
view[12] = Math.round(view[12] / align.roundx) * align.roundx + align.dx;
view[13] = Math.round(view[13] / align.roundy) * align.roundy + align.dy;
}
this.set(view);
};
return this;
};
inherit(vgl.modelViewOriginUniform, vgl.uniform);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class projectionUniform
*
* @param name
* @returns {vgl.projectionUniform}
*/
///////////////////////////////////////////////////////////////////////////////
vgl.projectionUniform = function (name) {
'use strict';
if (!(this instanceof vgl.projectionUniform)) {
return new vgl.projectionUniform(name);
}
if (name.length === 0) {
name = 'projectionMatrix';
}
vgl.uniform.call(this, vgl.GL.FLOAT_MAT4, name);
this.set(mat4.create());
/////////////////////////////////////////////////////////////////////////////
/**
* Update the uniform given a render state and shader program
*
* @param renderState
* @param program
*/
/////////////////////////////////////////////////////////////////////////////
this.update = function (renderState, program) {
program = program; /* unused parameter */
this.set(renderState.m_projectionMatrix);
};
return this;
};
inherit(vgl.projectionUniform, vgl.uniform);
///////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class floatUniform
*
* @param name
* @param value
* @returns {vgl.floatUniform}
*/
///////////////////////////////////////////////////////////////////////////////
vgl.floatUniform = function (name, value) {
'use strict';
if (!(this instanceof vgl.floatUniform)) {
return new vgl.floatUniform(name, value);
}
if (name.length === 0) {
name = 'floatUniform';
}
value = value === undefined ? 1.0 : value;
vgl.uniform.call(this, vgl.GL.FLOAT, name);
this.set(value);
};
inherit(vgl.floatUniform, vgl.uniform);
///////////////////////////////////////////////////////////////////////////////
/**
* Create new instance of class normalMatrixUniform
*
* @param name
* @returns {vgl.normalMatrixUniform}
*/
///////////////////////////////////////////////////////////////////////////////
vgl.normalMatrixUniform = function (name) {
'use strict';
if (!(this instanceof vgl.normalMatrixUniform)) {
return new vgl.normalMatrixUniform(name);
}
if (name.length === 0) {
name = 'normalMatrix';
}
vgl.uniform.call(this, vgl.GL.FLOAT_MAT4, name);
this.set(mat4.create());
/////////////////////////////////////////////////////////////////////////////
/**
* Update the uniform given a render state and shader program
*
* @param {vgl.renderState} renderState
* @param {vgl.shaderProgram} program
*/
/////////////////////////////////////////////////////////////////////////////
this.update = function (renderState, program) {
program = program; /* unused parameter */
this.set(renderState.m_normalMatrix);
};
return this;
};
inherit(vgl.normalMatrixUniform, vgl.uniform);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Keys to identify vertex attributes
*
* @type {{Position: number, Normal: number, TextureCoordinate: number,
* Color: number, Scalar: number, Scalar2: number, Scalar3: number,
* Scalar4: number, Scalar5: number, Scalar6: number, Scalar7: number,
* CountAttributeIndex: number}}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.vertexAttributeKeys = {
'Position' : 0,
'Normal' : 1,
'TextureCoordinate' : 2,
'Color' : 3,
'Scalar': 4,
'CountAttributeIndex' : 5
};
vgl.vertexAttributeKeysIndexed = {
'Zero' : 0,
'One' : 1,
'Two' : 2,
'Three' : 3,
'Four' : 4,
'Five' : 5,
'Six' : 6,
'Seven' : 7,
'Eight' : 8,
'Nine' : 9
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of vertexAttribute
*
* @param {string} name
* @returns {vgl.vertexAttribute}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.vertexAttribute = function (name) {
'use strict';
if (!(this instanceof vgl.vertexAttribute)) {
return new vgl.vertexAttribute(name);
}
var m_name = name;
////////////////////////////////////////////////////////////////////////////
/**
* Get name of the vertex attribute
*
* @returns {string}
*/
//////////////////////////////////////////////////////////////////////////////
this.name = function () {
return m_name;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Bind vertex data to the given render state
*
* @param {vgl.renderState} renderState
* @param {vgl.vertexAttributeKeys} key
*/
//////////////////////////////////////////////////////////////////////////////
this.bindVertexData = function (renderState, key) {
var geometryData = renderState.m_mapper.geometryData(),
sourceData = geometryData.sourceData(key),
program = renderState.m_material.shaderProgram();
renderState.m_context.vertexAttribPointer(program.attributeLocation(
m_name), sourceData
.attributeNumberOfComponents(key), sourceData.attributeDataType(key),
sourceData.normalized(key), sourceData
.attributeStride(key), sourceData
.attributeOffset(key));
renderState.m_context.enableVertexAttribArray(program.attributeLocation(m_name));
};
//////////////////////////////////////////////////////////////////////////////
/**
* Undo bind vertex data for a given render state
*
* @param {vgl.renderState} renderState
* @param {vgl.vertexAttributeKeys} key
*/
//////////////////////////////////////////////////////////////////////////////
this.undoBindVertexData = function (renderState, key) {
key = key; /* unused parameter */
var program = renderState.m_material.shaderProgram();
renderState.m_context.disableVertexAttribArray(program.attributeLocation(m_name));
};
};
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class source
*
* @returns {vgl.source}
*/
///////////////////////////////////////////////////////////////////////////////
vgl.source = function () {
'use strict';
if (!(this instanceof vgl.source)) {
return new vgl.source();
}
vgl.object.call(this);
/////////////////////////////////////////////////////////////////////////////
/**
* Virtual function to create a source instance
*/
/////////////////////////////////////////////////////////////////////////////
this.create = function () {
};
return this;
};
inherit(vgl.source, vgl.object);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class planeSource
*
* @class
* @returns {vgl.planeSource}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.planeSource = function () {
'use strict';
if (!(this instanceof vgl.planeSource)) {
return new vgl.planeSource();
}
vgl.source.call(this);
var m_origin = [0.0, 0.0, 0.0],
m_point1 = [1.0, 0.0, 0.0],
m_point2 = [0.0, 1.0, 0.0],
m_normal = [0.0, 0.0, 1.0],
m_xresolution = 1,
m_yresolution = 1,
m_geom = null;
////////////////////////////////////////////////////////////////////////////
/**
* Set origin of the plane
*
* @param x
* @param y
* @param z
*/
////////////////////////////////////////////////////////////////////////////
this.setOrigin = function (x, y, z) {
m_origin[0] = x;
m_origin[1] = y;
m_origin[2] = z;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set point that defines the first axis of the plane
*
* @param x
* @param y
* @param z
*/
////////////////////////////////////////////////////////////////////////////
this.setPoint1 = function (x, y, z) {
m_point1[0] = x;
m_point1[1] = y;
m_point1[2] = z;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set point that defines the first axis of the plane
*
* @param x
* @param y
* @param z
*/
////////////////////////////////////////////////////////////////////////////
this.setPoint2 = function (x, y, z) {
m_point2[0] = x;
m_point2[1] = y;
m_point2[2] = z;
};
////////////////////////////////////////////////////////////////////////////
/**
* Create a plane geometry given input parameters
*
* @returns {null}
*/
////////////////////////////////////////////////////////////////////////////
this.create = function () {
m_geom = new vgl.geometryData();
var x = [], tc = [], v1 = [], v2 = [],
pts = [], i, j, k, ii, numPts, numPolys,
posIndex = 0, normIndex = 0, colorIndex = 0, texCoordIndex = 0,
positions = [], normals = [], colors = [],
texCoords = [], indices = [], tristrip = null,
sourcePositions = null, sourceColors = null, sourceTexCoords;
x.length = 3;
tc.length = 2;
v1.length = 3;
v2.length = 3;
pts.length = 3;
// Check input
for (i = 0; i < 3; i += 1) {
v1[i] = m_point1[i] - m_origin[i];
v2[i] = m_point2[i] - m_origin[i];
}
// TODO Compute center and normal
// Set things up; allocate memory
numPts = (m_xresolution + 1) * (m_yresolution + 1);
numPolys = m_xresolution * m_yresolution * 2;
positions.length = 3 * numPts;
normals.length = 3 * numPts;
texCoords.length = 2 * numPts;
indices.length = numPts;
for (k = 0, i = 0; i < (m_yresolution + 1); i += 1) {
tc[1] = i / m_yresolution;
for (j = 0; j < (m_xresolution + 1); j += 1) {
tc[0] = j / m_xresolution;
for (ii = 0; ii < 3; ii += 1) {
x[ii] = m_origin[ii] + tc[0] * v1[ii] + tc[1] * v2[ii];
}
//jshint plusplus: false
positions[posIndex++] = x[0];
positions[posIndex++] = x[1];
positions[posIndex++] = x[2];
colors[colorIndex++] = 1.0;
colors[colorIndex++] = 1.0;
colors[colorIndex++] = 1.0;
normals[normIndex++] = m_normal[0];
normals[normIndex++] = m_normal[1];
normals[normIndex++] = m_normal[2];
texCoords[texCoordIndex++] = tc[0];
texCoords[texCoordIndex++] = tc[1];
//jshint plusplus: true
}
}
/// Generate polygon connectivity
for (i = 0; i < m_yresolution; i += 1) {
for (j = 0; j < m_xresolution; j += 1) {
pts[0] = j + i * (m_xresolution + 1);
pts[1] = pts[0] + 1;
pts[2] = pts[0] + m_xresolution + 2;
pts[3] = pts[0] + m_xresolution + 1;
}
}
for (i = 0; i < numPts; i += 1) {
indices[i] = i;
}
tristrip = new vgl.triangleStrip();
tristrip.setIndices(indices);
sourcePositions = vgl.sourceDataP3fv();
sourcePositions.pushBack(positions);
sourceColors = vgl.sourceDataC3fv();
sourceColors.pushBack(colors);
sourceTexCoords = vgl.sourceDataT2fv();
sourceTexCoords.pushBack(texCoords);
m_geom.addSource(sourcePositions);
m_geom.addSource(sourceColors);
m_geom.addSource(sourceTexCoords);
m_geom.addPrimitive(tristrip);
return m_geom;
};
};
inherit(vgl.planeSource, vgl.source);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class pointSource
*
* @class
* @returns {vgl.pointSource}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.pointSource = function () {
'use strict';
if (!(this instanceof vgl.pointSource)) {
return new vgl.pointSource();
}
vgl.source.call(this);
var m_this = this,
m_positions = [],
m_colors = [],
m_textureCoords = [],
m_size = [],
m_geom = null;
////////////////////////////////////////////////////////////////////////////
/**
* Get positions for the points
*/
////////////////////////////////////////////////////////////////////////////
this.getPositions = function () {
return m_positions;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set positions for the source
*
* @param positions
*/
////////////////////////////////////////////////////////////////////////////
this.setPositions = function (positions) {
if (positions instanceof Array) {
m_positions = positions;
} else {
console
.log('[ERROR] Invalid data type for positions. Array is required.');
}
m_this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Get colors for the points
*/
////////////////////////////////////////////////////////////////////////////
this.getColors = function () {
return m_colors;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set colors for the points
*
* @param colors
*/
////////////////////////////////////////////////////////////////////////////
this.setColors = function (colors) {
if (colors instanceof Array) {
m_colors = colors;
} else {
console.log('[ERROR] Invalid data type for colors. Array is required.');
}
m_this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Get size for the points
*/
////////////////////////////////////////////////////////////////////////////
this.getSize = function () {
return m_size;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set colors for the points
*
* @param colors
*/
////////////////////////////////////////////////////////////////////////////
this.setSize = function (size) {
m_size = size;
this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Set texture coordinates for the points
*
* @param texcoords
*/
////////////////////////////////////////////////////////////////////////////
this.setTextureCoordinates = function (texcoords) {
if (texcoords instanceof Array) {
m_textureCoords = texcoords;
} else {
console.log('[ERROR] Invalid data type for ' +
'texture coordinates. Array is required.');
}
m_this.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Create a point geometry given input parameters
*/
////////////////////////////////////////////////////////////////////////////
this.create = function () {
m_geom = new vgl.geometryData();
if (m_positions.length % 3 !== 0) {
console.log('[ERROR] Invalid length of the points array');
return;
}
var numPts = m_positions.length / 3,
i = 0,
indices = [],
pointsPrimitive,
sourcePositions,
sourceColors,
sourceTexCoords,
sourceSize;
indices.length = numPts;
for (i = 0; i < numPts; i += 1) {
indices[i] = i;
}
/// Generate array of size if needed
sourceSize = vgl.sourceDataDf();
if (numPts !== m_size.length) {
for (i = 0; i < numPts; i += 1) {
sourceSize.pushBack(m_size);
}
} else {
sourceSize.setData(m_size);
}
m_geom.addSource(sourceSize);
pointsPrimitive = new vgl.points();
pointsPrimitive.setIndices(indices);
sourcePositions = vgl.sourceDataP3fv();
sourcePositions.pushBack(m_positions);
m_geom.addSource(sourcePositions);
if ((m_colors.length > 0) && m_colors.length === m_positions.length) {
sourceColors = vgl.sourceDataC3fv();
sourceColors.pushBack(m_colors);
m_geom.addSource(sourceColors);
} else if ((m_colors.length > 0) && m_colors.length !== m_positions.length) {
console
.log('[ERROR] Number of colors are different than number of points');
}
if (m_textureCoords.length > 0 &&
m_textureCoords.length === m_positions.length) {
sourceTexCoords = vgl.sourceDataT2fv();
sourceTexCoords.pushBack(m_textureCoords);
m_geom.addSource(sourceTexCoords);
} else if (m_textureCoords.length > 0 &&
(m_textureCoords.length / 2) !== (m_positions.length / 3)) {
console
.log('[ERROR] Number of texture coordinates are different than ' +
'number of points');
}
m_geom.addPrimitive(pointsPrimitive);
return m_geom;
};
};
inherit(vgl.pointSource, vgl.source);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class lineSource
*
* @class
* @returns {vgl.lineSource}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.lineSource = function (positions, colors) {
'use strict';
if (!(this instanceof vgl.lineSource)) {
return new vgl.lineSource();
}
vgl.source.call(this);
var m_positions = positions,
m_colors = colors;
////////////////////////////////////////////////////////////////////////////
/**
* Set start positions for the lines
*
* @param positions
*/
////////////////////////////////////////////////////////////////////////////
this.setPositions = function (positions) {
if (positions instanceof Array) {
m_positions = positions;
this.modified();
return true;
}
console
.log('[ERROR] Invalid data type for positions. Array is required.');
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set colors for the lines
*
* @param colors
*/
////////////////////////////////////////////////////////////////////////////
this.setColors = function (colors) {
if (colors instanceof Array) {
m_colors = colors;
this.modified();
return true;
}
console.log('[ERROR] Invalid data type for colors. Array is required.');
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Create a point geometry given input parameters
*/
////////////////////////////////////////////////////////////////////////////
this.create = function () {
if (!m_positions) {
console.log('[error] Invalid positions');
return;
}
if (m_positions.length % 3 !== 0) {
console.log('[error] Line source requires 3d points');
return;
}
if (m_positions.length % 3 !== 0) {
console.log('[ERROR] Invalid length of the points array');
return;
}
var m_geom = new vgl.geometryData(),
numPts = m_positions.length / 3,
i,
indices = [],
linesPrimitive,
sourcePositions,
sourceColors;
indices.length = numPts;
for (i = 0; i < numPts; i += 1) {
indices[i] = i;
}
linesPrimitive = new vgl.lines();
linesPrimitive.setIndices(indices);
sourcePositions = vgl.sourceDataP3fv();
sourcePositions.pushBack(m_positions);
m_geom.addSource(sourcePositions);
if (m_colors && (m_colors.length > 0) &&
m_colors.length === m_positions.length) {
sourceColors = vgl.sourceDataC3fv();
sourceColors.pushBack(m_colors);
m_geom.addSource(sourceColors);
} else if (m_colors && (m_colors.length > 0) &&
m_colors.length !== m_positions.length) {
console
.log('[error] Number of colors are different than number of points');
}
m_geom.addPrimitive(linesPrimitive);
return m_geom;
};
};
inherit(vgl.lineSource, vgl.source);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global document, vgl, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class utils
*
* Utility class provides helper functions such as functions to create
* shaders, geometry etc.
*
* @returns {vgl.utils}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils = function () {
'use strict';
if (!(this instanceof vgl.utils)) {
return new vgl.utils();
}
vgl.object.call(this);
return this;
};
inherit(vgl.utils, vgl.object);
//////////////////////////////////////////////////////////////////////////////
/**
* Helper function to compute power of 2 number
*
* @param value
* @param pow
*
* @returns {number}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.computePowerOfTwo = function (value, pow) {
'use strict';
pow = pow || 1;
while (pow < value) {
pow *= 2;
}
return pow;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of default vertex shader that uses a texture
*
* Helper function to create default vertex shader
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createTextureVertexShader = function (context) {
'use strict';
var vertexShaderSource = [
'attribute vec3 vertexPosition;',
'attribute vec3 textureCoord;',
'uniform mediump float pointSize;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'varying highp vec3 iTextureCoord;',
'void main(void)',
'{',
'gl_PointSize = pointSize;',
'gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);',
' iTextureCoord = textureCoord;', '}'].join('\n');
return vgl.getCachedShader(vgl.GL.VERTEX_SHADER, context,
vertexShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of default fragment shader that uses a texture
*
* Helper function to create default fragment shader with sampler
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createTextureFragmentShader = function (context) {
'use strict';
var fragmentShaderSource = [
'varying highp vec3 iTextureCoord;',
'uniform sampler2D sampler2d;',
'uniform mediump float opacity;',
'void main(void) {',
'gl_FragColor = vec4(texture2D(sampler2d, vec2(iTextureCoord.s, ' +
'iTextureCoord.t)).xyz, opacity);',
'}'].join('\n');
return vgl.getCachedShader(vgl.GL.FRAGMENT_SHADER, context,
fragmentShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create variation of createTextureFragmentShader which uses texture alpha
*
* Helper function to create default fragment shader with sampler
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createRgbaTextureFragmentShader = function (context) {
'use strict';
var fragmentShaderSource = [
'varying highp vec3 iTextureCoord;',
'uniform sampler2D sampler2d;',
'uniform mediump float opacity;',
'void main(void) {',
' mediump vec4 color = vec4(texture2D(sampler2d, vec2(' +
'iTextureCoord.s, iTextureCoord.t)).xyzw);',
' color.w *= opacity;',
' gl_FragColor = color;',
'}'
].join('\n');
return vgl.getCachedShader(vgl.GL.FRAGMENT_SHADER, context,
fragmentShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of default vertex shader
*
* Helper function to create default vertex shader
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createVertexShader = function (context) {
'use strict';
var vertexShaderSource = [
'attribute vec3 vertexPosition;',
'attribute vec3 vertexColor;',
'uniform mediump float pointSize;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'varying mediump vec3 iVertexColor;',
'varying highp vec3 iTextureCoord;',
'void main(void)',
'{',
'gl_PointSize = pointSize;',
'gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);',
' iVertexColor = vertexColor;', '}'].join('\n');
return vgl.getCachedShader(vgl.GL.VERTEX_SHADER, context,
vertexShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of default vertex shader
*
* Helper function to create default vertex shader
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createPointVertexShader = function (context) {
'use strict';
var vertexShaderSource = [
'attribute vec3 vertexPosition;',
'attribute vec3 vertexColor;',
'attribute float vertexSize;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'varying mediump vec3 iVertexColor;',
'varying highp vec3 iTextureCoord;',
'void main(void)',
'{',
'gl_PointSize = vertexSize;',
'gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);',
' iVertexColor = vertexColor;', '}'].join('\n');
return vgl.getCachedShader(vgl.GL.VERTEX_SHADER, context,
vertexShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of vertex shader with a solid color
*
* Helper function to create default vertex shader
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createVertexShaderSolidColor = function (context) {
'use strict';
var vertexShaderSource = [
'attribute vec3 vertexPosition;',
'uniform mediump float pointSize;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'void main(void)',
'{',
'gl_PointSize = pointSize;',
'gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);',
'}'].join('\n');
return vgl.getCachedShader(vgl.GL.VERTEX_SHADER, context,
vertexShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of vertex shader that passes values through
* for color mapping
*
* Helper function to create default vertex shader
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createVertexShaderColorMap = function (context, min, max) {
'use strict';
min = min; /* unused parameter */
max = max; /* unused parameter */
var vertexShaderSource = [
'attribute vec3 vertexPosition;',
'attribute float vertexScalar;',
'uniform mediump float pointSize;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'uniform float lutMin;',
'uniform float lutMax;',
'varying mediump float iVertexScalar;',
'void main(void)',
'{',
'gl_PointSize = pointSize;',
'gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);',
'iVertexScalar = (vertexScalar-lutMin)/(lutMax-lutMin);',
'}'].join('\n');
return vgl.getCachedShader(vgl.GL.VERTEX_SHADER, context,
vertexShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of default fragment shader
*
* Helper function to create default fragment shader
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createFragmentShader = function (context) {
'use strict';
var fragmentShaderSource = ['varying mediump vec3 iVertexColor;',
'uniform mediump float opacity;',
'void main(void) {',
'gl_FragColor = vec4(iVertexColor, opacity);',
'}'].join('\n');
return vgl.getCachedShader(vgl.GL.FRAGMENT_SHADER, context,
fragmentShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a Phong vertex shader
*
* Helper function to create Phong vertex shader
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createPhongVertexShader = function (context) {
'use strict';
var vertexShaderSource = [
'attribute highp vec3 vertexPosition;',
'attribute mediump vec3 vertexNormal;',
'attribute mediump vec3 vertexColor;',
'uniform highp mat4 projectionMatrix;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 normalMatrix;',
'varying highp vec4 varPosition;',
'varying mediump vec3 varNormal;',
'varying mediump vec3 varVertexColor;',
'void main(void)',
'{',
'varPosition = modelViewMatrix * vec4(vertexPosition, 1.0);',
'gl_Position = projectionMatrix * varPosition;',
'varNormal = vec3(normalMatrix * vec4(vertexNormal, 0.0));',
'varVertexColor = vertexColor;',
'}'].join('\n');
return vgl.getCachedShader(vgl.GL.VERTEX_SHADER, context,
vertexShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of Phong fragment shader
*
* Helper function to create Phong fragment shader
*
* NOTE: Shader assumes directional light
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createPhongFragmentShader = function (context) {
'use strict';
var fragmentShaderSource = [
'uniform mediump float opacity;',
'precision mediump float;',
'varying vec3 varNormal;',
'varying vec4 varPosition;',
'varying mediump vec3 varVertexColor;',
'const vec3 lightPos = vec3(0.0, 0.0,10000.0);',
'const vec3 ambientColor = vec3(0.01, 0.01, 0.01);',
'const vec3 specColor = vec3(0.0, 0.0, 0.0);',
'void main() {',
'vec3 normal = normalize(varNormal);',
'vec3 lightDir = normalize(lightPos);',
'vec3 reflectDir = -reflect(lightDir, normal);',
'vec3 viewDir = normalize(-varPosition.xyz);',
'float lambertian = max(dot(lightDir, normal), 0.0);',
'vec3 color = vec3(0.0);',
'if(lambertian > 0.0) {',
' color = lambertian * varVertexColor;',
'}',
'gl_FragColor = vec4(color * opacity, 1.0 - opacity);',
'}'].join('\n');
return vgl.getCachedShader(vgl.GL.FRAGMENT_SHADER, context,
fragmentShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of fragment shader with an assigned constant color.
*
* Helper function to create default fragment shader
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createFragmentShaderSolidColor = function (context, color) {
'use strict';
var fragmentShaderSource = [
'uniform mediump float opacity;',
'void main(void) {',
'gl_FragColor = vec4(' + color[0] + ',' + color[1] + ',' + color[2] + ', opacity);',
'}'].join('\n');
return vgl.getCachedShader(vgl.GL.FRAGMENT_SHADER, context,
fragmentShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of fragment shader that maps values into colors bia lookup table
*
* Helper function to create default fragment shader
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createFragmentShaderColorMap = function (context) {
'use strict';
var fragmentShaderSource = [
'varying mediump float iVertexScalar;',
'uniform sampler2D sampler2d;',
'uniform mediump float opacity;',
'void main(void) {',
'gl_FragColor = vec4(texture2D(sampler2d, vec2(iVertexScalar, ' +
'0.0)).xyz, opacity);',
'}'].join('\n');
return vgl.getCachedShader(vgl.GL.FRAGMENT_SHADER, context,
fragmentShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of vertex shader for point sprites
*
* Helper function to create default point sprites vertex shader
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createPointSpritesVertexShader = function (context) {
'use strict';
var vertexShaderSource = [
'attribute vec3 vertexPosition;',
'attribute vec3 vertexColor;',
'uniform mediump vec2 pointSize;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'uniform float height;',
'varying mediump vec3 iVertexColor;',
'varying highp float iVertexScalar;',
'void main(void)',
'{',
'mediump float realPointSize = pointSize.y;',
'if (pointSize.x > pointSize.y) {',
' realPointSize = pointSize.x;}',
'gl_PointSize = realPointSize ;',
'iVertexScalar = vertexPosition.z;',
'gl_Position = projectionMatrix * modelViewMatrix * ' +
'vec4(vertexPosition.xy, height, 1.0);',
' iVertexColor = vertexColor;', '}'].join('\n');
return vgl.getCachedShader(vgl.GL.VERTEX_SHADER, context,
vertexShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of fragment shader for point sprites
*
* Helper function to create default point sprites fragment shader
*
* @param context
* @returns {vgl.shader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createPointSpritesFragmentShader = function (context) {
'use strict';
var fragmentShaderSource = [
'varying mediump vec3 iVertexColor;',
'varying highp float iVertexScalar;',
'uniform sampler2D opacityLookup;',
'uniform highp float lutMin;',
'uniform highp float lutMax;',
'uniform sampler2D scalarsToColors;',
'uniform int useScalarsToColors;',
'uniform int useVertexColors;',
'uniform mediump vec2 pointSize;',
'uniform mediump float vertexColorWeight;',
'void main(void) {',
'mediump vec2 realTexCoord;',
'if (pointSize.x > pointSize.y) {',
' realTexCoord = vec2(1.0, pointSize.y/pointSize.x) * gl_PointCoord;',
'} else {',
' realTexCoord = vec2(pointSize.x/pointSize.y, 1.0) * gl_PointCoord;',
'}',
'highp float texOpacity = texture2D(opacityLookup, realTexCoord).w;',
'if (useScalarsToColors == 1) {',
' gl_FragColor = vec4(texture2D(scalarsToColors, vec2((' +
'iVertexScalar - lutMin)/(lutMax - lutMin), 0.0)).xyz, ' +
'texOpacity);',
'} else if (useVertexColors == 1) {',
' gl_FragColor = vec4(iVertexColor, texOpacity);',
'} else {',
' gl_FragColor = vec4(texture2D(opacityLookup, realTexCoord).xyz, texOpacity);',
'}}'
].join('\n');
return vgl.getCachedShader(vgl.GL.FRAGMENT_SHADER, context,
fragmentShaderSource);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of texture material
*
* Helper function to create a texture material
*
* @returns {vgl.material}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createTextureMaterial = function (isRgba, origin) {
'use strict';
var mat = new vgl.material(),
blend = new vgl.blend(),
prog = new vgl.shaderProgram(),
vertexShader = vgl.utils.createTextureVertexShader(vgl.GL),
fragmentShader = null,
posVertAttr = new vgl.vertexAttribute('vertexPosition'),
texCoordVertAttr = new vgl.vertexAttribute('textureCoord'),
pointsizeUniform = new vgl.floatUniform('pointSize', 5.0),
modelViewUniform,
projectionUniform = new vgl.projectionUniform('projectionMatrix'),
samplerUniform = new vgl.uniform(vgl.GL.INT, 'sampler2d'),
opacityUniform = null;
if (origin !== undefined) {
modelViewUniform = new vgl.modelViewOriginUniform('modelViewMatrix',
origin);
} else {
modelViewUniform = new vgl.modelViewUniform('modelViewMatrix');
}
samplerUniform.set(0);
prog.addVertexAttribute(posVertAttr, vgl.vertexAttributeKeys.Position);
prog.addVertexAttribute(texCoordVertAttr,
vgl.vertexAttributeKeys.TextureCoordinate);
prog.addUniform(pointsizeUniform);
prog.addUniform(modelViewUniform);
prog.addUniform(projectionUniform);
if (isRgba) {
fragmentShader = vgl.utils.createRgbaTextureFragmentShader(vgl.GL);
} else {
fragmentShader = vgl.utils.createTextureFragmentShader(vgl.GL);
}
opacityUniform = new vgl.floatUniform('opacity', 1.0);
prog.addUniform(opacityUniform);
prog.addShader(fragmentShader);
prog.addShader(vertexShader);
mat.addAttribute(prog);
mat.addAttribute(blend);
return mat;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of geometry material
*
* Helper function to create geometry material
*
* @returns {vgl.material}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createGeometryMaterial = function () {
'use strict';
var mat = new vgl.material(),
prog = new vgl.shaderProgram(),
pointSize = 5.0,
opacity = 1.0,
vertexShader = vgl.utils.createVertexShader(vgl.GL),
fragmentShader = vgl.utils.createFragmentShader(vgl.GL),
posVertAttr = new vgl.vertexAttribute('vertexPosition'),
colorVertAttr = new vgl.vertexAttribute('vertexColor'),
pointsizeUniform = new vgl.floatUniform('pointSize', pointSize),
opacityUniform = new vgl.floatUniform('opacity', opacity),
modelViewUniform = new vgl.modelViewUniform('modelViewMatrix'),
projectionUniform = new vgl.projectionUniform('projectionMatrix');
prog.addVertexAttribute(posVertAttr, vgl.vertexAttributeKeys.Position);
prog.addVertexAttribute(colorVertAttr, vgl.vertexAttributeKeys.Color);
prog.addUniform(pointsizeUniform);
prog.addUniform(opacityUniform);
prog.addUniform(modelViewUniform);
prog.addUniform(projectionUniform);
prog.addShader(fragmentShader);
prog.addShader(vertexShader);
mat.addAttribute(prog);
return mat;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of geometry material
*
* Helper function to create geometry material
*
* @returns {vgl.material}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createPointGeometryMaterial = function (opacity) {
'use strict';
opacity = opacity === undefined ? 1.0 : opacity;
var mat = new vgl.material(),
blend = new vgl.blend(),
prog = new vgl.shaderProgram(),
vertexShader = vgl.utils.createPointVertexShader(vgl.GL),
fragmentShader = vgl.utils.createFragmentShader(vgl.GL),
posVertAttr = new vgl.vertexAttribute('vertexPosition'),
colorVertAttr = new vgl.vertexAttribute('vertexColor'),
sizeVertAttr = new vgl.vertexAttribute('vertexSize'),
opacityUniform = new vgl.floatUniform('opacity', opacity),
modelViewUniform = new vgl.modelViewUniform('modelViewMatrix'),
projectionUniform = new vgl.projectionUniform('projectionMatrix');
prog.addVertexAttribute(posVertAttr, vgl.vertexAttributeKeys.Position);
prog.addVertexAttribute(colorVertAttr, vgl.vertexAttributeKeys.Color);
prog.addVertexAttribute(sizeVertAttr, vgl.vertexAttributeKeys.Scalar);
prog.addUniform(opacityUniform);
prog.addUniform(modelViewUniform);
prog.addUniform(projectionUniform);
prog.addShader(fragmentShader);
prog.addShader(vertexShader);
mat.addAttribute(prog);
mat.addAttribute(blend);
return mat;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of geometry material with the phong shader
*
* Helper function to create color phong shaded geometry material
*
* @returns {vgl.material}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createPhongMaterial = function () {
'use strict';
var mat = new vgl.material(),
prog = new vgl.shaderProgram(),
vertexShader = vgl.utils.createPhongVertexShader(vgl.GL),
fragmentShader = vgl.utils.createPhongFragmentShader(vgl.GL),
posVertAttr = new vgl.vertexAttribute('vertexPosition'),
normalVertAttr = new vgl.vertexAttribute('vertexNormal'),
colorVertAttr = new vgl.vertexAttribute('vertexColor'),
opacityUniform = new vgl.floatUniform('opacity', 1.0),
modelViewUniform = new vgl.modelViewUniform('modelViewMatrix'),
normalUniform = new vgl.normalMatrixUniform('normalMatrix'),
projectionUniform = new vgl.projectionUniform('projectionMatrix');
prog.addVertexAttribute(posVertAttr, vgl.vertexAttributeKeys.Position);
prog.addVertexAttribute(normalVertAttr, vgl.vertexAttributeKeys.Normal);
prog.addVertexAttribute(colorVertAttr, vgl.vertexAttributeKeys.Color);
prog.addUniform(opacityUniform);
prog.addUniform(modelViewUniform);
prog.addUniform(projectionUniform);
prog.addUniform(normalUniform);
prog.addShader(fragmentShader);
prog.addShader(vertexShader);
//mat.addAttribute(blend);
mat.addAttribute(prog);
return mat;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of colored geometry material
*
* Helper function to create color geometry material
*
* @returns {vgl.material}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createColorMaterial = function () {
'use strict';
var mat = new vgl.material(),
blend = new vgl.blend(),
prog = new vgl.shaderProgram(),
vertexShader = vgl.utils.createVertexShader(vgl.GL),
fragmentShader = vgl.utils.createFragmentShader(vgl.GL),
posVertAttr = new vgl.vertexAttribute('vertexPosition'),
texCoordVertAttr = new vgl.vertexAttribute('textureCoord'),
colorVertAttr = new vgl.vertexAttribute('vertexColor'),
pointsizeUniform = new vgl.floatUniform('pointSize', 5.0),
opacityUniform = new vgl.floatUniform('opacity', 1.0),
modelViewUniform = new vgl.modelViewUniform('modelViewMatrix'),
projectionUniform = new vgl.projectionUniform('projectionMatrix');
prog.addVertexAttribute(posVertAttr, vgl.vertexAttributeKeys.Position);
prog.addVertexAttribute(colorVertAttr, vgl.vertexAttributeKeys.Color);
prog.addVertexAttribute(texCoordVertAttr,
vgl.vertexAttributeKeys.TextureCoordinate);
prog.addUniform(pointsizeUniform);
prog.addUniform(opacityUniform);
prog.addUniform(modelViewUniform);
prog.addUniform(projectionUniform);
prog.addShader(fragmentShader);
prog.addShader(vertexShader);
mat.addAttribute(prog);
mat.addAttribute(blend);
return mat;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of geometry material
*
* Helper function to create geometry material
*
* @returns {vgl.material}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createColorMappedMaterial = function (lut) {
'use strict';
if (!lut) {
lut = new vgl.lookupTable();
}
var scalarRange = lut.range(),
mat = new vgl.material(),
blend = new vgl.blend(),
prog = new vgl.shaderProgram(),
vertexShader = vgl.utils.createVertexShaderColorMap(
vgl.GL, scalarRange[0], scalarRange[1]),
fragmentShader = vgl.utils.createFragmentShaderColorMap(vgl.GL),
posVertAttr = new vgl.vertexAttribute('vertexPosition'),
scalarVertAttr = new vgl.vertexAttribute('vertexScalar'),
pointsizeUniform = new vgl.floatUniform('pointSize', 5.0),
opacityUniform = new vgl.floatUniform('opacity', 1.0),
lutMinUniform = new vgl.floatUniform('lutMin', scalarRange[0]),
lutMaxUniform = new vgl.floatUniform('lutMax', scalarRange[1]),
modelViewUniform = new vgl.modelViewUniform('modelViewMatrix'),
projectionUniform = new vgl.projectionUniform('projectionMatrix'),
samplerUniform = new vgl.uniform(vgl.GL.FLOAT, 'sampler2d'),
lookupTable = lut;
samplerUniform.set(0);
prog.addVertexAttribute(posVertAttr, vgl.vertexAttributeKeys.Position);
prog.addVertexAttribute(scalarVertAttr, vgl.vertexAttributeKeys.Scalar);
prog.addUniform(pointsizeUniform);
prog.addUniform(opacityUniform);
prog.addUniform(lutMinUniform);
prog.addUniform(lutMaxUniform);
prog.addUniform(modelViewUniform);
prog.addUniform(projectionUniform);
prog.addShader(fragmentShader);
prog.addShader(vertexShader);
mat.addAttribute(prog);
mat.addAttribute(blend);
mat.addAttribute(lookupTable);
return mat;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Update color mapped material
*
* @param mat
* @param scalarRange
* @param lut
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.updateColorMappedMaterial = function (mat, lut) {
'use strict';
if (!mat) {
console.log('[warning] Invalid material. Nothing to update.');
return;
}
if (!lut) {
console.log('[warning] Invalid lookup table. Nothing to update.');
return;
}
var lutMin = mat.shaderProgram().uniform('lutMin'),
lutMax = mat.shaderProgram().uniform('lutMax');
lutMin.set(lut.range()[0]);
lutMax.set(lut.range()[1]);
// This will replace the existing lookup table
mat.setAttribute(lut);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of solid color material
*
* Helper function to create geometry material
*
* @returns {vgl.material}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createSolidColorMaterial = function (color) {
'use strict';
if (!color) {
color = [1.0, 1.0, 1.0];
}
var mat = new vgl.material(),
blend = new vgl.blend(),
prog = new vgl.shaderProgram(),
vertexShader = vgl.utils.createVertexShaderSolidColor(vgl.GL),
fragmentShader = vgl.utils.createFragmentShaderSolidColor(vgl.GL, color),
posVertAttr = new vgl.vertexAttribute('vertexPosition'),
pointsizeUniform = new vgl.floatUniform('pointSize', 5.0),
opacityUniform = new vgl.floatUniform('opacity', 1.0),
modelViewUniform = new vgl.modelViewUniform('modelViewMatrix'),
projectionUniform = new vgl.projectionUniform('projectionMatrix');
prog.addVertexAttribute(posVertAttr, vgl.vertexAttributeKeys.Position);
prog.addUniform(pointsizeUniform);
prog.addUniform(opacityUniform);
prog.addUniform(modelViewUniform);
prog.addUniform(projectionUniform);
prog.addShader(fragmentShader);
prog.addShader(vertexShader);
mat.addAttribute(prog);
mat.addAttribute(blend);
return mat;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of point sprites material
*
* Helper function to create point sprites material
*
* @returns {vgl.material}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createPointSpritesMaterial = function (image, lut) {
'use strict';
var scalarRange = lut === undefined ? [0, 1] : lut.range(),
mat = new vgl.material(),
blend = new vgl.blend(),
prog = new vgl.shaderProgram(),
vertexShader = vgl.utils.createPointSpritesVertexShader(vgl.GL),
fragmentShader = vgl.utils.createPointSpritesFragmentShader(vgl.GL),
posVertAttr = new vgl.vertexAttribute('vertexPosition'),
colorVertAttr = new vgl.vertexAttribute('vertexColor'),
heightUniform = new vgl.floatUniform('height', 0.0),
vertexColorWeightUniform =
new vgl.floatUniform('vertexColorWeight', 0.0),
lutMinUniform = new vgl.floatUniform('lutMin', scalarRange[0]),
lutMaxUniform = new vgl.floatUniform('lutMax', scalarRange[1]),
modelViewUniform = new vgl.modelViewUniform('modelViewMatrix'),
projectionUniform = new vgl.projectionUniform('projectionMatrix'),
samplerUniform = new vgl.uniform(vgl.GL.INT, 'opacityLookup'),
scalarsToColors = new vgl.uniform(vgl.GL.INT, 'scalarsToColors'),
useScalarsToColors = new vgl.uniform(vgl.GL.INT, 'useScalarsToColors'),
useVertexColors = new vgl.uniform(vgl.GL.INT, 'useVertexColors'),
pointSize = new vgl.uniform(vgl.GL.FLOAT_VEC2, 'pointSize'),
texture = new vgl.texture();
samplerUniform.set(0);
scalarsToColors.set(1);
useScalarsToColors.set(0);
useVertexColors.set(0);
pointSize.set([1.0, 1.0]);
prog.addVertexAttribute(posVertAttr, vgl.vertexAttributeKeys.Position);
prog.addVertexAttribute(colorVertAttr, vgl.vertexAttributeKeys.Color);
prog.addUniform(heightUniform);
prog.addUniform(vertexColorWeightUniform);
prog.addUniform(modelViewUniform);
prog.addUniform(projectionUniform);
prog.addUniform(samplerUniform);
prog.addUniform(useVertexColors);
prog.addUniform(useScalarsToColors);
prog.addUniform(pointSize);
prog.addShader(fragmentShader);
prog.addShader(vertexShader);
mat.addAttribute(prog);
mat.addAttribute(blend);
if (lut) {
prog.addUniform(scalarsToColors);
useScalarsToColors.set(1);
prog.addUniform(lutMinUniform);
prog.addUniform(lutMaxUniform);
lut.setTextureUnit(1);
mat.addAttribute(lut);
}
texture.setImage(image);
texture.setTextureUnit(0);
mat.addAttribute(texture);
return mat;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of an actor that contains a plane geometry
*
* Function to create a plane node This method will create a plane actor
* with texture coordinates, eventually normal, and plane material.
*
* @returns {vgl.actor}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createPlane = function (originX, originY, originZ,
point1X, point1Y, point1Z,
point2X, point2Y, point2Z) {
'use strict';
var mapper = new vgl.mapper(),
planeSource = new vgl.planeSource(),
mat = vgl.utils.createGeometryMaterial(),
actor = new vgl.actor();
planeSource.setOrigin(originX, originY, originZ);
planeSource.setPoint1(point1X, point1Y, point1Z);
planeSource.setPoint2(point2X, point2Y, point2Z);
mapper.setGeometryData(planeSource.create());
actor.setMapper(mapper);
actor.setMaterial(mat);
return actor;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of an actor that contains a texture plane geometry
*
* Helper function to create a plane textured node This method will create
* a plane actor with texture coordinates, eventually normal, and plane
* material.
*
* @returns {vgl.actor}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createTexturePlane = function (originX, originY, originZ,
point1X, point1Y, point1Z,
point2X, point2Y, point2Z,
isRgba) {
'use strict';
var mapper = new vgl.mapper(),
planeSource = new vgl.planeSource(),
mat = vgl.utils.createTextureMaterial(isRgba,
[originX, originY, originZ]),
actor = new vgl.actor();
planeSource.setPoint1(point1X - originX, point1Y - originY, point1Z - originZ);
planeSource.setPoint2(point2X - originX, point2Y - originY, point2Z - originZ);
mapper.setGeometryData(planeSource.create());
actor.setMapper(mapper);
actor.setMaterial(mat);
return actor;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of an actor that contains points
*
* Helper function to create a point node This method will create a point
* actor with texture coordinates, eventually normal, and plane material.
*
* @returns {vgl.actor}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createPoints = function (positions, size, colors, texcoords, opacity) {
'use strict';
if (!positions) {
console.log('[ERROR] Cannot create points without positions');
return null;
}
opacity = opacity === undefined ? 1.0 : opacity;
var mapper = new vgl.mapper(),
pointSource = new vgl.pointSource(),
mat = vgl.utils.createPointGeometryMaterial(opacity),
actor = new vgl.actor();
pointSource.setPositions(positions);
if (colors) {
pointSource.setColors(colors);
}
if (texcoords) {
pointSource.setTextureCoordinates(texcoords);
}
if (size) {
pointSource.setSize(size);
} else {
pointSource.setSize(1.0);
}
mapper.setGeometryData(pointSource.create());
actor.setMapper(mapper);
actor.setMaterial(mat);
return actor;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of an actor that contains point sprites
*
* Helper function to create a point sprites node This method will create
* a point sprites actor with texture coordinates, normals, and a point sprites
* material.
*
* @returns {vgl.actor}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createPointSprites = function (image, positions, colors,
texcoords) {
'use strict';
if (!image) {
console.log('[ERROR] Point sprites requires an image');
return null;
}
if (!positions) {
console.log('[ERROR] Cannot create points without positions');
return null;
}
var mapper = new vgl.mapper(),
pointSource = new vgl.pointSource(),
mat = vgl.utils.createPointSpritesMaterial(image),
actor = new vgl.actor();
pointSource.setPositions(positions);
if (colors) {
pointSource.setColors(colors);
}
if (texcoords) {
pointSource.setTextureCoordinates(texcoords);
}
mapper.setGeometryData(pointSource.create());
actor.setMapper(mapper);
actor.setMaterial(mat);
return actor;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create lines given positions, colors, and desired length
*
* @param positions
* @param colors
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createLines = function (positions, colors) {
'use strict';
if (!positions) {
console.log('[ERROR] Cannot create points without positions');
return null;
}
var mapper = new vgl.mapper(),
lineSource = new vgl.lineSource(),
mat = vgl.utils.createGeometryMaterial(),
actor = new vgl.actor();
lineSource.setPositions(positions);
if (colors) {
lineSource.setColors(colors);
}
mapper.setGeometryData(lineSource.create());
actor.setMapper(mapper);
actor.setMaterial(mat);
return actor;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create color legend
*
* @param lookupTable
* @param width
* @param height
* @param origin
* @param divs
* @returns {Array}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.createColorLegend = function (varname, lookupTable, origin,
width, height, countMajor,
countMinor) {
'use strict';
if (!lookupTable) {
console.log('[error] Invalid lookup table');
return [];
}
//////////////////////////////////////////////////////////////////////////////
/**
* Create labels for the legend
*
* @param ticks
* @param range
* @param divs
*/
//////////////////////////////////////////////////////////////////////////////
function createLabels(varname, positions, range) {
if (!positions) {
console.log('[error] Create labels requires positions (x,y,z) array');
return;
}
if (positions.length % 3 !== 0) {
console.log('[error] Create labels require positions array contain 3d points');
return;
}
if (!range) {
console.log('[error] Create labels requires Valid range');
return;
}
var actor = null,
size = vgl.utils.computePowerOfTwo(48),
index = 0,
actors = [],
origin = [],
pt1 = [],
pt2 = [],
delta = (positions[6] - positions[0]),
axisLabelOffset = 4, i;
origin.length = 3;
pt1.length = 3;
pt2.length = 3;
// For now just create labels for end points
for (i = 0; i < 2; i += 1) {
index = i * (positions.length - 3);
origin[0] = positions[index] - delta;
origin[1] = positions[index + 1] - 2 * delta;
origin[2] = positions[index + 2];
pt1[0] = positions[index] + delta;
pt1[1] = origin[1];
pt1[2] = origin[2];
pt2[0] = origin[0];
pt2[1] = positions[1];
pt2[2] = origin[2];
actor = vgl.utils.createTexturePlane(
origin[0], origin[1], origin[2],
pt1[0], pt1[1], pt1[2],
pt2[0], pt2[1], pt2[2], true);
actor.setReferenceFrame(vgl.boundingObject.ReferenceFrame.Absolute);
actor.material().setBinNumber(vgl.material.RenderBin.Overlay);
actor.material().addAttribute(vgl.utils.create2DTexture(
range[i].toFixed(2).toString(), 12, null));
actors.push(actor);
}
// Create axis label
origin[0] = (positions[0] + positions[positions.length - 3] - size) * 0.5;
origin[1] = positions[1] + axisLabelOffset;
origin[2] = positions[2];
pt1[0] = origin[0] + size;
pt1[1] = origin[1];
pt1[2] = origin[2];
pt2[0] = origin[0];
pt2[1] = origin[1] + size;
pt2[2] = origin[2];
actor = vgl.utils.createTexturePlane(
origin[0], origin[1], origin[2],
pt1[0], pt1[1], pt1[2],
pt2[0], pt2[1], pt2[2], true);
actor.setReferenceFrame(vgl.boundingObject.ReferenceFrame.Absolute);
actor.material().setBinNumber(vgl.material.RenderBin.Overlay);
actor.material().addAttribute(vgl.utils.create2DTexture(
varname, 24, null));
actors.push(actor);
return actors;
}
//////////////////////////////////////////////////////////////////////////////
// TODO Currently we assume that the ticks are laid on x-axis
// and this is on a 2D plane (ignoring Z axis. For now lets
// not draw minor ticks.
/**
* Create ticks and labels
*
* @param originX
* @param originY
* @param originZ
* @param pt1X
* @param pt1Y
* @param pt1Z
* @param pt2X
* @param pt2Y
* @param pt2Z
* @param divs
* @param heightMajor
* @param heightMinor
* @returns {Array} Returns array of vgl.actor
*/
//////////////////////////////////////////////////////////////////////////////
function createTicksAndLabels(varname, lut,
originX, originY, originZ,
pt1X, pt1Y, pt1Z,
pt2X, pt2Y, pt2Z,
countMajor, countMinor,
heightMajor, heightMinor) {
heightMinor = heightMinor; /* unused parameter */
var width = pt2X - pt1X,
index = null,
delta = width / countMajor,
positions = [],
actors = [];
for (index = 0; index <= countMajor; index += 1) {
positions.push(pt1X + delta * index);
positions.push(pt1Y);
positions.push(pt1Z);
positions.push(pt1X + delta * index);
positions.push(pt1Y + heightMajor);
positions.push(pt1Z);
}
// TODO: Fix this
//actor = vgl.utils.createLines(positions, null);
//actor.setReferenceFrame(vgl.boundingObject.ReferenceFrame.Absolute);
//actor.material().setBinNumber(vgl.material.RenderBin.Overlay);
//actors.push(actor);
actors = actors.concat(createLabels(varname, positions, lut.range()));
return actors;
}
// TODO Currently we create only one type of legend
var pt1X = origin[0] + width,
pt1Y = origin[1],
pt1Z = 0.0,
pt2X = origin[0],
pt2Y = origin[1] + height,
pt2Z = 0.0,
actors = [],
actor = null,
mat = null,
group = vgl.groupNode();
actor = vgl.utils.createTexturePlane(
origin[0], origin[1], origin[2],
pt1X, pt1Y, pt1Z,
pt2X, pt2Y, pt2Z, true
);
mat = actor.material();
mat.addAttribute(lookupTable);
actor.setMaterial(mat);
group.addChild(actor);
actor.material().setBinNumber(vgl.material.RenderBin.Overlay);
actor.setReferenceFrame(vgl.boundingObject.ReferenceFrame.Absolute);
actors.push(actor);
actors = actors.concat(createTicksAndLabels(
varname,
lookupTable,
origin[0], origin[1], origin[1],
pt2X, pt1Y, pt1Z,
pt1X, pt1Y, pt1Z,
countMajor, countMinor, 5, 3));
// TODO This needs to change so that we can return a group node
// which should get appended to the scene graph
return actors;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Create 2D texture by rendering text using canvas2D context
*
* @param textToWrite
* @param textSize
* @param color
* @returns {vgl.texture}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.utils.create2DTexture = function (textToWrite, textSize,
color, font, alignment, baseline, bold) {
'use strict';
var canvas = document.getElementById('textRendering'),
ctx = null,
texture = vgl.texture();
font = font || 'sans-serif';
alignment = alignment || 'center';
baseline = baseline || 'bottom';
if (typeof bold === 'undefined') {
bold = true;
}
if (!canvas) {
canvas = document.createElement('canvas');
}
ctx = canvas.getContext('2d');
canvas.setAttribute('id', 'textRendering');
canvas.style.display = 'none';
// Make width and height equal so that we get pretty looking text.
canvas.height = vgl.utils.computePowerOfTwo(8 * textSize);
canvas.width = canvas.height;
ctx.fillStyle = 'rgba(0, 0, 0, 0)';
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
// This determines the text colour, it can take a hex value or rgba value
// (e.g. rgba(255,0,0,0.5))
ctx.fillStyle = 'rgba(200, 85, 10, 1.0)';
// This determines the alignment of text, e.g. left, center, right
ctx.textAlign = alignment;
// This determines the baseline of the text, e.g. top, middle, bottom
ctx.textBaseline = baseline;
// This determines the size of the text and the font family used
ctx.font = 4 * textSize + 'px ' + font;
if (bold) {
ctx.font = 'bold ' + ctx.font;
}
ctx.fillText(textToWrite, canvas.width / 2, canvas.height / 2, canvas.width);
texture.setImage(canvas);
texture.updateDimensions();
return texture;
};
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, vec4, inherit*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class picker
*
* @class vgl.picker
* @returns {vgl.picker}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.picker = function () {
'use strict';
if (!(this instanceof vgl.picker)) {
return new vgl.picker();
}
vgl.object.call(this);
/** @private */
var m_actors = [];
////////////////////////////////////////////////////////////////////////////
/**
* Get actors intersected
*/
////////////////////////////////////////////////////////////////////////////
this.getActors = function () {
return m_actors;
};
////////////////////////////////////////////////////////////////////////////
/**
* Perform pick operation
*/
////////////////////////////////////////////////////////////////////////////
this.pick = function (selectionX, selectionY, renderer) {
// Check if variables are acceptable
if (selectionX === undefined) {
return 0;
}
if (selectionY === undefined) {
return 0;
}
if (renderer === undefined) {
return 0;
}
// Clean list of actors intersected previously
m_actors = [];
//
var camera = renderer.camera(),
width = renderer.width(),
height = renderer.height(),
fpoint = camera.focalPoint(),
focusWorldPt = vec4.fromValues(fpoint[0], fpoint[1], fpoint[2], 1.0),
focusDisplayPt = renderer.worldToDisplay(
focusWorldPt, camera.viewMatrix(),
camera.projectionMatrix(), width, height),
displayPt = vec4.fromValues(selectionX,
selectionY, focusDisplayPt[2], 1.0),
// Convert selection point into world coordinates
worldPt = renderer.displayToWorld(displayPt, camera.viewMatrix(),
camera.projectionMatrix(), width, height),
cameraPos = camera.position(), ray = [], actors, count, i, bb,
tmin, tmax, tymin, tymax, tzmin, tzmax, actor;
for (i = 0; i < 3; i += 1) {
ray[i] = worldPt[i] - cameraPos[i];
}
// Go through all actors and check if intersects
actors = renderer.sceneRoot().children();
count = 0;
for (i = 0; i < actors.length; i += 1) {
actor = actors[i];
if (actor.visible() === true) {
bb = actor.bounds();
// Ray-aabb intersection - Smits' method
if (ray[0] >= 0) {
tmin = (bb[0] - cameraPos[0]) / ray[0];
tmax = (bb[1] - cameraPos[0]) / ray[0];
} else {
tmin = (bb[1] - cameraPos[0]) / ray[0];
tmax = (bb[0] - cameraPos[0]) / ray[0];
}
if (ray[1] >= 0) {
tymin = (bb[2] - cameraPos[1]) / ray[1];
tymax = (bb[3] - cameraPos[1]) / ray[1];
} else {
tymin = (bb[3] - cameraPos[1]) / ray[1];
tymax = (bb[2] - cameraPos[1]) / ray[1];
}
if ((tmin > tymax) || (tymin > tmax)) {
//jscs:disable disallowKeywords
continue;
//jscs:enable disallowKeywords
}
if (tymin > tmin) {
tmin = tymin;
}
if (tymax < tmax) {
tmax = tymax;
}
if (ray[2] >= 0) {
tzmin = (bb[4] - cameraPos[2]) / ray[2];
tzmax = (bb[5] - cameraPos[2]) / ray[2];
} else {
tzmin = (bb[5] - cameraPos[2]) / ray[2];
tzmax = (bb[4] - cameraPos[2]) / ray[2];
}
if ((tmin > tzmax) || (tzmin > tmax)) {
//jscs:disable disallowKeywords
continue;
//jscs:enable disallowKeywords
}
if (tzmin > tmin) {
tmin = tzmin;
}
if (tzmax < tmax) {
tmax = tzmax;
}
m_actors[count] = actor;
count += 1;
}
}
return count;
};
return this;
};
inherit(vgl.picker, vgl.object);
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, $*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of shapefile reader
*
* This contains code that reads a shapefile and produces vgl geometries
*
* @class
* @returns {vgl.shapefileReader}
*/
//////////////////////////////////////////////////////////////////////////////
vgl.shapefileReader = function () {
'use strict';
if (!(this instanceof vgl.shapefileReader)) {
return new vgl.shapefileReader();
}
var m_that = this;
var SHP_NULL = 0;
var SHP_POINT = 1;
var SHP_POLYGON = 5;
var SHP_POLYLINE = 3;
this.int8 = function (data, offset) {
return data.charCodeAt (offset);
};
/*jshint bitwise: false */
this.bint32 = function (data, offset) {
return (
((data.charCodeAt (offset) & 0xff) << 24) +
((data.charCodeAt (offset + 1) & 0xff) << 16) +
((data.charCodeAt (offset + 2) & 0xff) << 8) +
(data.charCodeAt (offset + 3) & 0xff)
);
};
this.lint32 = function (data, offset) {
return (
((data.charCodeAt (offset + 3) & 0xff) << 24) +
((data.charCodeAt (offset + 2) & 0xff) << 16) +
((data.charCodeAt (offset + 1) & 0xff) << 8) +
(data.charCodeAt (offset) & 0xff)
);
};
this.bint16 = function (data, offset) {
return (
((data.charCodeAt (offset) & 0xff) << 8) +
(data.charCodeAt (offset + 1) & 0xff)
);
};
this.lint16 = function (data, offset) {
return (
((data.charCodeAt (offset + 1) & 0xff) << 8) +
(data.charCodeAt (offset) & 0xff)
);
};
this.ldbl64 = function (data, offset) {
var b0 = data.charCodeAt (offset) & 0xff;
var b1 = data.charCodeAt (offset + 1) & 0xff;
var b2 = data.charCodeAt (offset + 2) & 0xff;
var b3 = data.charCodeAt (offset + 3) & 0xff;
var b4 = data.charCodeAt (offset + 4) & 0xff;
var b5 = data.charCodeAt (offset + 5) & 0xff;
var b6 = data.charCodeAt (offset + 6) & 0xff;
var b7 = data.charCodeAt (offset + 7) & 0xff;
var sign = 1 - 2 * (b7 >> 7);
var exp = (((b7 & 0x7f) << 4) + ((b6 & 0xf0) >> 4)) - 1023;
//var frac = (b6 & 0x0f) * Math.pow (2, -4) + b5 * Math.pow (2, -12) + b4 *
// Math.pow (2, -20) + b3 * Math.pow (2, -28) + b2 * Math.pow (2, -36) + b1 *
// Math.pow (2, -44) + b0 * Math.pow (2, -52);
//return sign * (1 + frac) * Math.pow (2, exp);
var frac = (b6 & 0x0f) * Math.pow (2, 48) + b5 * Math.pow (2, 40) + b4 *
Math.pow (2, 32) + b3 * Math.pow (2, 24) + b2 *
Math.pow (2, 16) + b1 * Math.pow (2, 8) + b0;
return sign * (1 + frac * Math.pow (2, -52)) * Math.pow (2, exp);
};
this.lfloat32 = function (data, offset) {
var b0 = data.charCodeAt (offset) & 0xff;
var b1 = data.charCodeAt (offset + 1) & 0xff;
var b2 = data.charCodeAt (offset + 2) & 0xff;
var b3 = data.charCodeAt (offset + 3) & 0xff;
var sign = 1 - 2 * (b3 >> 7);
var exp = (((b3 & 0x7f) << 1) + ((b2 & 0xfe) >> 7)) - 127;
var frac = (b2 & 0x7f) * Math.pow (2, 16) + b1 * Math.pow (2, 8) + b0;
return sign * (1 + frac * Math.pow (2, -23)) * Math.pow (2, exp);
};
/*jshint bitwise: true */
this.str = function (data, offset, length) {
var chars = [];
var index = offset;
while (index < offset + length) {
var c = data[index];
if (c.charCodeAt (0) !== 0) {
chars.push (c);
} else {
break;
}
index += 1;
}
return chars.join ('');
};
this.readHeader = function (data) {
var code = this.bint32(data, 0);
var length = this.bint32(data, 24);
var version = this.lint32(data, 28);
var shapetype = this.lint32(data, 32);
/*
var xmin = this.ldbl64(data, 36);
var ymin = this.ldbl64(data, 44);
var xmax = this.ldbl64(data, 52);
var ymax = this.ldbl64(data, 60);
*/
return {
code: code,
length: length,
version: version,
shapetype: shapetype
// bounds: new Box (vect (xmin, ymin), vect (xmax, ymax))
};
};
this.loadShx = function (data) {
var indices = [];
var appendIndex = function (offset) {
indices.push (2 * m_that.bint32(data, offset));
return offset + 8;
};
var offset = 100;
while (offset < data.length) {
offset = appendIndex (offset);
}
return indices;
};
this.Shapefile = function (options) {
var path = options.path;
$.ajax ({
url: path + '.shx',
mimeType: 'text/plain; charset=x-user-defined',
success: function (data) {
var indices = this.loadShx(data);
$.ajax ({
url: path + '.shp',
mimeType: 'text/plain; charset=x-user-defined',
success: function (data) {
$.ajax ({
url: path + '.dbf',
mimeType: 'text/plain; charset=x-user-defined',
success: function (dbf_data) {
var layer = this.loadShp (data, dbf_data, indices, options);
options.success (layer);
}
});
}
});
}
});
};
this.localShapefile = function (options) {
var shxFile = options.shx;
var shpFile = options.shp;
var dbfFile = options.dbf;
var shxReader = new FileReader();
shxReader.onloadend = function () {
var indices = m_that.loadShx(shxReader.result);
var shpReader = new FileReader();
shpReader.onloadend = function () {
var shpData = shpReader.result;
var dbfReader = new FileReader();
dbfReader.onloadend = function () {
var dbfData = dbfReader.result;
var layer = m_that.loadShp(shpData, dbfData, indices, options);
options.success(layer);
};
dbfReader.readAsBinaryString(dbfFile);
};
shpReader.readAsBinaryString(shpFile);
};
shxReader.readAsBinaryString(shxFile);
};
this.loadDBF = function (data) {
var readHeader = function (offset) {
var name = m_that.str(data, offset, 10);
var type = m_that.str(data, offset + 11, 1);
var length = m_that.int8(data, offset + 16);
return {
name: name,
type: type,
length: length
};
};
// Level of the dBASE file
var level = m_that.int8(data, 0);
if (level === 4) {
throw 'Level 7 dBASE not supported';
}
// Date of last update
/*
var year = m_that.int8(data, 1);
var month = m_that.int8(data, 2);
var day = m_that.int8(data, 3);
*/
var num_entries = m_that.lint32(data, 4);
var header_size = m_that.lint16(data, 8);
var record_size = m_that.lint16(data, 10);
var FIELDS_START = 32;
var HEADER_LENGTH = 32;
var header_offset = FIELDS_START;
var headers = [];
while (header_offset < header_size - 1) {
headers.push (readHeader(header_offset));
header_offset += HEADER_LENGTH;
}
var records = [];
var record_offset = header_size;
while (record_offset < header_size + num_entries * record_size) {
var declare = m_that.str(data, record_offset, 1);
if (declare === '*') {
// Record size in the header include the size of the delete indicator
record_offset += record_size;
} else {
// Move offset to the start of the actual data
record_offset += 1;
var record = {};
for (var i = 0; i < headers.length; i += 1) {
var header = headers[i];
var value;
if (header.type === 'C') {
value = m_that.str(data, record_offset, header.length).trim ();
} else if (header.type === 'N') {
value = parseFloat (m_that.str (data, record_offset, header.length));
}
record_offset += header.length;
record[header.name] = value;
}
records.push(record);
}
}
return records;
};
this.loadShp = function (data, dbf_data, indices, options) {
options = options; /* unused parameter */
var features = [];
var readRing = function (offset, start, end) {
var ring = [];
for (var i = end - 1; i >= start; i -= 1) {
var x = m_that.ldbl64(data, offset + 16 * i);
var y = m_that.ldbl64(data, offset + 16 * i + 8);
ring.push ([x, y]);
}
//if (ring.length <= 3)
// return [];
return ring;
};
var readRecord = function (offset) {
// var index = m_that.bint32(data, offset);
// var record_length = m_that.bint32(data, offset + 4);
var record_offset = offset + 8;
var geom_type = m_that.lint32(data, record_offset);
var num_parts, num_points, parts_start, points_start, i,
start, end, ring, rings;
if (geom_type === SHP_NULL) {
console.log ('NULL Shape');
//return offset + 12;
} else if (geom_type === SHP_POINT) {
var x = m_that.ldbl64(data, record_offset + 4);
var y = m_that.ldbl64(data, record_offset + 12);
features.push ({
type: 'Point',
attr: {},
geom: [[x, y]]
});
} else if (geom_type === SHP_POLYGON) {
num_parts = m_that.lint32(data, record_offset + 36);
num_points = m_that.lint32(data, record_offset + 40);
parts_start = offset + 52;
points_start = offset + 52 + 4 * num_parts;
rings = [];
for (i = 0; i < num_parts; i += 1) {
start = m_that.lint32(data, parts_start + i * 4);
if (i + 1 < num_parts) {
end = m_that.lint32(data, parts_start + (i + 1) * 4);
} else {
end = num_points;
}
ring = readRing (points_start, start, end);
rings.push (ring);
}
features.push ({
type: 'Polygon',
attr: {},
geom: [rings]
});
} else if (geom_type === SHP_POLYLINE) {
num_parts = m_that.lint32(data, record_offset + 36);
num_points = m_that.lint32(data, record_offset + 40);
parts_start = offset + 52;
points_start = offset + 52 + 4 * num_parts;
rings = [];
for (i = 0; i < num_parts; i += 1) {
start = m_that.lint32(data, parts_start + i * 4);
if (i + 1 < num_parts) {
end = m_that.lint32(data, parts_start + (i + 1) * 4);
} else {
end = num_points;
}
ring = readRing (points_start, start, end);
rings.push (ring);
}
features.push ({
type: 'Polyline',
attr: {},
geom: [rings]
});
} else {
throw 'Not Implemented: ' + geom_type;
}
//return offset + 2 * record_length + SHP_HEADER_LEN;
};
var attr = this.loadDBF(dbf_data), i;
//var offset = 100;
//while (offset < length * 2) {
// offset = readRecord (offset);
//}
for (i = 0; i < indices.length; i += 1) {
var offset = indices[i];
readRecord (offset);
}
var layer = []; //new Layer ();
for (i = 0; i < features.length; i += 1) {
var feature = features[i];
feature.attr = attr[i];
layer.push (feature);
}
return layer;
};
return this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* @module vgl
*/
/*global vgl, mat4, unescape, Float32Array, Int8Array, Uint16Array*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// vbgModule.vtkReader class
// This contains code that unpack a json base64 encoded vtkdataset,
// such as those produced by ParaView's webGL exporter (where much
// of the code originated from) and convert it to VGL representation.
//
//////////////////////////////////////////////////////////////////////////////
vgl.vtkReader = function () {
'use strict';
if (!(this instanceof vgl.vtkReader)) {
return new vgl.vtkReader();
}
var m_base64Chars =
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'],
m_reverseBase64Chars = [],
m_vtkRenderedList = {},
m_vtkObjectCount = 0,
m_vtkScene = null,
m_node = null,
END_OF_INPUT = -1,
m_base64Str = '',
m_base64Count = 0,
m_pos = 0,
m_viewer = null,
i = 0;
//initialize the array here if not already done.
if (m_reverseBase64Chars.length === 0) {
for (i = 0; i < m_base64Chars.length; i += 1) {
m_reverseBase64Chars[m_base64Chars[i]] = i;
}
}
////////////////////////////////////////////////////////////////////////////
/**
* ntos
*
* @param n
* @returns unescaped n
*/
////////////////////////////////////////////////////////////////////////////
this.ntos = function (n) {
var unN;
unN = n.toString(16);
if (unN.length === 1) {
unN = '0' + unN;
}
unN = '%' + unN;
return unescape(unN);
};
////////////////////////////////////////////////////////////////////////////
/**
* readReverseBase64
*
* @returns
*/
////////////////////////////////////////////////////////////////////////////
this.readReverseBase64 = function () {
var nextCharacter;
if (!m_base64Str) {
return END_OF_INPUT;
}
while (true) {
if (m_base64Count >= m_base64Str.length) {
return END_OF_INPUT;
}
nextCharacter = m_base64Str.charAt(m_base64Count);
m_base64Count += 1;
if (m_reverseBase64Chars[nextCharacter]) {
return m_reverseBase64Chars[nextCharacter];
}
if (nextCharacter === 'A') {
return 0;
}
}
return END_OF_INPUT;
};
////////////////////////////////////////////////////////////////////////////
/**
* decode64
*
* @param str
* @returns result
*/
////////////////////////////////////////////////////////////////////////////
this.decode64 = function (str) {
var result = '',
inBuffer = new Array(4),
done = false;
m_base64Str = str;
m_base64Count = 0;
while (!done &&
(inBuffer[0] = this.readReverseBase64()) !== END_OF_INPUT &&
(inBuffer[1] = this.readReverseBase64()) !== END_OF_INPUT) {
inBuffer[2] = this.readReverseBase64();
inBuffer[3] = this.readReverseBase64();
/*jshint bitwise: false */
result += this.ntos((((inBuffer[0] << 2) & 0xff) | inBuffer[1] >> 4));
if (inBuffer[2] !== END_OF_INPUT) {
result += this.ntos((((inBuffer[1] << 4) & 0xff) | inBuffer[2] >> 2));
if (inBuffer[3] !== END_OF_INPUT) {
result += this.ntos((((inBuffer[2] << 6) & 0xff) | inBuffer[3]));
} else {
done = true;
}
} else {
done = true;
}
/*jshint bitwise: true */
}
return result;
};
////////////////////////////////////////////////////////////////////////////
/**
* readNumber
*
* @param ss
* @returns v
*/
////////////////////////////////////////////////////////////////////////////
this.readNumber = function (ss) {
//jshint plusplus: false, bitwise: false
var v = ((ss[m_pos++]) +
(ss[m_pos++] << 8) +
(ss[m_pos++] << 16) +
(ss[m_pos++] << 24));
//jshint plusplus: true, bitwise: true
return v;
};
////////////////////////////////////////////////////////////////////////////
/**
* readF3Array
*
* @param numberOfPoints
* @param ss
* @returns points
*/
////////////////////////////////////////////////////////////////////////////
this.readF3Array = function (numberOfPoints, ss) {
var size = numberOfPoints * 4 * 3, test = new Int8Array(size),
points = null, i;
for (i = 0; i < size; i += 1) {
test[i] = ss[m_pos];
m_pos += 1;
}
points = new Float32Array(test.buffer);
return points;
};
////////////////////////////////////////////////////////////////////////////
/**
* readColorArray
*
* @param numberOfPoints
* @param ss
* @param vglcolors
* @returns points
*/
////////////////////////////////////////////////////////////////////////////
this.readColorArray = function (numberOfPoints, ss, vglcolors) {
var i, idx = 0, tmp = new Array(numberOfPoints * 3);
//jshint plusplus: false
for (i = 0; i < numberOfPoints; i += 1) {
tmp[idx++] = ss[m_pos++] / 255.0;
tmp[idx++] = ss[m_pos++] / 255.0;
tmp[idx++] = ss[m_pos++] / 255.0;
m_pos++;
}
//jshint plusplus: true
vglcolors.insert(tmp);
};
////////////////////////////////////////////////////////////////////////////
/**
* parseObject
*
* @param buffer
*/
////////////////////////////////////////////////////////////////////////////
this.parseObject = function (vtkObject) {
var geom = new vgl.geometryData(), mapper = vgl.mapper(), ss = [],
type = null, data = null, size, matrix = null, material = null,
actor, colorMapData, shaderProg, opacityUniform, lookupTable,
colorTable, windowSize, width, height, position;
//dehexlify
//data = this.decode64(vtkObject.data);
data = atob(vtkObject.data);
//jshint bitwise: false
for (i = 0; i < data.length; i += 1) {
ss[i] = data.charCodeAt(i) & 0xff;
}
//jshint bitwise: true
//Determine the Object type
m_pos = 0;
size = this.readNumber(ss);
type = String.fromCharCode(ss[m_pos]);
m_pos += 1;
geom.setName(type);
// Lines
if (type === 'L') {
matrix = this.parseLineData(geom, ss);
material = vgl.utils.createGeometryMaterial();
// Mesh
} else if (type === 'M') {
matrix = this.parseMeshData(geom, ss);
material = vgl.utils.createPhongMaterial();
// Points
} else if (type === 'P') {
matrix = this.parsePointData(geom, ss);
material = vgl.utils.createGeometryMaterial();
// ColorMap
} else if (type === 'C') {
colorMapData = this.parseColorMapData(geom, ss, size);
colorTable = [];
for (i = 0; i < colorMapData.colors.length; i += 1) {
colorTable.push(colorMapData.colors[i][1]);
colorTable.push(colorMapData.colors[i][2]);
colorTable.push(colorMapData.colors[i][3]);
colorTable.push(colorMapData.colors[i][0] * 255);
}
lookupTable = new vgl.lookupTable();
lookupTable.setColorTable(colorTable);
windowSize = m_viewer.renderWindow().windowSize();
width = colorMapData.size[0] * windowSize[0];
height = colorMapData.size[1] * windowSize[1];
position = [colorMapData.position[0] * windowSize[0],
(1 - colorMapData.position[1]) * windowSize[1], 0];
position[1] = position[1] - height;
// For now hardcode the height
height = 30;
return vgl.utils.createColorLegend(colorMapData.title,
lookupTable, position, width, height, 3, 0);
// Unknown
} else {
console.log('Ignoring unrecognized encoded data type ' + type);
}
mapper.setGeometryData(geom);
//default opacity === solid. If were transparent, set it lower.
if (vtkObject.hasTransparency) {
shaderProg = material.shaderProgram();
opacityUniform = shaderProg.uniform('opacity');
console.log('opacity ', vtkObject.opacity);
opacityUniform.set(vtkObject.opacity);
material.setBinNumber(1000);
}
actor = vgl.actor();
actor.setMapper(mapper);
actor.setMaterial(material);
actor.setMatrix(mat4.transpose(mat4.create(), matrix));
return [actor];
};
////////////////////////////////////////////////////////////////////////////
/**
* parseLineData
*
* @param geom, ss
* @returns matrix
*/
////////////////////////////////////////////////////////////////////////////
this.parseLineData = function (geom, ss) {
var vglpoints = null, vglcolors = null, vgllines = null,
matrix = mat4.create(),
numberOfIndex, numberOfPoints, points,
temp, index, size, m, i,
p = null, idx = 0;
numberOfPoints = this.readNumber(ss);
p = new Array(numberOfPoints * 3);
//Getting Points
vglpoints = new vgl.sourceDataP3fv();
points = this.readF3Array(numberOfPoints, ss);
//jshint plusplus: false
for (i = 0; i < numberOfPoints; i += 1) {
p[idx++] = points[i * 3/*+0*/];
p[idx++] = points[i * 3 + 1];
p[idx++] = points[i * 3 + 2];
}
//jshint plusplus: true
vglpoints.insert(p);
geom.addSource(vglpoints);
//Getting Colors
vglcolors = new vgl.sourceDataC3fv();
this.readColorArray(numberOfPoints, ss, vglcolors);
geom.addSource(vglcolors);
//Getting connectivity
vgllines = new vgl.lines();
geom.addPrimitive(vgllines);
numberOfIndex = this.readNumber(ss);
temp = new Int8Array(numberOfIndex * 2);
for (i = 0; i < numberOfIndex * 2; i += 1) {
temp[i] = ss[m_pos];
m_pos += 1;
}
index = new Uint16Array(temp.buffer);
vgllines.setIndices(index);
vgllines.setPrimitiveType(vgl.GL.LINES);
//Getting Matrix
size = 16 * 4;
temp = new Int8Array(size);
for (i = 0; i < size; i += 1) {
temp[i] = ss[m_pos];
m_pos += 1;
}
m = new Float32Array(temp.buffer);
mat4.copy(matrix, m);
return matrix;
};
////////////////////////////////////////////////////////////////////////////
/**
* parseMeshData
*
* @param geom, ss
* @returns matrix
*/
////////////////////////////////////////////////////////////////////////////
this.parseMeshData = function (geom, ss) {
var vglpoints = null, vglcolors = null,
normals = null, matrix = mat4.create(),
vgltriangles = null, numberOfIndex, numberOfPoints,
points, temp, index, size, m, i, tcoord,
pn = null, idx = 0;
numberOfPoints = this.readNumber(ss);
pn = new Array(numberOfPoints * 6);
//Getting Points
vglpoints = new vgl.sourceDataP3N3f();
points = this.readF3Array(numberOfPoints, ss);
//Getting Normals
normals = this.readF3Array(numberOfPoints, ss);
//jshint plusplus: false
for (i = 0; i < numberOfPoints; i += 1) {
pn[idx++] = points[i * 3/*+0*/];
pn[idx++] = points[i * 3 + 1];
pn[idx++] = points[i * 3 + 2];
pn[idx++] = normals[i * 3/*+0*/];
pn[idx++] = normals[i * 3 + 1];
pn[idx++] = normals[i * 3 + 2];
}
//jshint plusplus: true
vglpoints.insert(pn);
geom.addSource(vglpoints);
//Getting Colors
vglcolors = new vgl.sourceDataC3fv();
this.readColorArray(numberOfPoints, ss, vglcolors);
geom.addSource(vglcolors);
//Getting connectivity
temp = [];
vgltriangles = new vgl.triangles();
numberOfIndex = this.readNumber(ss);
temp = new Int8Array(numberOfIndex * 2);
for (i = 0; i < numberOfIndex * 2; i += 1) {
temp[i] = ss[m_pos];
m_pos += 1;
}
index = new Uint16Array(temp.buffer);
vgltriangles.setIndices(index);
geom.addPrimitive(vgltriangles);
//Getting Matrix
size = 16 * 4;
temp = new Int8Array(size);
for (i = 0; i < size; i += 1) {
temp[i] = ss[m_pos];
m_pos += 1;
}
m = new Float32Array(temp.buffer);
mat4.copy(matrix, m);
//Getting TCoord
//TODO: renderer is not doing anything with this yet
tcoord = null;
return matrix;
};
////////////////////////////////////////////////////////////////////////////
/**
* parsePointData
*
* @param geom, ss
* @returns matrix
*/
////////////////////////////////////////////////////////////////////////////
this.parsePointData = function (geom, ss) {
var numberOfPoints, points, indices, temp, size,
matrix = mat4.create(), vglpoints = null,
vglcolors = null, vglVertexes = null, m,
p = null, idx = 0;
numberOfPoints = this.readNumber(ss);
p = new Array(numberOfPoints * 3);
//Getting Points and creating 1:1 connectivity
vglpoints = new vgl.sourceDataP3fv();
points = this.readF3Array(numberOfPoints, ss);
indices = new Uint16Array(numberOfPoints);
//jshint plusplus: false
for (i = 0; i < numberOfPoints; i += 1) {
indices[i] = i;
p[idx++] = points[i * 3/*+0*/];
p[idx++] = points[i * 3 + 1];
p[idx++] = points[i * 3 + 2];
}
//jshint plusplus: true
vglpoints.insert(p);
geom.addSource(vglpoints);
//Getting Colors
vglcolors = new vgl.sourceDataC3fv();
this.readColorArray(numberOfPoints, ss, vglcolors);
geom.addSource(vglcolors);
//Getting connectivity
vglVertexes = new vgl.points();
vglVertexes.setIndices(indices);
geom.addPrimitive(vglVertexes);
//Getting matrix
size = 16 * 4;
temp = new Int8Array(size);
for (i = 0; i < size; i += 1) {
temp[i] = ss[m_pos];
m_pos += 1;
}
m = new Float32Array(temp.buffer);
mat4.copy(matrix, m);
return matrix;
};
////////////////////////////////////////////////////////////////////////////
/**
* parseColorMapData
*
* @param geom, ss
* @returns matrix
*/
////////////////////////////////////////////////////////////////////////////
this.parseColorMapData = function (geom, ss, numColors) {
var tmpArray, size, xrgb, i, c, obj = {};
// Set number of colors
obj.numOfColors = numColors;
// Getting Position
size = 8;
tmpArray = new Int8Array(size);
for (i = 0; i < size; i += 1) {
tmpArray[i] = ss[m_pos];
m_pos += 1;
}
obj.position = new Float32Array(tmpArray.buffer);
// Getting Size
size = 8;
tmpArray = new Int8Array(size);
for (i = 0; i < size; i += 1) {
tmpArray[i] = ss[m_pos];
m_pos += 1;
}
obj.size = new Float32Array(tmpArray.buffer);
//Getting Colors
obj.colors = [];
//jshint plusplus: false
for (c = 0; c < obj.numOfColors; c += 1) {
tmpArray = new Int8Array(4);
for (i = 0; i < 4; i += 1) {
tmpArray[i] = ss[m_pos];
m_pos += 1;
}
xrgb = [
new Float32Array(tmpArray.buffer)[0],
ss[m_pos++],
ss[m_pos++],
ss[m_pos++]
];
obj.colors[c] = xrgb;
}
obj.orientation = ss[m_pos++];
obj.numOfLabels = ss[m_pos++];
obj.title = '';
while (m_pos < ss.length) {
obj.title += String.fromCharCode(ss[m_pos++]);
}
//jshint plusplus: true
return obj;
};
////////////////////////////////////////////////////////////////////////////
/**
* parseSceneMetadata
*
* @param data
* @returns renderer
*/
////////////////////////////////////////////////////////////////////////////
this.parseSceneMetadata = function (renderer, layer) {
var sceneRenderer = m_vtkScene.Renderers[layer],
camera = renderer.camera(), bgc, localWidth, localHeight;
localWidth = (sceneRenderer.size[0] - sceneRenderer.origin[0]) * m_node.width;
localHeight = (sceneRenderer.size[1] - sceneRenderer.origin[1]) * m_node.height;
renderer.resize(localWidth, localHeight);
/// We are setting the center to the focal point because of
/// a possible paraview web bug. The center of rotation isn't
/// getting updated correctly on resetCamera.
camera.setCenterOfRotation(
[sceneRenderer.LookAt[1], sceneRenderer.LookAt[2],
sceneRenderer.LookAt[3]]);
camera.setViewAngleDegrees(sceneRenderer.LookAt[0]);
camera.setPosition(
sceneRenderer.LookAt[7], sceneRenderer.LookAt[8],
sceneRenderer.LookAt[9]);
camera.setFocalPoint(
sceneRenderer.LookAt[1], sceneRenderer.LookAt[2],
sceneRenderer.LookAt[3]);
camera.setViewUpDirection(
sceneRenderer.LookAt[4], sceneRenderer.LookAt[5],
sceneRenderer.LookAt[6]);
if (layer === 0) {
bgc = sceneRenderer.Background1;
renderer.setBackgroundColor(bgc[0], bgc[1], bgc[2], 1);
} else {
renderer.setResizable(false);
}
renderer.setLayer(layer);
};
////////////////////////////////////////////////////////////////////////////
/**
* initScene
*
* @returns viewer
*/
////////////////////////////////////////////////////////////////////////////
this.initScene = function () {
var renderer, layer;
if (m_vtkScene === null) {
return m_viewer;
}
for (layer = m_vtkScene.Renderers.length - 1; layer >= 0; layer -= 1) {
renderer = this.getRenderer(layer);
this.parseSceneMetadata(renderer, layer);
}
return m_viewer;
};
////////////////////////////////////////////////////////////////////////////
/**
* createViewer - Creates a viewer object.
*
* @param
*
* @returns viewer
*/
////////////////////////////////////////////////////////////////////////////
this.createViewer = function (node) {
var interactorStyle;
if (m_viewer === null) {
m_node = node;
m_viewer = vgl.viewer(node);
m_viewer.init();
m_viewer.renderWindow().removeRenderer(m_viewer.renderWindow().activeRenderer());
m_viewer.renderWindow().addRenderer(new vgl.depthPeelRenderer());
m_vtkRenderedList[0] = m_viewer.renderWindow().activeRenderer();
m_viewer.renderWindow().resize(node.width, node.height);
interactorStyle = vgl.pvwInteractorStyle();
m_viewer.setInteractorStyle(interactorStyle);
}
return m_viewer;
};
////////////////////////////////////////////////////////////////////////////
/**
* deleteViewer - Deletes the viewer object associated with the reader.
*
* @returns void
*/
////////////////////////////////////////////////////////////////////////////
this.deleteViewer = function () {
m_vtkRenderedList = {};
m_viewer = null;
};
////////////////////////////////////////////////////////////////////////////
/**
* updateCanvas -
*
* @param
*
* @returns void
*/
////////////////////////////////////////////////////////////////////////////
this.updateCanvas = function (node) {
m_node = node;
m_viewer.renderWindow().resize(node.width, node.height);
return m_viewer;
};
////////////////////////////////////////////////////////////////////////////
/**
* clearVtkObjectData - Clear out the list of VTK geometry data.
*
* @param void
* @returns void
*/
////////////////////////////////////////////////////////////////////////////
this.numObjects = function () {
return m_vtkObjectCount;
};
////////////////////////////////////////////////////////////////////////////
/**
* getRenderer - Gets (or creates) the renderer for a layer.
*
* @param layer
* @returns renderer
*/
////////////////////////////////////////////////////////////////////////////
this.getRenderer = function (layer) {
var renderer;
renderer = m_vtkRenderedList[layer];
if (renderer === null || typeof renderer === 'undefined') {
renderer = new vgl.renderer();
renderer.setResetScene(false);
renderer.setResetClippingRange(false);
m_viewer.renderWindow().addRenderer(renderer);
if (layer !== 0) {
renderer.camera().setClearMask(vgl.GL.DepthBufferBit);
}
m_vtkRenderedList[layer] = renderer;
}
return renderer;
};
////////////////////////////////////////////////////////////////////////////
/**
* setVtkScene - Set the VTK scene data for camera initialization.
*
* @param scene
* @returns void
*/
////////////////////////////////////////////////////////////////////////////
this.setVtkScene = function (scene) {
m_vtkScene = scene;
};
return this;
};
vgl.DataBuffers = function (initialSize) {
'use strict';
if (!(this instanceof vgl.DataBuffers)) {
return new vgl.DataBuffers(initialSize);
}
var data = {};
var size;
if (!initialSize && initialSize !== 0) {
size = 256;
} else {
size = initialSize;
}
var current = 0;
var copyArray = function (dst, src, start, count) {
if (!dst) {
console.log ('ack');
}
if (!start) {
start = 0;
}
if (!count) {
count = src.length;
}
for (var i = 0; i < count; i += 1) {
dst[start + i] = src[i];
}
};
var resize = function (min_expand) {
var new_size = size;
/* If the array would increase substantially, don't just double its
* size. If the array has been increasing gradually, double it as the
* expectation is that it will increase again. */
if (new_size * 2 < min_expand) {
new_size = min_expand;
}
while (new_size < min_expand) {
new_size *= 2;
}
size = new_size;
for (var name in data) {
if (data.hasOwnProperty(name)) {
var newArray = new Float32Array (new_size * data[name].len);
var oldArray = data[name].array;
copyArray (newArray, oldArray);
data[name].array = newArray;
data[name].dirty = true;
}
}
};
this.create = function (name, len) {
if (!len) {
throw 'Length of buffer must be a positive integer';
}
var array = new Float32Array (size * len);
data[name] = {
array: array,
len: len,
dirty: false
};
return data[name].array;
};
this.alloc = function (num) {
if ((current + num) >= size) {
resize (current + num);
}
var start = current;
current += num;
return start;
};
this.get = function (name) {
return data[name].array;
};
this.write = function (name, array, start, count) {
copyArray (data[name].array, array, start * data[name].len, count * data[name].len);
data[name].dirty = true;
};
this.repeat = function (name, elem, start, count) {
for (var i = 0; i < count; i += 1) {
copyArray (data[name].array, elem,
(start + i) * data[name].len, data[name].len);
}
data[name].dirty = true;
};
this.count = function () {
return current;
};
this.data = function (name) {
return data[name].array;
};
};
return vgl;
}));
(function () {
'use strict';
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
/**
* Takes a variable number of arguments and returns the first numeric value
* it finds.
* @private
*/
function setNumeric() {
var i;
for (i = 0; i < arguments.length; i += 1) {
if (isFinite(arguments[i])) {
return arguments[i];
}
}
}
//////////////////////////////////////////////////////////////////////////////
/**
* Contains utility classes and methods used by geojs.
* @namespace
*/
//////////////////////////////////////////////////////////////////////////////
geo.util = {
/**
* Returns true if the given point lies in the given polygon.
* Algorithm description:
* http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
* @param {geo.screenPosition} point The test point
* @param {geo.screenPosition[]} outer The outer boundary of the polygon
* @param {geo.screenPosition[][]?} inner Inner boundaries (holes)
*/
pointInPolygon: function (point, outer, inner) {
var inside = false, n = outer.length;
if (n < 3) {
// we need 3 coordinates for this to make sense
return false;
}
outer.forEach(function (vert, i) {
var j = (n + i - 1) % n;
var intersect = (
((outer[i].y > point.y) !== (outer[j].y > point.y)) &&
(point.x < (outer[j].x - outer[i].x) *
(point.y - outer[i].y) /
(outer[j].y - outer[i].y) + outer[i].x)
);
if (intersect) {
inside = !inside;
}
});
(inner || []).forEach(function (hole) {
inside = inside && !geo.util.pointInPolygon(point, hole);
});
return inside;
},
/**
* Returns true if the argument is a function.
*/
isFunction: function (f) {
return typeof f === 'function';
},
/**
* Returns the argument if it is function, otherwise returns a function
* that returns the argument.
*/
ensureFunction: function (f) {
if (geo.util.isFunction(f)) {
return f;
} else {
return function () { return f; };
}
},
/**
* Return a random string of length n || 8.
*/
randomString: function (n) {
var s, i, r;
n = n || 8;
s = '';
for (i = 0; i < n; i += 1) {
r = Math.floor(Math.random() * chars.length);
s += chars.substring(r, r + 1);
}
return s;
},
/**
* Convert a color from hex value or css name to rgb objects
*/
convertColor: function (color) {
if (color.r !== undefined && color.g !== undefined &&
color.b !== undefined) {
return color;
}
if (typeof color === 'string') {
if (geo.util.cssColors.hasOwnProperty(color)) {
color = geo.util.cssColors[color];
} else if (color.charAt(0) === '#') {
color = parseInt(color.slice(1), 16);
}
}
if (isFinite(color)) {
color = {
r: ((color & 0xff0000) >> 16) / 255,
g: ((color & 0xff00) >> 8) / 255,
b: ((color & 0xff)) / 255
};
}
return color;
},
/**
* Normalize a coordinate object into {x: ..., y: ..., z: ... } form.
* Accepts 2-3d arrays,
* latitude -> lat -> y
* longitude -> lon -> lng -> x
*/
normalizeCoordinates: function (p) {
p = p || {};
if (Array.isArray(p)) {
return {
x: p[0],
y: p[1],
z: p[2] || 0
};
}
return {
x: setNumeric(
p.x,
p.longitude,
p.lng,
p.lon,
0
),
y: setNumeric(
p.y,
p.latitude,
p.lat,
0
),
z: setNumeric(
p.z,
p.elevation,
p.elev,
p.height,
0
)
};
},
/**
* Radius of the earth in meters, from the equatorial radius of SRID 4326.
*/
radiusEarth: 6378137,
/**
* Linearly combine two "coordinate-like" objects in a uniform way.
* Coordinate like objects have ``x``, ``y``, and optionally a ``z``
* key. The first object is mutated.
*
* a <= ca * a + cb * b
*
* @param {number} ca
* @param {object} a
* @param {number} [a.x=0]
* @param {number} [a.y=0]
* @param {number} [a.z=0]
* @param {number} cb
* @param {object} b
* @param {number} [b.x=0]
* @param {number} [b.y=0]
* @param {number} [b.z=0]
* @returns {object} ca * a + cb * b
*/
lincomb: function (ca, a, cb, b) {
a.x = ca * (a.x || 0) + cb * (b.x || 0);
a.y = ca * (a.y || 0) + cb * (b.y || 0);
a.z = ca * (a.x || 0) + cb * (b.x || 0);
return a;
},
/**
* Element-wise product of two coordinate-like object. Mutates
* the first object. Note the default values for ``b``, which
* are intended to used as a anisotropic scaling factors.
*
* a <= a * b^pow
*
* @param {object} a
* @param {number} [a.x=0]
* @param {number} [a.y=0]
* @param {number} [a.z=0]
* @param {object} b
* @param {number} [b.x=1]
* @param {number} [b.y=1]
* @param {number} [b.z=1]
* @param {number} [pow=1]
* @returns {object} a * b^pow
*/
scale: function (a, b, pow) {
a.x = (a.x || 0) * Math.pow(b.x || 1, pow);
a.y = (a.y || 0) * Math.pow(b.y || 1, pow);
a.z = (a.z || 0) * Math.pow(b.z || 1, pow);
return a;
},
/**
* Compare two arrays and return if their contents are equal.
* @param {array} a1 first array to compare
* @param {array} a2 second array to compare
* @returns {boolean} true if the contents of the arrays are equal.
*/
compareArrays: function (a1, a2) {
return (a1.length === a2.length && a1.every(function (el, idx) {
return el === a2[idx];
}));
},
/**
* Create a vec3 that is always an array. This should only be used if it
* will not be used in a WebGL context. Plain arrays usually use 64-bit
* float values, whereas vec3 defaults to 32-bit floats.
*
* @returns {Array} zeroed-out vec3 compatible array.
*/
vec3AsArray: function () {
return [0, 0, 0];
},
/**
* Create a mat4 that is always an array. This should only be used if it
* will not be used in a WebGL context. Plain arrays usually use 64-bit
* float values, whereas mat4 defaults to 32-bit floats.
*
* @returns {Array} identity mat4 compatible array.
*/
mat4AsArray: function () {
return [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
];
},
/**
* Get a buffer for a vgl geometry source. If a buffer already exists and
* is the correct size, return it. Otherwise, allocate a new buffer; any
* data in an old buffer is discarded.
*
* @param geom: the geometry to reference and modify.
* @param srcName: the name of the source.
* @param len: the number of elements for the array.
* @returns {Float32Array}
*/
getGeomBuffer: function (geom, srcName, len) {
var src = geom.sourceByName(srcName), data;
data = src.data();
if (data instanceof Float32Array && data.length === len) {
return data;
}
data = new Float32Array(len);
src.setData(data);
return data;
}
};
geo.util.cssColors = {
aliceblue: 0xf0f8ff,
antiquewhite: 0xfaebd7,
aqua: 0x00ffff,
aquamarine: 0x7fffd4,
azure: 0xf0ffff,
beige: 0xf5f5dc,
bisque: 0xffe4c4,
black: 0x000000,
blanchedalmond: 0xffebcd,
blue: 0x0000ff,
blueviolet: 0x8a2be2,
brown: 0xa52a2a,
burlywood: 0xdeb887,
cadetblue: 0x5f9ea0,
chartreuse: 0x7fff00,
chocolate: 0xd2691e,
coral: 0xff7f50,
cornflowerblue: 0x6495ed,
cornsilk: 0xfff8dc,
crimson: 0xdc143c,
cyan: 0x00ffff,
darkblue: 0x00008b,
darkcyan: 0x008b8b,
darkgoldenrod: 0xb8860b,
darkgray: 0xa9a9a9,
darkgreen: 0x006400,
darkgrey: 0xa9a9a9,
darkkhaki: 0xbdb76b,
darkmagenta: 0x8b008b,
darkolivegreen: 0x556b2f,
darkorange: 0xff8c00,
darkorchid: 0x9932cc,
darkred: 0x8b0000,
darksalmon: 0xe9967a,
darkseagreen: 0x8fbc8f,
darkslateblue: 0x483d8b,
darkslategray: 0x2f4f4f,
darkslategrey: 0x2f4f4f,
darkturquoise: 0x00ced1,
darkviolet: 0x9400d3,
deeppink: 0xff1493,
deepskyblue: 0x00bfff,
dimgray: 0x696969,
dimgrey: 0x696969,
dodgerblue: 0x1e90ff,
firebrick: 0xb22222,
floralwhite: 0xfffaf0,
forestgreen: 0x228b22,
fuchsia: 0xff00ff,
gainsboro: 0xdcdcdc,
ghostwhite: 0xf8f8ff,
gold: 0xffd700,
goldenrod: 0xdaa520,
gray: 0x808080,
green: 0x008000,
greenyellow: 0xadff2f,
grey: 0x808080,
honeydew: 0xf0fff0,
hotpink: 0xff69b4,
indianred: 0xcd5c5c,
indigo: 0x4b0082,
ivory: 0xfffff0,
khaki: 0xf0e68c,
lavender: 0xe6e6fa,
lavenderblush: 0xfff0f5,
lawngreen: 0x7cfc00,
lemonchiffon: 0xfffacd,
lightblue: 0xadd8e6,
lightcoral: 0xf08080,
lightcyan: 0xe0ffff,
lightgoldenrodyellow: 0xfafad2,
lightgray: 0xd3d3d3,
lightgreen: 0x90ee90,
lightgrey: 0xd3d3d3,
lightpink: 0xffb6c1,
lightsalmon: 0xffa07a,
lightseagreen: 0x20b2aa,
lightskyblue: 0x87cefa,
lightslategray: 0x778899,
lightslategrey: 0x778899,
lightsteelblue: 0xb0c4de,
lightyellow: 0xffffe0,
lime: 0x00ff00,
limegreen: 0x32cd32,
linen: 0xfaf0e6,
magenta: 0xff00ff,
maroon: 0x800000,
mediumaquamarine: 0x66cdaa,
mediumblue: 0x0000cd,
mediumorchid: 0xba55d3,
mediumpurple: 0x9370db,
mediumseagreen: 0x3cb371,
mediumslateblue: 0x7b68ee,
mediumspringgreen: 0x00fa9a,
mediumturquoise: 0x48d1cc,
mediumvioletred: 0xc71585,
midnightblue: 0x191970,
mintcream: 0xf5fffa,
mistyrose: 0xffe4e1,
moccasin: 0xffe4b5,
navajowhite: 0xffdead,
navy: 0x000080,
oldlace: 0xfdf5e6,
olive: 0x808000,
olivedrab: 0x6b8e23,
orange: 0xffa500,
orangered: 0xff4500,
orchid: 0xda70d6,
palegoldenrod: 0xeee8aa,
palegreen: 0x98fb98,
paleturquoise: 0xafeeee,
palevioletred: 0xdb7093,
papayawhip: 0xffefd5,
peachpuff: 0xffdab9,
peru: 0xcd853f,
pink: 0xffc0cb,
plum: 0xdda0dd,
powderblue: 0xb0e0e6,
purple: 0x800080,
red: 0xff0000,
rosybrown: 0xbc8f8f,
royalblue: 0x4169e1,
saddlebrown: 0x8b4513,
salmon: 0xfa8072,
sandybrown: 0xf4a460,
seagreen: 0x2e8b57,
seashell: 0xfff5ee,
sienna: 0xa0522d,
silver: 0xc0c0c0,
skyblue: 0x87ceeb,
slateblue: 0x6a5acd,
slategray: 0x708090,
slategrey: 0x708090,
snow: 0xfffafa,
springgreen: 0x00ff7f,
steelblue: 0x4682b4,
tan: 0xd2b48c,
teal: 0x008080,
thistle: 0xd8bfd8,
tomato: 0xff6347,
turquoise: 0x40e0d0,
violet: 0xee82ee,
wheat: 0xf5deb3,
white: 0xffffff,
whitesmoke: 0xf5f5f5,
yellow: 0xffff00,
yellowgreen: 0x9acd32
};
}());
//////////////////////////////////////////////////////////////////////////////
/*
* Includes several support classes adapted from wigglemaps.
*
* https://github.com/dotskapes/wigglemaps
*
* Copyright 2013 Preston and Krejci (dotSkapes Virtual Lab)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//////////////////////////////////////////////////////////////////////////////
(function () {
'use strict';
var RangeNode = function (elem, start, end, current) {
this.data = elem[current];
this.left = null;
this.right = null;
if (start !== current)
this.left = new RangeNode(elem, start, current - 1, parseInt((start + (current - 1)) / 2, 10));
if (end !== current)
this.right = new RangeNode(elem, current + 1, end, parseInt((end + (current + 1)) / 2, 10));
this.elem = elem;
this.start = start;
this.end = end;
this.subtree = null; /* This is populated as needed */
this.search = rangeNodeSearch;
};
var rangeNodeSearch = function (result, box) {
var m_this = this;
var xrange = function (b) {
return (b.x_in(m_this.elem[m_this.start]) &&
b.x_in(m_this.elem[m_this.end]));
};
var yrange = function (b, start, end) {
return (b.y_in(m_this.subtree[start]) &&
b.y_in(m_this.subtree[end]));
};
var subquery = function (result, box, start, end, current) {
if (yrange(box, start, end)) {
for (var i = start; i <= end; i ++) {
result.push(m_this.subtree[i]);
}
return;
}
if (box.y_in(m_this.subtree[current]))
result.push(m_this.subtree[current]);
if (box.y_left(m_this.subtree[current])){
if (current !== end)
subquery(result, box, current + 1, end, parseInt((end + (current + 1)) / 2, 10));
} else if (box.x_right(m_this.subtree[current])) {
if (current !== start)
subquery(result, box, start, current - 1, parseInt((start + (current - 1)) / 2, 10));
} else {
if (current !== end)
subquery(result, box, current + 1, end, parseInt((end + (current + 1)) / 2, 10));
if (current !== start)
subquery(result, box, start, current - 1, parseInt((start + (current - 1)) / 2, 10));
}
};
if (xrange(box)) {
if (!this.subtree) {
this.subtree = this.elem.slice(this.start, this.end + 1);
this.subtree.sort(function (a, b) {
return a.y - b.y;
});
}
subquery(result, box, 0, this.subtree.length - 1, parseInt((this.subtree.length - 1) / 2, 10));
return;
} else {
if (box.contains(this.data))
result.push(this.data);
if (box.x_left(this.data)) {
if (this.right)
this.right.search(result, box);
} else if (box.x_right(this.data)) {
if (this.left)
this.left.search(result, box);
} else {
if (this.left)
this.left.search(result, box);
if (this.right)
this.right.search(result, box);
}
}
};
var RangeTree = function (elem) {
elem.sort(function (a, b) {
return a.x - b.x;
});
if (elem.length > 0)
this.root = new RangeNode(elem, 0, elem.length - 1, parseInt((elem.length - 1) / 2, 10));
else
this.root = null;
this.search = function (_box) {
if (!this.root)
return [];
//var box = new Box (min, max);
var box = _box.clone ();
var result = [];
this.root.search (result, box);
return result;
};
};
var Box = function (v1, v2) {
this.min = v1.clone ();
this.max = v2.clone ();
this.contains = function (p) {
return (v1.x <= p.x) && (v2.x >= p.x) && (v1.y <= p.y) && (v2.y >= p.y);
};
this.x_in = function (p) {
return (v1.x <= p.x) && (v2.x >= p.x);
};
this.x_left = function (p) {
return (v1.x >= p.x);
};
this.x_right = function (p) {
return (v2.x <= p.x);
};
this.y_in = function (p) {
return (v1.y <= p.y) && (v2.y >= p.y);
};
this.y_left = function (p) {
return (v1.y >= p.y);
};
this.y_right = function (p) {
return (v2.y <= p.y);
};
this.area = function () {
return (this.max.x - this.min.x) * (this.max.y - this.min.y);
};
this.height = function () {
return this.max.y - this.min.y;
};
this.width = function () {
return this.max.x - this.min.x;
};
this.vertex = function (index) {
switch (index) {
case 0:
return this.min.clone ();
case 1:
return new vect (this.max.x, this.min.y);
case 2:
return this.max.clone ();
case 3:
return new vect (this.min.x, this.max.y);
default:
throw "Index out of bounds: " + index ;
}
};
this.intersects = function (box) {
for (var i = 0; i < 4; i ++) {
for (var j = 0; j < 4; j ++) {
if (vect.intersects (this.vertex (i), this.vertex ((i + 1) % 4),
box.vertex (j), box.vertex ((j + 1) % 4)))
return true;
}
}
if (this.contains (box.min) &&
this.contains (box.max) &&
this.contains (new vect (box.min.x, box.max.y)) &&
this.contains (new vect (box.max.x, box.min.y)))
return true;
if (box.contains (this.min) &&
box.contains (this.max) &&
box.contains (new vect (this.min.x, this.max.y)) &&
box.contains (new vect (this.max.x, this.min.y)))
return true;
return false;
};
this.union = function (b) {
this.min.x = Math.min (this.min.x, b.min.x);
this.min.y = Math.min (this.min.y, b.min.y);
this.max.x = Math.max (this.max.x, b.max.x);
this.max.y = Math.max (this.max.y, b.max.y);
};
this.centroid = function () {
return new vect ((this.max.x + this.min.x) / 2, (this.max.y + this.min.y) / 2);
};
this.clone = function () {
return new Box (v1, v2);
};
};
// A basic vector type. Supports standard 2D vector operations
var Vector2D = function (x, y) {
this.x = x;
this.y = y;
this.add = function (v) {
this.x += v.x;
this.y += v.y;
return this;
};
this.sub = function (v) {
this.x -= v.x;
this.y -= v.y;
return this;
};
this.scale = function (s) {
this.x *= s;
this.y *= s;
return this;
};
this.length = function () {
return Math.sqrt (this.x * this.x + this.y * this.y);
};
this.normalize = function () {
var scale = this.length ();
if (scale === 0)
return this;
this.x /= scale;
this.y /= scale;
return this;
};
this.div = function (v) {
this.x /= v.x;
this.y /= v.y;
return this;
};
this.floor = function () {
this.x = Math.floor (this.x);
this.y = Math.floor (this.y);
return this;
};
this.zero = function (tol) {
tol = tol || 0;
return (this.length() <= tol);
};
this.dot = function (v) {
return (this.x * v.x) + (this.y * v.y);
};
this.cross = function (v) {
return (this.x * v.y) - (this.y * v.x);
};
this.rotate = function (omega) {
var cos = Math.cos (omega);
var sin = Math.sin (omega);
xp = cos * this.x - sin * this.y;
yp = sin * this.x + cos * this.y;
this.x = xp;
this.y = yp;
return this;
};
this.clone = function () {
return new Vector2D (this.x, this.y);
};
this.array = function () {
return [this.x, this.y];
};
};
// A shortcut for the vector constructor
function vect (x, y) {
return new Vector2D (x, y);
}
// Shorthand operations for vectors for operations that make new vectors
vect.scale = function (v, s) {
return v.clone ().scale (s);
};
vect.add = function (v1, v2) {
return v1.clone ().add (v2);
};
vect.sub = function (v1, v2) {
return v1.clone ().sub (v2);
};
vect.dist = function (v1, v2) {
return v1.clone ().sub (v2).length ();
};
vect.dir = function (v1, v2) {
return v1.clone ().sub (v2).normalize ();
};
vect.dot = function (v1, v2) {
return (v1.x * v2.x) + (v1.y * v2.y);
};
vect.cross = function (v1, v2) {
return (v1.x * v2.y) - (v1.y * v2.x);
};
vect.left = function (a, b, c, tol) {
if (!tol)
tol = 0;
var v1 = vect.sub (b, a);
var v2 = vect.sub (c, a);
return (vect.cross (v1, v2) >= -tol);
};
vect.intersects = function (a, b, c, d, tol) {
if (!tol)
tol = 0;
return (vect.left (a, b, c, tol) != vect.left (a, b, d, tol) &&
vect.left (c, d, b, tol) != vect.left (c, d, a, tol));
};
vect.intersect2dt = function (a, b, c, d) {
var denom = a.x * (d.y - c.y) +
b.x * (c.y - d.y) +
d.x * (b.y - a.y) +
c.x * (a.y - b.y);
if (denom === 0)
return Infinity;
var num_s = a.x * (d.y - c.y) +
c.x * (a.y - d.y) +
d.x * (c.y - a.y);
var s = num_s / denom;
var num_t = -(a.x * (c.y - b.y) +
b.x * (a.y - c.y) +
c.x * (b.y - a.y));
var t = num_t / denom;
return t;
};
vect.intersect2dpos = function (a, b, c, d) {
var denom = a.x * (d.y - c.y) +
b.x * (c.y - d.y) +
d.x * (b.y - a.y) +
c.x * (a.y - b.y);
if (denom === 0)
return Infinity;
var num_s = a.x * (d.y - c.y) +
c.x * (a.y - d.y) +
d.x * (c.y - a.y);
var s = num_s / denom;
/*var num_t = -(a.x * (c.y - b.y) +
b.x * (a.y - c.y) +
c.x * (b.y - a.y));
var t = num_t / denom;*/
var dir = vect.sub (b, a);
dir.scale (s);
return vect.add (a, dir);
};
vect.rotate = function (v, omega) {
var cos = Math.cos (omega);
var sin = Math.sin (omega);
xp = cos * v.x - sin * v.y;
yp = sin * v.x + cos * v.y;
var c = new vect (xp, yp);
return c;
};
vect.normalize = function (v) {
return v.clone ().normalize ();
};
// Export to geo.util module
geo.util.RangeTree = RangeTree;
geo.util.Box = Box;
geo.util.vect = vect;
}());
/*
markercluster plugin:
Copyright 2012 David Leaver
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Leaflet utilities:
Copyright (c) 2010-2015, Vladimir Agafonkin
Copyright (c) 2010-2011, CloudMade
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* Code taken from https://github.com/Leaflet/Leaflet.markercluster
* to support faster hierarchical clustering of features.
* @copyright 2012, David Leaver
*/
(function () {
"use strict";
var L = {};
L.Util = {
// return unique ID of an object
stamp: function (obj) {
obj._leaflet_id = obj._leaflet_id || ++L.Util.lastId;
return obj._leaflet_id;
},
lastId: 0
};
geo.util.DistanceGrid = function (cellSize) {
this._cellSize = cellSize;
this._sqCellSize = cellSize * cellSize;
this._grid = {};
this._objectPoint = {};
};
geo.util.DistanceGrid.prototype = {
addObject: function (obj, point) {
var x = this._getCoord(point.x),
y = this._getCoord(point.y),
grid = this._grid,
row = grid[y] = grid[y] || {},
cell = row[x] = row[x] || [],
stamp = L.Util.stamp(obj);
point.obj = obj;
this._objectPoint[stamp] = point;
cell.push(obj);
},
updateObject: function (obj, point) {
this.removeObject(obj);
this.addObject(obj, point);
},
//Returns true if the object was found
removeObject: function (obj, point) {
var x = this._getCoord(point.x),
y = this._getCoord(point.y),
grid = this._grid,
row = grid[y] = grid[y] || {},
cell = row[x] = row[x] || [],
i, len;
delete this._objectPoint[L.Util.stamp(obj)];
for (i = 0, len = cell.length; i < len; i++) {
if (cell[i] === obj) {
cell.splice(i, 1);
if (len === 1) {
delete row[x];
}
return true;
}
}
},
eachObject: function (fn, context) {
var i, j, k, len, row, cell, removed,
grid = this._grid;
for (i in grid) {
row = grid[i];
for (j in row) {
cell = row[j];
for (k = 0, len = cell.length; k < len; k++) {
removed = fn.call(context, cell[k]);
if (removed) {
k--;
len--;
}
}
}
}
},
getNearObject: function (point) {
var x = this._getCoord(point.x),
y = this._getCoord(point.y),
i, j, k, row, cell, len, obj, dist,
objectPoint = this._objectPoint,
closestDistSq = this._sqCellSize,
closest = null;
for (i = y - 1; i <= y + 1; i++) {
row = this._grid[i];
if (row) {
for (j = x - 1; j <= x + 1; j++) {
cell = row[j];
if (cell) {
for (k = 0, len = cell.length; k < len; k++) {
obj = cell[k];
dist = this._sqDist(
objectPoint[L.Util.stamp(obj)],
point
);
if (dist < closestDistSq) {
closestDistSq = dist;
closest = obj;
}
}
}
}
}
}
return closest;
},
/* return the point coordinates contained in the structure */
contents: function () {
return $.map(this._objectPoint, function (val) { return val; });
},
_getCoord: function (x) {
return Math.floor(x / this._cellSize);
},
_sqDist: function (p, p2) {
var dx = p2.x - p.x,
dy = p2.y - p.y;
return dx * dx + dy * dy;
}
};
})();
/**
* @file
* Using methods adapted from leaflet to cluster an array of positions
* hierarchically given an array of length scales (zoom levels).
*/
(function () {
'use strict';
/**
* This class manages a group of nearby points that are clustered as a
* single object for display purposes. The class constructor is private
* and only meant to be created by the ClusterGroup object.
*
* This is a tree-like data structure. Each node in the tree is a
* cluster containing child clusters and unclustered points.
*
* @class
* @private
*
* @param {geo.util.ClusterGroup} group The source cluster group
* @param {number} zoom The zoom level of the current node
* @param {object[]} children An array of ClusterTrees or point objects
*/
function ClusterTree(group, zoom, children) {
this._group = group;
this._zoom = zoom;
this._points = []; // Unclustered points
this._clusters = []; // Child clusters
this._count = 0; // Total number of points
this._parent = null;
this._coord = null; // The cached coordinates
var that = this;
// add the children provided in the constructor call
(children || []).forEach(function (c) {
that._add(c);
});
}
/**
* Add a point or cluster as a child to the current cluster.
* @param {object} pt A ClusterTree or point object
* @private
*/
ClusterTree.prototype._add = function (pt) {
var inc = 1;
if (pt instanceof ClusterTree) {
// add a child cluster
this._clusters.push(pt);
inc = pt._count;
} else {
this._points.push(pt);
}
pt._parent = this;
// increment the counter
this._increment(inc);
};
/**
* Increment the child counter for this and the parent.
* @param {number} inc The value to increment by
* @private
*/
ClusterTree.prototype._increment = function (inc) {
this._coord = null;
this._count += inc;
if (this._parent) {
this._parent._increment(inc);
}
};
/**
* Return the total number of child points contained in the cluster.
* @returns {number} Total points contained
*/
ClusterTree.prototype.count = function () {
return this._count;
};
/**
* Recursively call a function on all points contained in the cluster.
* Calls the function with `this` as the current ClusterTree object, and
* arguments to arguments the point object and the zoom level:
* func.call(this, point, zoom)
*/
ClusterTree.prototype.each = function (func) {
var i;
for (i = 0; i < this._points.length; i += 1) {
func.call(this, this._points[i], this._zoom);
}
for (i = 0; i < this._clusters.length; i += 1) {
this._clusters[i].each.call(
this._clusters[i],
func
);
}
};
/**
* Get the coordinates of the cluster (the mean position of all the points
* contained). This is lazily calculated and cached.
*/
ClusterTree.prototype.coords = function () {
var i, center = {x: 0, y: 0};
if (this._coord) {
return this._coord;
}
// first add up the points at the node
for (i = 0; i < this._points.length; i += 1) {
center.x += this._points[i].x;
center.y += this._points[i].y;
}
// add up the contribution from the clusters
for (i = 0; i < this._clusters.length; i += 1) {
center.x += this._clusters[i].coords().x * this._clusters[i].count();
center.y += this._clusters[i].coords().y * this._clusters[i].count();
}
return {
x: center.x / this.count(),
y: center.y / this.count()
};
};
/**
* This class manages clustering of an array of positions hierarchically.
* The algorithm and code was adapted from the Leaflet marker cluster
* plugin by David Leaver: https://github.com/Leaflet/Leaflet.markercluster
*
* @class geo.util.ClusterGroup
* @param {object} opts An options object
* @param {number} width The width of the window; used for scaling.
* @param {number} height The height of the window; used for scaling.
* @param {number} maxZoom The maximimum zoom level to calculate
* @param {number} radius Proportional to the clustering radius in pixels
*/
function C(opts, width, height) {
// store the options
this._opts = $.extend({
maxZoom: 18,
radius: 0.05
}, opts);
this._opts.width = this._opts.width || width || 256;
this._opts.height = this._opts.height || height || 256;
// generate the initial datastructures
this._clusters = {}; // clusters at each zoom level
this._points = {}; // unclustered points at each zoom level
var zoom, scl;
for (zoom = this._opts.maxZoom; zoom >= 0; zoom -= 1) {
scl = this._scaleAtLevel(zoom, this._opts.width, this._opts.height);
this._clusters[zoom] = new geo.util.DistanceGrid(scl);
this._points[zoom] = new geo.util.DistanceGrid(scl);
}
this._topClusterLevel = new ClusterTree(this, -1);
}
/**
* Returns a characteristic distance scale at a particular zoom level. This
* scale is used to control the clustering radius. When the renderer supports
* it, this call should be replaced by a calculation involving the view port
* size in point coordinates at a particular zoom level.
* @private
*/
C.prototype._scaleAtLevel = function (zoom, width, height) {
return vgl.zoomToHeight(zoom, width, height) / 2 * this._opts.radius;
};
/**
* Add a position to the cluster group.
* @protected
*/
C.prototype.addPoint = function (point) {
var zoom, closest, parent, newCluster, lastParent, z;
// start at the maximum zoom level and search for nearby
//
// 1. existing clusters
// 2. unclustered points
//
// otherwise add the point as a new unclustered point
for (zoom = this._opts.maxZoom; zoom >= 0; zoom -= 1) {
// find near cluster
closest = this._clusters[zoom].getNearObject(point);
if (closest) {
// add the point to the cluster and return
closest._add(point);
return;
}
// find near point
closest = this._points[zoom].getNearObject(point);
if (closest) {
parent = closest._parent;
if (parent) {
// remove the point from the parent
for (z = parent._points.length - 1; z >= 0; z -= 1) {
if (parent._points[z] === closest) {
parent._points.splice(z, 1);
parent._increment(-1);
break;
}
}
}
if (!parent) {
$.noop();
}
// create a new cluster with these two points
newCluster = new ClusterTree(this, zoom, [closest, point]);
this._clusters[zoom].addObject(newCluster, newCluster.coords());
// create intermediate parent clusters that don't exist
lastParent = newCluster;
for (z = zoom - 1; z > parent._zoom; z -= 1) {
lastParent = new ClusterTree(this, z, [lastParent]);
this._clusters[z].addObject(lastParent, lastParent.coords());
}
parent._add(lastParent);
// remove closest from this zoom level and any above (replace with newCluster)
for (z = zoom; z >= 0; z -= 1) {
if (!this._points[z].removeObject(closest, closest)) {
break;
}
}
return;
}
// add an unclustered point
this._points[zoom].addObject(point, point);
}
// otherwise add to the top
this._topClusterLevel._add(point);
};
/**
* Return the unclustered points contained at a given zoom level.
* @param {number} zoom The zoom level
* @return {object[]} The array of unclustered points
*/
C.prototype.points = function (zoom) {
zoom = Math.min(Math.max(Math.floor(zoom), 0), this._opts.maxZoom - 1);
return this._points[Math.floor(zoom)].contents();
};
/**
* Return the clusters contained at a given zoom level.
* @param {number} zoom The zoom level
* @return {ClusterTree[]} The array of clusters
*/
C.prototype.clusters = function (zoom) {
zoom = Math.min(Math.max(Math.floor(zoom), 0), this._opts.maxZoom - 1);
return this._clusters[Math.floor(zoom)].contents();
};
geo.util.ClusterGroup = C;
})();
(function () {
'use strict';
geo.util.scale = {
d3: typeof d3 !== 'undefined' ? d3.scale : undefined
};
})();
/**
* @file
* Based on the following jquery throttle / debounce plugin:
*
* jQuery throttle / debounce - v1.1 - 3/7/2010
* http://benalman.com/projects/jquery-throttle-debounce-plugin/
*
* @copyright 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*
* The implementation included here is modified to support a callback
* method that can accumulate values between actual invocations of
* the throttled method.
*/
(function (window) {
'use strict';
// Internal method reference.
var _throttle;
/**
* Throttle execution of a function. Especially useful for rate limiting
* execution of handlers on events like resize and scroll. If you want to
* rate-limit execution of a function to a single time see
* {@link geo.util.debounce}.
*
* In this visualization, | is a throttled-function call and X is the actual
* callback execution:
*
* ::
* Throttled with `no_trailing` specified as false or unspecified:
* ||||||||||||||||||||||||| (pause) |||||||||||||||||||||||||
* X X X X X X X X X X X X
*
* Throttled with `no_trailing` specified as true:
* ||||||||||||||||||||||||| (pause) |||||||||||||||||||||||||
* X X X X X X X X X X
*
* @function geo.util.throttle
* @param {number} delay A zero-or-greater delay in milliseconds. For event
* callbacks, values around 100 or 250 (or even higher) are most useful.
* @param {boolean} [no_trailing=false] If no_trailing is
* true, callback will only execute every `delay` milliseconds while the
* throttled-function is being called. If no_trailing is false or
* unspecified, callback will be executed one final time after the last
* throttled-function call. (After the throttled-function has not been
* called for `delay` milliseconds, the internal counter is reset)
* @param {function} callback A function to be executed after `delay`
* milliseconds. The `this` context and all arguments are passed through,
* as-is, to `callback` when the throttled-function is executed.
* @param {function} [accumulator] A function to be executed (synchronously)
* during **each** call to the wrapped function. Typically, this
* this method is used to accumulate values that the callback uses
* when it finally executes.
*
* @returns {function} The throttled version of `callback`
*
* @example
* var throttled = geo.util.throttle( delay, [ no_trailing, ] callback );
* $('selector').bind( 'someevent', throttled );
* $('selector').unbind( 'someevent', throttled );
*/
geo.util.throttle = function (delay, no_trailing,
callback, accumulator, debounce_mode) {
// After wrapper has stopped being called, this timeout ensures that
// `callback` is executed at the proper times in `throttle` and `end`
// debounce modes.
var timeout_id,
// Keep track of the last time `callback` was executed.
last_exec = 0;
// `no_trailing` defaults to falsy.
if (typeof no_trailing !== 'boolean') {
debounce_mode = accumulator;
accumulator = callback;
callback = no_trailing;
no_trailing = undefined;
}
// accumulator defaults to no-op
if (typeof accumulator !== 'function') {
debounce_mode = accumulator;
accumulator = function () {};
}
// The `wrapper` function encapsulates all of the throttling / debouncing
// functionality and when executed will limit the rate at which `callback`
// is executed.
function wrapper() {
var that = this,
elapsed = +new Date() - last_exec,
args = arguments;
// Execute `callback` and update the `last_exec` timestamp.
function exec() {
last_exec = +new Date();
callback.apply(that, args);
}
// If `debounce_mode` is true (at_begin) this is used to clear the flag
// to allow future `callback` executions.
function clear() {
timeout_id = undefined;
}
// always call the accumulator first
accumulator.apply(that, args);
if (debounce_mode && !timeout_id) {
// Since `wrapper` is being called for the first time and
// `debounce_mode` is true (at_begin), execute `callback`.
exec();
}
// Clear any existing timeout.
void (
timeout_id && clearTimeout(timeout_id)
);
if (debounce_mode === undefined && elapsed > delay) {
// In throttle mode, if `delay` time has been exceeded, execute
// `callback`.
exec();
} else if (no_trailing !== true) {
// In trailing throttle mode, since `delay` time has not been
// exceeded, schedule `callback` to execute `delay` ms after most
// recent execution.
//
// If `debounce_mode` is true (at_begin), schedule `clear` to execute
// after `delay` ms.
//
// If `debounce_mode` is false (at end), schedule `callback` to
// execute after `delay` ms.
timeout_id = setTimeout(
debounce_mode ?
clear :
exec,
debounce_mode === undefined ?
delay - elapsed :
delay
);
}
}
// Return the wrapper function.
return wrapper;
};
_throttle = geo.util.throttle;
/**
* Debounce execution of a function. Debouncing, unlike throttling,
* guarantees that a function is only executed a single time, either at the
* very beginning of a series of calls, or at the very end. If you want to
* simply rate-limit execution of a function, see the <jQuery.throttle>
* method.
*
* In this visualization, | is a debounced-function call and X is the actual
* callback execution:
*
* ::
*
* Debounced with `at_begin` specified as false or unspecified:
* ||||||||||||||||||||||||| (pause) |||||||||||||||||||||||||
* X X
*
* Debounced with `at_begin` specified as true:
* ||||||||||||||||||||||||| (pause) |||||||||||||||||||||||||
* X X
*
*
* @param {number} delay A zero-or-greater delay in milliseconds. For event
* callbacks, values around 100 or 250 (or even higher) are most useful.
* @param {boolean} [at_begin=false] If at_begin is false or
* unspecified, callback will only be executed `delay` milliseconds after
* the last debounced-function call. If at_begin is true, callback will be
* executed only at the first debounced-function call. (After the
* throttled-function has not been called for `delay` milliseconds, the
* internal counter is reset)
* @param {function} callback A function to be executed after delay milliseconds.
* The `this` context and all arguments are passed through, as-is, to
* `callback` when the debounced-function is executed.
* @param {function} [accumulator] A function to be executed (synchronously)
* during **each** call to the wrapped function. Typically, this
* this method is used to accumulate values that the callback uses
* when it finally executes.
*
* @returns {function} A new, debounced, function.
*
* @example
* var debounced = geo.util.debounce( delay, [ at_begin, ] callback );
* $('selector').bind( 'someevent', debounced );
* $('selector').unbind( 'someevent', debounced );
*
*/
geo.util.debounce = function (delay, at_begin, callback, accumulator) {
if (typeof at_begin !== 'boolean') {
accumulator = callback;
callback = at_begin;
at_begin = false;
}
accumulator = accumulator || function () {};
return _throttle(delay, false, callback, accumulator, !!at_begin);
};
})(this);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class object
*
* @class
* @extends vgl.object
* @returns {geo.object}
*/
//////////////////////////////////////////////////////////////////////////////
geo.object = function () {
'use strict';
if (!(this instanceof geo.object)) {
return new geo.object();
}
var m_this = this,
m_eventHandlers = {},
m_idleHandlers = [],
m_promiseCount = 0;
//////////////////////////////////////////////////////////////////////////////
/**
* Bind a handler that will be called once when all internal promises are
* resolved.
*
* @param {function} handler A function taking no arguments
* @returns {geo.object[]|geo.object} this
*/
//////////////////////////////////////////////////////////////////////////////
this.onIdle = function (handler) {
if (m_promiseCount) {
m_idleHandlers.push(handler);
} else {
handler();
}
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Add a new promise object preventing idle event handlers from being called
* until it is resolved.
*
* @param {Promise} promise A promise object
*/
//////////////////////////////////////////////////////////////////////////////
this.addPromise = function (promise) {
// called on any resolution of the promise
function onDone() {
m_promiseCount -= 1;
if (!m_promiseCount) {
m_idleHandlers.splice(0, m_idleHandlers.length)
.forEach(function (handler) {
handler();
});
}
}
m_promiseCount += 1;
promise.then(onDone, onDone);
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Bind an event handler to this object
*
* @param {String} event
* An event from {geo.events}
* @param {function} handler
* A function that will be called when ``event`` is triggered. The
* function will be given an event object as a first parameter and
* optionally a second argument provided by the triggerer.
*/
//////////////////////////////////////////////////////////////////////////////
this.geoOn = function (event, handler) {
if (Array.isArray(event)) {
event.forEach(function (e) {
m_this.geoOn(e, handler);
});
return m_this;
}
if (!m_eventHandlers.hasOwnProperty(event)) {
m_eventHandlers[event] = [];
}
m_eventHandlers[event].push(handler);
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Trigger an event (or events) on this object and call all handlers
*
* @param {String} event An event from {geo.event}
* @param {Object} args An optional argument to pass to handlers
*/
//////////////////////////////////////////////////////////////////////////////
this.geoTrigger = function (event, args) {
// if we have an array of events, recall with single events
if (Array.isArray(event)) {
event.forEach(function (e) {
m_this.geoTrigger(e, args);
});
return m_this;
}
// append the event type to the argument object
args = args || {};
args.event = event;
if (m_eventHandlers.hasOwnProperty(event)) {
m_eventHandlers[event].forEach(function (handler) {
handler.call(m_this, args);
});
}
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Remove handlers from an event (or an array of events). If no event is
* provided all hanlders will be removed.
*
* @param {string?} event An event from {geo.events}
* @param {object?} arg A function or array of functions to remove from the events
* or if falsey remove all handlers from the events
*/
//////////////////////////////////////////////////////////////////////////////
this.geoOff = function (event, arg) {
if (event === undefined) {
m_eventHandlers = {};
m_idleHandlers = [];
m_promiseCount = 0;
}
if (Array.isArray(event)) {
event.forEach(function (e) {
m_this.geoOff(e, arg);
});
return m_this;
}
if (!arg) {
m_eventHandlers[event] = [];
} else if (Array.isArray(arg)) {
arg.forEach(function (handler) {
m_this.geoOff(event, handler);
});
return m_this;
}
// What do we do if the handler is not already bound?
// ignoring for now...
if (m_eventHandlers.hasOwnProperty(event)) {
m_eventHandlers[event] = m_eventHandlers[event].filter(function (f) {
return f !== arg;
}
);
}
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Free all resources and destroy the object.
*/
//////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.geoOff();
};
vgl.object.call(this);
return this;
};
inherit(geo.object, vgl.object);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class sceneObject, which extends the object's
* event handling with a tree-based event propagation.
*
* @class
* @extends geo.object
* @returns {geo.sceneObject}
*/
//////////////////////////////////////////////////////////////////////////////
geo.sceneObject = function (arg) {
'use strict';
if (!(this instanceof geo.sceneObject)) {
return new geo.sceneObject();
}
geo.object.call(this, arg);
var m_this = this,
m_parent = null,
m_children = [],
s_exit = this._exit,
s_trigger = this.geoTrigger,
s_addPromise = this.addPromise,
s_onIdle = this.onIdle;
//////////////////////////////////////////////////////////////////////////////
/**
* Override object.addPromise to propagate up the scene tree.
*/
//////////////////////////////////////////////////////////////////////////////
this.addPromise = function (promise) {
if (m_parent) {
m_parent.addPromise(promise);
} else {
s_addPromise(promise);
}
};
//////////////////////////////////////////////////////////////////////////////
/**
* Override object.onIdle to propagate up the scene tree.
*/
//////////////////////////////////////////////////////////////////////////////
this.onIdle = function (handler) {
if (m_parent) {
m_parent.onIdle(handler);
} else {
s_onIdle(handler);
}
};
//////////////////////////////////////////////////////////////////////////////
/**
* Get/set parent of the object
* @param {?geo.sceneObject} parent
*/
//////////////////////////////////////////////////////////////////////////////
this.parent = function (arg) {
if (arg === undefined) {
return m_parent;
}
m_parent = arg;
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Add a child (or an array of children) to the object
*/
//////////////////////////////////////////////////////////////////////////////
this.addChild = function (child) {
if (Array.isArray(child)) {
child.forEach(m_this.addChild);
return m_this;
}
child.parent(m_this);
m_children.push(child);
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Remove a child (or array of children) from the object
*/
//////////////////////////////////////////////////////////////////////////////
this.removeChild = function (child) {
if (Array.isArray(child)) {
child.forEach(m_this.removeChild);
return m_this;
}
m_children = m_children.filter(function (c) { return c !== child; });
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Get an array of child objects
*/
//////////////////////////////////////////////////////////////////////////////
this.children = function () {
return m_children.slice();
};
//////////////////////////////////////////////////////////////////////////////
/**
* Force redraw of a scene object, to be implemented by subclasses.
* Base class just calls draw of child objects.
*/
//////////////////////////////////////////////////////////////////////////////
this.draw = function (arg) {
m_this.children().forEach(function (child) {
child.draw(arg);
});
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Trigger an event (or events) on this object and call all handlers.
* @param {String} event the event to trigger
* @param {Object} args arbitrary argument to pass to the handler
* @param {Boolean} childrenOnly if true, only propagate down the tree
*/
//////////////////////////////////////////////////////////////////////////////
this.geoTrigger = function (event, args, childrenOnly) {
var geoArgs;
args = args || {};
geoArgs = args.geo || {};
args.geo = geoArgs;
// stop propagation if requested by the handler
if (geoArgs.stopPropagation) {
return m_this;
}
// If the event was not triggered by the parent, just propagate up the tree
if (!childrenOnly && m_parent && geoArgs._triggeredBy !== m_parent) {
geoArgs._triggeredBy = m_this;
m_parent.geoTrigger(event, args);
return m_this;
}
// call the object's own handlers
s_trigger.call(m_this, event, args);
// stop propagation if requested by the handler
if (geoArgs.stopPropagation) {
return m_this;
}
// trigger the event on the children
m_children.forEach(function (child) {
if (child.geoTrigger) {
geoArgs._triggeredBy = m_this;
child.geoTrigger(event, args);
}
});
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Free all resources and destroy the object.
*/
//////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.children = [];
delete m_this.parent;
s_exit();
};
return this;
};
inherit(geo.sceneObject, geo.object);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class timestamp
*
* @class
* @extends vgl.timestamp
* @returns {geo.timestamp}
*/
//////////////////////////////////////////////////////////////////////////////
geo.timestamp = function () {
'use strict';
if (!(this instanceof geo.timestamp)) {
return new geo.timestamp();
}
vgl.timestamp.call(this);
};
inherit(geo.timestamp, vgl.timestamp);
//////////////////////////////////////////////////////////////////////////////
/**
* This purpose of this class is to provide a generic interface for computing
* coordinate transformationss. The interface is taken from the proj4js,
* which also provides the geospatial projection implementation. The
* interface is intentionally simple to allow for custom, non-geospatial use
* cases. For further details, see http://proj4js.org/
*
* The default transforms lat/long coordinates into web mercator
* for use with standard tile sets.
*
* This class is intended to be extended in the future to support 2.5 and 3
* dimensional transformations. The forward/inverse methods take optional
* z values that are ignored in current mapping context, but will in the
* future perform more general 3D transformations.
*
* @class
* @extends geo.object
* @param {object} options Constructor options
* @param {string} options.source A proj4 string for the source projection
* @param {string} options.target A proj4 string for the target projection
* @returns {geo.transform}
*/
//////////////////////////////////////////////////////////////////////////////
geo.transform = function (options) {
'use strict';
if (!(this instanceof geo.transform)) {
return new geo.transform(options);
}
var m_this = this,
m_proj, // The raw proj4js object
m_source, // The source projection
m_target; // The target projection
/**
* Generate the internal proj4 object.
* @private
*/
function generate_proj4() {
m_proj = new proj4(
m_this.source(),
m_this.target()
);
}
/**
* Get/Set the source projection
*/
this.source = function (arg) {
if (arg === undefined) {
return m_source || 'EPSG:4326';
}
m_source = arg;
generate_proj4();
return m_this;
};
/**
* Get/Set the target projection
*/
this.target = function (arg) {
if (arg === undefined) {
return m_target || 'EPSG:3857';
}
m_target = arg;
generate_proj4();
return m_this;
};
/**
* Perform a forward transformation (source -> target)
* @protected
*
* @param {object} point The point coordinates
* @param {number} point.x The x-coordinate (i.e. longitude)
* @param {number} point.y The y-coordinate (i.e. latitude)
* @param {number} [point.z=0] The z-coordinate (i.e. elevation)
*
* @returns {object} A point object in the target coordinates
*/
this._forward = function (point) {
var pt = m_proj.forward(point);
pt.z = point.z || 0;
return pt;
};
/**
* Perform an inverse transformation (target -> source)
* @protected
*
* @param {object} point The point coordinates
* @param {number} point.x The x-coordinate (i.e. longitude)
* @param {number} point.y The y-coordinate (i.e. latitude)
* @param {number} [point.z=0] The z-coordinate (i.e. elevation)
*
* @returns {object} A point object in the source coordinates
*/
this._inverse = function (point) {
var pt = m_proj.inverse(point);
pt.z = point.z || 0;
return pt;
};
/**
* Perform a forward transformation (source -> target) in place
*
* @param {object[]} point The point coordinates or array of points
* @param {number} point.x The x-coordinate (i.e. longitude)
* @param {number} point.y The y-coordinate (i.e. latitude)
* @param {number} [point.z=0] The z-coordinate (i.e. elevation)
*
* @returns {object} A point object or array in the target coordinates
*/
this.forward = function (point) {
if (Array.isArray(point)) {
return point.map(m_this._forward);
}
return m_this._forward(point);
};
/**
* Perform an inverse transformation (target -> source) in place
* @protected
*
* @param {object[]} point The point coordinates or array of points
* @param {number} point.x The x-coordinate (i.e. longitude)
* @param {number} point.y The y-coordinate (i.e. latitude)
* @param {number} [point.z=0] The z-coordinate (i.e. elevation)
*
* @returns {object} A point object in the source coordinates
*/
this.inverse = function (point) {
if (Array.isArray(point)) {
return point.map(m_this._inverse);
}
return m_this._inverse(point);
};
// Set defaults given by the constructor
options = options || {};
try {
this.source(options.source);
} catch (err) {
console.error('Can\'t use transform source: ' + options.source);
this.source('EPSG:4326');
}
try {
this.target(options.target);
} catch (err) {
console.error('Can\'t use transform target: ' + options.target);
this.target('EPSG:3857');
}
geo.object.call(this);
return this;
};
/**
* Transform an array of coordinates from one projection into another. The
* transformation may occur in place (modifying the input coordinate array),
* depending on the input format. The coordinates can be an object with x, y,
* and (optionally z) or an array of 2 or 3 values, or an array of either of
* those, or a single flat array with 2 or 3 components per coordinate. Arrays
* are always modified in place. Individual point objects are not altered; new
* point objects are returned unless no transform is needed.
*
* @param {string} srcPrj The source projection
* @param {string} tgtPrj The destination projection
* @param {geoPosition[]} coordinates An array of coordinate objects
* @param {number} numberOfComponents for flat arrays, either 2 or 3.
*
* @returns {geoPosition[]} The transformed coordinates
*/
geo.transform.transformCoordinates = function (
srcPrj, tgtPrj, coordinates, numberOfComponents) {
'use strict';
if (srcPrj === tgtPrj) {
return coordinates;
}
var i, count, offset, xAcc, yAcc, zAcc, writer, output, projPoint,
trans = geo.transform({source: srcPrj, target: tgtPrj});
/// Default Z accessor
zAcc = function () {
return 0.0;
};
/// Helper methods
function handleArrayCoordinates() {
if (coordinates[0] instanceof Array) {
if (coordinates[0].length === 2) {
xAcc = function (index) {
return coordinates[index][0];
};
yAcc = function (index) {
return coordinates[index][1];
};
writer = function (index, x, y) {
output[index] = [x, y];
};
} else if (coordinates[0].length === 3) {
xAcc = function (index) {
return coordinates[index][0];
};
yAcc = function (index) {
return coordinates[index][1];
};
zAcc = function (index) {
return coordinates[index][2];
};
writer = function (index, x, y, z) {
output[index] = [x, y, z];
};
} else {
throw 'Invalid coordinates. Requires two or three components per array';
}
} else {
if (coordinates.length === 2) {
offset = 2;
xAcc = function (index) {
return coordinates[index * offset];
};
yAcc = function (index) {
return coordinates[index * offset + 1];
};
writer = function (index, x, y) {
output[index] = x;
output[index + 1] = y;
};
} else if (coordinates.length === 3) {
offset = 3;
xAcc = function (index) {
return coordinates[index * offset];
};
yAcc = function (index) {
return coordinates[index * offset + 1];
};
zAcc = function (index) {
return coordinates[index * offset + 2];
};
writer = function (index, x, y, z) {
output[index] = x;
output[index + 1] = y;
output[index + 2] = z;
};
} else if (numberOfComponents) {
if (numberOfComponents === 2 || numberOfComponents === 3) {
offset = numberOfComponents;
xAcc = function (index) {
return coordinates[index];
};
yAcc = function (index) {
return coordinates[index + 1];
};
if (numberOfComponents === 2) {
writer = function (index, x, y) {
output[index] = x;
output[index + 1] = y;
};
} else {
zAcc = function (index) {
return coordinates[index + 2];
};
writer = function (index, x, y, z) {
output[index] = x;
output[index + 1] = y;
output[index + 2] = z;
};
}
} else {
throw 'Number of components should be two or three';
}
} else {
throw 'Invalid coordinates';
}
}
}
/// Helper methods
function handleObjectCoordinates() {
if (coordinates[0] &&
'x' in coordinates[0] &&
'y' in coordinates[0]) {
xAcc = function (index) {
return coordinates[index].x;
};
yAcc = function (index) {
return coordinates[index].y;
};
if ('z' in coordinates[0]) {
zAcc = function (index) {
return coordinates[index].z;
};
writer = function (index, x, y, z) {
output[i] = {x: x, y: y, z: z};
};
} else {
writer = function (index, x, y) {
output[index] = {x: x, y: y};
};
}
} else if (coordinates && 'x' in coordinates && 'y' in coordinates) {
xAcc = function () {
return coordinates.x;
};
yAcc = function () {
return coordinates.y;
};
if ('z' in coordinates) {
zAcc = function () {
return coordinates.z;
};
writer = function (index, x, y, z) {
output = {x: x, y: y, z: z};
};
} else {
writer = function (index, x, y) {
output = {x: x, y: y};
};
}
} else {
throw 'Invalid coordinates';
}
}
if (coordinates instanceof Array) {
output = [];
output.length = coordinates.length;
count = coordinates.length;
if (coordinates[0] instanceof Array ||
coordinates[0] instanceof Object) {
offset = 1;
if (coordinates[0] instanceof Array) {
handleArrayCoordinates();
} else if (coordinates[0] instanceof Object) {
handleObjectCoordinates();
}
} else {
handleArrayCoordinates();
}
} else if (coordinates && coordinates instanceof Object) {
count = 1;
offset = 1;
if (coordinates && 'x' in coordinates && 'y' in coordinates) {
handleObjectCoordinates();
} else {
throw 'Coordinates are not valid';
}
}
for (i = 0; i < count; i += offset) {
projPoint = trans.forward({x: xAcc(i), y: yAcc(i), z: zAcc(i)});
writer(i, projPoint.x, projPoint.y, projPoint.z);
}
return output;
};
/**
* Apply an affine transformation consisting of a translation
* then a scaling to the given coordinate array. Note, the
* transformation occurs in place so the input coordinate
* object are mutated.
*
* (Possibly extend to support rotations as well)
*
* @param {object} def
* @param {object} def.origin The transformed origin
* @param {object} def.scale The transformed scale factor
* @param {object[]} coords An array of coordinate objects
*
* @returns {object[]} The transformed coordinates
*/
geo.transform.affineForward = function (def, coords) {
'use strict';
var i, origin = def.origin, scale = def.scale || {x: 1, y: 1, z: 1};
for (i = 0; i < coords.length; i += 1) {
coords[i].x = (coords[i].x - origin.x) * scale.x;
coords[i].y = (coords[i].y - origin.y) * scale.y;
coords[i].z = ((coords[i].z || 0) - (origin.z || 0)) * scale.z;
}
return coords;
};
/**
* Apply an inverse affine transformation which is the
* inverse to {@link geo.transform.affineForward}. Note, the
* transformation occurs in place so the input coordinate
* object are mutated.
*
* (Possibly extend to support rotations as well)
*
* @param {object} def
* @param {object} def.origin The transformed origin
* @param {object} def.scale The transformed scale factor
* @param {object[]} coords An array of coordinate objects
*
* @returns {object[]} The transformed coordinates
*/
geo.transform.affineInverse = function (def, coords) {
'use strict';
var i, origin = def.origin, scale = def.scale || {x: 1, y: 1, z: 1};
for (i = 0; i < coords.length; i += 1) {
coords[i].x = coords[i].x / scale.x + origin.x;
coords[i].y = coords[i].y / scale.y + origin.y;
coords[i].z = (coords[i].z || 0) / scale.z + (origin.z || 0);
}
return coords;
};
inherit(geo.transform, geo.object);
(function () {
'use strict';
//////////////////////////////////////////////////////////////////////////////
/**
* This class defines the raw interface for a camera. At a low level, the
* camera provides a methods for converting between a map's coordinate system
* to display pixel coordinates.
*
* For the moment, all camera transforms are assumed to be expressible as
* 4x4 matrices. More general cameras may follow that break this assumption.
*
* The interface for the camera is relatively stable for "map-like" views,
* e.g. when the camera is pointing in the direction [0, 0, -1], and placed
* above the z=0 plane. More general view changes and events have not yet
* been defined.
*
* The camera emits the following events when the view changes:
*
* * {@link geo.event.camera.pan} when the camera is translated in the
* x/y plane
* * {@link geo.event.camera.zoom} when the camera is changed in a way
* that modifies the current zoom level
* * {@link geo.event.camera.view} when the visible bounds change for
* any reason
* * {@link geo.event.camera.projection} when the projection type changes
* * {@link geo.event.camera.viewport} when the viewport changes
*
* By convention, protected methods do not update the internal matrix state,
* public methods do. There are a few primary methods that are intended to
* be used by external classes to mutate the internal state:
*
* * bounds: Set the visible bounds (for initialization and zooming)
* * pan: Translate the camera in x/y by an offset (for panning)
* * viewFromCenterSizeRotation: set the camera view based on a center
* point, boundary size, and rotation angle.
*
* @class
* @extends geo.object
* @param {object?} spec Options argument
* @param {string} spec.projection One of the supported geo.camera.projection
* @param {object} spec.viewport The initial camera viewport
* @param {object} spec.viewport.width
* @param {object} spec.viewport.height
* @returns {geo.camera}
*/
//////////////////////////////////////////////////////////////////////////////
geo.camera = function (spec) {
if (!(this instanceof geo.camera)) {
return new geo.camera(spec);
}
spec = spec || {};
geo.object.call(this, spec);
/**
* The view matrix
* @protected
*/
this._view = geo.util.mat4AsArray();
/**
* The projection matrix
* @protected
*/
this._proj = geo.util.mat4AsArray();
/**
* The projection type (one of `this.constructor.projection`)
* @protected
*/
this._projection = null;
/**
* The transform matrix (view * proj)
* @protected
*/
this._transform = geo.util.mat4AsArray();
/**
* The inverse transform matrix (view * proj)^-1
* @protected
*/
this._inverse = geo.util.mat4AsArray();
/**
* Cached bounds object recomputed on demand.
* @protected
*/
this._bounds = null;
/**
* Cached "display" matrix recomputed on demand.
* @see {@link geo.camera.display}
* @protected
*/
this._display = null;
/**
* Cached "world" matrix recomputed on demand.
* @see {@link geo.camera.world}
* @protected
*/
this._world = null;
/**
* The viewport parameters size and offset.
* @property {number} height Viewport height in pixels
* @property {number} width Viewport width in pixels
* @protected
*/
this._viewport = {width: 1, height: 1};
/**
* Set up the projection matrix for the current projection type.
* @protected
*/
this._createProj = function () {
var s = this.constructor.bounds.near / this.constructor.bounds.far;
// call mat4.frustum or mat4.ortho here
if (this._projection === 'perspective') {
mat4.frustum(
this._proj,
this.constructor.bounds.left * s,
this.constructor.bounds.right * s,
this.constructor.bounds.bottom * s,
this.constructor.bounds.top * s,
-this.constructor.bounds.near,
-this.constructor.bounds.far
);
} else if (this._projection === 'parallel') {
mat4.ortho(
this._proj,
this.constructor.bounds.left,
this.constructor.bounds.right,
this.constructor.bounds.bottom,
this.constructor.bounds.top,
this.constructor.bounds.near,
this.constructor.bounds.far
);
}
};
/**
* Update the internal state of the camera on change to camera
* parameters.
* @protected
*/
this._update = function () {
this._bounds = null;
this._display = null;
this._world = null;
this._transform = geo.camera.combine(this._proj, this._view);
mat4.invert(this._inverse, this._transform);
this.geoTrigger(geo.event.camera.view, {
camera: this
});
};
/**
* Getter/setter for the view matrix.
* @note copies the matrix value on set.
*/
Object.defineProperty(this, 'view', {
get: function () {
return this._view;
},
set: function (view) {
mat4.copy(this._view, view);
this._update();
}
});
/**
* Getter/setter for the view bounds.
*
* If not provided, near and far bounds will be set to [-1, 1] by
* default. We will probably want to change this to a unit specific
* value initialized by the map when drawing true 3D objects or
* tilting the camera.
*
* Returned near/far bounds are also -1, 1 for the moment.
*/
Object.defineProperty(this, 'bounds', {
get: function () {
if (this._bounds === null) {
this._bounds = this._getBounds();
}
return this._bounds;
},
set: function (bounds) {
this._setBounds(bounds);
this._update();
}
});
/**
* Getter for the "display" matrix. This matrix converts from
* world coordinates into display coordinates. This matrix exists to
* generate matrix3d css transforms that can be used in layers that
* render on the DOM.
*/
Object.defineProperty(this, 'display', {
get: function () {
var mat;
if (this._display === null) {
mat = geo.camera.affine(
{x: 1, y: 1}, // translate to: [0, 2] x [0, 2]
{
x: this.viewport.width / 2,
y: this.viewport.height / -2
} // scale to: [0, width] x [-height, 0]
);
// applies mat to the transform (world -> normalized)
this._display = geo.camera.combine(
mat,
this._transform
);
}
return this._display;
}
});
/**
* Getter for the "world" matrix. This matrix converts from
* display coordinates into world coordinates. This is constructed
* by inverting the "display" matrix.
*/
Object.defineProperty(this, 'world', {
get: function () {
if (this._world === null) {
this._world = mat4.invert(
geo.util.mat4AsArray(),
this.display
);
}
return this._world;
}
});
/**
* Getter/setter for the projection type.
*/
Object.defineProperty(this, 'projection', {
get: function () {
return this._projection;
},
set: function (type) {
if (!this.constructor.projection[type]) {
throw new Error('Unsupported projection type: ' + type);
}
if (type !== this._projection) {
this._projection = type;
this._createProj();
this._update();
this.geoTrigger(geo.event.camera.projection, {
camera: this,
projection: type
});
}
}
});
/**
* Getter for the projection matrix (when applicable).
* This generally shouldn't be modified directly because
* the rest of the code assumes that the clipping bounds
* are [-1, -1, -1] to [1, 1, 1] in camera coordinates.
*/
Object.defineProperty(this, 'projectionMatrix', {
get: function () {
return this._proj;
}
});
/**
* Getter for the transform matrix.
*/
Object.defineProperty(this, 'transform', {
get: function () {
return this._transform;
}
});
/**
* Getter for the inverse transform matrix.
*/
Object.defineProperty(this, 'inverse', {
get: function () {
return this._inverse;
}
});
/**
* Getter/setter for the viewport.
*/
Object.defineProperty(this, 'viewport', {
get: function () {
return {width: this._viewport.width, height: this._viewport.height};
},
set: function (viewport) {
if (!(viewport.width > 0 &&
viewport.height > 0)) {
throw new Error('Invalid viewport dimensions');
}
if (viewport.width === this._viewport.width &&
viewport.height === this._viewport.height) {
return;
}
// apply scaling to the view matrix to account for the new aspect ratio
// without changing the apparent zoom level
if (this._viewport.width && this._viewport.height) {
this._scale([
this._viewport.width / viewport.width,
this._viewport.height / viewport.height,
1
]);
// translate by half the difference to keep the center the same
this._translate([
(viewport.width - this._viewport.width) / 2,
(viewport.height - this._viewport.height) / 2,
0
]);
}
this._viewport = {width: viewport.width, height: viewport.height};
this._update();
this.geoTrigger(geo.event.camera.viewport, {
camera: this,
viewport: this.viewport
});
}
});
/**
* Reset the view matrix to its initial (identity) state.
* @protected
* @returns {this} Chainable
*/
this._resetView = function () {
mat4.identity(this._view);
return this;
};
/**
* Uses `mat4.translate` to translate the camera by the given vector amount.
* @protected
* @param {vec3|Array} offset The camera translation vector
* @returns {this} Chainable
*/
this._translate = function (offset) {
mat4.translate(this._view, this._view, offset);
};
/**
* Uses `mat4.scale` to scale the camera by the given vector amount.
* @protected
* @param {vec3|Array} scale The scaling vector
* @returns {this} Chainable
*/
this._scale = function (scale) {
mat4.scale(this._view, this._view, scale);
};
/**
* Project a vec4 from world space into clipped space [-1, 1] in place
* @protected
* @param {vec4} point The point in world coordinates (mutated)
* @returns {vec4} The point in clip space coordinates
*/
this._worldToClip4 = function (point) {
return geo.camera.applyTransform(this._transform, point);
};
/**
* Project a vec4 from clipped space into world space in place
* @protected
* @param {vec4} point The point in clipped coordinates (mutated)
* @returns {vec4} The point in world space coordinates
*/
this._clipToWorld4 = function (point) {
return geo.camera.applyTransform(this._inverse, point);
};
/**
* Apply the camera's projection transform to the given point.
* @param {vec4} pt a point in clipped coordinates
* @returns {vec4} the point in normalized coordinates
*/
this.applyProjection = function (pt) {
var w;
if (this._projection === 'perspective') {
w = 1 / (pt[3] || 1);
pt[0] = w * pt[0];
pt[1] = w * pt[1];
pt[2] = w * pt[2];
pt[3] = w;
} else {
pt[3] = 1;
}
return pt;
};
/**
* Unapply the camera's projection transform from the given point.
* @param {vec4} pt a point in normalized coordinates
* @returns {vec4} the point in clipped coordinates
*/
this.unapplyProjection = function (pt) {
var w;
if (this._projection === 'perspective') {
w = pt[3] || 1;
pt[0] = w * pt[0];
pt[1] = w * pt[1];
pt[2] = w * pt[2];
pt[3] = w;
} else {
pt[3] = 1;
}
return pt;
};
/**
* Project a vec4 from world space into viewport space.
* @param {vec4} point The point in world coordinates (mutated)
* @returns {vec4} The point in display coordinates
*
* @note For the moment, this computation assumes the following:
* * point[3] > 0
* * depth range [0, 1]
*
* The clip space z and w coordinates are returned with the window
* x/y coordinates.
*/
this.worldToDisplay4 = function (point) {
// This is because z = 0 is the far plane exposed to the user, but
// internally the far plane is at -2.
point[2] -= 2;
// convert to clip space
this._worldToClip4(point);
// apply projection specific transformation
point = this.applyProjection(point);
// convert to display space
point[0] = this._viewport.width * (1 + point[0]) / 2.0;
point[1] = this._viewport.height * (1 - point[1]) / 2.0;
point[2] = (1 + point[2]) / 2.0;
return point;
};
/**
* Project a vec4 from display space into world space in place.
* @param {vec4} point The point in display coordinates (mutated)
* @returns {vec4} The point in world space coordinates
*
* @note For the moment, this computation assumes the following:
* * point[3] > 0
* * depth range [0, 1]
*/
this.displayToWorld4 = function (point) {
// convert to clip space
point[0] = 2 * point[0] / this._viewport.width - 1;
point[1] = -2 * point[1] / this._viewport.height + 1;
point[2] = 2 * point[2] - 1;
// invert projection transform
point = this.unapplyProjection(point);
// convert to world coordinates
this._clipToWorld4(point);
// move far surface to z = 0
point[2] += 2;
return point;
};
/**
* Project a point object from world space into viewport space.
* @param {object} point The point in world coordinates
* @param {number} point.x
* @param {number} point.y
* @returns {object} The point in display coordinates
*/
this.worldToDisplay = function (point) {
// define some magic numbers:
var z = 0, // z coordinate of the surface in world coordinates
w = 1; // enables perspective divide (i.e. for point conversion)
point = this.worldToDisplay4(
[point.x, point.y, z, w]
);
return {x: point[0], y: point[1], z: point[2]};
};
/**
* Project a point object from viewport space into world space.
* @param {object} point The point in display coordinates
* @param {number} point.x
* @param {number} point.y
* @returns {object} The point in world coordinates
*/
this.displayToWorld = function (point) {
// define some magic numbers:
var z = 1, // the z coordinate of the surface
w = 2; // perspective divide at z = 1
point = this.displayToWorld4(
[point.x, point.y, z, w]
);
return {x: point[0], y: point[1]};
};
/**
* Calculate the current bounds in world coordinates from the
* current view matrix. This computes a matrix vector multiplication
* so the result is cached for public facing methods.
*
* @protected
* @returns {object} bounds object
*/
this._getBounds = function () {
var ul, ur, ll, lr, bds = {};
// get corners
ul = this.displayToWorld({x: 0, y: 0});
ur = this.displayToWorld({x: this._viewport.width, y: 0});
ll = this.displayToWorld({x: 0, y: this._viewport.height});
lr = this.displayToWorld({
x: this._viewport.width,
y: this._viewport.height
});
bds.left = Math.min(ul.x, ur.x, ll.x, lr.x);
bds.bottom = Math.min(ul.y, ur.y, ll.y, lr.y);
bds.right = Math.max(ul.x, ur.x, ll.x, lr.x);
bds.top = Math.max(ul.y, ur.y, ll.y, lr.y);
return bds;
};
/**
* Sets the view matrix so that the given world bounds
* are in view. To account for the viewport aspect ratio,
* the resulting bounds may be larger in width or height than
* the requested bound, but should be centered in the frame.
*
* @protected
* @param {object} bounds
* @param {number} bounds.left
* @param {number} bounds.right
* @param {number} bounds.bottom
* @param {number} bounds.top
* @param {number?} bounds.near Currently ignored
* @param {number?} bounds.far Currently ignored
* @return {this} Chainable
*/
this._setBounds = function (bounds) {
var size = {
width: bounds.right - bounds.left,
height: bounds.top - bounds.bottom
};
var center = {
x: (bounds.left + bounds.right) / 2,
y: (bounds.bottom + bounds.top) / 2
};
this._viewFromCenterSizeRotation(center, size, 0);
return this;
};
/**
* Sets the view matrix so that the given world center is centered, at
* least a certain width and height are visible, and a rotation is applied.
* The resulting bounds may be larger in width or height than the values if
* the viewport is a different aspect ratio.
*
* @protected
* @param {object} center
* @param {number} center.x
* @param {number} center.y
* @param {object} size
* @param {number} size.width
* @param {number} size.height
* @param {number} rotation in clockwise radians. Optional
* @return {this} Chainable
*/
this._viewFromCenterSizeRotation = function (center, size, rotation) {
var translate = geo.util.vec3AsArray(),
scale = geo.util.vec3AsArray(),
c_ar, v_ar, w, h;
// reset view to the identity
this._resetView();
w = Math.abs(size.width);
h = Math.abs(size.height);
c_ar = w / h;
v_ar = this._viewport.width / this._viewport.height;
if (c_ar >= v_ar) {
// grow camera bounds vertically
h = w / v_ar;
scale[0] = 2 / w;
scale[1] = 2 / h;
} else {
// grow bounds horizontally
w = h * v_ar;
scale[0] = 2 / w;
scale[1] = 2 / h;
}
scale[2] = 1;
this._scale(scale);
if (rotation) {
this._rotate(rotation);
}
// translate to the new center.
translate[0] = -center.x;
translate[1] = -center.y;
translate[2] = 0;
this._translate(translate);
return this;
};
/**
* Public exposure of the viewFromCenterSizeRotation function.
*/
this.viewFromCenterSizeRotation = function (center, size, rotation) {
this._viewFromCenterSizeRotation(center, size, rotation);
this._update();
return this;
};
/**
* Pans the view matrix by the given amount.
*
* @param {object} offset The delta in world space coordinates.
* @param {number} offset.x
* @param {number} offset.y
* @param {number} [offset.z=0]
*/
this.pan = function (offset) {
if (!offset.x && !offset.y && !offset.z) {
return;
}
this._translate([
offset.x,
offset.y,
offset.z || 0
]);
this._update();
};
/**
* Zooms the view matrix by the given amount.
*
* @param {number} zoom The zoom scale to apply
*/
this.zoom = function (zoom) {
if (zoom === 1) {
return;
}
mat4.scale(this._view, this._view, [
zoom,
zoom,
zoom
]);
this._update();
};
/**
* Rotate the view matrix by the given amount.
*
* @param {number} rotation Counter-clockwise rotation angle in radians.
* @param {object} center Center of rotation in world space coordinates.
* @param {vec3} axis acis of rotation. Defaults to [0, 0, -1]
*/
this._rotate = function (rotation, center, axis) {
if (!rotation) {
return;
}
axis = axis || [0, 0, -1];
if (!center) {
center = [0, 0, 0];
} else if (center.x !== undefined) {
center = [center.x || 0, center.y || 0, center.z || 0];
}
var invcenter = [-center[0], -center[1], -center[2]];
mat4.translate(this._view, this._view, center);
mat4.rotate(this._view, this._view, rotation, axis);
mat4.translate(this._view, this._view, invcenter);
};
/**
* Returns a CSS transform that converts (by default) from world coordinates
* into display coordinates. This allows users of this module to
* position elements using world coordinates directly inside DOM
* elements.
*
* @note This transform will not take into account projection specific
* transforms. For perspective projections, one can use the properties
* `perspective` and `perspective-origin` to apply the projection
* in css directly.
*
* @param {string} transform The transform to return
* * display
* * world
* @returns {string} The css transform string
*/
this.css = function (transform) {
var m;
switch ((transform || '').toLowerCase()) {
case 'display':
case '':
m = this.display;
break;
case 'world':
m = this.world;
break;
default:
throw new Error('Unknown transform ' + transform);
}
return geo.camera.css(m);
};
/**
* Represent a glmatrix as a pretty-printed string.
* @param {mat4} mat A 4 x 4 matrix
* @param {number} prec The number of decimal places
* @returns {string}
*/
this.ppMatrix = function (mat, prec) {
var t = mat;
prec = prec || 2;
function f(i) {
var d = t[i], s = d.toExponential(prec);
if (d >= 0) {
s = ' ' + s;
}
return s;
}
return [
[f(0), f(4), f(8), f(12)].join(' '),
[f(1), f(5), f(9), f(13)].join(' '),
[f(2), f(6), f(10), f(14)].join(' '),
[f(3), f(7), f(11), f(15)].join(' ')
].join('\n');
};
/**
* Pretty print the transform matrix.
*/
this.toString = function () {
return this.ppMatrix(this._transform);
};
/**
* Return a debugging string of the current camera state.
*/
this.debug = function () {
return [
'bounds',
JSON.stringify(this.bounds),
'view:',
this.ppMatrix(this._view),
'projection:',
this.ppMatrix(this._proj),
'transform:',
this.ppMatrix(this._transform)
].join('\n');
};
/**
* Represent the value of the camera as its transform matrix.
*/
this.valueOf = function () {
return this._transform;
};
// initialize the view matrix
this._resetView();
// set up the projection matrix
this.projection = spec.projection || 'parallel';
// initialize the viewport
if (spec.viewport) {
this.viewport = spec.viewport;
}
// trigger an initial update to set up the camera state
this._update();
return this;
};
/**
* Supported projection types.
*/
geo.camera.projection = {
perspective: true,
parallel: true
};
/**
* Camera clipping bounds, probably shouldn't be modified.
*/
geo.camera.bounds = {
left: -1,
right: 1,
top: 1,
bottom: -1,
far: -2,
near: -1
};
/**
* Output a mat4 as a css transform.
* @param {mat4} t A matrix transform
* @returns {string} A css transform string
*/
geo.camera.css = function (t) {
return (
'matrix3d(' +
[
t[0].toFixed(20),
t[1].toFixed(20),
t[2].toFixed(20),
t[3].toFixed(20),
t[4].toFixed(20),
t[5].toFixed(20),
t[6].toFixed(20),
t[7].toFixed(20),
t[8].toFixed(20),
t[9].toFixed(20),
t[10].toFixed(20),
t[11].toFixed(20),
t[12].toFixed(20),
t[13].toFixed(20),
t[14].toFixed(20),
t[15].toFixed(20)
].join(',') +
')'
);
};
/**
* Generate a mat4 representing an affine coordinate transformation.
*
* For the following affine transform:
*
* x |-> m * (x + a) + b
*
* applies the css transform:
*
* translate(b) scale(m) translate(a)
*
* @param {object?} pre Coordinate offset **before** scaling
* @param {object?} scale Coordinate scaling
* @param {object?} post Coordinate offset **after** scaling
* @returns {mat4} The new transform matrix
*/
geo.camera.affine = function (pre, scale, post) {
var mat = geo.util.mat4AsArray();
// Note: mat4 operations are applied to the right side of the current
// transform, so the first applied here is the last applied to the
// coordinate.
if (post) {
mat4.translate(mat, mat, [post.x || 0, post.y || 0, post.z || 0]);
}
if (scale) {
mat4.scale(mat, mat, [scale.x || 1, scale.y || 1, scale.z || 1]);
}
if (pre) {
mat4.translate(mat, mat, [pre.x || 0, pre.y || 0, pre.z || 0]);
}
return mat;
};
/**
* Apply the given transform matrix to a point in place.
* @param {mat4} t
* @param {vec4} pt
* @returns {vec4}
*/
geo.camera.applyTransform = function (t, pt) {
return vec4.transformMat4(pt, pt, t);
};
/**
* Combine two transforms by multiplying their matrix representations.
* @note The second transform provided will be the first applied in the
* coordinate transform.
* @param {mat4} A
* @param {mat4} B
* @returns {mat4} A * B
*/
geo.camera.combine = function (A, B) {
return mat4.mul(geo.util.mat4AsArray(), A, B);
};
inherit(geo.camera, geo.object);
})();
//////////////////////////////////////////////////////////////////////////////
/**
* @class
* @extends geo.sceneObject
* @param {Object?} arg An options argument
* @param {string} arg.attribution An attribution string to display
* @param {number} arg.zIndex The z-index to assign to the layer (defaults
* to the index of the layer inside the map)
* @returns {geo.layer}
*/
//////////////////////////////////////////////////////////////////////////////
geo.layer = function (arg) {
'use strict';
if (!(this instanceof geo.layer)) {
return new geo.layer(arg);
}
arg = arg || {};
geo.sceneObject.call(this, arg);
//////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
//////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_exit = this._exit,
m_id = arg.id === undefined ? geo.layer.newLayerId() : arg.id,
m_name = '',
m_map = arg.map === undefined ? null : arg.map,
m_node = null,
m_canvas = null,
m_renderer = null,
m_initialized = false,
m_rendererName = arg.renderer === undefined ? 'vgl' : arg.renderer,
m_dataTime = geo.timestamp(),
m_updateTime = geo.timestamp(),
m_sticky = arg.sticky === undefined ? true : arg.sticky,
m_active = arg.active === undefined ? true : arg.active,
m_opacity = arg.opacity === undefined ? 1 : arg.opacity,
m_attribution = arg.attribution || null,
m_zIndex;
m_rendererName = geo.checkRenderer(m_rendererName);
if (!m_map) {
throw new Error('Layers must be initialized on a map.');
}
////////////////////////////////////////////////////////////////////////////
/**
* Get the name of the renderer.
*
* @returns {string}
*/
////////////////////////////////////////////////////////////////////////////
this.rendererName = function () {
return m_rendererName;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get or set the z-index of the layer. The z-index controls the display
* order of the layers in much the same way as the CSS z-index property.
*
* @param {number} [zIndex] The new z-index
* @returns {number|this}
*/
////////////////////////////////////////////////////////////////////////////
this.zIndex = function (zIndex) {
if (zIndex === undefined) {
return m_zIndex;
}
m_zIndex = zIndex;
m_node.css('z-index', m_zIndex);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Bring the layer above the given number of layers. This will rotate the
* current z-indices for this and the next `n` layers.
*
* @param {number} [n=1] The number of positions to move
* @returns {this}
*/
////////////////////////////////////////////////////////////////////////////
this.moveUp = function (n) {
var order, i, me = null, tmp, sign;
// set the default
if (n === undefined) {
n = 1;
}
// set the sort direction that controls if we are moving up
// or down the z-index
sign = 1;
if (n < 0) {
sign = -1;
n = -n;
}
// get a sorted list of layers
order = m_this.map().layers().sort(
function (a, b) { return sign * (a.zIndex() - b.zIndex()); }
);
for (i = 0; i < order.length; i += 1) {
if (me === null) {
// loop until we get to the current layer
if (order[i] === m_this) {
me = i;
}
} else if (i - me <= n) {
// swap the next n layers
tmp = m_this.zIndex();
m_this.zIndex(order[i].zIndex());
order[i].zIndex(tmp);
} else {
// all the swaps are done now
break;
}
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Bring the layer below the given number of layers. This will rotate the
* current z-indices for this and the previous `n` layers.
*
* @param {number} [n=1] The number of positions to move
* @returns {this}
*/
////////////////////////////////////////////////////////////////////////////
this.moveDown = function (n) {
if (n === undefined) {
n = 1;
}
return m_this.moveUp(-n);
};
////////////////////////////////////////////////////////////////////////////
/**
* Bring the layer to the top of the map layers.
*
* @returns {this}
*/
////////////////////////////////////////////////////////////////////////////
this.moveToTop = function () {
return m_this.moveUp(m_this.map().children().length - 1);
};
////////////////////////////////////////////////////////////////////////////
/**
* Bring the layer to the bottom of the map layers.
*
* @returns {this}
*/
////////////////////////////////////////////////////////////////////////////
this.moveToBottom = function () {
return m_this.moveDown(m_this.map().children().length - 1);
};
////////////////////////////////////////////////////////////////////////////
/**
* Get whether or not the layer is sticky (navigates with the map).
*
* @returns {Boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.sticky = function () {
return m_sticky;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get whether or not the layer is active. An active layer will receive
* native mouse when the layer is on top. Non-active layers will never
* receive native mouse events.
*
* @returns {Boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.active = function () {
return m_active;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set root node of the layer
*
* @returns {div}
*/
////////////////////////////////////////////////////////////////////////////
this.node = function () {
return m_node;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set id of the layer
*
* @returns {String}
*/
////////////////////////////////////////////////////////////////////////////
this.id = function (val) {
if (val === undefined) {
return m_id;
}
m_id = geo.newLayerId();
m_this.modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set name of the layer
*
* @returns {String}
*/
////////////////////////////////////////////////////////////////////////////
this.name = function (val) {
if (val === undefined) {
return m_name;
}
m_name = val;
m_this.modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set map of the layer
*/
////////////////////////////////////////////////////////////////////////////
this.map = function () {
return m_map;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get renderer for the layer if any
*/
////////////////////////////////////////////////////////////////////////////
this.renderer = function () {
return m_renderer;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get canvas of the layer
*
*/
////////////////////////////////////////////////////////////////////////////
this.canvas = function () {
return m_canvas;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return last time data got changed
*/
////////////////////////////////////////////////////////////////////////////
this.dataTime = function () {
return m_dataTime;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return the modified time for the last update that did something
*/
////////////////////////////////////////////////////////////////////////////
this.updateTime = function () {
return m_updateTime;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set if the layer has been initialized
*/
////////////////////////////////////////////////////////////////////////////
this.initialized = function (val) {
if (val !== undefined) {
m_initialized = val;
return m_this;
}
return m_initialized;
};
////////////////////////////////////////////////////////////////////////////
/**
* Transform coordinates from world coordinates into a local coordinate
* system specific to the underlying renderer. This method is exposed
* to allow direct access the rendering context, but otherwise should
* not be called directly. The default implementation is the identity
* operator.
*/
////////////////////////////////////////////////////////////////////////////
this.toLocal = function (input) {
if (m_this._toLocalMatrix) {
geo.camera.applyTransform(m_this._toLocalMatrix, input);
}
return input;
};
////////////////////////////////////////////////////////////////////////////
/**
* Transform coordinates from a local coordinate system to world coordinates.
*/
////////////////////////////////////////////////////////////////////////////
this.fromLocal = function (input) {
if (m_this._fromLocalMatrix) {
geo.camera.applyTransform(m_this._fromLocalMatrix, input);
}
return input;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get or set the attribution html content that will displayed with the
* layer. By default, nothing will be displayed. Note, this content
* is **not** html escaped, so care should be taken when renderering
* user provided content.
* @param {string?} arg An html fragment
* @returns {string|this} Chainable as a setter
*/
////////////////////////////////////////////////////////////////////////////
this.attribution = function (arg) {
if (arg !== undefined) {
m_attribution = arg;
m_this.map().updateAttribution();
return m_this;
}
return m_attribution;
};
////////////////////////////////////////////////////////////////////////////
/**
* Init layer
*
* @param {boolean} noEvents if a subclass of this intends to bind the
* resize, pan, and zoom events itself, set this flag to true to avoid
* binding them here.
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (noEvents) {
if (m_initialized) {
return m_this;
}
m_map.node().append(m_node);
/* Pass along the arguments, but not the map reference */
var options = $.extend({}, arg);
delete options.map;
if (m_rendererName === null) {
// if given a "null" renderer, then pass the map element as the
// canvas
m_renderer = null;
m_canvas = m_node;
} else if (m_canvas) { // Share context if have valid one
m_renderer = geo.createRenderer(m_rendererName, m_this, m_canvas,
options);
} else {
m_renderer = geo.createRenderer(m_rendererName, m_this, undefined,
options);
m_canvas = m_renderer.canvas();
}
if (!m_this.active()) {
m_node.css('pointerEvents', 'none');
}
m_initialized = true;
if (!noEvents) {
/// Bind events to handlers
m_this.geoOn(geo.event.resize, function (event) {
m_this._update({event: event});
});
m_this.geoOn(geo.event.pan, function (event) {
m_this._update({event: event});
});
m_this.geoOn(geo.event.rotate, function (event) {
m_this._update({event: event});
});
m_this.geoOn(geo.event.zoom, function (event) {
m_this._update({event: event});
});
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Clean up resouces
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.geoOff();
if (m_renderer) {
m_renderer._exit();
}
m_node.off();
m_node.remove();
arg = {};
m_canvas = null;
m_renderer = null;
s_exit();
};
////////////////////////////////////////////////////////////////////////////
/**
* Update layer
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
};
////////////////////////////////////////////////////////////////////////////
/**
* Return the width of the layer in pixels.
* **DEPRECIATED: use map.size instead.
*/
////////////////////////////////////////////////////////////////////////////
this.width = function () {
return m_this.map().size().width;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return the height of the layer in pixels
* **DEPRECIATED: use map.size instead.
*/
////////////////////////////////////////////////////////////////////////////
this.height = function () {
return m_this.map().size().height;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get or set the current layer opacity.
*/
////////////////////////////////////////////////////////////////////////////
this.opacity = function (opac) {
if (opac !== undefined) {
m_opacity = opac;
m_node.css('opacity', m_opacity);
return m_this;
}
return m_opacity;
};
if (arg.zIndex === undefined) {
arg.zIndex = m_map.children().length;
}
m_zIndex = arg.zIndex;
// Create top level div for the layer
m_node = $(document.createElement('div'));
m_node.attr('id', m_name);
m_node.css('position', 'absolute');
m_node.css('width', '100%');
m_node.css('height', '100%');
m_this.opacity(m_opacity);
// set the z-index
m_this.zIndex(m_zIndex);
return m_this;
};
/**
* Gets a new id number for a layer.
* @protected
* @instance
* @returns {number}
*/
geo.layer.newLayerId = (function () {
'use strict';
var currentId = 1;
return function () {
var id = currentId;
currentId += 1;
return id;
};
}()
);
/**
* General object specification for feature types.
* @typedef geo.layer.spec
* @type {object}
* @property {string} [type='feature'] For feature compatibility
* with more than one kind of creatable layer
* @property {object[]} [data=[]] The default data array to
* apply to each feature if none exists
* @property {string} [renderer='vgl'] The renderer to use
* @property {geo.feature.spec[]} [features=[]] Features
* to add to the layer
*/
/**
* Create a layer from an object. Any errors in the creation
* of the layer will result in returning null.
* @param {geo.map} map The map to add the layer to
* @param {geo.layer.spec} spec The object specification
* @returns {geo.layer|null}
*/
geo.layer.create = function (map, spec) {
'use strict';
spec = spec || {};
// add osmLayer later
spec.type = 'feature';
if (spec.type !== 'feature') {
console.warn('Unsupported layer type');
return null;
}
spec.renderer = spec.renderer || 'vgl';
spec.renderer = geo.checkRenderer(spec.renderer);
if (!spec.renderer) {
console.warn('Invalid renderer');
return null;
}
var layer = map.createLayer(spec.type, spec);
if (!layer) {
console.warn('Unable to create a layer');
return null;
}
// probably move this down to featureLayer eventually
spec.features.forEach(function (f) {
f.data = f.data || spec.data;
f.feature = geo.feature.create(layer, f);
});
return layer;
};
inherit(geo.layer, geo.sceneObject);
//////////////////////////////////////////////////////////////////////////////
/**
* Layer to draw points, lines, and polygons on the map The polydata layer
* provide mechanisms to create and draw geometrical shapes such as points,
* lines, and polygons.
* @class
* @extends geo.layer
* @returns {geo.featureLayer}
*/
//////////////////////////////////////////////////////////////////////////////
geo.featureLayer = function (arg) {
'use strict';
if (!(this instanceof geo.featureLayer)) {
return new geo.featureLayer(arg);
}
geo.layer.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
m_features = [],
s_init = this._init,
s_exit = this._exit,
s_update = this._update,
s_draw = this.draw;
////////////////////////////////////////////////////////////////////////////
/**
* Create feature give a name
*
* @returns {geo.Feature} Will return a new feature
*/
////////////////////////////////////////////////////////////////////////////
this.createFeature = function (featureName, arg) {
var newFeature = geo.createFeature(
featureName, m_this, m_this.renderer(), arg);
m_this.addChild(newFeature);
m_features.push(newFeature);
m_this.features(m_features);
m_this.modified();
return newFeature;
};
////////////////////////////////////////////////////////////////////////////
/**
* Delete feature
*
*/
////////////////////////////////////////////////////////////////////////////
this.deleteFeature = function (feature) {
var i;
for (i = 0; i < m_features.length; i += 1) {
if (m_features[i] === feature) {
m_features[i]._exit();
m_this.dataTime().modified();
m_this.modified();
m_features.splice(i, 1);
}
}
m_this.removeChild(feature);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set drawables
*
* @returns {Array}
*/
////////////////////////////////////////////////////////////////////////////
this.features = function (val) {
if (val === undefined) {
return m_features;
} else {
m_features = val.slice(0);
m_this.dataTime().modified();
m_this.modified();
return m_this;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function () {
if (m_this.initialized()) {
return m_this;
}
/// Call super class init
s_init.call(m_this, true);
/// Bind events to handlers
m_this.geoOn(geo.event.resize, function (event) {
if (m_this.renderer()) {
m_this.renderer()._resize(event.x, event.y, event.width, event.height);
m_this._update({event: event});
m_this.renderer()._render();
} else {
m_this._update({event: event});
}
});
m_this.geoOn(geo.event.pan, function (event) {
m_this._update({event: event});
if (m_this.renderer()) {
m_this.renderer()._render();
}
});
m_this.geoOn(geo.event.rotate, function (event) {
m_this._update({event: event});
if (m_this.renderer()) {
m_this.renderer()._render();
}
});
m_this.geoOn(geo.event.zoom, function (event) {
m_this._update({event: event});
if (m_this.renderer()) {
m_this.renderer()._render();
}
});
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Update layer
*/
////////////////////////////////////////////////////////////////////////////
this._update = function (request) {
var i;
if (!m_features.length) {
return m_this;
}
/// Call base class update
s_update.call(m_this, request);
if (m_features && m_features.length === 0) {
console.log('[info] No valid data source found.');
return;
}
if (m_this.dataTime().getMTime() > m_this.updateTime().getMTime()) {
for (i = 0; i < m_features.length; i += 1) {
m_features[i].renderer(m_this.renderer());
}
}
for (i = 0; i < m_features.length; i += 1) {
m_features[i]._update();
}
m_this.updateTime().modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Free all resources
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.clear();
s_exit();
};
////////////////////////////////////////////////////////////////////////////
/**
* Draw
*/
////////////////////////////////////////////////////////////////////////////
this.draw = function () {
// Call sceneObject.draw, which calls draw on all child objects.
s_draw();
// Now call render on the renderer. In certain cases it may not do
// anything if the if the child objects are drawn on the screen already.
if (m_this.renderer()) {
m_this.renderer()._render();
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Clear all features in layer
*/
////////////////////////////////////////////////////////////////////////////
this.clear = function () {
var i;
if (!m_features.length) {
return m_this;
}
for (i = 0; i < m_features.length; i += 1) {
m_features[i]._exit();
m_this.removeChild(m_features[i]);
}
m_this.dataTime().modified();
m_this.modified();
m_features = [];
return m_this;
};
return m_this;
};
inherit(geo.featureLayer, geo.layer);
// Now register it
geo.registerLayer('feature', geo.featureLayer);
//////////////////////////////////////////////////////////////////////////////
/**
* Common object containing all event types that are provided by the GeoJS
* API. Each property contained here is a valid target for event handling
* via {@link geo.object#geoOn}. The event object provided to handlers is
* different for each event type. Each handler will generally be called
* with a the <code>this</code> context being the class that caused the event.<br>
* <br>
* The following properties are common to all event objects:
*
* @namespace
* @property {string} type The event type that was triggered
* @property {object} geo A universal event object for controlling propagation
*
* @example
* map.geoOn(geo.event.layerAdd, function (event) {
* // event is an object with type: {@link geo.event.layerAdd}
* });
*
*/
//////////////////////////////////////////////////////////////////////////////
geo.event = {};
//////////////////////////////////////////////////////////////////////////////
/*
* Event types
*/
//////////////////////////////////////////////////////////////////////////////
// The following were not triggered nor used anywhere. Removing until their
// purpose is defined more clearly.
//
// geo.event.update = 'geo_update';
// geo.event.opacityUpdate = 'geo_opacityUpdate';
// geo.event.layerSelect = 'geo_layerSelect';
// geo.event.layerUnselect = 'geo_layerUnselect';
// geo.event.query = 'geo_query';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered when a layer is added to the map.
*
* @property {geo.map} target The current map
* @property {geo.layer} layer The new layer
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.layerAdd = 'geo_layerAdd';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered when a layer is removed from the map.
*
* @property {geo.map} target The current map
* @property {geo.layer} layer The old layer
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.layerRemove = 'geo_layerRemove';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered when the map's zoom level is changed. Note that zoom is never
* triggered on the map itself. Instead it is triggered individually on
* layers, starting with the base layer.
*
* @property {number} zoomLevel New zoom level
* @property {object} screenPosition The screen position of mouse pointer
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.zoom = 'geo_zoom';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered when the map is rotated around the current map center (pointing
* downward so that positive angles are clockwise rotations).
*
* @property {number} angle The angle of the rotation in radians
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.rotate = 'geo_rotate';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered when the map is panned either by user interaction or map
* transition.
*
* @property {object} screenDelta The number of pixels to pan the map by
* @property {object} center The new map center
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.pan = 'geo_pan';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered when the map's canvas is resized.
*
* @property {number} width The new width in pixels
* @property {number} height The new height in pixels
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.resize = 'geo_resize';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered when the world coordinate system changes. Data in GCS
* coordinates can be transformed by the following formulas:
*
* x <- (x - origin.x) * scale.x
* y <- (y - origin.y) * scale.y
* z <- (z - origin.z) * scale.z
*
* Data in world coordinates can be updated using the following formulas:
*
* x <- (x * scaleChange.x - origin.x * (scale.x + scaleChange.x)
* - scale.x * originChange.x) * scale.x / scaleChange.x
* y <- (y * scaleChange.y - origin.y * (scale.y + scaleChange.y)
* - scale.y * originChange.y) * scale.y / scaleChange.y
* z <- (z * scaleChange.z - origin.z * (scale.z + scaleChange.z)
* - scale.z * originChange.z) * scale.z / scaleChange.z
*
* @property {geo.map} map The map whose coordinates changed
* @property {object} origin The new origin in GCS coordinates
* @property {number} origin.x
* @property {number} origin.y
* @property {number} origin.z
* @property {object} scale The new scale factor
* @property {number} scale.x
* @property {number} scale.y
* @property {number} scale.z
* @property {object} originChange Relative change from the old origin defined
* as `origin - oldorigin`.
* @property {object} scaleChange Relative change from the old scale defined
* as `scale / oldscale`.
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.worldChanged = 'geo_worldChanged';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered on every call to {@link geo.map#draw} before the map is rendered.
*
* @property {geo.map} target The current map
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.draw = 'geo_draw';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered on every call to {@link geo.map#draw} after the map is rendered.
*
* @property {geo.map} target The current map
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.drawEnd = 'geo_drawEnd';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered on every 'mousemove' over the map's DOM element. The event
* object extends {@link geo.mouseState}.
* @mixes geo.mouseState
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.mousemove = 'geo_mousemove';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered on every 'mousedown' over the map's DOM element. The event
* object extends {@link geo.mouseState}.
* @mixes geo.mouseState
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.mouseclick = 'geo_mouseclick';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered on every 'mousemove' during a brushing selection.
* The event object extends {@link geo.brushSelection}.
* @mixes geo.brushSelection
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.brush = 'geo_brush';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered after a brush selection ends.
* The event object extends {@link geo.brushSelection}.
* @mixes geo.brushSelection
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.brushend = 'geo_brushend';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered when a brush selection starts.
* The event object extends {@link geo.brushSelection}.
* @mixes geo.brushSelection
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.brushstart = 'geo_brushstart';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered before a map navigation animation begins. Set
* <code>event.geo.cancelAnimation</code> to cancel the animation
* of the navigation. This will cause the map to navigate to the
* target location immediately. Set <code>event.geo.cancelNavigation</code>
* to cancel the navigation completely. The transition options can
* be modified in place.
*
* @property {geo.geoPosition} center The target center
* @property {number} zoom The target zoom level
* @property {number} duration The duration of the transition in milliseconds
* @property {function} ease The easing function
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.transitionstart = 'geo_transitionstart';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered after a map navigation animation ends.
*
* @property {geo.geoPosition} center The target center
* @property {number} zoom The target zoom level
* @property {number} duration The duration of the transition in milliseconds
* @property {function} ease The easing function
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.transitionend = 'geo_transitionend';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered when the parallel projection mode is changes.
*
* @property paralellProjection {boolean} True if parallel projection is turned
* on.
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.parallelprojection = 'geo_parallelprojection';
////////////////////////////////////////////////////////////////////////////
/**
* @namespace
*/
////////////////////////////////////////////////////////////////////////////
geo.event.clock = {
play: 'geo_clock_play',
stop: 'geo_clock_stop',
pause: 'geo_clock_pause',
change: 'geo_clock_change'
};
////////////////////////////////////////////////////////////////////////////
/**
* This event object provides mouse/keyboard events that can be handled
* by the features. This provides a similar interface as core events,
* but with different names so the events don't interfere. Subclasses
* can override this to provide custom events.
*
* These events will only be triggered on features which were instantiated
* with the option 'selectionAPI'.
* @namespace
*/
////////////////////////////////////////////////////////////////////////////
geo.event.feature = {
mousemove: 'geo_feature_mousemove',
mouseover: 'geo_feature_mouseover',
mouseout: 'geo_feature_mouseout',
mouseon: 'geo_feature_mouseon',
mouseoff: 'geo_feature_mouseoff',
mouseclick: 'geo_feature_mouseclick',
brushend: 'geo_feature_brushend',
brush: 'geo_feature_brush'
};
////////////////////////////////////////////////////////////////////////////
/**
* These events are triggered by the camera when it's internal state is
* mutated.
* @namespace
*/
////////////////////////////////////////////////////////////////////////////
geo.event.camera = {};
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered after a general view matrix change (any change in the visible
* bounds). This is equivalent to the union of pan and zoom.
*
* @property {geo.camera} camera The camera instance
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.camera.view = 'geo_camera_view';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered after a pan in the x/y plane (no zoom level change).
*
* @property {geo.camera} camera The camera instance
* @property {object} delta The translation delta in world coordinates.
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.camera.pan = 'geo_camera_pan';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered after a view matrix change that is not a simple pan. This
* includes, but is not limited to, pure zooms.
*
* @property {geo.camera} camera The camera instance
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.camera.zoom = 'geo_camera_zoom';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered after a projection change.
*
* @property {geo.camera} camera The camera instance
* @property {string} type The projection type ('perspective'|'parallel')
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.camera.projection = 'geo_camera_projection';
//////////////////////////////////////////////////////////////////////////////
/**
* Triggered after a viewport change.
*
* @property {geo.camera} camera The camera instance
* @property {object} viewport The new viewport
* @property {number} viewport.width The new width
* @property {number} viewport.height The new height
*/
//////////////////////////////////////////////////////////////////////////////
geo.event.camera.viewport = 'geo_camera_viewport';
//////////////////////////////////////////////////////////////////////////////
/**
* The mapInteractor class is responsible for handling raw events from the
* browser and interpreting them as map navigation interactions. This class
* will call the navigation methods on the connected map, which will make
* modifications to the camera directly.
*
* @class
* @extends geo.object
* @returns {geo.mapInteractor}
*/
//////////////////////////////////////////////////////////////////////////////
geo.mapInteractor = function (args) {
'use strict';
if (!(this instanceof geo.mapInteractor)) {
return new geo.mapInteractor(args);
}
geo.object.call(this);
var m_options = args || {},
m_this = this,
m_mouse,
m_keyboard,
m_state,
m_queue,
$node,
m_selectionLayer = null,
m_selectionPlane = null,
m_paused = false,
m_clickMaybe = false,
m_callZoom = function () {};
// Helper method to decide if the current button/modifiers match a set of
// conditions.
// button: 'left' | 'right' | 'middle'
// modifiers: [ 'alt' | 'meta' | 'ctrl' | 'shift' ]
function eventMatch(button, modifiers) {
return (button === 'wheel' || m_mouse.buttons[button]) &&
(!!m_mouse.modifiers.alt) === (!!modifiers.alt) &&
(!!m_mouse.modifiers.meta) === (!!modifiers.meta) &&
(!!m_mouse.modifiers.shift) === (!!modifiers.shift) &&
(!!m_mouse.modifiers.ctrl) === (!!modifiers.ctrl);
}
// Helper method to calculate the speed from a velocity
function calcSpeed(v) {
var x = v.x, y = v.y;
return Math.sqrt(x * x + y * y);
}
// copy the options object with defaults
m_options = $.extend(
true,
{},
{
throttle: 30,
discreteZoom: false,
panMoveButton: 'left',
panMoveModifiers: {},
zoomMoveButton: 'right',
zoomMoveModifiers: {},
rotateMoveButton: 'left',
rotateMoveModifiers: {'ctrl': true},
panWheelEnabled: false,
panWheelModifiers: {},
zoomWheelEnabled: true,
zoomWheelModifiers: {},
rotateWheelEnabled: true,
rotateWheelModifiers: {'ctrl': true},
wheelScaleX: 1,
wheelScaleY: 1,
zoomScale: 1,
rotateWheelScale: 6 * Math.PI / 180,
selectionButton: 'left',
selectionModifiers: {'shift': true},
momentum: {
enabled: true,
maxSpeed: 2.5,
minSpeed: 0.01,
drag: 0.01
},
spring: {
enabled: false,
springConstant: 0.00005
},
click: {
enabled: true,
buttons: {left: true, right: true, middle: true},
duration: 0,
cancelOnMove: true
}
},
m_options
);
// options supported:
// {
// // throttle mouse events to at most this many milliseconds each (default 30)
// throttle: number
//
// // Clamp zoom events to discrete (integer) zoom levels. If a number is
// // provided then zoom events will be debounced (and accumulated)
// // with the given delay. The default debounce interval is 400 ms.
// discreteZoom: boolean | number > 0
//
// // button that must be pressed to initiate a pan on mousedown
// panMoveButton: 'right' | 'left' | 'middle'
//
// // modifier keys that must be pressed to initiate a pan on mousemove
// panMoveModifiers: { 'ctrl' | 'alt' | 'meta' | 'shift' }
//
// // button that must be pressed to initiate a zoom on mousedown
// zoomMoveButton: 'right' | 'left' | 'middle'
//
// // modifier keys that must be pressed to initiate a zoom on mousemove
// zoomMoveModifiers: { 'ctrl' | 'alt' | 'meta' | 'shift' }
//
// // button that must be pressed to initiate a rotate on mousedown
// rotateMoveButton: 'right' | 'left' | 'middle'
//
// // modifier keys that must be pressed to initiate a rotate on mousemove
// rotateMoveModifiers: { 'ctrl' | 'alt' | 'meta' | 'shift' }
//
// // enable or disable panning with the mouse wheel
// panWheelEnabled: true | false
//
// // modifier keys that must be pressed to trigger a pan on wheel
// panWheelModifiers: {...}
//
// // enable or disable zooming with the mouse wheel
// zoomWheelEnabled: true | false
//
// // modifier keys that must be pressed to trigger a zoom on wheel
// zoomWheelModifiers: {...}
//
// // enable or disable rotation with the mouse wheel
// rotateWheelEnabled: true | false
//
// // modifier keys that must be pressed to trigger a rotate on wheel
// rotateWheelModifiers: {...}
//
// // wheel scale factor to change the magnitude of wheel interactions
// wheelScaleX: 1
// wheelScaleY: 1
//
// // zoom scale factor to change the magnitude of zoom move interactions
// zoomScale: 1
//
// // scale factor to change the magnitude of wheel rotation interactions
// rotateWheelScale: 1
//
// // button that must be pressed to enable drag selection
// selectionButton: 'right' | 'left' | 'middle'
//
// // keyboard modifiers that must be pressed to initiate a selection
// selectionModifiers: {...}
//
// // enable momentum when panning
// momentum: {
// enabled: true | false,
// drag: number, // drag coefficient
// maxSpeed: number, // don't allow animation to pan faster than this
// minSpeed: number // stop animations if the speed is less than this
// }
//
// // enable spring clamping to screen edges to enforce clamping
// spring: {
// enabled: true | false,
// springConstant: number,
// }
//
// // enable the "click" event
// // A click will be registered when a mouse down is followed
// // by a mouse up in less than the given number of milliseconds
// // and the standard handler will *not* be called
// // If the duration is <= 0, then clicks will only be canceled by
// // a mousemove.
// click: {
// enabled: true | false,
// buttons: {'left': true, 'right': true, 'middle': true}
// duration: 0,
// cancelOnMove: true // cancels click if the mouse is moved before release
// }
// }
// A bunch of type definitions for api documentation:
/**
* General representation of rectangular bounds in world coordinates
* @typedef geo.geoBounds
* @type {object}
* @property {geo.geoPosition} upperLeft Upper left corner
* @property {geo.geoPosition} upperRight Upper right corner
* @property {geo.geoPosition} lowerLeft Lower left corner
* @property {geo.geoPosition} lowerRight Lower right corner
*/
/**
* General representation of rectangular bounds in pixel coordinates
* @typedef geo.screenBounds
* @type {object}
* @property {geo.screenPosition} upperLeft Upper left corner
* @property {geo.screenPosition} upperRight Upper right corner
* @property {geo.screenPosition} lowerLeft Lower left corner
* @property {geo.screenPosition} lowerRight Lower right corner
*/
/**
* General representation of a point on the screen.
* @typedef geo.screenPosition
* @type {object}
* @property {Number} x Horizontal coordinate in pixels
* @property {Number} y Vertical coordinate in pixels
*/
/**
* General represention of a point on the earth.
* @typedef geo.geoPosition
* @type {object}
* @property {Number} x Horizontal coordinate in degrees longitude
* @property {Number} y Vertical coordinate in degrees latitude
*/
/**
* The status of all mouse buttons.
* @typedef geo.mouseButtons
* @type {object}
* @property {true|false} left The left mouse button
* @property {true|false} right The right mouse button
* @property {true|false} middle The middle mouse button
*/
/**
* The status of all modifier keys these are copied from the
* standard DOM events.
* @typedef geo.modifierKeys
* @type {object}
* @property {true|false} alt <code>Event.alt</code>
* @property {true|false} ctrl <code>Event.ctrl</code>
* @property {true|false} shift <code>Event.shift</code>
* @property {true|false} meta <code>Event.meta</code>
*/
/**
* Provides information about the state of the mouse
* @typedef geo.mouseState
* @type {object}
* @property {geo.screenPosition} page Mouse location in pixel space
* @property {geo.geoPosition} map Mouse location in world space
* @property {geo.mouseButtons} buttons The current state of the mouse buttons
* @property {geo.modifierKeys} modifiers The current state of all modifier keys
* @property {Date} time The timestamp the event took place
* @property {Number} deltaTime The time in milliseconds since the last mouse event
* @property {geo.screenPosition} velocity The velocity of the mouse pointer
* in pixels per second
*/
/**
* @typedef geo.brushSelection
* @type {object}
* @property {geo.screenBounds} display The selection bounds in pixel space
* @property {geo.geoBounds} gcs The selection bounds in world space
* @property {geo.mouseState} mouse The current mouse state
* @property {geo.mouseState} origin The mouse state at the start of the
* brush action
*/
// default mouse object
m_mouse = {
page: { // mouse position relative to the page
x: 0,
y: 0
},
map: { // mouse position relative to the map
x: 0,
y: 0
},
// mouse button status
buttons: {
left: false,
right: false,
middle: false
},
// keyboard modifier status
modifiers: {
alt: false,
ctrl: false,
shift: false,
meta: false
},
// time the event was captured
time: new Date(),
// time elapsed since the last mouse event
deltaTime: 1,
// pixels/ms
velocity: {
x: 0,
y: 0
}
};
// default keyboard object
// (keyboard events not implemented yet)
m_keyboard = {
};
// The interactor state determines what actions are taken in response to
// core browser events.
//
// i.e.
// {
// 'action': 'pan', // an ongoing pan event
// 'origin': {...}, // mouse object at the start of the action
// 'delta': {x: *, y: *} // mouse movement since action start
// // not including the current event
// }
//
// {
// 'action': 'zoom', // an ongoing zoom event
// ...
// }
//
// {
// 'action': 'rotate', // an ongoing rotate event
// 'origin': {...}, // mouse object at the start of the action
// 'delta': {x: *, y: *} // mouse movement since action start
// // not including the current event
// }
//
// {
// 'acton': 'select',
// 'origin': {...},
// 'delta': {x: *, y: *}
// }
//
// {
// 'action': 'momentum',
// 'origin': {...},
// 'handler': function () { }, // called in animation loop
// 'timer': animate loop timer
// }
m_state = {};
/**
* Store queued map navigation commands (due to throttling) here
* {
* kind: 'move' | 'wheel', // what kind of mouse action triggered this
* method: function () {}, // the throttled method
* scroll: {x: ..., y: ...} // accumulated scroll wheel deltas
* }
*/
m_queue = {};
////////////////////////////////////////////////////////////////////////////
/**
* Connects events to a map. If the map is not set, then this does nothing.
* @returns {geo.mapInteractor}
*/
////////////////////////////////////////////////////////////////////////////
this._connectEvents = function () {
if (!m_options.map) {
return m_this;
}
// prevent double binding to dom elements
m_this._disconnectEvents();
// store the connected element
$node = $(m_options.map.node());
// set methods related to asyncronous event handling
m_this._handleMouseWheel = throttled_wheel();
m_callZoom = debounced_zoom();
// add event handlers
$node.on('wheel.geojs', m_this._handleMouseWheel);
$node.on('mousemove.geojs', m_this._handleMouseMove);
$node.on('mousedown.geojs', m_this._handleMouseDown);
$node.on('mouseup.geojs', m_this._handleMouseUp);
// Disable dragging images and such
$node.on('dragstart', function () { return false; });
if (m_options.panMoveButton === 'right' ||
m_options.zoomMoveButton === 'right' ||
m_options.rotateMoveButton === 'right' ||
m_options.selectionButton === 'right') {
$node.on('contextmenu.geojs', function () { return false; });
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Disonnects events to a map. If the map is not set, then this does nothing.
* @returns {geo.mapInteractor}
*/
////////////////////////////////////////////////////////////////////////////
this._disconnectEvents = function () {
if ($node) {
$node.off('.geojs');
$node = null;
}
m_this._handleMouseWheel = function () {};
m_callZoom = function () {};
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Sets or gets map for this interactor, adds draw region layer if needed
*
* @param {geo.map} newMap optional
* @returns {geo.interactorStyle|geo.map}
*/
////////////////////////////////////////////////////////////////////////////
this.map = function (val) {
if (val !== undefined) {
m_options.map = val;
m_this._connectEvents();
return m_this;
}
return m_options.map;
};
////////////////////////////////////////////////////////////////////////////
/**
* Gets/sets the options object for the interactor.
*
* @param {Object} opts optional
* @returns {geo.interactorStyle|Object}
*/
////////////////////////////////////////////////////////////////////////////
this.options = function (opts) {
if (opts === undefined) {
return $.extend({}, m_options);
}
$.extend(m_options, opts);
// reset event handlers for new options
this._connectEvents();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Stores the current mouse position from an event
*/
////////////////////////////////////////////////////////////////////////////
this._getMousePosition = function (evt) {
var offset = $node.offset(), dt, t;
t = (new Date()).valueOf();
dt = t - m_mouse.time;
m_mouse.time = t;
m_mouse.deltaTime = dt;
m_mouse.velocity = {
x: (evt.pageX - m_mouse.page.x) / dt,
y: (evt.pageY - m_mouse.page.y) / dt
};
m_mouse.page = {
x: evt.pageX,
y: evt.pageY
};
m_mouse.map = {
x: evt.pageX - offset.left,
y: evt.pageY - offset.top
};
try {
m_mouse.geo = m_this.map().displayToGcs(m_mouse.map);
} catch (e) {
// catch georeferencing problems and move on
// needed for handling the map before the base layer
// is attached
m_mouse.geo = null;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Stores the current mouse button
*/
////////////////////////////////////////////////////////////////////////////
this._getMouseButton = function (evt) {
if (evt.which === 1) {
m_mouse.buttons.left = evt.type !== 'mouseup';
} else if (evt.which === 3) {
m_mouse.buttons.right = evt.type !== 'mouseup';
} else if (evt.which === 2) {
m_mouse.buttons.middle = evt.type !== 'mouseup';
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Stores the current keyboard modifiers
*/
////////////////////////////////////////////////////////////////////////////
this._getMouseModifiers = function (evt) {
m_mouse.modifiers.alt = evt.altKey;
m_mouse.modifiers.ctrl = evt.ctrlKey;
m_mouse.modifiers.meta = evt.metaKey;
m_mouse.modifiers.shift = evt.shiftKey;
};
////////////////////////////////////////////////////////////////////////////
/**
* Compute a selection information object.
* @private
* @returns {Object}
*/
////////////////////////////////////////////////////////////////////////////
this._getSelection = function () {
var origin = m_state.origin,
mouse = m_this.mouse(),
map = m_this.map(),
display = {}, gcs = {};
// TODO: clamp to map bounds
// Get the display coordinates
display.upperLeft = {
x: Math.min(origin.map.x, mouse.map.x),
y: Math.min(origin.map.y, mouse.map.y)
};
display.lowerRight = {
x: Math.max(origin.map.x, mouse.map.x),
y: Math.max(origin.map.y, mouse.map.y)
};
display.upperRight = {
x: display.lowerRight.x,
y: display.upperLeft.y
};
display.lowerLeft = {
x: display.upperLeft.x,
y: display.lowerRight.y
};
// Get the gcs coordinates
gcs.upperLeft = map.displayToGcs(display.upperLeft);
gcs.lowerRight = map.displayToGcs(display.lowerRight);
gcs.upperRight = map.displayToGcs(display.upperRight);
gcs.lowerLeft = map.displayToGcs(display.lowerLeft);
m_selectionPlane.origin([
display.lowerLeft.x,
display.lowerLeft.y,
0
]);
m_selectionPlane.upperLeft([
display.upperLeft.x,
display.upperLeft.y,
0
]);
m_selectionPlane.lowerRight([
display.lowerRight.x,
display.lowerRight.y,
0
]);
m_selectionPlane.draw();
return {
display: display,
gcs: gcs,
mouse: mouse,
origin: $.extend({}, m_state.origin)
};
};
////////////////////////////////////////////////////////////////////////////
/**
* Immediately cancel an ongoing action.
*
* @param {string?} action The action type, if null cancel any action
* @returns {bool} If an action was canceled
*/
////////////////////////////////////////////////////////////////////////////
this.cancel = function (action) {
var out;
if (!action) {
out = !!m_state.action;
} else {
out = m_state.action === action;
}
if (out) {
// cancel any queued interaction events
m_queue = {};
m_state = {};
}
return out;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle event when a mouse button is pressed
*/
////////////////////////////////////////////////////////////////////////////
this._handleMouseDown = function (evt) {
var action = null;
if (m_paused) {
return;
}
// cancel momentum on click
m_this.cancel('momentum');
m_this._getMousePosition(evt);
m_this._getMouseButton(evt);
m_this._getMouseModifiers(evt);
if (m_options.click.enabled &&
(!m_mouse.buttons.left || m_options.click.buttons.left) &&
(!m_mouse.buttons.right || m_options.click.buttons.right) &&
(!m_mouse.buttons.middle || m_options.click.buttons.middle)) {
m_clickMaybe = true;
if (m_options.click.duration > 0) {
window.setTimeout(function () {
m_clickMaybe = false;
}, m_options.click.duration);
}
}
if (eventMatch(m_options.panMoveButton, m_options.panMoveModifiers)) {
action = 'pan';
} else if (eventMatch(m_options.zoomMoveButton, m_options.zoomMoveModifiers)) {
action = 'zoom';
} else if (eventMatch(m_options.rotateMoveButton, m_options.rotateMoveModifiers)) {
action = 'rotate';
} else if (eventMatch(m_options.selectionButton, m_options.selectionModifiers)) {
action = 'select';
}
m_mouse.velocity = {
x: 0,
y: 0
};
if (action) {
// cancel any ongoing interaction queue
m_queue = {
kind: 'move'
};
// store the state object
m_state = {
action: action,
origin: $.extend(true, {}, m_mouse),
delta: {x: 0, y: 0}
};
if (action === 'select') {
// Make sure the old selection layer is gone.
if (m_selectionLayer) {
m_selectionLayer.clear();
m_this.map().deleteLayer(m_selectionLayer);
m_selectionLayer = null;
}
// Create a feature layer and plane feature to show the selection bounds
m_selectionLayer = m_this.map().createLayer('feature', {renderer: 'd3'});
m_selectionPlane = m_selectionLayer.createFeature('plane');
m_selectionPlane.style({
screenCoordinates: true,
fillOpacity: function () { return 0.25; }
});
m_this.map().geoTrigger(geo.event.brushstart, m_this._getSelection());
}
// bind temporary handlers to document
if (m_options.throttle > 0) {
$(document).on(
'mousemove.geojs',
geo.util.throttle(
m_options.throttle,
m_this._handleMouseMoveDocument
)
);
} else {
$(document).on('mousemove.geojs', m_this._handleMouseMoveDocument);
}
$(document).on('mouseup.geojs', m_this._handleMouseUpDocument);
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse move event
*/
////////////////////////////////////////////////////////////////////////////
this._handleMouseMove = function (evt) {
if (m_paused) {
return;
}
if (m_state.action) {
// If currently performing a navigation action, the mouse
// coordinates will be captured by the document handler.
return;
}
if (m_options.click.cancelOnMove) {
m_clickMaybe = false;
}
m_this._getMousePosition(evt);
m_this._getMouseButton(evt);
m_this._getMouseModifiers(evt);
if (m_clickMaybe) {
return;
}
m_this.map().geoTrigger(geo.event.mousemove, m_this.mouse());
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse move event on the document (temporary bindings)
*/
////////////////////////////////////////////////////////////////////////////
this._handleMouseMoveDocument = function (evt) {
var dx, dy, selectionObj;
if (m_paused || m_queue.kind !== 'move') {
return;
}
m_this._getMousePosition(evt);
m_this._getMouseButton(evt);
m_this._getMouseModifiers(evt);
if (m_options.click.cancelOnMove) {
m_clickMaybe = false;
}
if (m_clickMaybe) {
return;
}
if (!m_state.action) {
// This shouldn't happen
console.log('WARNING: Invalid state in mapInteractor.');
return;
}
// calculate the delta from the origin point to avoid
// accumulation of floating point errors
dx = m_mouse.map.x - m_state.origin.map.x - m_state.delta.x;
dy = m_mouse.map.y - m_state.origin.map.y - m_state.delta.y;
m_state.delta.x += dx;
m_state.delta.y += dy;
if (m_state.action === 'pan') {
m_this.map().pan({x: dx, y: dy});
} else if (m_state.action === 'zoom') {
m_callZoom(-dy * m_options.zoomScale / 120, m_state);
} else if (m_state.action === 'rotate') {
var cx, cy;
if (m_state.origin.rotation === undefined) {
cx = m_state.origin.map.x - m_this.map().size().width / 2;
cy = m_state.origin.map.y - m_this.map().size().height / 2;
m_state.origin.rotation = m_this.map().rotation() - Math.atan2(cy, cx);
}
cx = m_mouse.map.x - m_this.map().size().width / 2;
cy = m_mouse.map.y - m_this.map().size().height / 2;
m_this.map().rotation(m_state.origin.rotation + Math.atan2(cy, cx));
} else if (m_state.action === 'select') {
// Get the bounds of the current selection
selectionObj = m_this._getSelection();
m_this.map().geoTrigger(geo.event.brush, selectionObj);
}
// Prevent default to stop text selection in particular
evt.preventDefault();
};
/**
* Use interactor options to modify the mouse velocity by momentum
* or spring equations depending on the current map state.
* @private
* @param {object} v Current velocity in pixels / ms
* @param {number} deltaT The time delta
* @returns {object} New velocity
*/
function modifyVelocity(v, deltaT) {
deltaT = deltaT <= 0 ? 30 : deltaT;
var sf = springForce();
var speed = calcSpeed(v);
var vx = v.x / speed;
var vy = v.y / speed;
speed = speed * Math.exp(-m_options.momentum.drag * deltaT);
// |force| + |velocity| < c <- stopping condition
if (calcSpeed(sf) * deltaT + speed < m_options.momentum.minSpeed) {
return null;
}
if (speed > 0) {
vx = vx * speed;
vy = vy * speed;
} else {
vx = 0;
vy = 0;
}
return {
x: vx - sf.x * deltaT,
y: vy - sf.y * deltaT
};
}
/**
* Get the spring force for the current map bounds
* (This method might need to move elsewhere to deal
* with different projections)
* @private
* @returns {object} The spring force
*/
function springForce() {
var xplus, // force to the right
xminus, // force to the left
yplus, // force to the top
yminus; // force to the bottom
if (!m_options.spring.enabled) {
return {x: 0, y: 0};
}
// get screen coordinates of corners
var ul = m_this.map().gcsToDisplay({
x: -180,
y: 82
});
var lr = m_this.map().gcsToDisplay({
x: 180,
y: -82
});
var c = m_options.spring.springConstant;
// Arg... map needs to expose the canvas size
var width = m_this.map().node().width();
var height = m_this.map().node().height();
xplus = c * Math.max(0, ul.x);
xminus = c * Math.max(0, width - lr.x);
yplus = c * Math.max(0, ul.y) / 2;
yminus = c * Math.max(0, height - lr.y) / 2;
return {
x: xplus - xminus,
y: yplus - yminus
};
}
////////////////////////////////////////////////////////////////////////////
/**
* Handle event when a mouse button is unpressed on the document.
* Removes temporary bindings.
*/
////////////////////////////////////////////////////////////////////////////
this._handleMouseUpDocument = function (evt) {
var selectionObj, oldAction;
if (m_paused) {
return;
}
// cancel queued interactions
m_queue = {};
m_clickMaybe = false;
m_this._getMouseButton(evt);
m_this._getMouseModifiers(evt);
// unbind temporary handlers on document
$(document).off('.geojs');
if (m_mouse.buttons.right) {
evt.preventDefault();
}
if (m_state.action === 'select') {
selectionObj = m_this._getSelection();
m_selectionLayer.clear();
m_this.map().deleteLayer(m_selectionLayer);
m_selectionLayer = null;
m_selectionPlane = null;
m_this.map().geoTrigger(geo.event.brushend, selectionObj);
}
// reset the interactor state
oldAction = m_state.action;
m_state = {};
// if momentum is enabled, start the action here
if (m_options.momentum.enabled && oldAction === 'pan') {
m_this.springBack(true);
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle event when a mouse button is unpressed
*/
////////////////////////////////////////////////////////////////////////////
this._handleMouseUp = function (evt) {
if (m_paused) {
return;
}
if (m_clickMaybe) {
m_this._handleMouseClick(evt);
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle event when a mouse click is detected. A mouse click is a simulated
* event that occurs when the time between a mouse down and mouse up
* is less than the configured duration and (optionally) if no mousemove
* events were triggered in the interim.
*/
////////////////////////////////////////////////////////////////////////////
this._handleMouseClick = function (evt) {
m_this._getMouseButton(evt);
m_this._getMouseModifiers(evt);
// cancel any ongoing pan action
m_this.cancel('pan');
// unbind temporary handlers on document
$(document).off('.geojs');
// reset click detector variable
m_clickMaybe = false;
// fire a click event
m_this.map().geoTrigger(geo.event.mouseclick, m_this.mouse());
};
////////////////////////////////////////////////////////////////////////////
/**
* Private wrapper around the map zoom method that is debounced to support
* discrete zoom interactions.
* @param {number} deltaZ The zoom increment
*/
////////////////////////////////////////////////////////////////////////////
function debounced_zoom() {
var deltaZ = 0, delay = 400, direction;
function accum(dz, dir) {
var map = m_this.map(), zoom;
direction = dir;
deltaZ += dz;
// Respond to debounced events when they add up to a change in the
// discrete zoom level.
if (map && Math.abs(deltaZ) >= 1 && m_options.discreteZoom) {
zoom = Math.round(deltaZ + map.zoom());
// delta is what is left over from the zoom delta after the new zoom value
deltaZ = deltaZ + map.zoom() - zoom;
map.zoom(zoom, direction);
}
}
function apply() {
var map = m_this.map(), zoom;
if (map) {
zoom = deltaZ + map.zoom();
if (m_options.discreteZoom) {
// round off the zoom to an integer and throw away the rest
zoom = Math.round(zoom);
}
map.zoom(zoom, direction);
}
deltaZ = 0;
}
if (m_options.discreteZoom !== true && m_options.discreteZoom > 0) {
delay = m_options.discreteZoom;
}
if (m_options.discreteZoom === true || m_options.discreteZoom > 0) {
return geo.util.debounce(delay, false, apply, accum);
} else {
return function (dz, dir) {
accum(dz, dir);
apply(dz, dir);
};
}
}
////////////////////////////////////////////////////////////////////////////
/**
* Attaches wrapped methods for accumulating fast mouse wheel events and
* throttling map interactions.
* @private
*/
////////////////////////////////////////////////////////////////////////////
function throttled_wheel() {
var my_queue = {};
function accum(evt) {
var dx, dy;
if (m_paused) {
return;
}
if (my_queue !== m_queue) {
my_queue = {
kind: 'wheel',
scroll: {x: 0, y: 0}
};
m_queue = my_queue;
}
evt.preventDefault();
// try to normalize deltas using the wheel event standard:
// https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent
evt.deltaFactor = 1;
if (evt.originalEvent.deltaMode === 1) {
// DOM_DELTA_LINE -- estimate line height
evt.deltaFactor = 40;
} else if (evt.originalEvent.deltaMode === 2) {
// DOM_DELTA_PAGE -- get window height
evt.deltaFactor = $(window).height();
}
// prevent NaN's on legacy browsers
dx = evt.originalEvent.deltaX || 0;
dy = evt.originalEvent.deltaY || 0;
// scale according to the options
dx = dx * m_options.wheelScaleX * evt.deltaFactor / 120;
dy = dy * m_options.wheelScaleY * evt.deltaFactor / 120;
my_queue.scroll.x += dx;
my_queue.scroll.y += dy;
}
function wheel(evt) {
var zoomFactor;
// If the current queue doesn't match the queue passed in as an argument,
// assume it was cancelled and do nothing.
if (my_queue !== m_queue) {
return;
}
// perform the map navigation event
m_this._getMouseModifiers(evt);
if (m_options.panWheelEnabled &&
eventMatch('wheel', m_options.panWheelModifiers)) {
m_this.map().pan({
x: m_queue.scroll.x,
y: m_queue.scroll.y
});
} else if (m_options.zoomWheelEnabled &&
eventMatch('wheel', m_options.zoomWheelModifiers)) {
zoomFactor = -m_queue.scroll.y;
m_callZoom(zoomFactor, m_mouse);
} else if (m_options.rotateWheelEnabled &&
eventMatch('wheel', m_options.rotateWheelModifiers)) {
m_this.map().rotation(
m_this.map().rotation() +
m_queue.scroll.y * m_options.rotateWheelScale,
m_mouse);
}
// reset the queue
m_queue = {};
}
if (m_options.throttle > 0) {
return geo.util.throttle(m_options.throttle, false, wheel, accum);
} else {
return function (evt) {
accum(evt);
wheel(evt);
};
}
}
////////////////////////////////////////////////////////////////////////////
/**
* Handle mouse wheel event. (Defined inside _connectEvents).
*/
////////////////////////////////////////////////////////////////////////////
this._handleMouseWheel = function () {};
////////////////////////////////////////////////////////////////////////////
/**
* Start up a spring back action when the map bounds are out of range.
* Not to be user callable.
* @todo Move this and momentum handling to the map class
* @protected
*
*/
////////////////////////////////////////////////////////////////////////////
this.springBack = function (initialVelocity) {
if (m_state.action === 'momentum') {
return;
}
if (!initialVelocity) {
m_mouse.velocity = {
x: 0,
y: 0
};
}
m_state.action = 'momentum';
m_state.origin = m_this.mouse();
m_state.start = new Date();
m_state.handler = function () {
var v, s, last, dt;
// Not sure the correct way to do this. We need the delta t for the
// next time step... Maybe use a better interpolator and the time
// parameter from requestAnimationFrame.
dt = Math.min(m_mouse.deltaTime, 30);
if (m_state.action !== 'momentum' ||
!m_this.map() ||
m_this.map().transition()) {
// cancel if a new action was performed
return;
}
last = m_state.start.valueOf();
m_state.start = new Date();
v = modifyVelocity(m_mouse.velocity, m_state.start - last);
// stop panning when the speed is below the threshold
if (!v) {
m_state = {};
return;
}
s = calcSpeed(v);
if (s > m_options.momentum.maxSpeed) {
s = m_options.momentum.maxSpeed / s;
v.x = v.x * s;
v.y = v.y * s;
}
if (!isFinite(v.x) || !isFinite(v.y)) {
v.x = 0;
v.y = 0;
}
m_mouse.velocity.x = v.x;
m_mouse.velocity.y = v.y;
m_this.map().pan({
x: m_mouse.velocity.x * dt,
y: m_mouse.velocity.y * dt
});
if (m_state.handler) {
window.requestAnimationFrame(m_state.handler);
}
};
if (m_state.handler) {
window.requestAnimationFrame(m_state.handler);
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle double click event
*/
////////////////////////////////////////////////////////////////////////////
this._handleDoubleClick = function () {
};
////////////////////////////////////////////////////////////////////////////
/**
* Public method that unbinds all events
*/
////////////////////////////////////////////////////////////////////////////
this.destroy = function () {
m_this._disconnectEvents();
m_this.map(null);
};
////////////////////////////////////////////////////////////////////////////
/**
* Get current mouse information
*/
////////////////////////////////////////////////////////////////////////////
this.mouse = function () {
return $.extend(true, {}, m_mouse);
};
////////////////////////////////////////////////////////////////////////////
/**
* Get current keyboard information
*/
////////////////////////////////////////////////////////////////////////////
this.keyboard = function () {
return $.extend(true, {}, m_keyboard);
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the current interactor state
*/
////////////////////////////////////////////////////////////////////////////
this.state = function () {
return $.extend(true, {}, m_state);
};
////////////////////////////////////////////////////////////////////////////
/**
* Get or set the pause state of the interactor, which
* ignores all native mouse and keyboard events.
*
* @param {bool} [value] The pause state to set or undefined to return the
* current state.
* @returns {bool|this}
*/
////////////////////////////////////////////////////////////////////////////
this.pause = function (value) {
if (value === undefined) {
return m_paused;
}
m_paused = !!value;
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Simulate a DOM mouse event on connected map.
*
* The options for creating the events are as follows, not all
* options are required for every event type. ::
*
* options = {
* page: {x, y} // position on the page
* map: {x, y} // position on the map (overrides page)
* button: 'left' | 'right' | 'middle'
* modifiers: [ 'alt' | 'ctrl' | 'meta' | 'shift' ]
* wheelDelta: {x, y}
* }
*
* @param {string} type Event type 'mousemove', 'mousedown', 'mouseup', ...
* @param {object} options
* @returns {mapInteractor}
*/
////////////////////////////////////////////////////////////////////////////
this.simulateEvent = function (type, options) {
var evt, page, offset, which;
if (!m_this.map()) {
return m_this;
}
page = options.page || {};
if (options.map) {
offset = $node.offset();
page.x = options.map.x + offset.left;
page.y = options.map.y + offset.top;
}
if (options.button === 'left') {
which = 1;
} else if (options.button === 'right') {
which = 3;
} else if (options.button === 'middle') {
which = 2;
}
options.modifiers = options.modifiers || [];
options.wheelDelta = options.wheelDelta || {};
evt = $.Event(
type,
{
pageX: page.x,
pageY: page.y,
which: which,
altKey: options.modifiers.indexOf('alt') >= 0,
ctrlKey: options.modifiers.indexOf('ctrl') >= 0,
metaKey: options.modifiers.indexOf('meta') >= 0,
shiftKey: options.modifiers.indexOf('shift') >= 0,
originalEvent: {
deltaX: options.wheelDelta.x,
deltaY: options.wheelDelta.y,
deltaMode: options.wheelMode
}
}
);
$node.trigger(evt);
};
this._connectEvents();
return this;
};
inherit(geo.mapInteractor, geo.object);
//////////////////////////////////////////////////////////////////////////////
/**
* Stores the current time for a map, triggers time keeping events, and
* handles the animation state and interaction.
*
* @class geo.clock
* @extends geo.object
* @returns {geo.clock}
*/
//////////////////////////////////////////////////////////////////////////////
geo.clock = function (opts) {
'use strict';
if (!(this instanceof geo.clock)) {
return new geo.clock(opts);
}
opts = opts || {};
geo.object.call(this, opts);
//////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
//////////////////////////////////////////////////////////////////////////////
var m_this = this,
m_now = new Date(0),
m_start = null,
m_end = null,
m_step = null,
m_rate = null,
m_loop = Number.POSITIVE_INFINITY,
m_currentLoop = 0,
m_state = 'stop',
m_currentAnimation = null,
m_object = null;
//////////////////////////////////////////////////////////////////////////////
/**
* Get or set the geo.object to trigger events on.
*/
//////////////////////////////////////////////////////////////////////////////
this.object = function (arg) {
if (arg === undefined) {
return m_object;
}
m_object = arg;
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Returns true if attached to a valid geo.object.
* @private
*/
//////////////////////////////////////////////////////////////////////////////
this._attached = function () {
return (m_object instanceof geo.object);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Get or set the current time.
*/
//////////////////////////////////////////////////////////////////////////////
this.now = function (arg) {
var previous = m_now;
if (arg === undefined) {
return m_now;
}
m_now = arg;
if (m_now !== previous &&
m_this._attached()) {
m_this.object().geoTrigger(geo.event.clock.change, {
previous: previous,
current: m_now,
clock: m_this
});
}
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Get or set the animation start time.
*/
//////////////////////////////////////////////////////////////////////////////
this.start = function (arg) {
if (arg === undefined) {
return m_start;
}
m_start = arg;
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Get or set the animation end time.
*/
//////////////////////////////////////////////////////////////////////////////
this.end = function (arg) {
if (arg === undefined) {
return m_end;
}
m_end = arg;
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Get or set the animation time step.
*/
//////////////////////////////////////////////////////////////////////////////
this.step = function (arg) {
if (arg === undefined) {
return m_step;
}
m_step = arg;
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Get or set looping control of the clock. This controls how many times the
* animation will repeat before stopping. Default
* ``Number.POSITIVE_INFINITY``, the animation repeats forever.
*/
//////////////////////////////////////////////////////////////////////////////
this.loop = function (arg) {
if (arg === undefined) {
return m_loop;
}
m_loop = arg;
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Get or set the animation state. Valid values are:
*
* * 'stop'
* * 'play'
* * 'pause'
*
* This will also trigger relevant events, but they may be fired
* asynchronously.
*/
//////////////////////////////////////////////////////////////////////////////
this.state = function (arg, step) {
if (arg === undefined) {
return m_state;
}
if (['stop', 'play', 'pause'].indexOf(arg) < 0) {
console.log('WARNING: Ignored invalid state: ' + arg);
return m_this;
}
if (arg === 'play' && m_state === 'stop') {
// reset animation parameters
m_currentLoop = 0;
m_this.now(m_this.start());
}
if (arg === 'play' && m_state !== 'play') {
// Start a new animation.
m_state = arg;
m_this._animate(step || 1);
}
m_state = arg;
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Get or set the animation frame rate. This is approximately the number
* of frames displayed per second. A null value will use the browser's
* native requestAnimationFrame to draw new frames.
*/
//////////////////////////////////////////////////////////////////////////////
this.framerate = function (arg) {
if (arg === undefined) {
return m_rate;
}
m_rate = arg;
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Step to the next frame in the animation. Pauses the animation if it is
* playing.
*/
//////////////////////////////////////////////////////////////////////////////
this.stepForward = function () {
m_this.state('pause');
m_this._setNextFrame(1);
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Step to the previous frame in the animation. Pauses the animation if it is
* playing.
*/
//////////////////////////////////////////////////////////////////////////////
this.stepBackward = function () {
m_this.state('pause');
m_this._setNextFrame(-1);
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Step to the next frame in the animation. Will set the state to stop
* if the animation has reached the end and there are no more loops.
* @private
*/
//////////////////////////////////////////////////////////////////////////////
this._setNextFrame = function (step) {
var next = new Date(m_this.now().valueOf() + step * m_this.step());
if (next >= m_this.end() || next <= m_this.start()) {
if (m_this.loop() <= m_currentLoop) {
m_this.state('stop');
return;
}
m_currentLoop += 1;
if (step >= 0) {
m_this.now(m_this.start());
} else {
m_this.now(m_this.end());
}
return;
}
m_this.now(next);
};
//////////////////////////////////////////////////////////////////////////////
/**
* Start an animation.
* @param {integer} step The animation frame step (+1 for forward -1 for
* reverse, etc).
* @private
*/
//////////////////////////////////////////////////////////////////////////////
this._animate = function (step) {
var myAnimation = {};
m_currentAnimation = myAnimation;
function frame() {
if (myAnimation !== m_currentAnimation) {
// A new animation has started, so kill this one.
return;
}
m_this._setNextFrame(step);
if (m_this.state() === 'play') {
// Queue the next frame
if (!m_this.framerate()) {
window.requestAnimationFrame(frame);
} else {
window.setTimeout(frame, 1000 / m_this.framerate());
}
} else if (m_this._attached()) {
m_this.object().geoTrigger(geo.event.clock[m_this.state()], {
current: m_this.now(),
clock: m_this
});
}
}
// trigger the play event
if (m_this._attached()) {
m_this.object().geoTrigger(geo.event.clock.play, {
current: m_this.now(),
clock: m_this
});
}
// Queue the first frame
if (!m_this.framerate()) {
window.requestAnimationFrame(frame);
} else {
window.setTimeout(frame, 1000 / m_this.framerate());
}
};
};
inherit(geo.clock, geo.object);
(function () {
'use strict';
//////////////////////////////////////////////////////////////////////////////
/**
* This class defines the raw interface for a "tile" on a map. A tile is
* defined as a rectangular section of a map. The base implementation
* is independent of the actual content of the tile, but assumes that
* the content is loaded asynchronously via a url. The tile object
* has a promise-like interface. For example,
*
* tile.then(function (data) {...}).catch(function (data) {...});
*
* @class
* @param {Object} spec The tile specification object
*
* @param {Object} spec.index The global position of the tile
* @param {Number} spec.index.x The x-coordinate (usually the column number)
* @param {Number} spec.index.y The y-coordinate (usually the row number)
*
* @param {Object} spec.size The size of each tile
* @param {Number} spec.size.x Width (usually in pixels)
* @param {Number} spec.size.y Height (usually in pixels)
*
* @param {Object|String} spec.url A url or jQuery ajax config object
*
* @param {Object?} spec.overlap The size of overlap with neighboring tiles
* @param {Number} [spec.overlap.x=0]
* @param {Number} [spec.overlap.y=0]
*/
//////////////////////////////////////////////////////////////////////////////
geo.tile = function (spec) {
if (!(this instanceof geo.tile)) {
return new geo.tile(spec);
}
this._index = spec.index;
this._size = spec.size;
this._overlap = spec.overlap || {x: 0, y: 0};
this._wrap = spec.wrap || {x: 1, y: 1};
this._url = spec.url;
this._fetched = false;
this._queue = spec.queue || null;
/**
* Return the index coordinates.
*/
Object.defineProperty(this, 'index', {
get:
function () { return this._index; }
});
/**
* Return the tile sizes.
*/
Object.defineProperty(this, 'size', {
get: function () { return this._size; }
});
/**
* Return the tile overlap sizes.
*/
Object.defineProperty(this, 'overlap', {
get: function () { return this._overlap; }
});
/**
* Initiate the ajax request and add a promise interface
* to the tile object. This method exists to allow
* derived classes the ability to override how the tile
* is obtained. For example, imageTile uses an Image
* element rather than $.get.
*/
this.fetch = function () {
if (!this._fetched) {
$.get(this._url).then(function () {
this._fetched = true;
}.bind(this)).promise(this);
}
return this;
};
/**
* Return whether this tile has been fetched already.
*
* @returns {boolean} True if the tile has been fetched.
*/
this.fetched = function () {
return this._fetched;
};
/**
* Add a method to be called with the data when the ajax request is
* successfully resolved.
*
* @param {function?} onSuccess The success handler
* @param {function?} onFailure The failure handler
* @returns {this} Supports chained calling
*
*/
this.then = function (onSuccess, onFailure) {
// both fetch and _queueAdd can replace the current then method
if (!this.fetched() && this._queue && this._queue.add && (!this.state ||
this.state() === 'pending')) {
this._queue.add(this, this.fetch);
} else {
this.fetch();
}
// Call then on the new promise
this.then(onSuccess, onFailure);
return this;
};
/**
* Add a method to be called with the data when the ajax fails.
*
* @param {function} method The rejection handler
* @returns {this} Supports chained calling
*
*/
this.catch = function (method) {
this.then(undefined, method);
return this;
};
/**
* Return a unique string representation of the given tile useable
* as a hash key. Possibly extend later to include url information
* to make caches aware of the tile source.
* @returns {string}
*/
this.toString = function () {
return [this._index.level || 0, this._index.y, this._index.x].join('_');
};
/**
* Return the bounds of the tile given an index offset and
* a translation.
*
* @param {object} index The tile index containing (0, 0)
* @param {object} shift The coordinates of (0, 0) inside the tile
*/
this.bounds = function (index, shift) {
var left, right, bottom, top;
left = this.size.x * (this.index.x - index.x) - this.overlap.x - shift.x;
right = left + this.size.x + this.overlap.x * 2;
top = this.size.y * (this.index.y - index.y) - this.overlap.y - shift.y;
bottom = top + this.size.y + this.overlap.y * 2;
return {
left: left,
right: right,
bottom: bottom,
top: top
};
};
/**
* Computes the global coordinates of the bottom edge.
* @returns {number}
*/
Object.defineProperty(this, 'bottom', {
get: function () {
return this.size.y * (this.index.y + 1) + this.overlap.y;
}
});
/**
* Computes the global coordinates of the top edge.
* @returns {number}
*/
Object.defineProperty(this, 'top', {
get: function () {
return this.size.y * this.index.y - this.overlap.y;
}
});
/**
* Computes the global coordinates of the left edge.
* @returns {number}
*/
Object.defineProperty(this, 'left', {
get: function () {
return this.size.x * this.index.x - this.overlap.x;
}
});
/**
* Computes the global coordinates of the right edge.
* @returns {number}
*/
Object.defineProperty(this, 'right', {
get: function () {
return this.size.x * (this.index.x + 1) + this.overlap.x;
}
});
/**
* Returns the global image size at this level.
* @returns {number}
*/
Object.defineProperty(this, 'levelSize', {
value: {
width: Math.pow(2, this.index.level || 0) * this.size.x,
height: Math.pow(2, this.index.level || 0) * this.size.y
}
});
/**
* Set the opacity of the tile to 0 and gradually fade in
* over the given number of milliseconds. This will also
* resolve the embedded promise interface.
* @param {number} duration the duration of the animation in ms
* @returns {this} chainable
*/
this.fadeIn = function (duration) {
$.noop(duration);
return this;
};
};
})();
(function () {
'use strict';
//////////////////////////////////////////////////////////////////////////////
/**
* This class defines a tile that is part of a standard "image pyramid", such
* as an open street map tile set. Every tile is uniquely indexed by a row,
* column, and zoom level. The number of rows/columns at zoom level z is
* `2^z`, the number of pixels per tile is configurable.
*
* By default, this class assumes that images are fetch from the url, but
* subclasses may define additional rendering steps to produce the images
* before passing them off to the handlers.
*
* @class
* @param {Object} spec The tile specification object
*
* @param {Object} spec.index The global position of the tile
* @param {Number} spec.index.x The x-coordinate (usually the column number)
* @param {Number} spec.index.y The y-coordinate (usually the row number)
* @param {Number} spec.index.level The zoom level
*
* @param {Object?} spec.size The size of each tile
* @param {Number} [spec.size.x=256] Width in pixels
* @param {Number} [spec.size.y=256] Height in pixels
*
* @param {String} spec.url A url to the image
* @param {String} [spec.crossDomain='anonymous'] Image CORS attribute
*
* @param {Object} spec.overlap The size of overlap with neighboring tiles
* @param {Number} [spec.overlap.x=0]
* @param {Number} [spec.overlap.y=0]
*/
//////////////////////////////////////////////////////////////////////////////
geo.imageTile = function (spec) {
if (!(this instanceof geo.imageTile)) {
return new geo.imageTile(spec);
}
spec.size = spec.size || {x: 256, y: 256};
this._image = null;
// Cache the coordinate scaling
this._cors = spec.crossDomain || 'anonymous';
// Call superclass constructor
geo.tile.call(this, spec);
/**
* Read only accessor to the Image object used by the
* tile. Note, this method does not gaurantee that the
* image data is available. Use the promise interface
* to add asyncronous handlers.
* @returns {Image}
*/
Object.defineProperty(this, 'image', {
get: function () { return this._image; }
});
/**
* Initiate the image request.
*/
this.fetch = function () {
var defer;
if (!this._image) {
this._image = new Image(this.size.x, this.size.y);
// Only set the crossOrigin parameter if this is going across origins.
if (this._url.indexOf(':') >= 0 && this._url.indexOf('/') >= 0 &&
this._url.indexOf(':') < this._url.indexOf('/')) {
this._image.crossOrigin = this._cors;
}
defer = new $.Deferred();
this._image.onload = defer.resolve;
this._image.onerror = defer.reject;
this._image.src = this._url;
// attach a promise interface to `this`
defer.then(function () {
this._fetched = true;
}.bind(this)).promise(this);
}
return this;
};
/**
* Set the opacity of the tile to 0 and gradually fade in
* over the given number of milliseconds. This will also
* resolve the embedded promise interface.
* @param {number} duration the duration of the animation in ms
* @returns {this} chainable
*/
this.fadeIn = function (duration) {
var promise = this.fetch(), defer = new $.Deferred();
$(this._image).css('display', 'none');
promise.then(function () {
$(this._image).fadeIn(duration, function () {
defer.resolve();
});
}.bind(this));
return defer.promise(this);
};
return this;
};
inherit(geo.imageTile, geo.tile);
})();
(function () {
'use strict';
//////////////////////////////////////////////////////////////////////////////
/**
* This class implements a simple cache for tile objects. Each tile is
* stored in cache object keyed by a configurable hashing function. Another
* array keeps track of last access times for each tile to purge old tiles
* once the maximum cache size is reached.
*
* @class
*
* @param {Object?} [options] A configuratoin object for the cache
* @param {Number} [options.size=64] The maximum number of tiles to store
*/
//////////////////////////////////////////////////////////////////////////////
geo.tileCache = function (options) {
if (!(this instanceof geo.tileCache)) {
return new geo.tileCache(options);
}
options = options || {};
this._size = options.size || 64;
/**
* Get/set the maximum cache size.
*/
Object.defineProperty(this, 'size', {
get: function () { return this._size; },
set: function (n) {
while (this._atime.length > n) {
this.remove(this._atime[this._atime.length - 1]);
}
this._size = n;
}
});
/**
* Get the current cache size.
*/
Object.defineProperty(this, 'length', {
get: function () { return this._atime.length; }
});
/**
* Get the position of the tile in the access queue.
* @param {string} hash The tile's hash value
* @returns {number} The position in the queue or -1
*/
this._access = function (hash) {
return this._atime.indexOf(hash);
};
/**
* Remove a tile from the cache.
* @param {string|geo.tile} tile The tile or its hash
* @returns {bool} true if a tile was removed
*/
this.remove = function (tile) {
var hash = typeof tile === 'string' ? tile : tile.toString();
// if the tile is not in the cache
if (!(hash in this._cache)) {
return false;
}
// Remove the tile from the access queue
this._atime.splice(this._access(hash), 1);
// Remove the tile from the cache
delete this._cache[hash];
return true;
};
/**
* Remove all tiles from the cache.
*/
this.clear = function () {
this._cache = {}; // The hash -> tile mapping
this._atime = []; // The access queue (the hashes are stored)
return this;
};
/**
* Get a tile from the cache if it exists, otherwise
* return null. This method also moves the tile to the
* front of the access queue.
*
* @param {string|geo.tile} hash The tile or the tile hash value
* @param {boolean} noMove if true, don't move the tile to the front of the
* access queue.
* @returns {geo.tile|null}
*/
this.get = function (hash, noMove) {
hash = typeof hash === 'string' ? hash : hash.toString();
if (!(hash in this._cache)) {
return null;
}
if (!noMove) {
this._atime.splice(this._access(hash), 1);
this._atime.unshift(hash);
}
return this._cache[hash];
};
/**
* Add a tile to the cache.
* @param {geo.tile} tile
* @param {function} removeFunc if specified and tiles must be purged from
* the cache, call this function on each tile before purging.
* @param {boolean} noPurge if true, don't purge tiles.
*/
this.add = function (tile, removeFunc, noPurge) {
// remove any existing tiles with the same hash
this.remove(tile);
var hash = tile.toString();
// add the tile
this._cache[hash] = tile;
this._atime.unshift(hash);
if (!noPurge) {
this.purge(removeFunc);
}
};
/**
* Purge tiles from the cache if it is full.
* @param {function} removeFunc if specified and tiles must be purged from
* the cache, call this function on each tile before purging.
*/
this.purge = function (removeFunc) {
var hash;
while (this._atime.length > this.size) {
hash = this._atime.pop();
var tile = this._cache[hash];
if (removeFunc) {
removeFunc(tile);
}
delete this._cache[hash];
}
};
this.clear();
return this;
};
})();
(function () {
'use strict';
/**
* Standard modulo operator where the output is in [0, b) for all inputs.
* @private
*/
function modulo(a, b) {
return ((a % b) + b) % b;
}
/**
* Pick a subdomain from a list of subdomains based on a the tile location.
*
* @param {number} x: the x tile coordinate.
* @param {number} y: the y tile coordinate.
* @param {list} subdomains: the list of known subdomains.
*/
function m_getTileSubdomain(x, y, subdomains) {
return subdomains[modulo(x + y, subdomains.length)];
}
/**
* Returns an OSM tile server formatting function from a standard format
* string. Replaces {s}, {z}, {x}, and {y}.
*
* @param {string} base The tile format string
* @returns: a conversion function.
* @private.
*/
function m_tileUrlFromTemplate(base) {
return function (x, y, z, subdomains) {
return base.replace('{s}', m_getTileSubdomain(x, y, subdomains))
.replace('{z}', z)
.replace('{x}', x)
.replace('{y}', y);
};
}
//////////////////////////////////////////////////////////////////////////////
/**
* This method defines a tileLayer, which is an abstract class defining a
* layer divided into tiles of arbitrary data. Notably, this class provides
* the core functionality of the osmLayer, but hooks exist to render tiles
* dynamically from vector data, or to display arbitrary grids of images
* in a custom coordinate system. When multiple zoom levels are present
* in a given dataset, this class assumes that the space occupied by
* tile (i, j) at level z is covered by a 2x2 grid of tiles at zoom
* level z + 1:
*
* (2i, 2j), (2i, 2j + 1)
* (2i + 1, 2j), (2i + 1, 2j + 1)
*
* The higher level tile set should represent a 2x increase in resolution.
*
* Although not currently supported, this class is intended to extend to
* 3D grids of tiles as well where 1 tile is covered by a 2x2x2 grid of
* tiles at the next level. The tiles are assumed to be rectangular,
* identically sized, and aligned with x/y axis of the underlying
* coordinate system. The local coordinate system is in pixels relative
* to the current zoom level and changes when the zoom level crosses an
* integer threshold.
*
* The constructor takes a number of optional parameters that customize
* the display of the tiles. The default values of these options are
* stored as the `defaults` attribution on the constructor. Supporting
* alternate tiling protocols often only requires adjusting these
* defaults.
*
* @class
* @extends geo.featureLayer
* @param {object?} options
* @param {number} [options.minLevel=0] The minimum zoom level available
* @param {number} [options.maxLevel=18] The maximum zoom level available
* @param {number} [options.tileOverlap=0]
* Number of pixels of overlap between tiles
* @param {number} [options.tileWidth=256]
* The tile width as displayed without overlap
* @param {number} [options.tileHeight=256]
* The tile height as displayed without overlap
* @param {number} [options.cacheSize=400] The maximum number of tiles to
* cache. The default is 200 if keepLower is false.
* @param {bool} [options.keepLower=true]
* Keep lower zoom level tiles when showing high zoom level tiles. This
* uses more memory but results in smoother transitions.
* @param {bool} [options.wrapX=true] Wrap in the x-direction
* @param {bool} [options.wrapY=false] Wrap in the y-direction
* @param {function|string} [options.url=null]
* A function taking the current tile indices and returning a URL or jquery
* ajax config to be passed to the {geo.tile} constructor.
* Example:
* (x, y, z, subdomains) => "http://example.com/z/y/x.png"
* If this is a string, a template url with {x}, {y}, {z}, and {s} as
* template variables. {s} picks one of the subdomains parameter.
* @param {string|list} [options.subdomain="abc"] Subdomains to use in
* template url strings. If a string, this is converted to a list before
* being passed to a url function.
* @param {string} [options.baseUrl=null] If defined, use the old-style base
* url instead of the options.url parameter. This is functionally the same
* as using a url of baseUrl/{z}/{x}/{y}.(options.imageFormat || png). If
* the specified string does not end in a slash, one is added.
* @param {string} [options.imageFormat='png']
* This is only used if a baseUrl is specified, in which case it determines
* the image name extension used in the url.
* @param {number} [options.animationDuration=0]
* The number of milliseconds for the tile loading animation to occur. **This
* option is currently buggy because old tiles will purge before the animation
* is complete.**
* @param {string} [options.attribution]
* An attribution to display with the layer (accepts HTML)
* @param {function} [options.tileRounding=Math.round]
* This function determines which tiles will be loaded when the map is at
* a non-integer zoom. For example, `Math.floor`, will use tile level 2
* when the map is at zoom 2.9.
* @param {function} [options.tileOffset]
* This function takes a zoom level argument and returns, in units of
* pixels, the coordinates of the point (0, 0) at the given zoom level
* relative to the bottom left corner of the domain.
* @param {bool} [options.topDown=false] True if the gcs is top-down,
* false if bottom-up (the ingcs does not matter, only the gcs coordinate
* system). When false, this inverts the gcs y-coordinate when calculating
* local coordinates.
* @returns {geo.tileLayer}
*/
//////////////////////////////////////////////////////////////////////////////
geo.tileLayer = function (options) {
if (!(this instanceof geo.tileLayer)) {
return new geo.tileLayer(options);
}
geo.featureLayer.call(this, options);
options = $.extend(true, {}, this.constructor.defaults, options || {});
if (!options.cacheSize) {
// this size should be sufficient for a 4k display
options.cacheSize = options.keepLower ? 600 : 200;
}
if ($.type(options.subdomains) === 'string') {
options.subdomains = options.subdomains.split('');
}
/* We used to call the url option baseUrl. If a baseUrl is specified, use
* it instead of url, interpretting it as before. */
if (options.baseUrl) {
var url = options.baseUrl;
if (url && url.charAt(url.length - 1) !== '/') {
url += '/';
}
options.url = url + '{z}/{x}/{y}.' + (options.imageFormat || 'png');
}
/* Save the original url so that we can return it if asked */
options.originalUrl = options.url;
if ($.type(options.url) === 'string') {
options.url = m_tileUrlFromTemplate(options.url);
}
var s_init = this._init,
s_exit = this._exit,
m_lastTileSet = [],
m_exited;
// copy the options into a private variable
this._options = $.extend(true, {}, options);
// set the layer attribution text
this.attribution(options.attribution);
// initialize the object that keeps track of actively drawn tiles
this._activeTiles = {};
// initialize the object that stores active tile regions in a
// tree-like structure providing quick queries to determine
// if tiles are completely obscured or not.
this._tileTree = {};
// initialize the in memory tile cache
this._cache = geo.tileCache({size: options.cacheSize});
// initialize the tile fetch queue
this._queue = geo.fetchQueue({
// this should probably be 6 * subdomains.length if subdomains are used
size: 6,
// if track is the same as the cache size, then neither processing time
// nor memory will be wasted. Larger values will use more memory,
// smaller values will do needless computations.
track: options.cacheSize,
needed: function (tile) {
return tile === this.cache.get(tile.toString(), true);
}.bind(this)
});
var m_tileOffsetValues = {};
/**
* Readonly accessor to the options object
*/
Object.defineProperty(this, 'options', {get: function () {
return $.extend({}, this._options);
}});
/**
* Readonly accessor to the tile cache object.
*/
Object.defineProperty(this, 'cache', {get: function () {
return this._cache;
}});
/**
* Readonly accessor to the active tile mapping. This is an object containing
* all currently drawn tiles (hash(tile) => tile).
*/
Object.defineProperty(this, 'activeTiles', {get: function () {
return $.extend({}, this._activeTiles); // copy on output
}});
/**
* The number of tiles at the given zoom level
* The default implementation just returns `Math.pow(2, z)`.
* @param {number} level A zoom level
* @returns {{x: nx, y: ny}} The number of tiles in each axis
*/
this.tilesAtZoom = function (level) {
var s = Math.pow(2, level);
return {x: s, y: s};
};
/**
* Returns true if the given tile index is valid:
* * min level <= level <= max level
* * 0 <= x <= 2^level - 1
* * 0 <= y <= 2^level - 1
* @param {object} index The tile index
* @param {number} index.x
* @param {number} index.y
* @param {number} index.level
* @returns {geo.tile}
*/
this.isValid = function (index) {
if (!(this._options.minLevel <= index.level &&
index.level <= this._options.maxLevel)) {
return false;
}
if (!(this._options.wrapX ||
0 <= index.x &&
index.x <= this.tilesAtZoom(index.level).x - 1)) {
return false;
}
if (!(this._options.wrapY ||
0 <= index.y &&
index.y <= this.tilesAtZoom(index.level).y - 1)) {
return false;
}
return true;
};
/**
* Returns the current origin tile and offset at the given zoom level.
* This is intended to be cached in the future to optimize coordinate
* transformations.
* @protected
* @param {number} level The target zoom level
* @returns {object} {index: {x, y}, offset: {x, y}}
*/
this._origin = function (level) {
var origin = this.toLevel(this.toLocal(this.map().origin()), level),
o = this._options,
index, offset;
// get the tile index
index = {
x: Math.floor(origin.x / o.tileWidth),
y: Math.floor(origin.y / o.tileHeight)
};
// get the offset inside the tile (in pixels)
// This computation should contain the only numerically unstable
// subtraction in this class. All other methods will assume
// coordinates are given relative to the map origin.
offset = {
x: origin.x - o.tileWidth * index.x,
y: origin.y - o.tileHeight * index.y
};
return {index: index, offset: offset};
};
/**
* Returns a tile's bounds in its level coordinates.
* @param {geo.tile} tile
* @returns {object} bounds
*/
this._tileBounds = function (tile) {
var origin = this._origin(tile.index.level);
return tile.bounds(origin.index, origin.offset);
};
/**
* Returns the tile indices at the given point.
* @param {object} point The coordinates in pixels relative to the map origin.
* @param {number} point.x
* @param {number} point.y
* @param {number} level The target zoom level
* @returns {object} The tile indices
*/
this.tileAtPoint = function (point, level) {
var o = this._origin(level);
var map = this.map();
point = this.displayToLevel(map.gcsToDisplay(point, null), level);
if (isNaN(point.x)) { point.x = 0; }
if (isNaN(point.y)) { point.y = 0; }
var to = this._tileOffset(level);
if (to) {
point.x += to.x;
point.y += to.y;
}
var tile = {
x: Math.floor(
o.index.x + (o.offset.x + point.x) / this._options.tileWidth
),
y: Math.floor(
o.index.y + (o.offset.y + point.y) / this._options.tileHeight
)
};
return tile;
};
/**
* Returns a tile's bounds in a gcs.
* @param {object|tile} either a tile or an object with {x, y, level}
* specifying a tile.
* @param {string|geo.transform} [gcs] undefined to use the interface gcs,
* null to use the map gcs, or any other transform.
* @returns {object} The tile bounds in the specified gcs.
*/
this.gcsTileBounds = function (indexOrTile, gcs) {
var tile = (indexOrTile.index ? indexOrTile : geo.tile({
index: indexOrTile,
size: {x: this._options.tileWidth, y: this._options.tileHeight},
url: ''
}));
var to = this._tileOffset(tile.index.level),
bounds = tile.bounds({x: 0, y: 0}, to),
map = this.map(),
unit = map.unitsPerPixel(tile.index.level);
var coord = [{
x: bounds.left * unit, y: this._topDown() * bounds.top * unit
}, {
x: bounds.right * unit, y: this._topDown() * bounds.bottom * unit
}];
gcs = (gcs === null ? map.gcs() : (
gcs === undefined ? map.ingcs() : gcs));
if (gcs !== map.gcs()) {
coord = geo.transform.transformCoordinates(gcs, map.gcs(), coord);
}
return {
left: coord[0].x,
top: coord[0].y,
right: coord[1].x,
bottom: coord[1].y
};
};
/**
* Returns an instantiated tile object with the given indices. This
* method always returns a new tile object. Use `_getTileCached`
* to use the caching layer.
* @param {object} index The tile index
* @param {number} index.x
* @param {number} index.y
* @param {number} index.level
* @param {object} source The tile index used for constructing the url
* @param {number} source.x
* @param {number} source.y
* @param {number} source.level
* @returns {geo.tile}
*/
this._getTile = function (index, source) {
var urlParams = source || index;
return geo.tile({
index: index,
size: {x: this._options.tileWidth, y: this._options.tileHeight},
queue: this._queue,
url: this._options.url(urlParams.x, urlParams.y, urlParams.level || 0,
this._options.subdomains)
});
};
/**
* Returns an instantiated tile object with the given indices. This
* method is similar to `_getTile`, but checks the cache before
* generating a new tile.
* @param {object} index The tile index
* @param {number} index.x
* @param {number} index.y
* @param {number} index.level
* @param {object} source The tile index used for constructing the url
* @param {number} source.x
* @param {number} source.y
* @param {number} source.level
* @param {boolean} delayPurge If true, don't purge tiles from the cache
* @returns {geo.tile}
*/
this._getTileCached = function (index, source, delayPurge) {
var tile = this.cache.get(this._tileHash(index));
if (tile === null) {
tile = this._getTile(index, source);
this.cache.add(tile, this.remove.bind(this), delayPurge);
}
return tile;
};
/**
* Returns a string representation of the tile at the given index.
* This method is used as a hashing function for the caching layer.
*
* Note: This method _must_ return the same string as:
*
* tile({index: index}).toString();
*
* @param {object} index The tile index
* @param {number} index.x
* @param {number} index.y
* @param {number} index.level
* @returns {string}
*/
this._tileHash = function (index) {
return [index.level || 0, index.y, index.x].join('_');
};
/**
* Returns the optimal starting and ending tile indices
* (inclusive) necessary to fill the given viewport.
* @param {number} level The zoom level
* @param {object} bounds The map bounds in world coordinates
*/
this._getTileRange = function (level, bounds) {
var corners = [
this.tileAtPoint({x: bounds.left, y: bounds.top}, level),
this.tileAtPoint({x: bounds.right, y: bounds.top}, level),
this.tileAtPoint({x: bounds.left, y: bounds.bottom}, level),
this.tileAtPoint({x: bounds.right, y: bounds.bottom}, level)
];
return {
start: {
x: Math.min(corners[0].x, corners[1].x, corners[2].x, corners[3].x),
y: Math.min(corners[0].y, corners[1].y, corners[2].y, corners[3].y)
},
end: {
x: Math.max(corners[0].x, corners[1].x, corners[2].x, corners[3].x),
y: Math.max(corners[0].y, corners[1].y, corners[2].y, corners[3].y)
}
};
};
/**
* Returns a list of tiles necessary to fill the screen at the given
* zoom level, center point, and viewport size. The list is optionally
* ordered by loading priority (center tiles first).
*
* @protected
* @param {number} maxLevel The zoom level
* @param {object} bounds The map bounds
* @param {boolean} sorted Return a sorted list
* @param {boolean} onlyIfChanged If the set of tiles have not changed
* (even if their desired order has), return undefined instead of an
* array of tiles.
* @returns {geo.tile[]} An array of tile objects
*/
this._getTiles = function (maxLevel, bounds, sorted, onlyIfChanged) {
var i, j, tiles = [], index, nTilesLevel,
start, end, indexRange, source, center, changed = false, old, level,
minLevel = (this._options.keepLower ? this._options.minLevel :
maxLevel);
if (maxLevel < minLevel) {
maxLevel = minLevel;
}
/* Generate a list of the tiles that we want to create. This is done
* before sorting, because we want to actually generate the tiles in
* the sort order. */
for (level = minLevel; level <= maxLevel; level += 1) {
// get the tile range to fetch
indexRange = this._getTileRange(level, bounds);
start = indexRange.start;
end = indexRange.end;
// total number of tiles existing at this level
nTilesLevel = this.tilesAtZoom(level);
if (!this._options.wrapX) {
start.x = Math.min(Math.max(start.x, 0), nTilesLevel.x - 1);
end.x = Math.min(Math.max(end.x, 0), nTilesLevel.x - 1);
if (level === minLevel) {
start.x = 0;
end.x = nTilesLevel.x - 1;
}
}
if (!this._options.wrapY) {
start.y = Math.min(Math.max(start.y, 0), nTilesLevel.y - 1);
end.y = Math.min(Math.max(end.y, 0), nTilesLevel.y - 1);
if (level === minLevel) {
start.y = 0;
end.y = nTilesLevel.y - 1;
}
}
/* If we are reprojecting tiles, we need a check to not use all levels
* if the number of tiles is excessive. */
if (this._options.gcs && this._options.gcs !== this.map().gcs() &&
level !== minLevel &&
(end.x + 1 - start.x) * (end.y + 1 - start.y) >
(this.map().size().width * this.map().size().height /
this._options.tileWidth / this._options.tileHeight) * 16) {
break;
}
// loop over the tile range
for (i = start.x; i <= end.x; i += 1) {
for (j = start.y; j <= end.y; j += 1) {
index = {level: level, x: i, y: j};
source = {level: level, x: i, y: j};
if (this._options.wrapX) {
source.x = modulo(source.x, nTilesLevel.x);
}
if (this._options.wrapY) {
source.y = modulo(source.y, nTilesLevel.y);
}
if (this.isValid(source)) {
if (onlyIfChanged && tiles.length < m_lastTileSet.length) {
old = m_lastTileSet[tiles.length];
changed = changed || (index.level !== old.level ||
index.x !== old.x || index.y !== old.y);
}
tiles.push({index: index, source: source});
}
}
}
}
if (onlyIfChanged) {
if (!changed && tiles.length === m_lastTileSet.length) {
return;
}
m_lastTileSet.splice(0, m_lastTileSet.length);
$.each(tiles, function (idx, tile) {
m_lastTileSet.push(tile.index);
});
}
if (sorted) {
center = {
x: (start.x + end.x) / 2,
y: (start.y + end.y) / 2,
level: maxLevel,
bottomLevel: maxLevel
};
var numTiles = Math.max(end.x - start.x, end.y - start.y) + 1;
for (; numTiles >= 1; numTiles /= 2) {
center.bottomLevel -= 1;
}
tiles.sort(this._loadMetric(center));
/* If we are using a fetch queue, start a new batch */
if (this._queue) {
this._queue.batch(true);
}
}
if (this.cache.size < tiles.length) {
console.log('Increasing cache size to ' + tiles.length);
this.cache.size = tiles.length;
}
/* Actually get the tiles. */
for (i = 0; i < tiles.length; i += 1) {
tiles[i] = this._getTileCached(tiles[i].index, tiles[i].source, true);
}
this.cache.purge(this.remove.bind(this));
return tiles;
};
/**
* Prefetches tiles up to a given zoom level around a given bounding box.
*
* @param {number} level The zoom level
* @param {object} bounds The map bounds
* @returns {$.Deferred} resolves when all of the tiles are fetched
*/
this.prefetch = function (level, bounds) {
var tiles;
tiles = this._getTiles(level, bounds, true);
return $.when.apply($,
tiles.map(function (tile) {
return tile.fetch();
})
);
};
/**
* This method returns a metric that determines tile loading order. The
* default implementation prioritizes tiles that are closer to the center,
* or at a lower zoom level.
* @protected
* @param {index1} center The center tile
* @param {number} center.x
* @param {number} center.y
* @returns {function} A function accepted by Array.prototype.sort
*/
this._loadMetric = function (center) {
return function (a, b) {
var a0, b0, dx, dy, cx, cy, scale;
a = a.index || a;
b = b.index || b;
// shortcut if zoom level differs
if (a.level !== b.level) {
if (center.bottomLevel && ((a.level >= center.bottomLevel) !==
(b.level >= center.bottomLevel))) {
return a.level >= center.bottomLevel ? -1 : 1;
}
return a.level - b.level;
}
/* compute the center coordinates relative to a.level. Since we really
* care about the center of the tiles, use an offset */
scale = Math.pow(2, a.level - center.level);
cx = (center.x + 0.5) * scale - 0.5;
cy = (center.y + 0.5) * scale - 0.5;
// calculate distances to the center squared
dx = a.x - cx;
dy = a.y - cy;
a0 = dx * dx + dy * dy;
dx = b.x - cx;
dy = b.y - cy;
b0 = dx * dx + dy * dy;
// return negative if a < b, or positive if a > b
return a0 - b0;
};
};
/**
* Convert a coordinate from pixel coordinates at the given zoom
* level to world coordinates.
* @param {object} coord
* @param {number} coord.x The offset in pixels (level 0) from the left edge
* @param {number} coord.y The offset in pixels (level 0) from the bottom edge
* @param {number} level The zoom level of the source coordinates
*/
this.fromLevel = function (coord, level) {
var s = Math.pow(2, -level);
return {
x: coord.x * s,
y: coord.y * s
};
};
/**
* Convert a coordinate from layer coordinates to pixel coordinates at the
* given zoom level.
* @param {object} coord
* @param {number} coord.x The offset in pixels (level 0) from the left edge
* @param {number} coord.y The offset in pixels (level 0) from the bottom edge
* @param {number} level The zoom level of the new coordinates
*/
this.toLevel = function (coord, level) {
var s = Math.pow(2, level);
return {
x: coord.x * s,
y: coord.y * s
};
};
/**
* Draw the given tile on the active canvas.
* @param {geo.tile} tile The tile to draw
*/
this.drawTile = function (tile) {
var hash = tile.toString();
if (this._activeTiles.hasOwnProperty(hash)) {
// the tile is already drawn, move it to the top
this._moveToTop(tile);
} else {
// pass to the rendering implementation
this._drawTile(tile);
}
// add the tile to the active cache
this._activeTiles[hash] = tile;
};
/**
* Render the tile on the canvas. This implementation draws the tiles directly
* on the DOM using <img> tags. Derived classes should override this method
* to draw the tile on a renderer specific context.
* @protected
* @param {geo.tile} tile The tile to draw
*/
this._drawTile = function (tile) {
// Make sure this method is not called when there is
// a renderer attached.
//
if (this.renderer() !== null) {
throw new Error('This draw method is not valid on renderer managed layers.');
}
// get the layer node
var div = $(this._getSubLayer(tile.index.level)),
bounds = this._tileBounds(tile),
duration = this._options.animationDuration,
container = $('<div class="geo-tile-container"/>').attr(
'tile-reference', tile.toString());
// apply a transform to place the image correctly
container.append(tile.image);
container.css({
position: 'absolute',
left: (bounds.left - parseInt(div.attr('offsetx') || 0, 10)) + 'px',
top: (bounds.top - parseInt(div.attr('offsety') || 0, 10)) + 'px'
});
// apply fade in animation
if (duration > 0) {
tile.fadeIn(duration);
}
// append the image element
div.append(container);
// add an error handler
tile.catch(function () {
// May want to do something special here later
console.warn('Could not load tile at ' + tile.index);
this._remove(tile);
}.bind(this));
};
/**
* Remove the given tile from the canvas and the active cache.
* @param {geo.tile|string} tile The tile (or hash) to remove
* @returns {geo.tile} the tile removed from the active layer
*/
this.remove = function (tile) {
var hash = tile.toString();
var value = this._activeTiles[hash];
if (value instanceof geo.tile) {
this._remove(value);
}
delete this._activeTiles[hash];
return value;
};
/**
* Remove the given tile from the canvas. This implementation just
* finds and removes the <img> element created for the tile.
* @param {geo.tile|string} tile The tile object to remove
*/
this._remove = function (tile) {
if (tile.image) {
if (tile.image.parentElement) {
$(tile.image.parentElement).remove();
} else {
/* This shouldn't happen, but sometimes does. Originally it happened
* when a tile was removed from the cache before it was finished
* being used; there is still some much rarer condition that can
* cause it. Log that it happened until we can figure out how to fix
* the issue. */
console.log('No parent element to remove ' + tile.toString(), tile);
}
$(tile.image).remove();
}
};
/**
* Move the given tile to the top on the canvas.
* @param {geo.tile} tile The tile object to move
*/
this._moveToTop = function (tile) {
$.noop(tile);
};
/**
* Query the attached map for the current bounds and return them
* as pixels at the current zoom level.
* @returns {object}
* Bounds object with left, right, top, bottom keys
* @protected
*/
this._getViewBounds = function () {
var map = this.map(),
mapZoom = map.zoom(),
zoom = this._options.tileRounding(mapZoom),
scale = Math.pow(2, mapZoom - zoom),
size = map.size();
var ul = this.displayToLevel({x: 0, y: 0}),
ur = this.displayToLevel({x: size.width, y: 0}),
ll = this.displayToLevel({x: 0, y: size.height}),
lr = this.displayToLevel({x: size.width, y: size.height});
return {
level: zoom,
scale: scale,
left: Math.min(ul.x, ur.x, ll.x, lr.x),
right: Math.max(ul.x, ur.x, ll.x, lr.x),
top: Math.min(ul.y, ur.y, ll.y, lr.y),
bottom: Math.max(ul.y, ur.y, ll.y, lr.y)
};
};
/**
* Remove all inactive tiles from the display. An inactive tile
* is one that is no longer visible either because it was panned
* out of the active view or the zoom has changed.
* @protected
* @param {number} zoom Tiles (in bounds) at this zoom level will be kept
* @param {boolean} doneLoading If true, allow purging additional tiles.
* @param {object} bounds view bounds. If not specified, this is
* obtained from _getViewBounds().
*/
this._purge = function (zoom, doneLoading, bounds) {
var tile, hash;
// Don't purge tiles in an active update
if (this._updating) {
return;
}
// get the view bounds
if (!bounds) {
bounds = this._getViewBounds();
}
for (hash in this._activeTiles) {
tile = this._activeTiles[hash];
if (this._canPurge(tile, bounds, zoom, doneLoading)) {
this.remove(tile);
}
}
return this;
};
/**
* Remove all active tiles from the canvas.
* @returns {geo.tile[]} The array of tiles removed
*/
this.clear = function () {
var tiles = [], tile;
// ignoring the warning here because this is a privately
// controlled object with simple keys
for (tile in this._activeTiles) {
tiles.push(this.remove(tile));
}
// clear out the tile coverage tree
this._tileTree = {};
m_lastTileSet = [];
return tiles;
};
/**
* Reset the layer to the initial state, clearing the canvas
* and resetting the tile cache.
* @returns {this} Chainable
*/
this.reset = function () {
this.clear();
this._cache.clear();
};
/**
* Compute local coordinates from the given world coordinates. The
* tile layer uses units of pixels relative to the world space
* coordinate origin.
* @param {object} pt A point in world space coordinates
* @param {number|undefined} zoom If unspecified, use the map zoom.
* @returns {object} Local coordinates
*/
this.toLocal = function (pt, zoom) {
var map = this.map(),
unit = map.unitsPerPixel(zoom === undefined ? map.zoom() : zoom);
return {
x: pt.x / unit,
y: this._topDown() * pt.y / unit
};
};
/**
* Compute world coordinates from the given local coordinates. The
* tile layer uses units of pixels relative to the world space
* coordinate origin.
* @param {object} pt A point in world space coordinates
* @param {number|undefined} zoom If unspecified, use the map zoom.
* @returns {object} Local coordinates
*/
this.fromLocal = function (pt, zoom) {
//DWM:: these need to alawys use the *layer* unitsPerPixel, or possibly
//DWM:: convert tile space using a transform
var map = this.map(),
unit = map.unitsPerPixel(zoom === undefined ? map.zoom() : zoom);
return {
x: pt.x * unit,
y: this._topDown() * pt.y * unit
};
};
/**
* Return a factor for invertin the y units as appropriate.
* @return {number}
*/
this._topDown = function () {
return this._options.topDown ? 1 : -1;
};
/**
* Return the DOM element containing a level specific layer. This will
* create the element if it doesn't already exist.
* @param {number} level The zoom level of the layer to fetch
* @return {DOM}
*/
this._getSubLayer = function (level) {
if (!this.canvas()) {
return;
}
var node = this.canvas()
.find('div[data-tile-layer=' + level.toFixed() + ']').get(0);
if (!node) {
node = $(
'<div class=geo-tile-layer data-tile-layer="' + level.toFixed() + '"/>'
).css({
'transform-origin': '0px 0px',
'line-height': 0,
'font-size': 0
}).get(0);
this.canvas().append(node);
}
return node;
};
/**
* Set sublayer transforms to align them with the given zoom level.
* @param {number} level The target zoom level
* @param {object} view The view bounds. The top and left are used to
* adjust the offset of tile layers.
* @return {object} the x and y offsets for the current level.
*/
this._updateSubLayers = function (level, view) {
var canvas = this.canvas(),
lastlevel = parseInt(canvas.attr('lastlevel'), 10),
lastx = parseInt(canvas.attr('lastoffsetx') || 0, 10),
lasty = parseInt(canvas.attr('lastoffsety') || 0, 10);
if (lastlevel === level && Math.abs(lastx - view.left) < 65536 &&
Math.abs(lasty - view.top) < 65536) {
return {x: lastx, y: lasty};
}
var map = this.map(),
to = this._tileOffset(level),
x = parseInt((view.left + view.right - map.size().width) / 2 + to.x, 10),
y = parseInt((view.top + view.bottom - map.size().height) / 2 + to.y, 10);
canvas.find('.geo-tile-layer').each(function (idx, el) {
var $el = $(el),
layer = parseInt($el.data('tileLayer'), 10);
$el.css(
'transform',
'scale(' + Math.pow(2, level - layer) + ')'
);
var layerx = parseInt(x / Math.pow(2, level - layer), 10),
layery = parseInt(y / Math.pow(2, level - layer), 10),
dx = layerx - parseInt($el.attr('offsetx') || 0, 10),
dy = layery - parseInt($el.attr('offsety') || 0, 10);
$el.attr({offsetx: layerx, offsety: layery});
$el.find('.geo-tile-container').each(function (tileidx, tileel) {
$(tileel).css({
left: (parseInt($(tileel).css('left'), 10) - dx) + 'px',
top: (parseInt($(tileel).css('top'), 10) - dy) + 'px'
});
});
});
canvas.attr({lastoffsetx: x, lastoffsety: y, lastlevel: level});
return {x: x, y: y};
};
/**
* Update the view according to the map/camera.
* @returns {this} Chainable
*/
this._update = function (evt) {
/* Ignore zoom and rotate events, as they are ALWAYS followed by a pan
* event */
if (evt && evt.event && (evt.event.event === geo.event.zoom ||
evt.event.event === geo.event.rotate)) {
return;
}
var map = this.map(),
bounds = map.bounds(undefined, null),
tiles;
if (this._updateSubLayers) {
var mapZoom = map.zoom(),
zoom = this._options.tileRounding(mapZoom),
view = this._getViewBounds();
// Update the transform for the local layer coordinates
var offset = this._updateSubLayers(zoom, view) || {x: 0, y: 0};
var to = this._tileOffset(zoom);
if (this.renderer() === null) {
var scale = Math.pow(2, mapZoom - zoom),
rotation = map.rotation(),
rx = -to.x + -(view.left + view.right) / 2 + offset.x,
ry = -to.y + -(view.bottom + view.top) / 2 + offset.y,
dx = (rx + map.size().width / 2) * scale,
dy = (ry + map.size().height / 2) * scale;
this.canvas().css({
'transform-origin': '' +
-rx + 'px ' +
-ry + 'px'
});
var transform = 'translate(' + dx + 'px' + ',' + dy + 'px' + ')' +
'scale(' + scale + ')';
if (rotation) {
transform += 'rotate(' + (rotation * 180 / Math.PI) + 'deg)';
}
this.canvas().css('transform', transform);
}
/* Set some attributes that can be used by non-css based viewers. This
* doesn't include the map center, as that may need to be handled
* differently from the view center. */
this.canvas().attr({
scale: Math.pow(2, mapZoom - zoom),
dx: -to.x + -(view.left + view.right) / 2,
dy: -to.y + -(view.bottom + view.top) / 2,
offsetx: offset.x,
offsety: offset.y,
rotation: map.rotation()
});
}
tiles = this._getTiles(
zoom, bounds, true, true
);
if (tiles === undefined) {
return;
}
// reset the tile coverage tree
this._tileTree = {};
tiles.forEach(function (tile) {
if (tile.fetched()) {
/* if we have already fetched the tile, we know we can just draw it,
* as the bounds won't have changed since the call to _getTiles. */
this.drawTile(tile);
// mark the tile as covered
this._setTileTree(tile);
} else {
if (!tile._queued) {
tile.then(function () {
if (m_exited) {
/* If we have disconnected the renderer, do nothing. This
* happens when the layer is being deleted. */
return;
}
if (tile !== this.cache.get(tile.toString())) {
/* If the tile has fallen out of the cache, don't draw it -- it
* is untracked. This may be an indication that a larger cache
* should have been used. */
return;
}
/* Check if a tile is still desired. Don't draw it if it
* isn't. */
var mapZoom = map.zoom(),
zoom = this._options.tileRounding(mapZoom),
view = this._getViewBounds();
if (this._canPurge(tile, view, zoom)) {
this.remove(tile);
return;
}
this.drawTile(tile);
// mark the tile as covered
this._setTileTree(tile);
}.bind(this));
this.addPromise(tile);
tile._queued = true;
} else {
/* If we are using a fetch queue, tell the queue so this tile can
* be reprioritized. */
var pos = this._queue ? this._queue.get(tile) : -1;
if (pos >= 0) {
this._queue.add(tile);
}
}
}
}.bind(this));
// purge all old tiles when the new tiles are loaded (successfully or not)
$.when.apply($, tiles)
.done(// called on success and failure
function () {
var map = this.map(),
mapZoom = map.zoom(),
zoom = this._options.tileRounding(mapZoom);
this._purge(zoom, true);
}.bind(this)
);
};
/**
* Set a value in the tile tree object indicating that the given area of
* the canvas is covered by the tile.
* @protected
* @param {geo.tile} tile
*/
this._setTileTree = function (tile) {
if (this._options.keepLower) {
return;
}
var index = tile.index;
this._tileTree[index.level] = this._tileTree[index.level] || {};
this._tileTree[index.level][index.x] = this._tileTree[index.level][index.x] || {};
this._tileTree[index.level][index.x][index.y] = tile;
};
/**
* Get a value in the tile tree object if it exists or return null.
* @protected
* @param {object} index A tile index object
* @param {object} index.level
* @param {object} index.x
* @param {object} index.y
* @returns {geo.tile|null}
*/
this._getTileTree = function (index) {
return (
(
this._tileTree[index.level] || {}
)[index.x] || {}
)[index.y] || null;
};
/**
* Returns true if the tile is completely covered by other tiles on the canvas.
* Currently this method only checks layers +/- 1 away from `tile`. If the
* zoom level is allowed to change by 2 or more in a single update step, this
* method will need to be refactored to make a more robust check. Returns
* an array of tiles covering it or null if any part of the tile is exposed.
* @protected
* @param {geo.tile} tile
* @returns {geo.tile[]|null}
*/
this._isCovered = function (tile) {
var level = tile.index.level,
x = tile.index.x,
y = tile.index.y,
tiles = [];
// Check one level up
tiles = this._getTileTree({
level: level - 1,
x: Math.floor(x / 2),
y: Math.floor(y / 2)
});
if (tiles) {
return [tiles];
}
// Check one level down
tiles = [
this._getTileTree({
level: level + 1,
x: 2 * x,
y: 2 * y
}),
this._getTileTree({
level: level + 1,
x: 2 * x + 1,
y: 2 * y
}),
this._getTileTree({
level: level + 1,
x: 2 * x,
y: 2 * y + 1
}),
this._getTileTree({
level: level + 1,
x: 2 * x + 1,
y: 2 * y + 1
})
];
if (tiles.every(function (t) { return t !== null; })) {
return tiles;
}
return null;
};
/**
* Returns true if the provided tile is outside of the current view bounds
* and can be removed from the canvas.
* @protected
* @param {geo.tile} tile
* @param {object?} bounds The view bounds
* @param {object?} bounds.left
* @param {object?} bounds.right
* @param {object?} bounds.top
* @param {object?} bounds.bottom
* @returns {boolean}
*/
this._outOfBounds = function (tile, bounds) {
/* We may want to add an (n) tile edge buffer so we appear more
* responsive */
var to = this._tileOffset(tile.index.level);
var scale = 1;
if (tile.index.level !== bounds.level) {
scale = Math.pow(2, (bounds.level || 0) - (tile.index.level || 0));
}
return (tile.bottom - to.y) * scale < bounds.top ||
(tile.left - to.x) * scale > bounds.right ||
(tile.top - to.y) * scale > bounds.bottom ||
(tile.right - to.x) * scale < bounds.left;
};
/**
* Returns true if the provided tile can be purged from the canvas. This method
* will return `true` if the tile is completely covered by one or more other tiles
* or it is outside of the active view bounds. This method returns the logical and
* of `_isCovered` and `_outOfBounds`.
* @protected
* @param {geo.tile} tile
* @param {object?} bounds The view bounds (if empty, assume global bounds)
* @param {number} bounds.left
* @param {number} bounds.right
* @param {number} bounds.top
* @param {number} bounds.bottom
* @param {number} bounds.level The zoom level the bounds are given as
* @param {number} zoom Keep in bound tile at this zoom level
* @param {boolean} doneLoading If true, allow purging additional tiles.
* @returns {boolean}
*/
this._canPurge = function (tile, bounds, zoom, doneLoading) {
if (this._options.keepLower) {
zoom = zoom || 0;
if (zoom < tile.index.level &&
tile.index.level !== this._options.minLevel) {
return true;
}
if (tile.index.level === this._options.minLevel &&
!this._options.wrapX && !this._options.wrapY) {
return false;
}
} else {
/* For tile layers that should only keep one layer, if loading is
* finished, purge all but the current layer. This is important for
* semi-transparanet layers. */
if ((doneLoading || this._isCovered(tile)) &&
zoom !== tile.index.level) {
return true;
}
}
if (bounds) {
return this._outOfBounds(tile, bounds);
}
return false;
};
/**
* Convert display pixel coordinates (where (0,0) is the upper left) to
* layer pixel coordinates (typically (0,0) is the center of the map and
* the upper-left has the most negative values).
* By default, this is done at the current base zoom level.
*
* @param pt: the point to convert. If undefined, use the center of the
* display.
* @param zoom: if specified, the zoom level to use.
* @returns: the point in level coordinates.
*/
this.displayToLevel = function (pt, zoom) {
var map = this.map(),
mapzoom = map.zoom(),
roundzoom = this._options.tileRounding(mapzoom),
unit = map.unitsPerPixel(zoom === undefined ? roundzoom : zoom);
if (pt === undefined) {
var size = map.size();
pt = {x: size.width / 2, y: size.height / 2};
}
/* Reverse the y coordinate, since we expect the gcs coordinate system
* to be right-handed and the level coordinate system to be
* left-handed. */
var gcsPt = map.displayToGcs(pt, this._options.gcs || null),
lvlPt = {x: gcsPt.x / unit, y: this._topDown() * gcsPt.y / unit};
return lvlPt;
};
/**
* Get or set the tile url string or function. If changed, load the new
* tiles.
*
* @param {string|function} [url] The new tile url.
* @returns {string|function|this}
*/
this.url = function (url) {
if (url === undefined) {
return this._options.originalUrl;
}
if (url === this._options.originalUrl) {
return this;
}
this._options.originalUrl = url;
if ($.type(url) === 'string') {
url = m_tileUrlFromTemplate(url);
}
this._options.url = url;
this.reset();
this.map().draw();
return this;
};
/**
* Get or set the subdomains used for templating.
*
* @param {string|list} [subdomains] A comma-separated list, a string of
* single character subdomains, or a list.
* @returns {string|list|this}
*/
this.subdomains = function (subdomains) {
if (subdomains === undefined) {
return this._options.subdomains;
}
if (subdomains) {
if ($.type(subdomains) === 'string') {
if (subdomains.indexOf(',') >= 0) {
subdomains = subdomains.split(',');
} else {
subdomains = subdomains.split('');
}
}
this._options.subdomains = subdomains;
this.reset();
this.map().draw();
}
return this;
};
/**
* Return a value from the tileOffset function, caching it for different
* levels.
*
* @param {Number} level the level to pass to the tileOffset function.
* @returns {Object} a tile offset object with x and y properties.
*/
this._tileOffset = function (level) {
if (m_tileOffsetValues[level] === undefined) {
m_tileOffsetValues[level] = this._options.tileOffset(level);
}
return m_tileOffsetValues[level];
};
/**
* Initialize after the layer is added to the map.
*/
this._init = function () {
var sublayer;
// call super method
s_init.apply(this, arguments);
if (this.renderer() === null) {
// Initialize sublayers in the correct order
for (sublayer = 0; sublayer <= this._options.maxLevel; sublayer += 1) {
this._getSubLayer(sublayer);
}
}
return this;
};
/**
* Clean up the layer.
*/
this._exit = function () {
this.reset();
// call super method
s_exit.apply(this, arguments);
m_exited = true;
return this;
};
geo.adjustLayerForRenderer('tile', this);
return this;
};
/**
* This object contains the default options used to initialize the tileLayer.
*/
geo.tileLayer.defaults = {
minLevel: 0,
maxLevel: 18,
tileOverlap: 0,
tileWidth: 256,
tileHeight: 256,
wrapX: true,
wrapY: false,
url: null,
subdomains: 'abc',
tileOffset: function (level) {
void (level);
return {x: 0, y: 0};
},
topDown: false,
keepLower: true,
// cacheSize: 400, // set depending on keepLower
tileRounding: Math.round,
attribution: '',
animationDuration: 0
};
inherit(geo.tileLayer, geo.featureLayer);
})();
(function () {
'use strict';
//////////////////////////////////////////////////////////////////////////////
/**
* This class implements a queue for Deferred objects. Whenever one of the
* objects in the queue completes (resolved or rejected), another item in the
* queue is processed. The number of concurrently processing items can be
* adjusted. At this time (2015-12-29) most major browsers support 6
* concurrent requests from any given server, so, when using the queue for
* tile images, thie number of concurrent requests should be 6 * (number of
* subdomains serving tiles).
*
* @class
*
* @param {Object?} [options] A configuration object for the queue
* @param {Number} [options.size=6] The maximum number of concurrent deferred
* objects.
* @param {Number} [options.track=600] The number of objects that are tracked
* that trigger checking if any of them have been abandoned. The fetch
* queue can grow to the greater of this size and the number of items that
* are still needed. Setting this to a low number will increase
* processing time, to a high number can increase memory. Ideally, it
* should reflect the number of items that are kept in memory elsewhere.
* If needed is null, this is ignored.
* @param {function} [options.needed=null] If set, this function is passed a
* Deferred object and must return a truthy value if the object is still
* needed.
*/
//////////////////////////////////////////////////////////////////////////////
geo.fetchQueue = function (options) {
if (!(this instanceof geo.fetchQueue)) {
return new geo.fetchQueue(options);
}
options = options || {};
this._size = options.size || 6;
this._track = options.track || 600;
this._needed = options.needed || null;
this._batch = false;
var m_this = this,
m_next_batch = 1;
/**
* Get/set the maximum concurrent deferred object size.
*/
Object.defineProperty(this, 'size', {
get: function () { return this._size; },
set: function (n) {
this._size = n;
this.next_item();
}
});
/**
* Get the current queue size.
*/
Object.defineProperty(this, 'length', {
get: function () { return this._queue.length; }
});
/**
* Get the current number of processing items.
*/
Object.defineProperty(this, 'processing', {
get: function () { return this._processing; }
});
/**
* Remove all items from the queue.
*/
this.clear = function () {
this._queue = [];
this._processing = 0;
return this;
};
/**
* Add a Deferred object to the queue.
* @param {Deferred} defer Deferred object to add to the queue.
* @param {function} callback a function to call when the item's turn is
* granted.
* @param {boolean} atEnd if false, add the item to the front of the queue
* if batching is turned off or at the end of the current batch if it is
* turned on. If true, always add the item to the end of the queue.
*/
this.add = function (defer, callback, atEnd) {
if (defer.__fetchQueue) {
var pos = $.inArray(defer, this._queue);
if (pos >= 0) {
this._queue.splice(pos, 1);
this._addToQueue(defer, atEnd);
return defer;
}
}
var wait = new $.Deferred();
var process = new $.Deferred();
wait.then(function () {
$.when(callback.call(defer)).always(process.resolve);
}, process.resolve);
defer.__fetchQueue = wait;
this._addToQueue(defer, atEnd);
$.when(wait, process).always(function () {
if (m_this._processing > 0) {
m_this._processing -= 1;
}
m_this.next_item();
}).promise(defer);
m_this.next_item();
return defer;
};
/**
* Add an item to the queue. If batches are being used, add it at after
* other items in the same batch.
* @param {Deferred} defer Deferred object to add to the queue.
* @param {boolean} atEnd if false, add the item to the front of the queue
* if batching is turned off or at the end of the current batch if it is
* turned on. If true, always add the item to the end of the queue.
*/
this._addToQueue = function (defer, atEnd) {
defer.__fetchQueue._batch = this._batch;
if (atEnd) {
this._queue.push(defer);
} else if (!this._batch) {
this._queue.unshift(defer);
} else {
for (var i = 0; i < this._queue.length; i += 1) {
if (this._queue[i].__fetchQueue._batch !== this._batch) {
break;
}
}
this._queue.splice(i, 0, defer);
}
};
/**
* Get the position of a deferred object in the queue.
* @param {Deferred} defer Deferred object to get the position of.
* @returns {number} -1 if not in the queue, or the position in the queue.
*/
this.get = function (defer) {
return $.inArray(defer, this._queue);
};
/**
* Remove a Deferred object from the queue.
* @param {Deferred} defer Deferred object to add to the queue.
* @returns {bool} true if the object was removed
*/
this.remove = function (defer) {
var pos = $.inArray(defer, this._queue);
if (pos >= 0) {
this._queue.splice(pos, 1);
return true;
}
return false;
};
/**
* Start a new batch or clear using batches.
* @param {boolean} start true to start a new batch, false to turn off
* using batches. Undefined to return the current
* state of batches.
* @return {Number|boolean|Object} the current batch state or this object.
*/
this.batch = function (start) {
if (start === undefined) {
return this._batch;
}
if (!start) {
this._batch = false;
} else {
this._batch = m_next_batch;
m_next_batch += 1;
}
return this;
};
/**
* Check if any items are queued and if there if there are not too many
* deferred objects being processed. If so, process more items.
*/
this.next_item = function () {
if (m_this._innextitem) {
return;
}
m_this._innextitem = true;
/* if the queue is greater than the track size, check each item to see
* if it is still needed. */
if (m_this._queue.length > m_this._track && this._needed) {
for (var i = m_this._queue.length - 1; i >= 0; i -= 1) {
if (!m_this._needed(m_this._queue[i])) {
var discard = m_this._queue.splice(i, 1)[0];
m_this._processing += 1;
discard.__fetchQueue.reject();
delete discard.__fetchQueue;
}
}
}
while (m_this._processing < m_this._size && m_this._queue.length) {
var defer = m_this._queue.shift();
if (defer.__fetchQueue) {
m_this._processing += 1;
var needed = m_this._needed ? m_this._needed(defer) : true;
if (needed) {
defer.__fetchQueue.resolve();
} else {
defer.__fetchQueue.reject();
}
delete defer.__fetchQueue;
}
}
m_this._innextitem = false;
};
this.clear();
return this;
};
})();
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class fileReader
*
* @class
* @extends geo.object
* @returns {geo.fileReader}
*/
//////////////////////////////////////////////////////////////////////////////
geo.fileReader = function (arg) {
'use strict';
if (!(this instanceof geo.fileReader)) {
return new geo.fileReader(arg);
}
geo.object.call(this);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
arg = arg || {};
if (!(arg.layer instanceof geo.featureLayer)) {
throw 'fileReader must be given a feature layer';
}
var m_layer = arg.layer;
////////////////////////////////////////////////////////////////////////////
/**
* Get the feature layer attached to the reader
*/
////////////////////////////////////////////////////////////////////////////
this.layer = function () {
return m_layer;
};
////////////////////////////////////////////////////////////////////////////
/**
* Tells the caller if it can handle the given file by returning a boolean.
*/
////////////////////////////////////////////////////////////////////////////
this.canRead = function () {
return false;
};
////////////////////////////////////////////////////////////////////////////
/**
* Reads the file object and calls the done function when finished. As an
* argument to done, provides a boolean that reports if the read was a
* success. Possibly, it can call done with an object containing details
* of the read operation.
*/
////////////////////////////////////////////////////////////////////////////
this.read = function (file, done) {
done(false);
};
////////////////////////////////////////////////////////////////////////////
/**
* Return a FileReader with handlers attached.
*/
////////////////////////////////////////////////////////////////////////////
function newFileReader(done, progress) {
var reader = new FileReader();
if (progress) {
reader.onprogress = progress;
}
reader.onloadend = function () {
if (!reader.result) {
done(reader.error);
}
done(reader.result);
};
return reader;
}
////////////////////////////////////////////////////////////////////////////
/**
* Private method for reading a file object as a string. Calls done with
* the string content when finished or an error object if unsuccessful.
* Optionally, the caller can provide a progress method that is called
* after reading each slice.
*/
////////////////////////////////////////////////////////////////////////////
this._getString = function (file, done, progress) {
var reader = newFileReader(done, progress);
reader.readAsText(file);
};
////////////////////////////////////////////////////////////////////////////
/**
* Like _getString, but returns an ArrayBuffer object.
*/
////////////////////////////////////////////////////////////////////////////
this._getArrayBuffer = function (file, done, progress) {
var reader = newFileReader(done, progress);
reader.readAsText(file);
};
return this;
};
inherit(geo.fileReader, geo.object);
/*global File*/
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class jsonReader
*
* @class
* @extends geo.fileReader
* @returns {geo.jsonReader}
*/
//////////////////////////////////////////////////////////////////////////////
geo.jsonReader = function (arg) {
'use strict';
if (!(this instanceof geo.jsonReader)) {
return new geo.jsonReader(arg);
}
var m_this = this, m_style = arg.style || {};
m_style = $.extend({
'strokeWidth': 2,
'strokeColor': {r: 0, g: 0, b: 0},
'strokeOpacity': 1,
'fillColor': {r: 1, g: 0, b: 0},
'fillOpacity': 1
}, m_style);
geo.fileReader.call(this, arg);
this.canRead = function (file) {
if (file instanceof File) {
return (file.type === 'application/json' || file.name.match(/\.json$/));
} else if (typeof file === 'string') {
try {
JSON.parse(file);
} catch (e) {
return false;
}
return true;
}
try {
if (Array.isArray(m_this._featureArray(file))) {
return true;
}
} catch (e) {}
return false;
};
this._readObject = function (file, done, progress) {
var object;
function onDone(fileString) {
if (typeof fileString !== 'string') {
done(false);
}
// We have two possibilities here:
// 1) fileString is a JSON string or is
// a URL.
try {
object = JSON.parse(fileString);
done(object);
} catch (e) {
if (!object) {
$.ajax({
type: 'GET',
url: fileString,
dataType: 'text'
}).done(function (data) {
object = JSON.parse(data);
done(object);
}).fail(function () {
done(false);
});
}
}
}
if (file instanceof File) {
m_this._getString(file, onDone, progress);
} else if (typeof file === 'string') {
onDone(file);
} else {
done(file);
}
};
this._featureArray = function (spec) {
if (spec.type === 'FeatureCollection') {
return spec.features || [];
}
if (spec.type === 'GeometryCollection') {
throw 'GeometryCollection not yet implemented.';
}
if (Array.isArray(spec.coordinates)) {
return spec;
}
throw 'Unsupported collection type: ' + spec.type;
};
this._featureType = function (spec) {
var geometry = spec.geometry || {};
if (geometry.type === 'Point' || geometry.type === 'MultiPoint') {
return 'point';
}
if (geometry.type === 'LineString') {
return 'line';
}
if (geometry.type === 'Polygon') {
return 'polygon';
}
if (geometry.type === 'MultiPolygon') {
return 'multipolygon';
}
return null;
};
this._getCoordinates = function (spec) {
var geometry = spec.geometry || {},
coordinates = geometry.coordinates || [], elv;
if ((coordinates.length === 2 || coordinates.length === 3) &&
(isFinite(coordinates[0]) && isFinite(coordinates[1]))) {
// Do we have a elevation component
if (isFinite(coordinates[2])) {
elv = coordinates[2];
}
// special handling for single point coordinates
return [{x: coordinates[0], y: coordinates[1], z: elv}];
}
// need better handling here, but we can plot simple polygons
// by taking just the outer linearring
if (Array.isArray(coordinates[0][0])) {
coordinates = coordinates[0];
}
// return an array of points for LineString, MultiPoint, etc...
return coordinates.map(function (c) {
return {
x: c[0],
y: c[1],
z: c[2]
};
});
};
this._getStyle = function (spec) {
return spec.properties;
};
this.read = function (file, done, progress) {
function _done(object) {
var features, allFeatures = [];
features = m_this._featureArray(object);
features.forEach(function (feature) {
var type = m_this._featureType(feature),
coordinates = m_this._getCoordinates(feature),
style = m_this._getStyle(feature);
if (type) {
if (type === 'line') {
style.fill = style.fill || false;
allFeatures.push(m_this._addFeature(
type,
[coordinates],
style,
feature.properties
));
} else if (type === 'point') {
style.stroke = style.stroke || false;
allFeatures.push(m_this._addFeature(
type,
coordinates,
style,
feature.properties
));
} else if (type === 'polygon') {
style.fill = style.fill === undefined ? true : style.fill;
style.fillOpacity = (
style.fillOpacity === undefined ? 0.25 : style.fillOpacity
);
// polygons not yet supported
allFeatures.push(m_this._addFeature(
type,
[[coordinates]], //double wrap for the data method below
style,
feature.properties
));
} else if (type === 'multipolygon') {
style.fill = style.fill === undefined ? true : style.fill;
style.fillOpacity = (
style.fillOpacity === undefined ? 0.25 : style.fillOpacity
);
coordinates = feature.geometry.coordinates.map(function (c) {
return [m_this._getCoordinates({
geometry: {
type: 'Polygon',
coordinates: c
}
})];
});
allFeatures.push(m_this._addFeature(
'polygon', //there is no multipolygon feature class
coordinates,
style,
feature.properties
));
}
} else {
console.log('unsupported feature type: ' + feature.geometry.type);
}
});
if (done) {
done(allFeatures);
}
}
m_this._readObject(file, _done, progress);
};
////////////////////////////////////////////////////////////////////////////
/**
* Build the data array for a feature given the coordinates and properties
* from the geojson.
*
* @private
* @param {Object[]} coordinates Coordinate data array
* @param {Object} properties Geojson properties object
* @param {Object} style Global style defaults
* @returns {Object[]}
*/
//////////////////////////////////////////////////////////////////////////////
this._buildData = function (coordinates, properties, style) {
return coordinates.map(function (coord) {
return {
coordinates: coord,
properties: properties,
style: style
};
});
};
this._addFeature = function (type, coordinates, style, properties) {
var _style = $.extend({}, m_style, style);
var feature = m_this.layer().createFeature(type)
.data(m_this._buildData(coordinates, properties, style))
.style(_style);
if (type === 'line') {
feature.line(function (d) { return d.coordinates; });
} else if (type === 'polygon') {
feature.position(function (d) {
return {
x: d.x,
y: d.y,
z: d.z
};
}).polygon(function (d) {
return {
'outer': d.coordinates[0],
'inner': d.coordinates[1]
};
});
} else {
feature.position(function (d) {
return d.coordinates;
});
}
return feature;
};
};
inherit(geo.jsonReader, geo.fileReader);
geo.registerFileReader('jsonReader', geo.jsonReader);
//////////////////////////////////////////////////////////////////////////////
/**
* Creates a new map object
*
* Map coordinates for default world map, where c = half circumference at
* equator in meters, o = origin:
* (-c, c) + o (c, c) + o
* (center.x, center.y) + o <-- center of viewport
* (-c, -c) + o (c, -c) + o
*
* @class
* @extends geo.sceneObject
*
* *** Always required ***
* @param {string} node DOM selector for the map container
*
* *** Required when using a domain/CS different from OSM ***
* @param {string|geo.transform} [gcs='EPSG:3857']
* The main coordinate system of the map
* @param {number} [maxZoom=16] The maximum zoom level
* @param {string|geo.transform} [ingcs='EPSG:4326']
* The default coordinate system of interface calls.
* @param {number} [unitsPerPixel=156543] GCS to pixel unit scaling at zoom 0
* (i.e. meters per pixel or degrees per pixel).
* @param {object?} maxBounds The maximum visable map bounds
* @param {number} [maxBounds.left=-20037508] The left bound
* @param {number} [maxBounds.right=20037508] The right bound
* @param {number} [maxBounds.bottom=-20037508] The bottom bound
* @param {number} [maxBounds.top=20037508] The top bound
*
* *** Initial view ***
* @param {number} [zoom=4] Initial zoom
* @param {object?} center Map center
* @param {number} [center.x=0]
* @param {number} [center.y=0]
* @param {number} [rotation=0] Clockwise rotation in radians
* @param {number?} width The map width (default node width)
* @param {number?} height The map height (default node height)
*
* *** Navigation ***
* @param {number} [min=0] Minimum zoom level (though fitting to the viewport
* may make it so this is smaller than the smallest possible value)
* @param {number} [max=16] Maximum zoom level
* @param {boolean} [discreteZoom=false] True to only allow integer zoom
* levels. False for any zoom level.
* @param {boolean} [allowRotation=true] False prevents rotation, true allows
* any rotation. If a function, the function is called with a rotation
* (angle in radians) and returns a valid rotation (this can be used to
* constrain the rotation to a range or specific values).
*
* *** Advanced parameters ***
* @param {geo.camera?} camera The camera to control the view
* @param {geo.mapInteractor?} interactor The UI event handler
* @param {geo.clock?} clock The clock used to synchronize time events
* @param {boolean} [autoResize=true] Adjust map size on window resize
* @param {boolean} [clampBoundsX=false] Prevent panning outside of the
* maximum bounds in the horizontal direction.
* @param {boolean} [clampBoundsY=true] Prevent panning outside of the
* maximum bounds in the vertical direction.
* @param {boolean} [clampZoom=true] Prevent zooming out so that the map area
* is smaller than the window.
*
* @returns {geo.map}
*/
//////////////////////////////////////////////////////////////////////////////
geo.map = function (arg) {
'use strict';
if (!(this instanceof geo.map)) {
return new geo.map(arg);
}
arg = arg || {};
if (arg.node === undefined || arg.node === null) {
console.warn('map creation requires a node');
return this;
}
geo.sceneObject.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* Private member variables
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_exit = this._exit,
// See https://en.wikipedia.org/wiki/Web_Mercator
// phiMax = 180 / Math.PI * (2 * Math.atan(Math.exp(Math.PI)) - Math.PI / 2),
m_x = 0,
m_y = 0,
m_node = $(arg.node),
m_width = arg.width || m_node.width() || 512,
m_height = arg.height || m_node.height() || 512,
m_gcs = arg.gcs === undefined ? 'EPSG:3857' : arg.gcs,
m_ingcs = arg.ingcs === undefined ? 'EPSG:4326' : arg.ingcs,
m_center = {x: 0, y: 0},
m_zoom = arg.zoom === undefined ? 4 : arg.zoom,
m_rotation = 0,
m_fileReader = null,
m_interactor = null,
m_validZoomRange = {min: 0, max: 16, origMin: 0},
m_transition = null,
m_queuedTransition = null,
m_clock = null,
m_discreteZoom = arg.discreteZoom ? true : false,
m_allowRotation = (typeof arg.allowRotation === 'function' ?
arg.allowRotation : (arg.allowRotation === undefined ?
true : !!arg.allowRotation)),
m_maxBounds = arg.maxBounds || {},
m_camera = arg.camera || geo.camera(),
m_unitsPerPixel,
m_clampBoundsX,
m_clampBoundsY,
m_clampZoom,
m_origin,
m_scale = {x: 1, y: 1, z: 1}; // constant and ignored for the moment
/* Compute the maximum bounds on our map projection. By default, x ranges
* from [-180, 180] in the interface projection, and y matches the x range in
* the map (not the interface) projection. For images, this might be
* [0, width] and [0, height] instead. */
var mcx = ((m_maxBounds.left || 0) + (m_maxBounds.right || 0)) / 2,
mcy = ((m_maxBounds.bottom || 0) + (m_maxBounds.top || 0)) / 2;
m_maxBounds.left = geo.transform.transformCoordinates(m_ingcs, m_gcs, [{
x: m_maxBounds.left !== undefined ? m_maxBounds.left : -180, y: mcy
}])[0].x;
m_maxBounds.right = geo.transform.transformCoordinates(m_ingcs, m_gcs, [{
x: m_maxBounds.right !== undefined ? m_maxBounds.right : 180, y: mcy
}])[0].x;
m_maxBounds.top = (m_maxBounds.top !== undefined ?
geo.transform.transformCoordinates(m_ingcs, m_gcs, [{
x: mcx, y: m_maxBounds.top}])[0].y : m_maxBounds.right);
m_maxBounds.bottom = (m_maxBounds.bottom !== undefined ?
geo.transform.transformCoordinates(m_ingcs, m_gcs, [{
x: mcx, y: m_maxBounds.bottom}])[0].y : m_maxBounds.left);
m_unitsPerPixel = (arg.unitsPerPixel || (
m_maxBounds.right - m_maxBounds.left) / 256);
m_camera.viewport = {width: m_width, height: m_height};
arg.center = geo.util.normalizeCoordinates(arg.center);
arg.autoResize = arg.autoResize === undefined ? true : arg.autoResize;
m_clampBoundsX = arg.clampBoundsX === undefined ? false : arg.clampBoundsX;
m_clampBoundsY = arg.clampBoundsY === undefined ? true : arg.clampBoundsY;
m_clampZoom = arg.clampZoom === undefined ? true : arg.clampZoom;
////////////////////////////////////////////////////////////////////////////
/**
* Get/set the number of world space units per display pixel at the given
* zoom level.
*
* @param {Number} [zoom=0] The target zoom level
* @param {Number?} unit If present, set the unitsPerPixel otherwise return
* the current value.
* @returns {Number|this}
*/
////////////////////////////////////////////////////////////////////////////
this.unitsPerPixel = function (zoom, unit) {
zoom = zoom || 0;
if (unit) {
// get the units at level 0
m_unitsPerPixel = Math.pow(2, zoom) * unit;
// redraw all the things
m_this.draw();
return m_this;
}
return Math.pow(2, -zoom) * m_unitsPerPixel;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/set the clampBoundsX setting. If changed, adjust the bounds of the
* map as needed.
*
* @param {boolean?} clamp The new clamp value.
* @returns {boolean|this}
*/
////////////////////////////////////////////////////////////////////////////
this.clampBoundsX = function (clamp) {
if (clamp === undefined) {
return m_clampBoundsX;
}
if (clamp !== m_clampBoundsX) {
m_clampBoundsX = !!clamp;
m_this.pan({x: 0, y: 0});
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/set the clampBoundsY setting. If changed, adjust the bounds of the
* map as needed.
*
* @param {boolean?} clamp The new clamp value.
* @returns {boolean|this}
*/
////////////////////////////////////////////////////////////////////////////
this.clampBoundsY = function (clamp) {
if (clamp === undefined) {
return m_clampBoundsY;
}
if (clamp !== m_clampBoundsY) {
m_clampBoundsY = !!clamp;
m_this.pan({x: 0, y: 0});
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/set the clampZoom setting. If changed, adjust the bounds of the map
* as needed.
*
* @param {boolean?} clamp The new clamp value.
* @returns {boolean|this}
*/
////////////////////////////////////////////////////////////////////////////
this.clampZoom = function (clamp) {
if (clamp === undefined) {
return m_clampZoom;
}
if (clamp !== m_clampZoom) {
m_clampZoom = !!clamp;
reset_minimum_zoom();
m_this.zoom(m_zoom);
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/set the allowRotation setting. If changed, adjust the map as needed.
*
* @param {boolean|function} allowRotation the new allowRotation value.
* @returns {boolean|function|this}
*/
////////////////////////////////////////////////////////////////////////////
this.allowRotation = function (allowRotation) {
if (allowRotation === undefined) {
return m_allowRotation;
}
if (typeof allowRotation !== 'function') {
allowRotation = !!allowRotation;
}
if (allowRotation !== m_allowRotation) {
m_allowRotation = allowRotation;
m_this.rotation(m_rotation);
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the map's world coordinate origin in gcs coordinates
*
* @returns {object}
*/
////////////////////////////////////////////////////////////////////////////
this.origin = function () {
return $.extend({}, m_origin);
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the map's world coordinate scaling relative gcs units
*
* @returns {object}
*/
////////////////////////////////////////////////////////////////////////////
this.scale = function () {
return $.extend({}, m_scale);
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the camera
*
* @returns {geo.camera}
*/
////////////////////////////////////////////////////////////////////////////
this.camera = function () {
return m_camera;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get map gcs
*
* @returns {string}
*/
////////////////////////////////////////////////////////////////////////////
this.gcs = function (arg) {
if (arg === undefined) {
return m_gcs;
}
if (arg !== m_gcs) {
var oldCenter = m_this.center(undefined, undefined);
m_gcs = arg;
reset_minimum_zoom();
var newZoom = fix_zoom(m_zoom);
if (newZoom !== m_zoom) {
m_this.zoom(newZoom);
}
m_this.center(oldCenter, undefined);
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get map interface gcs
*
* @returns {string}
*/
////////////////////////////////////////////////////////////////////////////
this.ingcs = function (arg) {
if (arg === undefined) {
return m_ingcs;
}
m_ingcs = arg;
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get root node of the map
*
* @returns {object}
*/
////////////////////////////////////////////////////////////////////////////
this.node = function () {
return m_node;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set zoom level of the map
*
* @returns {Number|geo.map}
*/
////////////////////////////////////////////////////////////////////////////
this.zoom = function (val, origin, ignoreDiscreteZoom) {
var evt, bounds;
if (val === undefined) {
return m_zoom;
}
/* The ignoreDiscreteZoom flag is intended to allow non-integer zoom values
* during animation. */
val = fix_zoom(val, ignoreDiscreteZoom);
if (val === m_zoom) {
return m_this;
}
m_zoom = val;
bounds = m_this.boundsFromZoomAndCenter(val, m_center, m_rotation, null);
m_this.modified();
camera_bounds(bounds, m_rotation);
evt = {
geo: {},
zoomLevel: m_zoom,
screenPosition: origin ? origin.map : undefined
};
m_this.geoTrigger(geo.event.zoom, evt);
if (origin && origin.geo && origin.map) {
var shifted = m_this.gcsToDisplay(origin.geo);
m_this.pan({x: origin.map.x - shifted.x, y: origin.map.y - shifted.y});
} else {
m_this.pan({x: 0, y: 0});
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Pan the map by (x: dx, y: dy) pixels.
*
* @param {Object} delta
* @returns {geo.map}
*/
////////////////////////////////////////////////////////////////////////////
this.pan = function (delta) {
var evt, unit;
evt = {
geo: {},
screenDelta: delta
};
unit = m_this.unitsPerPixel(m_zoom);
var sinr = Math.sin(m_rotation), cosr = Math.cos(m_rotation);
m_camera.pan({
x: (delta.x * cosr - (-delta.y) * sinr) * unit,
y: (delta.x * sinr + (-delta.y) * cosr) * unit
});
/* If m_clampBounds* is true, clamp the pan */
var bounds = fix_bounds(m_camera.bounds, m_rotation);
if (bounds !== m_camera.bounds) {
var panPos = m_this.gcsToDisplay({
x: m_camera.bounds.left, y: m_camera.bounds.top}, null);
bounds = m_this.boundsFromZoomAndCenter(m_zoom, {
x: (bounds.left + bounds.right) / 2,
y: (bounds.top + bounds.bottom) / 2
}, m_rotation, null);
camera_bounds(bounds, m_rotation);
var clampPos = m_this.gcsToDisplay({
x: m_camera.bounds.left, y: m_camera.bounds.top}, null);
evt.screenDelta.x += clampPos.x - panPos.x;
evt.screenDelta.y += clampPos.y - panPos.y;
}
m_center = m_camera.displayToWorld({
x: m_width / 2,
y: m_height / 2
});
m_this.geoTrigger(geo.event.pan, evt);
m_this.modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/set the map rotation. The rotation is performed around the current
* view center.
*
* @param {Object} rotation angle in radians (positive is clockwise)
* @param {Object} origin is specified, rotate about this origin
* @param {boolean} ignoreRotationFunc if true, don't constrain the rotation.
* @returns {geo.map}
*/
////////////////////////////////////////////////////////////////////////////
this.rotation = function (rotation, origin, ignoreRotationFunc) {
if (rotation === undefined) {
return m_rotation;
}
rotation = fix_rotation(rotation, ignoreRotationFunc);
if (rotation === m_rotation) {
return m_this;
}
m_rotation = rotation;
var bounds = m_this.boundsFromZoomAndCenter(
m_zoom, m_center, m_rotation, null);
m_this.modified();
camera_bounds(bounds, m_rotation);
var evt = {
geo: {},
rotation: m_rotation,
screenPosition: origin ? origin.map : undefined
};
m_this.geoTrigger(geo.event.rotate, evt);
if (origin && origin.geo && origin.map) {
var shifted = m_this.gcsToDisplay(origin.geo);
m_this.pan({x: origin.map.x - shifted.x, y: origin.map.y - shifted.y});
} else {
m_this.pan({x: 0, y: 0});
}
/* Changing the rotation can change our minimum zoom */
reset_minimum_zoom();
m_this.zoom(m_zoom);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Set center of the map to the given geographic coordinates, or get the
* current center. Uses bare objects {x: 0, y: 0}.
*
* @param {Object} coordinates
* @param {string|geo.transform} [gcs] undefined to use the interface gcs,
* null to use the map gcs, or any other transform. If setting the
* center, they are converted from this gcs to the map projection. The
* returned center are converted from the map projection to this gcs.
* @returns {Object|geo.map}
*/
////////////////////////////////////////////////////////////////////////////
this.center = function (coordinates, gcs) {
var center;
if (coordinates === undefined) {
center = $.extend({}, m_this.worldToGcs(m_center, gcs));
return center;
}
// get the screen coordinates of the new center
m_center = $.extend({}, m_this.gcsToWorld(coordinates, gcs));
camera_bounds(m_this.boundsFromZoomAndCenter(
m_zoom, m_center, m_rotation, null), m_rotation);
m_this.modified();
// trigger a pan event
m_this.geoTrigger(
geo.event.pan,
{
geo: coordinates,
screenDelta: null
}
);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Add layer to the map
*
* @param {geo.layer} layer to be added to the map
* @return {geom.map}
*/
////////////////////////////////////////////////////////////////////////////
this.createLayer = function (layerName, arg) {
arg = arg || {};
var newLayer = geo.createLayer(
layerName, m_this, arg);
if (newLayer) {
m_this.addChild(newLayer);
m_this.children().forEach(function (c) {
if (c instanceof geo.gui.uiLayer) {
c.moveToTop();
}
});
newLayer._update();
m_this.modified();
m_this.geoTrigger(geo.event.layerAdd, {
type: geo.event.layerAdd,
target: m_this,
layer: newLayer
});
}
return newLayer;
};
////////////////////////////////////////////////////////////////////////////
/**
* Remove layer from the map
*
* @param {geo.layer} layer that should be removed from the map
* @return {geo.map}
*/
////////////////////////////////////////////////////////////////////////////
this.deleteLayer = function (layer) {
if (layer !== null && layer !== undefined) {
layer._exit();
m_this.removeChild(layer);
m_this.modified();
m_this.geoTrigger(geo.event.layerRemove, {
type: geo.event.layerRemove,
target: m_this,
layer: layer
});
}
/// Return deleted layer (similar to createLayer) as in the future
/// we may provide extension of this method to support deletion of
/// layer using id or some sort.
return layer;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get or set the size of the map.
*
* @param {Object?} arg
* @param {Number} arg.width width in pixels
* @param {Number} arg.height height in pixels
* @returns {Object} An object containing width and height as keys
*/
////////////////////////////////////////////////////////////////////////////
this.size = function (arg) {
if (arg === undefined) {
return {
width: m_width,
height: m_height
};
}
m_this.resize(0, 0, arg.width, arg.height);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the rotated size of the map. This is the width and height of the
* non-rotated area necessary to enclose the rotated area in pixels.
*
* @returns {Object} An object containing width and height as keys
*/
////////////////////////////////////////////////////////////////////////////
this.rotatedSize = function () {
if (!this.rotation()) {
return {
width: m_width,
height: m_height
};
}
var bds = rotate_bounds_center(
{x: 0, y: 0}, {width: m_width, height: m_height}, this.rotation());
return {
width: Math.abs(bds.right - bds.left),
height: Math.abs(bds.top - bds.bottom)
};
};
////////////////////////////////////////////////////////////////////////////
/**
* Resize map (deprecated)
*
* @param {Number} x x-offset in display space
* @param {Number} y y-offset in display space
* @param {Number} w width in display space
* @param {Number} h height in display space
*/
////////////////////////////////////////////////////////////////////////////
this.resize = function (x, y, w, h) {
// store the original center and restore it after the resize
var oldCenter = m_this.center();
m_x = x;
m_y = y;
m_width = w;
m_height = h;
reset_minimum_zoom();
var newZoom = fix_zoom(m_zoom);
if (newZoom !== m_zoom) {
m_this.zoom(newZoom);
}
m_this.camera().viewport = {width: w, height: h};
m_this.center(oldCenter);
m_this.geoTrigger(geo.event.resize, {
type: geo.event.resize,
target: m_this,
x: m_x,
y: m_y,
width: w,
height: h
});
m_this.modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Convert from gcs coordinates to map world coordinates.
* @param {object} c The input coordinate to convert
* @param {object} c.x
* @param {object} c.y
* @param {object} [c.z=0]
* @param {string?} gcs The gcs of the input (map.gcs() by default)
* @return {object} World space coordinates
*/
////////////////////////////////////////////////////////////////////////////
this.gcsToWorld = function (c, gcs) {
gcs = (gcs === null ? m_gcs : (gcs === undefined ? m_ingcs : gcs));
if (gcs !== m_gcs) {
c = geo.transform.transformCoordinates(gcs, m_gcs, [c])[0];
}
return geo.transform.affineForward(
{origin: m_origin},
[c]
)[0];
};
////////////////////////////////////////////////////////////////////////////
/**
* Convert from map world coordinates to gcs coordinates.
* @param {object} c The input coordinate to convert
* @param {object} c.x
* @param {object} c.y
* @param {object} [c.z=0]
* @param {string|geo.transform} [gcs] undefined to use the interface gcs,
* null to use the map gcs, or any other transform.
* @return {object} GCS space coordinates
*/
////////////////////////////////////////////////////////////////////////////
this.worldToGcs = function (c, gcs) {
c = geo.transform.affineInverse(
{origin: m_origin},
[c]
)[0];
gcs = (gcs === null ? m_gcs : (gcs === undefined ? m_ingcs : gcs));
if (gcs !== m_gcs) {
c = geo.transform.transformCoordinates(m_gcs, gcs, [c])[0];
}
return c;
};
////////////////////////////////////////////////////////////////////////////
/**
* Convert from gcs coordinates to display coordinates.
*
* gcsToWorld | worldToDisplay
*
* @param {object} c The input coordinate to convert
* @param {object} c.x
* @param {object} c.y
* @param {object} [c.z=0]
* @param {string|geo.transform} [gcs] undefined to use the interface gcs,
* null to use the map gcs, or any other transform.
* @return {object} Display space coordinates
*/
////////////////////////////////////////////////////////////////////////////
this.gcsToDisplay = function (c, gcs) {
c = m_this.gcsToWorld(c, gcs);
return m_this.worldToDisplay(c);
};
////////////////////////////////////////////////////////////////////////////
/**
* Convert from world coordinates to display coordinates using the attached
* camera.
* @param {object} c The input coordinate to convert
* @param {object} c.x
* @param {object} c.y
* @param {object} [c.z=0]
* @return {object} Display space coordinates
*/
////////////////////////////////////////////////////////////////////////////
this.worldToDisplay = function (c) {
return m_camera.worldToDisplay(c);
};
////////////////////////////////////////////////////////////////////////////
/**
* Convert from display to gcs coordinates
*
* displayToWorld | worldToGcs
*
* @param {object} c The input display coordinate to convert
* @param {object} c.x
* @param {object} c.y
* @param {object} [c.z=0]
* @param {string|geo.transform} [gcs] undefined to use the interface gcs,
* null to use the map gcs, or any other transform.
* @return {object} GCS space coordinates
*/
////////////////////////////////////////////////////////////////////////////
this.displayToGcs = function (c, gcs) {
c = m_this.displayToWorld(c); // done via camera
return m_this.worldToGcs(c, gcs);
};
////////////////////////////////////////////////////////////////////////////
/**
* Convert from display coordinates to world coordinates using the attached
* camera.
* @param {object} c The input coordinate to convert
* @param {object} c.x
* @param {object} c.y
* @param {object} [c.z=0]
* @return {object} World space coordinates
*/
////////////////////////////////////////////////////////////////////////////
this.displayToWorld = function (c) {
return m_camera.displayToWorld(c);
};
////////////////////////////////////////////////////////////////////////////
/**
* Manually force to render map
*/
////////////////////////////////////////////////////////////////////////////
this.draw = function () {
var i, layers = m_this.children();
m_this.geoTrigger(geo.event.draw, {
type: geo.event.draw,
target: m_this
}
);
m_this._update();
for (i = 0; i < layers.length; i += 1) {
layers[i].draw();
}
m_this.geoTrigger(geo.event.drawEnd, {
type: geo.event.drawEnd,
target: m_this
}
);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Attach a file reader to a layer in the map to be used as a drop target.
*/
////////////////////////////////////////////////////////////////////////////
this.fileReader = function (readerType, opts) {
var layer, renderer;
opts = opts || {};
if (!readerType) {
return m_fileReader;
}
layer = opts.layer;
if (!layer) {
renderer = opts.renderer;
if (!renderer) {
renderer = 'd3';
}
layer = m_this.createLayer('feature', {renderer: renderer});
}
opts.layer = layer;
opts.renderer = renderer;
m_fileReader = geo.createFileReader(readerType, opts);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize the map
*/
////////////////////////////////////////////////////////////////////////////
this._init = function () {
if (m_node === undefined || m_node === null) {
throw 'Map require DIV node';
}
m_node.css('position', 'relative');
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Update map
*/
////////////////////////////////////////////////////////////////////////////
this._update = function (request) {
var i, layers = m_this.children();
for (i = 0; i < layers.length; i += 1) {
layers[i]._update(request);
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Exit this map
*/
////////////////////////////////////////////////////////////////////////////
this.exit = function () {
var i, layers = m_this.children();
for (i = 0; i < layers.length; i += 1) {
layers[i]._exit();
}
if (m_this.interactor()) {
m_this.interactor().destroy();
m_this.interactor(null);
}
m_this.node().off('.geo');
$(window).off('resize', resizeSelf);
s_exit();
};
this._init(arg);
// set up drag/drop handling
this.node().on('dragover.geo', function (e) {
var evt = e.originalEvent;
if (m_this.fileReader()) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy';
}
})
.on('drop.geo', function (e) {
var evt = e.originalEvent, reader = m_this.fileReader(),
i, file;
function done() {
m_this.draw();
}
if (reader) {
evt.stopPropagation();
evt.preventDefault();
for (i = 0; i < evt.dataTransfer.files.length; i += 1) {
file = evt.dataTransfer.files[i];
if (reader.canRead(file)) {
reader.read(file, done); // to do: trigger event on done
}
}
}
});
////////////////////////////////////////////////////////////////////////////
/**
* Get or set the map interactor
*/
////////////////////////////////////////////////////////////////////////////
this.interactor = function (arg) {
if (arg === undefined) {
return m_interactor;
}
m_interactor = arg;
// this makes it possible to set a null interactor
// i.e. map.interactor(null);
if (m_interactor) {
m_interactor.map(m_this);
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get or set the map clock
*/
////////////////////////////////////////////////////////////////////////////
this.clock = function (arg) {
if (arg === undefined) {
return m_clock;
}
m_clock = arg;
if (m_clock) {
m_clock.object(m_this);
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get or set the min/max zoom range.
*
* @param {Object} arg {min: minimumzoom, max: maximumzom}
* @param {boolean} noRefresh if true, don't update the map if the zoom level
* has changed.
* @returns {Object|geo.map}
*/
////////////////////////////////////////////////////////////////////////////
this.zoomRange = function (arg, noRefresh) {
if (arg === undefined) {
return $.extend({}, m_validZoomRange);
}
if (arg.max !== undefined) {
m_validZoomRange.max = arg.max;
}
if (arg.min !== undefined) {
m_validZoomRange.min = m_validZoomRange.origMin = arg.min;
}
reset_minimum_zoom();
if (!noRefresh) {
m_this.zoom(m_zoom);
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Start an animated zoom/pan/rotate. If a second transition is requested
* while a transition is already in progress, a new transition is created
* that is functionally from whereever the map has moved to (possibly partway
* through the first transition) going to the end point of the new
* transition.
*
* Options:
* <pre>
* opts = {
* center: { x: ... , y: ... } // the new center
* zoom: ... // the new zoom level
* rotation: ... // the new rotation angle
* duration: ... // the duration (in ms) of the transition
* ease: ... // an easing function [0, 1] -> [0, 1]
* }
* </pre>
*
* Call with no arguments to return the current transition information.
*
* @param {object?} opts
* @param {string|geo.transform} [gcs] undefined to use the interface gcs,
* null to use the map gcs, or any other transform. Applies only to the
* center coordinate of the opts and to converting zoom values to height,
* if specified.
* @returns {geo.map}
*/
////////////////////////////////////////////////////////////////////////////
this.transition = function (opts, gcs) {
if (opts === undefined) {
return m_transition;
}
if (m_transition) {
m_queuedTransition = opts;
return m_this;
}
function interp1(p0, p1, t) {
return p0 + (p1 - p0) * t;
}
function defaultInterp(p0, p1) {
return function (t) {
var result = [];
$.each(p0, function (idx) {
result.push(interp1(p0[idx], p1[idx], t));
});
return result;
};
}
var units = m_this.unitsPerPixel(0);
// Transform zoom level into z-coordinate and inverse
function zoom2z(z) {
return vgl.zoomToHeight(z + 1, m_width, m_height) * units;
}
function z2zoom(z) {
return vgl.heightToZoom(z / units, m_width, m_height) - 1;
}
var defaultOpts = {
center: m_this.center(undefined, null),
zoom: m_this.zoom(),
rotation: m_this.rotation(),
duration: 1000,
ease: function (t) {
return t;
},
interp: defaultInterp,
done: null,
zCoord: true
};
if (opts.center) {
gcs = (gcs === null ? m_gcs : (gcs === undefined ? m_ingcs : gcs));
opts = $.extend(true, {}, opts);
opts.center = geo.util.normalizeCoordinates(opts.center);
if (gcs !== m_gcs) {
opts.center = geo.transform.transformCoordinates(gcs, m_gcs, [
opts.center])[0];
}
}
opts = $.extend(true, {}, defaultOpts, opts);
m_transition = {
start: {
center: m_this.center(undefined, null),
zoom: m_this.zoom(),
rotation: m_this.rotation()
},
end: {
center: opts.center,
zoom: fix_zoom(opts.zoom),
rotation: fix_rotation(opts.rotation, undefined, true)
},
ease: opts.ease,
zCoord: opts.zCoord,
done: opts.done,
duration: opts.duration
};
if (opts.zCoord) {
m_transition.interp = opts.interp(
[
m_transition.start.center.x,
m_transition.start.center.y,
zoom2z(m_transition.start.zoom),
m_transition.start.rotation
],
[
m_transition.end.center.x,
m_transition.end.center.y,
zoom2z(m_transition.end.zoom),
m_transition.end.rotation
]
);
} else {
m_transition.interp = opts.interp(
[
m_transition.start.center.x,
m_transition.start.center.y,
m_transition.start.zoom,
m_transition.start.rotation
],
[
m_transition.end.center.x,
m_transition.end.center.y,
m_transition.end.zoom,
m_transition.end.rotation
]
);
}
function anim(time) {
var done = m_transition.done, next;
next = m_queuedTransition;
if (!m_transition.start.time) {
m_transition.start.time = time;
m_transition.end.time = time + opts.duration;
}
m_transition.time = time - m_transition.start.time;
if (time >= m_transition.end.time || next) {
if (!next) {
m_this.center(m_transition.end.center, null);
m_this.zoom(m_transition.end.zoom);
m_this.rotation(fix_rotation(m_transition.end.rotation));
}
m_transition = null;
m_this.geoTrigger(geo.event.transitionend, opts);
if (done) {
done();
}
if (next) {
m_queuedTransition = null;
m_this.transition(next);
}
return;
}
var z = m_transition.ease(
(time - m_transition.start.time) / opts.duration
);
var p = m_transition.interp(z);
if (m_transition.zCoord) {
p[2] = z2zoom(p[2]);
}
if (fix_zoom(p[2], true) === m_zoom) {
m_this.center({
x: p[0],
y: p[1]
}, null);
} else {
m_center = m_this.gcsToWorld({x: p[0], y: p[1]}, null);
m_this.zoom(p[2], undefined, true);
}
m_this.rotation(p[3], undefined, true);
window.requestAnimationFrame(anim);
}
m_this.geoTrigger(geo.event.transitionstart, opts);
if (geo.event.cancelNavigation) {
m_transition = null;
m_this.geoTrigger(geo.event.transitionend, opts);
return m_this;
} else if (geo.event.cancelAnimation) {
// run the navigation synchronously
opts.duration = 0;
anim(0);
} else {
window.requestAnimationFrame(anim);
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/set the locations of the current map corners as latitudes/longitudes.
* When provided the argument should be an object containing the keys left,
* top, right, bottom declaring the desired new map bounds. The new bounds
* will contain at least the min/max lat/lngs provided modified by clamp
* settings. In any case, the actual new bounds will be returned by this
* function.
*
* @param {geo.geoBounds} [bds] The requested map bounds
* @param {string|geo.transform} [gcs] undefined to use the interface gcs,
* null to use the map gcs, or any other transform. If setting the
* bounds, they are converted from this gcs to the map projection. The
* returned bounds are converted from the map projection to this gcs.
* @return {geo.geoBounds} The actual new map bounds
*/
////////////////////////////////////////////////////////////////////////////
this.bounds = function (bds, gcs) {
var nav;
gcs = (gcs === null ? m_gcs : (gcs === undefined ? m_ingcs : gcs));
if (bds !== undefined) {
if (gcs !== m_gcs) {
var trans = geo.transform.transformCoordinates(gcs, m_gcs, [{
x: bds.left, y: bds.top}, {x: bds.right, y: bds.bottom}]);
bds = {
left: trans[0].x,
top: trans[0].y,
right: trans[1].x,
bottom: trans[1].y
};
}
bds = fix_bounds(bds, m_rotation);
nav = m_this.zoomAndCenterFromBounds(bds, m_rotation, null);
// This might have consequences in terms of bounds/zoom clamping.
// What behavior do we expect from this method in that case?
m_this.zoom(nav.zoom);
m_this.center(nav.center, null);
}
return m_this.boundsFromZoomAndCenter(m_zoom, m_center, m_rotation, gcs);
};
this.maxBounds = function (bounds, gcs) {
gcs = (gcs === null ? m_gcs : (gcs === undefined ? m_ingcs : gcs));
if (bounds === undefined) {
return {
left: geo.transform.transformCoordinates(m_gcs, gcs, [{
x: m_maxBounds.left, y: 0}])[0].x,
right: geo.transform.transformCoordinates(m_gcs, gcs, [{
x: m_maxBounds.right, y: 0}])[0].x,
bottom: geo.transform.transformCoordinates(m_gcs, gcs, [{
x: 0, y: m_maxBounds.bottom}])[0].y,
top: geo.transform.transformCoordinates(m_gcs, gcs, [{
x: 0, y: m_maxBounds.top}])[0].y
};
}
var cx = ((bounds.left || 0) + (bounds.right || 0)) / 2,
cy = ((bounds.bottom || 0) + (bounds.top || 0)) / 2;
if (bounds.left !== undefined) {
m_maxBounds.left = geo.transform.transformCoordinates(gcs, m_gcs, [{
x: bounds.left, y: cy}])[0].x;
}
if (bounds.right !== undefined) {
m_maxBounds.right = geo.transform.transformCoordinates(gcs, m_gcs, [{
x: bounds.right, y: cy}])[0].x;
}
if (bounds.bottom !== undefined) {
m_maxBounds.bottom = geo.transform.transformCoordinates(gcs, m_gcs, [{
x: cx, y: bounds.bottom}])[0].y;
}
if (bounds.top !== undefined) {
m_maxBounds.top = geo.transform.transformCoordinates(gcs, m_gcs, [{
x: cx, y: bounds.top}])[0].y;
}
reset_minimum_zoom();
m_this.zoom(m_zoom);
m_this.pan({x: 0, y: 0});
return this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the center zoom level necessary to display the given lat/lon bounds.
*
* @param {geo.geoBounds} [bds] The requested map bounds
* @param {number} rotation Rotation in clockwise radians.
* @param {string|geo.transform} [gcs] undefined to use the interface gcs,
* null to use the map gcs, or any other transform.
* @return {object} Object containing keys 'center' and 'zoom'
*/
////////////////////////////////////////////////////////////////////////////
this.zoomAndCenterFromBounds = function (bounds, rotation, gcs) {
var center, zoom;
gcs = (gcs === null ? m_gcs : (gcs === undefined ? m_ingcs : gcs));
if (gcs !== m_gcs) {
var trans = geo.transform.transformCoordinates(gcs, m_gcs, [{
x: bounds.left, y: bounds.top}, {x: bounds.right, y: bounds.bottom}]);
bounds = {
left: trans[0].x,
top: trans[0].y,
right: trans[1].x,
bottom: trans[1].y
};
}
if (bounds.left >= bounds.right || bounds.bottom >= bounds.top) {
throw new Error('Invalid bounds provided');
}
// calculate the zoom to fit the bounds
zoom = fix_zoom(calculate_zoom(bounds, rotation));
// clamp bounds if necessary
bounds = fix_bounds(bounds, rotation);
/* This relies on having the map projection coordinates be uniform
* regardless of location. If not, the center will not be correct. */
// calculate new center
center = {
x: (bounds.left + bounds.right) / 2 - m_origin.x,
y: (bounds.top + bounds.bottom) / 2 - m_origin.y
};
if (gcs !== m_gcs) {
center = geo.transform.transformCoordinates(m_gcs, gcs, [center])[0];
}
return {
zoom: zoom,
center: center
};
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the bounds that will be displayed with the given zoom and center.
*
* Note: the bounds may not have the requested zoom and center due to map
* restrictions.
*
* @param {number} zoom The requested zoom level
* @param {geo.geoPosition} center The requested center
* @param {number} rotation The requested rotation
* @param {string|geo.transform} [gcs] undefined to use the interface gcs,
* null to use the map gcs, or any other transform.
* @return {geo.geoBounds}
*/
////////////////////////////////////////////////////////////////////////////
this.boundsFromZoomAndCenter = function (zoom, center, rotation, gcs) {
var width, height, halfw, halfh, bounds, units;
gcs = (gcs === null ? m_gcs : (gcs === undefined ? m_ingcs : gcs));
// preprocess the arguments
zoom = fix_zoom(zoom);
units = m_this.unitsPerPixel(zoom);
center = m_this.gcsToWorld(center, gcs);
// get half the width and height in world coordinates
width = m_width * units;
height = m_height * units;
halfw = width / 2;
halfh = height / 2;
// calculate the bounds. This is only valid if the map projection has
// uniform units in each direction. If not, then worldToGcs should be
// used.
if (rotation) {
center.x += m_origin.x;
center.y += m_origin.y;
bounds = rotate_bounds_center(
center, {width: width, height: height}, rotation);
// correct the bounds when clamping is enabled
bounds.width = width;
bounds.height = height;
bounds = fix_bounds(bounds, rotation);
} else {
bounds = {
left: center.x - halfw + m_origin.x,
right: center.x + halfw + m_origin.x,
bottom: center.y - halfh + m_origin.y,
top: center.y + halfh + m_origin.y
};
// correct the bounds when clamping is enabled
bounds = fix_bounds(bounds, 0);
}
if (gcs !== m_gcs) {
var bds = geo.transform.transformCoordinates(
m_gcs, gcs,
[[bounds.left, bounds.top], [bounds.right, bounds.bottom]]);
bounds = {
left: bds[0][0], top: bds[0][1], right: bds[1][0], bottom: bds[1][1]
};
}
/* Add the original width and height of the viewport before rotation. */
bounds.width = width;
bounds.height = height;
return bounds;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/set the discrete zoom flag.
*
* @param {bool} If specified, the discrete zoom flag.
* @return {bool} The current discrete zoom flag if no parameter is
* specified, otherwise the map object.
*/
////////////////////////////////////////////////////////////////////////////
this.discreteZoom = function (discreteZoom) {
if (discreteZoom === undefined) {
return m_discreteZoom;
}
discreteZoom = discreteZoom ? true : false;
if (m_discreteZoom !== discreteZoom) {
m_discreteZoom = discreteZoom;
if (m_discreteZoom) {
m_this.zoom(Math.round(m_this.zoom()));
}
m_this.interactor().options({discreteZoom: m_discreteZoom});
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the layers contained in the map.
* Alias of {@linkcode geo.sceneObject.children}.
*/
////////////////////////////////////////////////////////////////////////////
this.layers = this.children;
////////////////////////////////////////////////////////////////////////////
/**
* Update the attribution notice displayed on the bottom right corner of
* the map. The content of this notice is managed by individual layers.
* This method queries all of the visible layers and joins the individual
* attribution notices into a single element. By default, this method
* is called on each of the following events:
*
* * geo.event.layerAdd
* * geo.event.layerRemove
*
* In addition, layers should call this method when their own attribution
* notices has changed. Users, in general, should not need to call this.
* @returns {this} Chainable
*/
////////////////////////////////////////////////////////////////////////////
this.updateAttribution = function () {
// clear any existing attribution content
m_this.node().find('.geo-attribution').remove();
// generate a new attribution node
var $a = $('<div/>')
.addClass('geo-attribution')
.css({
position: 'absolute',
right: '0px',
bottom: '0px',
'padding-right': '5px',
cursor: 'auto',
font: '11px/1.5 "Helvetica Neue", Arial, Helvetica, sans-serif',
'z-index': '1001',
background: 'rgba(255,255,255,0.7)',
clear: 'both',
display: 'block',
'pointer-events': 'auto'
}).on('mousedown', function (evt) {
evt.stopPropagation();
});
// append content from each layer
m_this.children().forEach(function (layer) {
var content = layer.attribution();
if (content) {
$('<span/>')
.addClass('geo-attribution-layer')
.css({
'padding-left': '5px'
})
.html(content)
.appendTo($a);
}
});
$a.appendTo(m_this.node());
return m_this;
};
////////////////////////////////////////////////////////////////////////////
//
// The following are some private methods for interacting with the camera.
// In order to hide the complexity of dealing with map aspect ratios,
// clamping behavior, reseting zoom levels on resize, etc. from the
// layers, the map handles camera movements directly. This requires
// passing all camera movement events through the map initially. The
// map uses these methods to fix up the events according to the constraints
// of the display and passes the event to the layers.
//
////////////////////////////////////////////////////////////////////////////
/**
* Calculate the scaling factor to fit the given map bounds
* into the viewport with the correct aspect ratio.
* @param {object} bounds A desired bounds
* @return {object} Multiplicative aspect ratio correction
* @private
*/
function camera_scaling(bounds) {
var width = bounds.right - bounds.left,
height = bounds.top - bounds.bottom,
ar_bds = Math.abs(width / height),
ar_vp = m_width / m_height,
sclx, scly;
if (ar_bds > ar_vp) {
// fit left and right
sclx = 1;
// grow top and bottom
scly = ar_bds / ar_vp;
} else {
// fit top and bottom
scly = 1;
// grow left and right
sclx = ar_vp / ar_bds;
}
return {x: sclx, y: scly};
}
/**
* Adjust a set of bounds based on a rotation.
* @private.
*/
function rotate_bounds(bounds, rotation) {
if (rotation) {
var center = {
x: (bounds.left + bounds.right) / 2,
y: (bounds.top + bounds.bottom) / 2
};
var size = {
width: Math.abs(bounds.left - bounds.right),
height: Math.abs(bounds.top - bounds.bottom)
};
bounds = rotate_bounds_center(center, size, rotation);
}
return bounds;
}
/**
* Generate a set of bounds based on a center point, a width and height, and
* a rotation.
* @private.
*/
function rotate_bounds_center(center, size, rotation) {
// calculate the half width and height
var width = size.width / 2, height = size.height / 2;
var sinr = Math.sin(rotation), cosr = Math.cos(rotation);
var ul = {}, ur = {}, ll = {}, lr = {};
ul.x = center.x + (-width) * cosr - (-height) * sinr;
ul.y = center.y + (-width) * sinr + (-height) * cosr;
ur.x = center.x + width * cosr - (-height) * sinr;
ur.y = center.y + width * sinr + (-height) * cosr;
ll.x = center.x + (-width) * cosr - height * sinr;
ll.y = center.y + (-width) * sinr + height * cosr;
lr.x = center.x + width * cosr - height * sinr;
lr.y = center.y + width * sinr + height * cosr;
return {
left: Math.min(ul.x, ur.x, ll.x, lr.x),
right: Math.max(ul.x, ur.x, ll.x, lr.x),
bottom: Math.min(ul.y, ur.y, ll.y, lr.y),
top: Math.max(ul.y, ur.y, ll.y, lr.y)
};
}
/**
* Calculate the minimum zoom level to fit the given
* bounds inside the view port using the view port size,
* the given bounds, and the number of units per
* pixel. The method sets the valid zoom bounds as well
* as the current zoom level to be within that range.
* @private
*/
function calculate_zoom(bounds, rotation) {
if (rotation === undefined) {
rotation = m_rotation;
}
bounds = rotate_bounds(bounds, rotation);
// compare the aspect ratios of the viewport and bounds
var scl = camera_scaling(bounds), z;
if (scl.y > scl.x) {
// left to right matches exactly
// center map vertically and have blank borders on the
// top and bottom (or repeat tiles)
z = -Math.log2(
Math.abs(bounds.right - bounds.left) * scl.x /
(m_width * m_unitsPerPixel)
);
} else {
// top to bottom matches exactly, blank border on the
// left and right (or repeat tiles)
z = -Math.log2(
Math.abs(bounds.top - bounds.bottom) * scl.y /
(m_height * m_unitsPerPixel)
);
}
return z;
}
/**
* Reset the minimum zoom level given the current window size.
* @private
*/
function reset_minimum_zoom() {
if (m_clampZoom) {
m_validZoomRange.min = Math.max(
m_validZoomRange.origMin, calculate_zoom(m_maxBounds));
} else {
m_validZoomRange.min = m_validZoomRange.origMin;
}
}
/**
* Return the nearest valid zoom level to the requested zoom.
* @private
*/
function fix_zoom(zoom, ignoreDiscreteZoom) {
zoom = Math.max(
Math.min(
m_validZoomRange.max,
zoom
),
m_validZoomRange.min
);
if (m_discreteZoom && !ignoreDiscreteZoom) {
zoom = Math.round(zoom);
if (zoom < m_validZoomRange.min) {
zoom = Math.ceil(m_validZoomRange.min);
}
}
return zoom;
}
/**
* Return a valid rotation angle.
* @private
*/
function fix_rotation(rotation, ignoreRotationFunc, noRangeLimit) {
if (!m_allowRotation) {
return 0;
}
if (!ignoreRotationFunc && typeof m_allowRotation === 'function') {
rotation = m_allowRotation(rotation);
}
/* Ensure that the rotation is in the range [0, 2pi) */
if (!noRangeLimit) {
var range = Math.PI * 2;
rotation = (rotation % range) + (rotation >= 0 ? 0 : range);
if (Math.min(Math.abs(rotation), Math.abs(rotation - range)) < 0.00001) {
rotation = 0;
}
}
return rotation;
}
/**
* Return the nearest valid bounds maintaining the
* width and height. Does nothing if m_clampBounds* is
* false.
* @private
*/
function fix_bounds(bounds, rotation) {
if (!m_clampBoundsX && !m_clampBoundsY) {
return bounds;
}
var dx, dy, maxBounds = m_maxBounds;
if (rotation) {
maxBounds = $.extend({}, m_maxBounds);
/* When rotated, expand the maximum bounds so that they will allow the
* corners to be visible. We know the rotated bounding box, plus the
* original maximum bounds. To fit the corners of the maximum bounds, we
* can expand the total bounds by the same factor that the rotated
* bounding box is expanded from the non-rotated bounding box (for a
* small rotation, this is sin(rotation) * (original bounding box height)
* in the width). This feels like appropriate behaviour with one of the
* two bounds clamped. With both, it seems mildly peculiar. */
var bw = Math.abs(bounds.right - bounds.left),
bh = Math.abs(bounds.top - bounds.bottom),
absinr = Math.abs(Math.sin(rotation)),
abcosr = Math.abs(Math.cos(rotation)),
ow, oh;
if (bounds.width && bounds.height) {
ow = bounds.width;
oh = bounds.height;
} else if (Math.abs(absinr - abcosr) < 0.0005) {
/* If we are close to a 45 degree rotation, it is ill-determined to
* compute the original (pre-rotation) bounds width and height. In
* this case, assume that we are using the map's aspect ratio. */
if (m_width && m_height) {
var aspect = Math.abs(m_width / m_height);
var fac = Math.pow(1 + Math.pow(aspect, 2), 0.5);
ow = Math.max(bw, bh) / fac;
oh = ow * aspect;
} else {
/* Fallback if we don't have width or height */
ow = bw * abcosr;
oh = bh * absinr;
}
} else {
/* Compute the pre-rotation (original) bounds width and height */
ow = (abcosr * bw - absinr * bh) / (abcosr * abcosr - absinr * absinr);
oh = (abcosr * bh - absinr * bw) / (abcosr * abcosr - absinr * absinr);
}
/* Our maximum bounds are expanded based on the projected length of a
* tilted side of the original bounding box in the rotated bounding box.
* To handle all rotations, take the minimum difference in width or
* height. */
var bdx = bw - Math.max(abcosr * ow, absinr * oh),
bdy = bh - Math.max(abcosr * oh, absinr * ow);
maxBounds.left -= bdx;
maxBounds.right += bdx;
maxBounds.top += bdy;
maxBounds.bottom -= bdy;
}
if (m_clampBoundsX) {
if (bounds.right - bounds.left > maxBounds.right - maxBounds.left) {
dx = maxBounds.left - ((bounds.right - bounds.left - (
maxBounds.right - maxBounds.left)) / 2) - bounds.left;
} else if (bounds.left < maxBounds.left) {
dx = maxBounds.left - bounds.left;
} else if (bounds.right > maxBounds.right) {
dx = maxBounds.right - bounds.right;
}
if (dx) {
bounds = {
left: bounds.left += dx,
right: bounds.right += dx,
top: bounds.top,
bottom: bounds.bottom
};
}
}
if (m_clampBoundsY) {
if (bounds.top - bounds.bottom > maxBounds.top - maxBounds.bottom) {
dy = maxBounds.bottom - ((bounds.top - bounds.bottom - (
maxBounds.top - maxBounds.bottom)) / 2) - bounds.bottom;
} else if (bounds.top > maxBounds.top) {
dy = maxBounds.top - bounds.top;
} else if (bounds.bottom < maxBounds.bottom) {
dy = maxBounds.bottom - bounds.bottom;
}
if (dy) {
bounds = {
top: bounds.top += dy,
bottom: bounds.bottom += dy,
left: bounds.left,
right: bounds.right
};
}
}
return bounds;
}
/**
* Call the camera bounds method with the given bounds, but
* correct for the viewport aspect ratio.
* @private
*/
function camera_bounds(bounds, rotation) {
m_camera.rotation = rotation || 0;
/* When dealing with rotation, use the original width and height of the
* bounds, as the rotation will have expanded them. */
if (bounds.width && bounds.height && rotation) {
var cx = (bounds.left + bounds.right) / 2,
cy = (bounds.top + bounds.bottom) / 2;
m_camera.viewFromCenterSizeRotation({x: cx, y: cy}, bounds, rotation);
} else {
m_camera.bounds = bounds;
}
}
////////////////////////////////////////////////////////////////////////////
//
// All the methods are now defined. From here, we are initializing all
// internal variables and event handlers.
//
////////////////////////////////////////////////////////////////////////////
// Set the world origin
m_origin = {x: 0, y: 0};
// Fix the zoom level (minimum and initial)
this.zoomRange(arg, true);
m_zoom = fix_zoom(m_zoom);
m_rotation = fix_rotation(m_rotation);
// Now update to the correct center and zoom level
this.center($.extend({}, arg.center || m_center), undefined);
this.interactor(arg.interactor || geo.mapInteractor({discreteZoom: m_discreteZoom}));
this.clock(arg.clock || geo.clock());
function resizeSelf() {
m_this.resize(0, 0, m_node.width(), m_node.height());
}
if (arg.autoResize) {
$(window).resize(resizeSelf);
}
// attach attribution updates to layer events
m_this.geoOn([
geo.event.layerAdd,
geo.event.layerRemove
], m_this.updateAttribution);
return this;
};
/**
* General object specification for map types. Any additional
* values in the object are passed to the map constructor.
* @typedef geo.map.spec
* @type {object}
* @property {object[]} [data=[]] The default data array to
* apply to each feature if none exists
* @property {geo.layer.spec[]} [layers=[]] Layers to create
*/
/**
* Create a map from an object. Any errors in the creation
* of the map will result in returning null.
* @param {geo.map.spec} spec The object specification
* @returns {geo.map|null}
*/
geo.map.create = function (spec) {
'use strict';
var map = geo.map(spec);
/* If the spec is bad, we still end up with an object, but it won't have a
* zoom function */
if (!map || !map.zoom) {
console.warn('Could not create map.');
return null;
}
spec.data = spec.data || [];
spec.layers = spec.layers || [];
spec.layers.forEach(function (l) {
l.data = l.data || spec.data;
l.layer = geo.layer.create(map, l);
});
return map;
};
inherit(geo.map, geo.sceneObject);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class feature
*
* @class
* @extends geo.sceneObject
* @returns {geo.feature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.feature = function (arg) {
'use strict';
if (!(this instanceof geo.feature)) {
return new geo.feature(arg);
}
geo.sceneObject.call(this);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
arg = arg || {};
var m_this = this,
s_exit = this._exit,
m_selectionAPI = arg.selectionAPI === undefined ? false : arg.selectionAPI,
m_style = {},
m_layer = arg.layer === undefined ? null : arg.layer,
m_gcs = arg.gcs,
m_visible = arg.visible === undefined ? true : arg.visible,
m_bin = arg.bin === undefined ? 0 : arg.bin,
m_renderer = arg.renderer === undefined ? null : arg.renderer,
m_dataTime = geo.timestamp(),
m_buildTime = geo.timestamp(),
m_updateTime = geo.timestamp(),
m_selectedFeatures = [];
////////////////////////////////////////////////////////////////////////////
/**
* Private method to bind mouse handlers on the map element.
*/
////////////////////////////////////////////////////////////////////////////
this._bindMouseHandlers = function () {
// Don't bind handlers for improved performance on features that don't
// require it.
if (!m_selectionAPI) {
return;
}
// First unbind to be sure that the handlers aren't bound twice.
m_this._unbindMouseHandlers();
m_this.geoOn(geo.event.mousemove, m_this._handleMousemove);
m_this.geoOn(geo.event.mouseclick, m_this._handleMouseclick);
m_this.geoOn(geo.event.brushend, m_this._handleBrushend);
m_this.geoOn(geo.event.brush, m_this._handleBrush);
};
////////////////////////////////////////////////////////////////////////////
/**
* Private method to unbind mouse handlers on the map element.
*/
////////////////////////////////////////////////////////////////////////////
this._unbindMouseHandlers = function () {
m_this.geoOff(geo.event.mousemove, m_this._handleMousemove);
m_this.geoOff(geo.event.mouseclick, m_this._handleMouseclick);
m_this.geoOff(geo.event.brushend, m_this._handleBrushend);
m_this.geoOff(geo.event.brush, m_this._handleBrush);
};
////////////////////////////////////////////////////////////////////////////
/**
* For binding mouse events, use functions with
* the following call signatures:
*
* function handler(arg) {
* // arg.data - the data object of the feature
* // arg.index - the index inside the data array of the featue
* // arg.mouse - mouse information object (see src/core/mapInteractor.js)
* }
*
* i.e.
*
* feature.geoOn(geo.event.feature.mousemove, function (arg) {
* // do something with the feature marker.
* });
*/
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/**
* Search for features containing the given point.
*
* Returns an object: ::
*
* {
* data: [...] // an array of data objects for matching features
* index: [...] // an array of indices of the matching features
* }
*
* @argument {Object} coordinate
* @returns {Object}
*/
////////////////////////////////////////////////////////////////////////////
this.pointSearch = function () {
// base class method does nothing
return {
index: [],
found: []
};
};
////////////////////////////////////////////////////////////////////////////
/**
* Private mousemove handler
*/
////////////////////////////////////////////////////////////////////////////
this._handleMousemove = function () {
var mouse = m_this.layer().map().interactor().mouse(),
data = m_this.data(),
over = m_this.pointSearch(mouse.geo),
newFeatures = [], oldFeatures = [], lastTop = -1, top = -1;
// Get the index of the element that was previously on top
if (m_selectedFeatures.length) {
lastTop = m_selectedFeatures[m_selectedFeatures.length - 1];
}
// There are probably faster ways of doing this:
newFeatures = over.index.filter(function (i) {
return m_selectedFeatures.indexOf(i) < 0;
});
oldFeatures = m_selectedFeatures.filter(function (i) {
return over.index.indexOf(i) < 0;
});
geo.feature.eventID += 1;
// Fire events for mouse in first.
newFeatures.forEach(function (i, idx) {
m_this.geoTrigger(geo.event.feature.mouseover, {
data: data[i],
index: i,
mouse: mouse,
eventID: geo.feature.eventID,
top: idx === newFeatures.length - 1
}, true);
});
geo.feature.eventID += 1;
// Fire events for mouse out next
oldFeatures.forEach(function (i, idx) {
m_this.geoTrigger(geo.event.feature.mouseout, {
data: data[i],
index: i,
mouse: mouse,
eventID: geo.feature.eventID,
top: idx === oldFeatures.length - 1
}, true);
});
geo.feature.eventID += 1;
// Fire events for mouse move last
over.index.forEach(function (i, idx) {
m_this.geoTrigger(geo.event.feature.mousemove, {
data: data[i],
index: i,
mouse: mouse,
eventID: geo.feature.eventID,
top: idx === over.index.length - 1
}, true);
});
// Replace the selected features array
m_selectedFeatures = over.index;
// Get the index of the element that is now on top
if (m_selectedFeatures.length) {
top = m_selectedFeatures[m_selectedFeatures.length - 1];
}
if (lastTop !== top) {
// The element on top changed so we need to fire mouseon/mouseoff
if (lastTop !== -1) {
m_this.geoTrigger(geo.event.feature.mouseoff, {
data: data[lastTop],
index: lastTop,
mouse: mouse
}, true);
}
if (top !== -1) {
m_this.geoTrigger(geo.event.feature.mouseon, {
data: data[top],
index: top,
mouse: mouse
}, true);
}
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Private mouseclick handler
*/
////////////////////////////////////////////////////////////////////////////
this._handleMouseclick = function () {
var mouse = m_this.layer().map().interactor().mouse(),
data = m_this.data(),
over = m_this.pointSearch(mouse.geo);
geo.feature.eventID += 1;
over.index.forEach(function (i, idx) {
m_this.geoTrigger(geo.event.feature.mouseclick, {
data: data[i],
index: i,
mouse: mouse,
eventID: geo.feature.eventID,
top: idx === over.index.length - 1
}, true);
});
};
////////////////////////////////////////////////////////////////////////////
/**
* Private brush handler.
*/
////////////////////////////////////////////////////////////////////////////
this._handleBrush = function (brush) {
var idx = m_this.boxSearch(brush.gcs.lowerLeft, brush.gcs.upperRight),
data = m_this.data();
geo.feature.eventID += 1;
idx.forEach(function (i, idx) {
m_this.geoTrigger(geo.event.feature.brush, {
data: data[i],
index: i,
mouse: brush.mouse,
brush: brush,
eventID: geo.feature.eventID,
top: idx === idx.length - 1
}, true);
});
};
////////////////////////////////////////////////////////////////////////////
/**
* Private brushend handler.
*/
////////////////////////////////////////////////////////////////////////////
this._handleBrushend = function (brush) {
var idx = m_this.boxSearch(brush.gcs.lowerLeft, brush.gcs.upperRight),
data = m_this.data();
geo.feature.eventID += 1;
idx.forEach(function (i, idx) {
m_this.geoTrigger(geo.event.feature.brushend, {
data: data[i],
index: i,
mouse: brush.mouse,
brush: brush,
eventID: geo.feature.eventID,
top: idx === idx.length - 1
}, true);
});
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set style used by the feature
*/
////////////////////////////////////////////////////////////////////////////
this.style = function (arg1, arg2) {
if (arg1 === undefined) {
return m_style;
} else if (typeof arg1 === 'string' && arg2 === undefined) {
return m_style[arg1];
} else if (arg2 === undefined) {
m_style = $.extend({}, m_style, arg1);
m_this.modified();
return m_this;
} else {
m_style[arg1] = arg2;
m_this.modified();
return m_this;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* A uniform getter that always returns a function even for constant styles.
* Maybe extend later to support accessor-like objects. If undefined input,
* return all the styles as an object.
*
* @param {string|undefined} key
* @return {function}
*/
////////////////////////////////////////////////////////////////////////////
this.style.get = function (key) {
var tmp, out;
if (key === undefined) {
var all = {}, k;
for (k in m_style) {
if (m_style.hasOwnProperty(k)) {
all[k] = m_this.style.get(k);
}
}
return all;
}
if (key.toLowerCase().match(/color$/)) {
if (geo.util.isFunction(m_style[key])) {
tmp = geo.util.ensureFunction(m_style[key]);
out = function () {
return geo.util.convertColor(
tmp.apply(this, arguments)
);
};
} else {
// if the color is not a function, only convert it once
out = geo.util.ensureFunction(geo.util.convertColor(m_style[key]));
}
} else {
out = geo.util.ensureFunction(m_style[key]);
}
return out;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get layer referenced by the feature
*/
////////////////////////////////////////////////////////////////////////////
this.layer = function () {
return m_layer;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get renderer used by the feature
*/
////////////////////////////////////////////////////////////////////////////
this.renderer = function () {
return m_renderer;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set projection of the feature
*/
////////////////////////////////////////////////////////////////////////////
this.gcs = function (val) {
if (val === undefined) {
if (m_gcs === undefined && m_renderer) {
return m_renderer.layer().map().ingcs();
}
return m_gcs;
} else {
m_gcs = val;
m_this.modified();
return m_this;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Convert from the renderer's input gcs coordinates to display coordinates.
*
* @param {object} c The input coordinate to convert
* @param {object} c.x
* @param {object} c.y
* @param {object} [c.z=0]
* @return {object} Display space coordinates
*/
this.featureGcsToDisplay = function (c) {
var map = m_renderer.layer().map();
c = map.gcsToWorld(c, map.ingcs());
c = map.worldToDisplay(c);
if (m_renderer.baseToLocal) {
c = m_renderer.baseToLocal(c);
}
return c;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set visibility of the feature
*/
////////////////////////////////////////////////////////////////////////////
this.visible = function (val) {
if (val === undefined) {
return m_visible;
} else {
m_visible = val;
m_this.modified();
// bind or unbind mouse handlers on visibility change
if (m_visible) {
m_this._bindMouseHandlers();
} else {
m_this._unbindMouseHandlers();
}
return m_this;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set bin of the feature
*
* Bin number is typically used for sorting the order of rendering
*/
////////////////////////////////////////////////////////////////////////////
this.bin = function (val) {
if (val === undefined) {
return m_bin;
} else {
m_bin = val;
m_this.modified();
return m_this;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set timestamp of data change
*/
////////////////////////////////////////////////////////////////////////////
this.dataTime = function (val) {
if (val === undefined) {
return m_dataTime;
} else {
m_dataTime = val;
m_this.modified();
return m_this;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set timestamp of last time build happened
*/
////////////////////////////////////////////////////////////////////////////
this.buildTime = function (val) {
if (val === undefined) {
return m_buildTime;
} else {
m_buildTime = val;
m_this.modified();
return m_this;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set timestamp of last time update happened
*/
////////////////////////////////////////////////////////////////////////////
this.updateTime = function (val) {
if (val === undefined) {
return m_updateTime;
} else {
m_updateTime = val;
m_this.modified();
return m_this;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set the data array for the feature.
*
* @returns {Array|this}
*/
////////////////////////////////////////////////////////////////////////////
this.data = function (data) {
if (data === undefined) {
return m_this.style('data') || [];
} else {
m_this.style('data', data);
m_this.dataTime().modified();
m_this.modified();
return m_this;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Query if the selection API is enabled for this feature.
* @returns {bool}
*/
////////////////////////////////////////////////////////////////////////////
this.selectionAPI = function () {
return m_selectionAPI;
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*
* Derived class should implement this
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
if (!m_layer) {
throw 'Feature requires a valid layer';
}
m_style = $.extend({},
{'opacity': 1.0}, arg.style === undefined ? {} :
arg.style);
m_this._bindMouseHandlers();
};
////////////////////////////////////////////////////////////////////////////
/**
* Build
*
* Derived class should implement this
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
*
* Derived class should implement this
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
};
////////////////////////////////////////////////////////////////////////////
/**
* Destroy
*
* Derived class should implement this
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this._unbindMouseHandlers();
m_selectedFeatures = [];
m_style = {};
arg = {};
s_exit();
};
this._init(arg);
return this;
};
/**
* The most recent feature event triggered.
* @type {number}
*/
geo.feature.eventID = 0;
/**
* General object specification for feature types.
* @typedef geo.feature.spec
* @type {object}
* @property {string} type A supported feature type.
* @property {object[]} [data=[]] An array of arbitrary objects used to
* construct the feature. These objects (and their associated
* indices in the array) will be passed back to style and attribute
* accessors provided by the user. In general the number of
* "markers" drawn will be equal to the length of this array.
*/
/**
* Create a feature from an object. The implementation here is
* meant to define the general interface of creating features
* from a javascript object. See documentation from individual
* feature types for specific details. In case of an error in
* the arguments this method will return null;
* @param {geo.layer} layer The layer to add the feature to
* @param {geo.feature.spec} [spec={}] The object specification
* @returns {geo.feature|null}
*/
geo.feature.create = function (layer, spec) {
'use strict';
var type = spec.type;
// Check arguments
if (!(layer instanceof geo.layer)) {
console.warn('Invalid layer');
return null;
}
if (typeof spec !== 'object') {
console.warn('Invalid spec');
return null;
}
var feature = layer.createFeature(type);
if (!feature) {
console.warn("Could not create feature type '" + type + "'");
return null;
}
spec = spec || {};
spec.data = spec.data || [];
return feature.style(spec);
};
inherit(geo.feature, geo.sceneObject);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class pointFeature
*
* @class
* @param {object} arg Options object
* @param {boolean} arg.clustering Enable point clustering
* @extends geo.feature
* @returns {geo.pointFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.pointFeature = function (arg) {
'use strict';
if (!(this instanceof geo.pointFeature)) {
return new geo.pointFeature(arg);
}
arg = arg || {};
geo.feature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_init = this._init,
m_rangeTree = null,
m_rangeTreeTime = geo.timestamp(),
s_data = this.data,
m_maxRadius = 0,
m_clustering = arg.clustering,
m_clusterTree = null,
m_allData = [],
m_lastZoom = null,
m_ignoreData = false; // flag to ignore data() calls made locally
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set clustering option
*
* @returns {geo.pointFeature|boolean}
*/
////////////////////////////////////////////////////////////////////////////
this.clustering = function (val) {
if (val === undefined) {
return m_clustering;
}
if (m_clustering && !val) {
// Throw out the cluster tree and reset the data
m_clusterTree = null;
m_clustering = false;
s_data(m_allData);
m_allData = null;
} else if (!m_clustering && val) {
// Generate the cluster tree
m_clustering = true;
m_this._clusterData();
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Generate the clustering tree from positions. This might be async in the
* future.
*/
////////////////////////////////////////////////////////////////////////////
this._clusterData = function () {
if (!m_clustering) {
// clustering is not enabled, so this is a no-op
return;
}
// set clustering options to default if an options argument wasn't supplied
var opts = m_clustering === true ? {radius: 0.01} : m_clustering;
// generate the cluster tree from the raw data
var position = m_this.position();
m_clusterTree = new geo.util.ClusterGroup(
opts, m_this.layer().width(), m_this.layer().height());
m_allData.forEach(function (d, i) {
// for each point in the data set normalize the coordinate
// representation and add the point to the cluster treee
var pt = geo.util.normalizeCoordinates(position(d, i));
pt.index = i;
m_clusterTree.addPoint(pt);
});
// reset the last zoom state and trigger a redraw at the current zoom level
m_lastZoom = null;
m_this._handleZoom(m_this.layer().map().zoom());
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle zoom events for clustering. This keeps track of the last
* clustering level, and only regenerates the displayed points when the
* zoom level changes.
*/
////////////////////////////////////////////////////////////////////////////
this._handleZoom = function (zoom) {
// get the current zoom level rounded down
var z = Math.floor(zoom);
if (!m_clustering || z === m_lastZoom) {
// short cut when there is nothing to do
return;
}
// store the current zoom level privately
m_lastZoom = z;
// get the raw data elements for the points at the current level
var data = m_clusterTree.points(z).map(function (d) {
return m_allData[d.index];
});
// append the clusters at the current level
m_clusterTree.clusters(z).forEach(function (d) {
// mark the datum as a cluster for accessor methods
d.__cluster = true;
// store all of the data objects for each point in the cluster as __data
d.__data = [];
d.obj.each(function (e) {
d.__data.push(m_allData[e.index]);
});
data.push(d);
});
// prevent recomputing the clustering and set the new data array
m_ignoreData = true;
m_this.data(data);
m_this.layer().map().draw(); // replace with m_this.draw() when gl is fixed
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set position
*
* @returns {geo.pointFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.position = function (val) {
if (val === undefined) {
return m_this.style('position');
} else {
val = geo.util.ensureFunction(val);
m_this.style('position', function (d, i) {
if (d.__cluster) {
return d;
} else {
return val(d, i);
}
});
m_this.dataTime().modified();
m_this.modified();
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Update the current range tree object. Should be called whenever the
* data changes.
*/
////////////////////////////////////////////////////////////////////////////
this._updateRangeTree = function () {
if (m_rangeTreeTime.getMTime() >= m_this.dataTime().getMTime()) {
return;
}
var pts, position,
radius = m_this.style.get('radius'),
stroke = m_this.style.get('stroke'),
strokeWidth = m_this.style.get('strokeWidth');
position = m_this.position();
m_maxRadius = 0;
// create an array of positions in geo coordinates
pts = m_this.data().map(function (d, i) {
var pt = position(d);
pt.idx = i;
// store the maximum point radius
m_maxRadius = Math.max(
m_maxRadius,
radius(d, i) + (stroke(d, i) ? strokeWidth(d, i) : 0)
);
return pt;
});
m_rangeTree = new geo.util.RangeTree(pts);
m_rangeTreeTime.modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Returns an array of datum indices that contain the given point.
* Largely adapted from wigglemaps pointQuerier:
*
* https://github.com/dotskapes/wigglemaps/blob/cf5bed3fbfe2c3e48d31799462a80c564be1fb60/src/query/PointQuerier.js
*/
////////////////////////////////////////////////////////////////////////////
this.pointSearch = function (p) {
var min, max, data, idx = [], box, found = [], ifound = [], map, pt,
corners,
stroke = m_this.style.get('stroke'),
strokeWidth = m_this.style.get('strokeWidth'),
radius = m_this.style.get('radius');
if (!m_this.selectionAPI()) {
return [];
}
data = m_this.data();
if (!data || !data.length) {
return {
found: [],
index: []
};
}
map = m_this.layer().map();
pt = map.gcsToDisplay(p);
// check all corners to make sure we handle rotations
corners = [
map.displayToGcs({x: pt.x - m_maxRadius, y: pt.y - m_maxRadius}),
map.displayToGcs({x: pt.x + m_maxRadius, y: pt.y - m_maxRadius}),
map.displayToGcs({x: pt.x - m_maxRadius, y: pt.y + m_maxRadius}),
map.displayToGcs({x: pt.x + m_maxRadius, y: pt.y + m_maxRadius})
];
min = {
x: Math.min(corners[0].x, corners[1].x, corners[2].x, corners[3].x),
y: Math.min(corners[0].y, corners[1].y, corners[2].y, corners[3].y)
};
max = {
x: Math.max(corners[0].x, corners[1].x, corners[2].x, corners[3].x),
y: Math.max(corners[0].y, corners[1].y, corners[2].y, corners[3].y)
};
// Find points inside the bounding box
box = new geo.util.Box(geo.util.vect(min.x, min.y), geo.util.vect(max.x, max.y));
m_this._updateRangeTree();
m_rangeTree.search(box).forEach(function (q) {
idx.push(q.idx);
});
// Filter by circular region
idx.forEach(function (i) {
var d = data[i],
p = m_this.position()(d, i),
dx, dy, rad, rad2;
rad = radius(data[i], i);
rad += stroke(data[i], i) ? strokeWidth(data[i], i) : 0;
rad2 = rad * rad;
p = map.gcsToDisplay(p);
dx = p.x - pt.x;
dy = p.y - pt.y;
if (dx * dx + dy * dy <= rad2) {
found.push(d);
ifound.push(i);
}
});
return {
data: found,
index: ifound
};
};
////////////////////////////////////////////////////////////////////////////
/**
* Returns an array of datum indices that are contained in the given box.
*/
////////////////////////////////////////////////////////////////////////////
this.boxSearch = function (lowerLeft, upperRight) {
var pos = m_this.position(),
idx = [];
// TODO: use the range tree
m_this.data().forEach(function (d, i) {
var p = pos(d);
if (p.x >= lowerLeft.x &&
p.x <= upperRight.x &&
p.y >= lowerLeft.y &&
p.y <= upperRight.y
) {
idx.push(i);
}
});
return idx;
};
////////////////////////////////////////////////////////////////////////////
/**
* Overloaded data method that updates the internal range tree on write.
*/
////////////////////////////////////////////////////////////////////////////
this.data = function (data) {
if (data === undefined) {
return s_data();
}
if (m_clustering && !m_ignoreData) {
m_allData = data;
m_this._clusterData();
} else {
s_data(data);
}
m_ignoreData = false;
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Returns the bounding box for a given datum in screen coordinates as an
* object: ::
*
* {
* min: {
* x: value,
* y: value
* },
* max: {
* x: value,
* y: value
* }
* }
*
* @returns {object}
*/
////////////////////////////////////////////////////////////////////////////
this._boundingBox = function (d) {
var pt, radius;
// get the position in geo coordinates
pt = m_this.position()(d);
// convert to screen coordinates
pt = m_this.layer().map().gcsToDisplay(pt);
// get the radius of the points (should we add stroke width?)
radius = m_this.style().radius(d);
return {
min: {
x: pt.x - radius,
y: pt.y - radius
},
max: {
x: pt.x + radius,
y: pt.y + radius
}
};
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
var defaultStyle = $.extend(
{},
{
radius: 5.0,
stroke: true,
strokeColor: { r: 0.851, g: 0.604, b: 0.0 },
strokeWidth: 1.25,
strokeOpacity: 1.0,
fillColor: { r: 1.0, g: 0.839, b: 0.439 },
fill: true,
fillOpacity: 0.8,
sprites: false,
sprites_image: null,
position: function (d) { return d; }
},
arg.style === undefined ? {} : arg.style
);
if (arg.position !== undefined) {
defaultStyle.position = arg.position;
}
m_this.style(defaultStyle);
m_this.dataTime().modified();
// bind to the zoom handler for point clustering
m_this.geoOn(geo.event.zoom, function (evt) {
m_this._handleZoom(evt.zoomLevel);
});
};
return m_this;
};
geo.event.pointFeature = $.extend({}, geo.event.feature);
/**
* Object specification for a point feature.
*
* @extends geo.feature.spec // need to make a jsdoc plugin for this to work
* @typedef geo.pointFeature.spec
* @type {object}
*/
/**
* Create a pointFeature from an object.
* @see {@link geo.feature.create}
* @param {geo.layer} layer The layer to add the feature to
* @param {geo.pointFeature.spec} spec The object specification
* @returns {geo.pointFeature|null}
*/
geo.pointFeature.create = function (layer, renderer, spec) {
'use strict';
spec.type = 'point';
return geo.feature.create(layer, spec);
};
inherit(geo.pointFeature, geo.feature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class lineFeature
*
* @class
* @extends geo.feature
* @returns {geo.lineFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.lineFeature = function (arg) {
'use strict';
if (!(this instanceof geo.lineFeature)) {
return new geo.lineFeature(arg);
}
arg = arg || {};
geo.feature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_init = this._init;
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set line accessor
*
* @returns {geo.pointFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.line = function (val) {
if (val === undefined) {
return m_this.style('line');
} else {
m_this.style('line', val);
m_this.dataTime().modified();
m_this.modified();
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set position accessor
*
* @returns {geo.pointFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.position = function (val) {
if (val === undefined) {
return m_this.style('position');
} else {
m_this.style('position', val);
m_this.dataTime().modified();
m_this.modified();
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Returns an array of datum indices that contain the given point.
* This is a slow implementation with runtime order of the number of
* vertices.
*/
////////////////////////////////////////////////////////////////////////////
this.pointSearch = function (p) {
var data, pt, line, width, indices = [], found = [], pos;
data = m_this.data();
if (!data || !data.length) {
return {
found: [],
index: []
};
}
line = m_this.line();
width = m_this.style.get('strokeWidth');
pos = m_this.position();
pt = m_this.featureGcsToDisplay(p);
// minimum l2 distance squared from
// q -> line(u, v)
function lineDist2(q, u, v) {
var t, l2 = dist2(u, v);
if (l2 < 1) {
// u, v are within 1 pixel
return dist2(q, u);
}
t = ((q.x - u.x) * (v.x - u.x) + (q.y - u.y) * (v.y - u.y)) / l2;
if (t < 0) { return dist2(q, u); }
if (t > 1) { return dist2(q, v); }
return dist2(
q,
{
x: u.x + t * (v.x - u.x),
y: u.y + t * (v.y - u.y)
}
);
}
// l2 distance squared from u to v
function dist2(u, v) {
var dx = u.x - v.x,
dy = u.y - v.y;
return dx * dx + dy * dy;
}
// for each line
data.forEach(function (d, index) {
var last = null;
try {
line(d, index).forEach(function (current, j) {
// get the screen coordinates of the current point
var p = pos(current, j, d, index);
var s = m_this.featureGcsToDisplay(p);
var r = Math.ceil(width(p, j, d, index) / 2) + 2;
r = r * r;
if (last) {
// test the line segment s -> last
if (lineDist2(pt, s, last) <= r) {
// short circuit the loop here
throw 'found';
}
}
last = s;
});
} catch (err) {
if (err !== 'found') {
throw err;
}
found.push(d);
indices.push(index);
}
});
return {
data: found,
index: indices
};
};
////////////////////////////////////////////////////////////////////////////
/**
* Returns an array of line indices that are contained in the given box.
*/
////////////////////////////////////////////////////////////////////////////
this.boxSearch = function (lowerLeft, upperRight, opts) {
var pos = m_this.position(),
idx = [],
line = m_this.line();
opts = opts || {};
opts.partial = opts.partial || false;
if (opts.partial) {
throw 'Unimplemented query method.';
}
m_this.data().forEach(function (d, i) {
var inside = true;
line(d, i).forEach(function (e, j) {
if (!inside) { return; }
var p = pos(e, j, d, i);
if (!(p.x >= lowerLeft.x &&
p.x <= upperRight.x &&
p.y >= lowerLeft.y &&
p.y <= upperRight.y)
) {
inside = false;
}
});
if (inside) {
idx.push(i);
}
});
return idx;
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
var defaultStyle = $.extend(
{},
{
'strokeWidth': 1.0,
// Default to gold color for lines
'strokeColor': { r: 1.0, g: 0.8431372549, b: 0.0 },
'strokeStyle': 'solid',
'strokeOpacity': 1.0,
'line': function (d) { return d; },
'position': function (d) { return d; }
},
arg.style === undefined ? {} : arg.style
);
if (arg.line !== undefined) {
defaultStyle.line = arg.line;
}
if (arg.position !== undefined) {
defaultStyle.position = arg.position;
}
m_this.style(defaultStyle);
m_this.dataTime().modified();
};
this._init(arg);
return this;
};
/**
* Create a lineFeature from an object.
* @see {@link geo.feature.create}
* @param {geo.layer} layer The layer to add the feature to
* @param {geo.lineFeature.spec} spec The object specification
* @returns {geo.lineFeature|null}
*/
geo.lineFeature.create = function (layer, spec) {
'use strict';
spec.type = 'line';
return geo.feature.create(layer, spec);
};
inherit(geo.lineFeature, geo.feature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class pathFeature
*
* @class
* @extends geo.feature
* @returns {geo.pathFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.pathFeature = function (arg) {
'use strict';
if (!(this instanceof geo.pathFeature)) {
return new geo.pathFeature(arg);
}
arg = arg || {};
geo.feature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
m_position = arg.position === undefined ? [] : arg.position,
s_init = this._init;
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set positions
*
* @returns {geo.pathFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.position = function (val) {
if (val === undefined) {
return m_position;
}
// Copy incoming array of positions
m_position = val;
m_this.dataTime().modified();
m_this.modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
var defaultStyle = $.extend(
{},
{
'strokeWidth': function () { return 1; },
'strokeColor': function () { return { r: 1.0, g: 1.0, b: 1.0 }; }
},
arg.style === undefined ? {} : arg.style
);
m_this.style(defaultStyle);
if (m_position) {
m_this.dataTime().modified();
}
};
this._init(arg);
return this;
};
inherit(geo.pathFeature, geo.feature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class polygonFeature
*
* @class
* @extends geo.feature
* @returns {geo.polygonFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.polygonFeature = function (arg) {
'use strict';
if (!(this instanceof geo.polygonFeature)) {
return new geo.polygonFeature(arg);
}
arg = arg || {};
geo.feature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
m_position,
m_polygon,
s_init = this._init,
s_data = this.data,
m_coordinates = {outer: [], inner: []};
if (arg.polygon === undefined) {
m_polygon = function (d) {
return d;
};
} else {
m_polygon = arg.polygon;
}
if (arg.position === undefined) {
m_position = function (d) {
return d;
};
} else {
m_position = arg.position;
}
////////////////////////////////////////////////////////////////////////////
/**
* Override the parent data method to keep track of changes to the
* internal coordinates.
*/
////////////////////////////////////////////////////////////////////////////
this.data = function (arg) {
var ret = s_data(arg);
if (arg !== undefined) {
getCoordinates();
}
return ret;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the internal coordinates whenever the data changes. For now, we do
* the computation in world coordinates, but we will need to work in GCS
* for other projections.
* @private
*/
////////////////////////////////////////////////////////////////////////////
function getCoordinates() {
var posFunc = m_this.position(),
polyFunc = m_this.polygon();
m_coordinates = m_this.data().map(function (d, i) {
var poly = polyFunc(d);
var outer, inner;
outer = (poly.outer || []).map(function (d0, j) {
return posFunc.call(m_this, d0, j, d, i);
});
inner = (poly.inner || []).map(function (hole) {
return (hole || []).map(function (d0, k) {
return posFunc.call(m_this, d0, k, d, i);
});
});
return {
outer: outer,
inner: inner
};
});
}
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set polygon accessor
*
* @returns {geo.pointFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.polygon = function (val) {
if (val === undefined) {
return m_polygon;
} else {
m_polygon = val;
m_this.dataTime().modified();
m_this.modified();
getCoordinates();
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set position accessor
*
* @returns {geo.pointFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.position = function (val) {
if (val === undefined) {
return m_position;
} else {
m_position = val;
m_this.dataTime().modified();
m_this.modified();
getCoordinates();
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Point searce method for selection api. Returns markers containing the
* given point.
* @argument {Object} coordinate
* @returns {Object}
*/
////////////////////////////////////////////////////////////////////////////
this.pointSearch = function (coordinate) {
var found = [], indices = [], data = m_this.data();
m_coordinates.forEach(function (coord, i) {
var inside = geo.util.pointInPolygon(
coordinate,
coord.outer,
coord.inner
);
if (inside) {
indices.push(i);
found.push(data[i]);
}
});
return {
index: indices,
found: found
};
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
var defaultStyle = $.extend(
{},
{
'fillColor': { r: 0.0, g: 0.5, b: 0.5 },
'fillOpacity': 1.0
},
arg.style === undefined ? {} : arg.style
);
m_this.style(defaultStyle);
if (m_position) {
m_this.dataTime().modified();
}
};
this._init(arg);
return this;
};
inherit(geo.polygonFeature, geo.feature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class planeFeature
*
* @class
* @extends geo.polygonFeature
* @returns {geo.planeFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.planeFeature = function (arg) {
'use strict';
if (!(this instanceof geo.planeFeature)) {
return new geo.planeFeature(arg);
}
arg = arg || {};
// Defaults
arg.ul = arg.ul === undefined ? [0.0, 1.0, 0.0] : arg.ul;
arg.lr = arg.lr === undefined ? [1.0, 0.0, 0.0] : arg.lr;
arg.depth = arg.depth === undefined ? 0.0 : arg.depth;
geo.polygonFeature.call(this, arg);
var m_this = this,
m_origin = [arg.ul.x, arg.lr.y, arg.depth],
m_upperLeft = [arg.ul.x, arg.ul.y, arg.depth],
m_lowerRight = [arg.lr.x, arg.lr.y, arg.depth],
m_defaultDepth = arg.depth,
m_drawOnAsyncResourceLoad = arg.drawOnAsyncResourceLoad === undefined ?
true : false,
s_init = this._init;
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set origin
*
* @returns {geo.planeFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.origin = function (val) {
if (val === undefined) {
return m_origin;
} else if (val instanceof Array) {
if (val.length > 3 || val.length < 2) {
throw 'Origin point requires point in 2 or 3 dimension';
}
m_origin = val.slice(0);
if (m_origin.length === 2) {
m_origin[2] = m_defaultDepth;
}
}
m_this.dataTime().modified();
m_this.modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set pt1
*
* @returns {geo.planeFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.upperLeft = function (val) {
if (val === undefined) {
return m_upperLeft;
} else if (val instanceof Array) {
if (val.length > 3 || val.length < 2) {
throw 'Upper left point requires point in 2 or 3 dimension';
}
m_upperLeft = val.slice(0);
if (m_upperLeft.length === 2) {
m_upperLeft[2] = m_defaultDepth;
}
}
m_this.dataTime().modified();
m_this.modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set origin
*
* @returns {geo.planeFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.lowerRight = function (val) {
if (val === undefined) {
return m_lowerRight;
} else if (val instanceof Array) {
if (val.length > 3 || val.length < 2) {
throw 'Lower right point requires point in 2 or 3 dimension';
}
m_lowerRight = val.slice(0);
if (m_lowerRight.length === 2) {
m_lowerRight[2] = m_defaultDepth;
}
m_this.dataTime().modified();
}
m_this.dataTime().modified();
m_this.modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set if draw should happen as soon as a async resource is loaded
*/
////////////////////////////////////////////////////////////////////////////
this.drawOnAsyncResourceLoad = function (val) {
if (val === undefined) {
return m_drawOnAsyncResourceLoad;
} else {
m_drawOnAsyncResourceLoad = val;
return m_this;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
var style = null;
s_init.call(m_this, arg);
style = m_this.style();
if (style.image === undefined) {
style.image = null;
}
m_this.style(style);
};
this._init(arg);
return this;
};
inherit(geo.planeFeature, geo.polygonFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class quadFeature
*
* @class
* @param {Object} arg Options object
* @extends geo.feature
* @param {Object|string|Function} [color] Color for quads without images.
* Default is white ({r: 1, g: 1, b: 1}).
* @param {number|Function} [opacity=1] Opacity for quad
* @param {number|Function} [depth=0] Default z-coordinate for positions that
* don't explicitly specify one.
* @param {boolean|Function} [drawOnAsyncResourceLoaded=true] Redraw quads
* when images are loaded after initial render.
* @param {Image|string|Function} [image] Image for each data item. If
* undefined or null, the quad is a solid color. Default is (data).image.
* @param {Object|string|Function} [previewColor=null] If specified, a color to
* show on image quads while waiting for the image to load.
* @param {Image|string|Function} [previewImage=null] If specified, an image to
* show on image quads while waiting for the quad-specific image to load.
* This will only be shown if it is already loaded.
* @param {Object|Function} [position] Position of the quad. Default is
* (data). The position is an Object which specifies the corners of the
* quad: ll, lr, ur, ul. At least two opposite corners must be specified.
* The corners do not have to physically correspond to the order specified,
* but rather correspond to that part of an image (if there is one). If a
* corner is unspecified, it will use the x coordinate from one adjacent
* corner, the y coordinate from the other adjacent corner, and the average
* z value of those two corners. For instance, if ul is unspecified, it is
* {x: ll.x, y: ur.y}. Note that each quad is rendered as a pair of
* triangles: (ll, lr, ul) and (ur, ul, lr). Nothing special is done for
* quads that are not convex or quads that have substantially different
* transformations for those two triangles.
* @param {boolean} [cacheQuads=true] If true, a set of internal information is
* stored on each data item in the _cachedQuad attribute. If this is false,
* the data item is not altered. If the data (positions, opacity, etc,) of
* individual quads will change, set this to false or delete the _cachedQuad
* attribute of the data item.
* @returns {geo.quadFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.quadFeature = function (arg) {
'use strict';
if (!(this instanceof geo.quadFeature)) {
return new geo.quadFeature(arg);
}
arg = arg || {};
geo.feature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_init = this._init,
m_cacheQuads,
m_images = [],
m_quads;
/**
* Track a list of object->object mappings. The mappings are kept in a list.
* This marks all known mappings as unused. If they are not marked used
* before _objectListEnd is called, that function will remove them.
*
* @param {array} list the list of mappings.
*/
this._objectListStart = function (list) {
$.each(list, function (idx, item) {
item.used = false;
});
};
/**
* Get the value from a list of object->object mappings. If the key object
* is not present, return undefined. If found, the entry is marked as being
* in use.
*
* @param {array} list the list of mappings.
* @param {object} entry the key to search for.
* @returns {object} the associated object or undefined.
*/
this._objectListGet = function (list, entry) {
for (var i = 0; i < list.length; i += 1) {
if (list[i].entry === entry) {
list[i].used = true;
return list[i].value;
}
}
return undefined;
};
/**
* Add a new object to a list of object->object mappings. The key object
* should not exist, or this will create a duplicated. The new entry is
* marked as being in use.
*
* @param {array} list the list of mappings.
* @param {object} entry the key to add.
* @param {object} value the value to store with the entry.
*/
this._objectListAdd = function (list, entry, value) {
list.push({entry: entry, value: value, used: true});
};
/**
* Remove all unused entries from a list of object->object mappings.
*
* @param {array} list the list of mappings.
*/
this._objectListEnd = function (list) {
for (var i = list.length - 1; i >= 0; i -= 1) {
if (!list[i].used) {
list.splice(i, 1);
}
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Point search method for selection api. Returns markers containing the
* given point.
* @argument {Object} coordinate
* @returns {Object}
*/
////////////////////////////////////////////////////////////////////////////
this.pointSearch = function (coordinate) {
var found = [], indices = [], data = m_this.data(), i,
poly1 = [{}, {}, {}, {}], poly2 = [{}, {}, {}, {}],
map = m_this.layer().map(),
order1 = [0, 1, 2, 0], order2 = [1, 2, 3, 1];
coordinate = geo.transform.transformCoordinates(
map.ingcs(), map.gcs(), [coordinate])[0];
if (!m_quads) {
this._generateQuads();
}
$.each([m_quads.clrQuads, m_quads.imgQuads], function (idx, quadList) {
quadList.forEach(function (quad, idx) {
for (i = 0; i < order1.length; i += 1) {
poly1[i].x = quad.pos[order1[i] * 3];
poly1[i].y = quad.pos[order1[i] * 3 + 1];
poly1[i].z = quad.pos[order1[i] * 3 + 2];
poly2[i].x = quad.pos[order2[i] * 3];
poly2[i].y = quad.pos[order2[i] * 3 + 1];
poly2[i].z = quad.pos[order2[i] * 3 + 2];
}
if (geo.util.pointInPolygon(coordinate, poly1) ||
geo.util.pointInPolygon(coordinate, poly2)) {
indices.push(quad.idx);
found.push(data[quad.idx]);
}
});
});
return {
index: indices,
found: found
};
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set position
*
* @returns {geo.quadFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.position = function (val) {
if (val === undefined) {
return m_this.style('position');
} else {
m_this.style('position', geo.util.ensureFunction(val));
m_this.dataTime().modified();
m_this.modified();
}
return m_this;
};
/**
* Given a data item and its index, fetch its position and ensure we have
* compelte information for the quad. This generates missing corners and z
* values.
*
* @param {function} posFunc a function to call to get the position of a data
* item. It is passed (d, i).
* @param {function} depthFunc a function to call to get the z-value of a
* data item. It is passed (d, i).
* @param d a data item. Used to fetch position and possibly depth.
* @param i the index within the data. Used to fetch position and possibly
* depth.
* @returns {Object|undefined} either an object with all four corners, or
* undefined if no such object can be generated. The coordinates have been
* converted to map coordinates.
*/
this._positionToQuad = function (posFunc, depthFunc, d, i) {
var initPos = posFunc.call(m_this, d, i);
if ((!initPos.ll || !initPos.ur) && (!initPos.ul || !initPos.lr)) {
return;
}
var gcs = m_this.gcs(),
map_gcs = m_this.layer().map().gcs(),
pos = {};
$.each(['ll', 'lr', 'ul', 'ur'], function (idx, key) {
if (initPos[key] !== undefined) {
pos[key] = {};
if (initPos[key].x === undefined) {
pos[key] = [initPos[key][0], initPos[key][1], initPos[key][2]];
} else {
pos[key] = [initPos[key].x, initPos[key].y, initPos[key].z];
}
if (pos[key][2] === undefined) {
pos[key][2] = depthFunc.call(m_this, d, i);
}
if (gcs !== map_gcs) {
pos[key] = geo.transform.transformCoordinates(
gcs, map_gcs, pos[key]);
}
}
});
pos.ll = pos.ll || [pos.ul[0], pos.lr[1], (pos.ul[2] + pos.lr[2]) / 2];
pos.lr = pos.lr || [pos.ur[0], pos.ll[1], (pos.ur[2] + pos.ll[2]) / 2];
pos.ur = pos.ur || [pos.lr[0], pos.ul[1], (pos.lr[2] + pos.ul[2]) / 2];
pos.ul = pos.ul || [pos.ll[0], pos.ur[1], (pos.ll[2] + pos.ur[2]) / 2];
return pos;
};
/**
* Convert the current data set to a pair of arrays, one of quads that are
* solid color and one of qudas that have an image. All quads are objects
* with pos (a 12 value array containing 4 three-dimensional position
* coordinates), and opacity. Color quads also have a color. Image quads
* may have an image element, if the image is loaded. If it isn't, this
* element will be missing. For preview images, the image quad will have a
* reference to the preview element that may later be removed. If a preview
* color is used, the quad will be in both lists, but may be removed from the
* color quad list once the image is loaded.
*
* The value for origin is one of an ll corner from one of the quads with the
* smallest sum of diagonals. The assumption is that, if using the origin to
* improve precision, the smallest quads are the ones most in need of this
* benefit.
*
* @returns {Object} An object with clrQuads and imgQuads, each of which is
* an array, and origin, which is a triplet that is guaranteed to be one of
* the quads corners for a quad with the smallest sum of diagonal lengths.
*/
this._generateQuads = function () {
var posFunc = m_this.position(),
imgFunc = geo.util.ensureFunction(m_this.style('image')),
colorFunc = geo.util.ensureFunction(m_this.style('color')),
depthFunc = geo.util.ensureFunction(m_this.style('depth')),
opacityFunc = geo.util.ensureFunction(m_this.style('opacity')),
loadedFunc = geo.util.ensureFunction(m_this.style(
'drawOnAsyncResourceLoaded')),
previewColorFunc = geo.util.ensureFunction(m_this.style(
'previewColor')),
previewImageFunc = geo.util.ensureFunction(m_this.style(
'previewImage')),
data = m_this.data(),
clrQuads = [], imgQuads = [],
origin = [0, 0, 0], origindiag2, diag2;
/* Keep track of images that we are using. This prevents creating
* additional Image elemnts for repeated urls. */
m_this._objectListStart(m_images);
$.each(data, function (i, d) {
if (d._cachedQuad) {
diag2 = d._cachedQuad.diag2;
if (origindiag2 === undefined || (d._cachedQuad.diag2 &&
d._cachedQuad.diag2 < origindiag2)) {
origin = d._cachedQuad.ll;
origindiag2 = d._cachedQuad.diag2;
}
if (d._cachedQuad.clrquad) {
clrQuads.push(d._cachedQuad.clrquad);
}
if (d._cachedQuad.imgquad) {
imgQuads.push(d._cachedQuad.imgquad);
}
return;
}
var quad, reload, image, prev_onload,
pos, img, opacity, previewColor, previewImage, quadinfo = {};
pos = m_this._positionToQuad(posFunc, depthFunc, d, i);
opacity = opacityFunc.call(m_this, d, i);
if (pos === undefined || !opacity) {
return;
}
diag2 = Math.pow(pos.ll[0] - pos.ur[0], 2) + Math.pow(pos.ll[1] -
pos.ur[1], 2) + Math.pow(pos.ll[2] - pos.ur[0], 2) + Math.pow(
pos.lr[0] - pos.ur[0], 2) + Math.pow(pos.lr[1] - pos.ur[1], 2) +
Math.pow(pos.lr[2] - pos.ur[0], 2);
quadinfo.diag2 = diag2;
quadinfo.ll = pos.ll;
if (origindiag2 === undefined || (diag2 && diag2 < origindiag2)) {
origin = pos.ll;
origindiag2 = diag2;
}
pos = [pos.ll[0], pos.ll[1], pos.ll[2],
pos.lr[0], pos.lr[1], pos.lr[2],
pos.ul[0], pos.ul[1], pos.ul[2],
pos.ur[0], pos.ur[1], pos.ur[2]];
img = imgFunc.call(m_this, d, i);
if (!img) {
quad = {
idx: i,
pos: pos,
opacity: opacity,
color: geo.util.convertColor(colorFunc.call(m_this, d, i))
};
clrQuads.push(quad);
quadinfo.clrquad = quad;
} else {
image = m_this._objectListGet(m_images, img);
if (image === undefined) {
if (img instanceof Image) {
image = img;
} else {
image = new Image();
image.src = img;
}
m_this._objectListAdd(m_images, img, image);
}
quad = {
idx: i,
pos: pos,
opacity: opacity
};
if (image.complete && image.naturalWidth && image.naturalHeight) {
quad.image = image;
} else {
previewColor = undefined;
previewImage = previewImageFunc.call(m_this, d, i);
if (previewImage && previewImage instanceof Image &&
previewImage.complete && previewImage.naturalWidth &&
previewImage.naturalHeight) {
quad.image = previewImage;
} else {
previewColor = previewColorFunc.call(m_this, d, i);
if (previewColor === null) {
previewColor = undefined;
}
if (previewColor !== undefined) {
quad.color = geo.util.convertColor(previewColor);
clrQuads.push(quad);
quadinfo.keep = false;
}
}
reload = loadedFunc.call(m_this, d, i);
if (reload) {
prev_onload = image.onload;
image.onload = function () {
if (previewColor !== undefined) {
if ($.inArray(quad, clrQuads) >= 0) {
clrQuads.splice($.inArray(quad, clrQuads), 1);
}
}
quad.image = image;
m_this.dataTime().modified();
m_this.modified();
m_this._update();
m_this.layer().draw();
if (prev_onload) {
return prev_onload.apply(this, arguments);
}
};
} else if (previewColor === undefined && !quad.image) {
return;
}
}
imgQuads.push(quad);
quadinfo.imgquad = quad;
}
if (m_cacheQuads !== false && quadinfo.keep !== false) {
d._cachedQuad = quadinfo;
}
});
m_this._objectListEnd(m_images);
m_quads = {clrQuads: clrQuads, imgQuads: imgQuads, origin: origin};
return m_quads;
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
arg = arg || {};
s_init.call(m_this, arg);
m_cacheQuads = (arg.cacheQuads !== false);
var style = $.extend(
{},
{
color: { r: 1.0, g: 1, b: 1 },
opacity: 1,
depth: 0,
drawOnAsyncResourceLoaded: true,
previewColor: null,
previewImage: null,
image: function (d) { return d.image; },
position: function (d) { return d; }
},
arg.style === undefined ? {} : arg.style
);
if (arg.position !== undefined) {
style.position = geo.util.ensureFunction(arg.position);
}
m_this.style(style);
m_this.dataTime().modified();
};
return m_this;
};
geo.event.quadFeature = $.extend({}, geo.event.feature);
/**
* Object specification for a quad feature.
*
* @extends geo.feature.spec // need to make a jsdoc plugin for this to work
* @typedef geo.quadFeature.spec
* @type {object}
*/
/**
* Create a quadFeature from an object.
* @see {@link geo.feature.create}
* @param {geo.layer} layer The layer to add the feature to
* @param {geo.quadFeature.spec} spec The object specification
* @returns {geo.quadFeature|null}
*/
geo.quadFeature.create = function (layer, spec) {
'use strict';
spec = spec || {};
spec.type = 'quad';
return geo.feature.create(layer, spec);
};
inherit(geo.quadFeature, geo.feature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class vectorFeature
*
* @class
* @extends geo.feature
* @returns {geo.vectorFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.vectorFeature = function (arg) {
'use strict';
if (!(this instanceof geo.vectorFeature)) {
return new geo.vectorFeature(arg);
}
arg = arg || {};
geo.feature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_init = this._init,
s_style = this.style;
////////////////////////////////////////////////////////////////////////////
/**
* Get or set the accessor for the origin of the vector. This is the point
* that the vector base resides at. Defaults to (0, 0, 0).
* @param {geo.accessor|geo.geoPosition} [accessor] The origin accessor
*/
////////////////////////////////////////////////////////////////////////////
this.origin = function (val) {
if (val === undefined) {
return s_style('origin');
} else {
s_style('origin', val);
m_this.dataTime().modified();
m_this.modified();
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get or set the accessor for the displacement (coordinates) of the vector.
* @param {geo.accessor|geo.geoPosition} [accessor] The accessor
*/
////////////////////////////////////////////////////////////////////////////
this.delta = function (val) {
if (val === undefined) {
return s_style('delta');
} else {
s_style('delta', val);
m_this.dataTime().modified();
m_this.modified();
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
* @protected
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
var defaultStyle = $.extend(
{},
{
strokeColor: 'black',
strokeWidth: 2.0,
strokeOpacity: 1.0,
// TODO: define styles for the end markers
// originStyle: 'none',
// endStyle: 'arrow',
origin: {x: 0, y: 0, z: 0},
delta: function (d) { return d; },
scale: null // size scaling factor (null -> renderer decides)
},
arg.style === undefined ? {} : arg.style
);
if (arg.origin !== undefined) {
defaultStyle.origin = arg.origin;
}
m_this.style(defaultStyle);
m_this.dataTime().modified();
};
};
inherit(geo.vectorFeature, geo.feature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class geomFeature
*
* @class
* @extends geo.feature
* @returns {geo.geomFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.geomFeature = function (arg) {
'use strict';
if (!(this instanceof geo.geomFeature)) {
return new geo.geomFeature(arg);
}
arg = arg || {};
geo.feature.call(this, arg);
arg.style = arg.style === undefined ? $.extend({}, {
'color': [1.0, 1.0, 1.0],
'point_sprites': false,
'point_sprites_image': null
}, arg.style) : arg.style;
// Update style
this.style(arg.style);
return this;
};
inherit(geo.geomFeature, geo.feature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class graphFeature
*
* @class
* @extends geo.feature
* @returns {geo.graphFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.graphFeature = function (arg) {
'use strict';
if (!(this instanceof geo.graphFeature)) {
return new geo.graphFeature(arg);
}
arg = arg || {};
geo.feature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_draw = this.draw,
s_style = this.style,
m_nodes = null,
m_points = null,
m_children = function (d) { return d.children; },
m_links = [],
s_init = this._init,
s_exit = this._exit;
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
var defaultStyle = $.extend(true, {},
{
nodes: {
radius: 5.0,
fill: true,
fillColor: { r: 1.0, g: 0.0, b: 0.0 },
strokeColor: { r: 0, g: 0, b: 0 }
},
links: {
strokeColor: { r: 0.0, g: 0.0, b: 0.0 }
},
linkType: 'path' /* 'path' || 'line' */
},
arg.style === undefined ? {} : arg.style
);
m_this.style(defaultStyle);
m_this.nodes(function (d) { return d; });
};
////////////////////////////////////////////////////////////////////////////
/**
* Call child _build methods
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
m_this.children().forEach(function (child) {
child._build();
});
};
////////////////////////////////////////////////////////////////////////////
/**
* Call child _update methods
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
m_this.children().forEach(function (child) {
child._update();
});
};
////////////////////////////////////////////////////////////////////////////
/**
* Custom _exit method to remove all sub-features
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.data([]);
m_links.forEach(function (link) {
link._exit();
m_this.removeChild(link);
});
m_links = [];
m_points._exit();
m_this.removeChild(m_points);
s_exit();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set style
*/
////////////////////////////////////////////////////////////////////////////
this.style = function (arg, arg2) {
var out = s_style.call(m_this, arg, arg2);
if (out !== m_this) {
return out;
}
// set styles for sub-features
m_points.style(arg.nodes);
m_links.forEach(function (l) {
l.style(arg.links);
});
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set links accessor.
*/
////////////////////////////////////////////////////////////////////////////
this.links = function (arg) {
if (arg === undefined) {
return m_children;
}
m_children = geo.util.ensureFunction(arg);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set nodes
*/
////////////////////////////////////////////////////////////////////////////
this.nodes = function (val) {
if (val === undefined) {
return m_nodes;
}
m_nodes = val;
m_this.modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get internal node feature
*/
////////////////////////////////////////////////////////////////////////////
this.nodeFeature = function () {
return m_points;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get internal link features
*/
////////////////////////////////////////////////////////////////////////////
this.linkFeatures = function () {
return m_links;
};
////////////////////////////////////////////////////////////////////////////
/**
* Build the feature for drawing
*/
////////////////////////////////////////////////////////////////////////////
this.draw = function () {
var layer = m_this.layer(),
data = m_this.data(),
nLinks = 0,
style;
// get the feature style object
style = m_this.style();
// Bind data to the point nodes
m_points.data(data);
m_points.style(style.nodes);
// get links from node connections
data.forEach(function (source) {
(source.children || []).forEach(function (target) {
var link;
nLinks += 1;
if (m_links.length < nLinks) {
link = geo.createFeature(
style.linkType, layer, layer.renderer()
).style(style.links);
m_this.addChild(link);
m_links.push(link);
}
m_links[nLinks - 1].data([source, target]);
});
});
m_links.splice(nLinks, m_links.length - nLinks).forEach(function (l) {
l._exit();
m_this.removeChild(l);
});
s_draw();
return m_this;
};
m_points = geo.createFeature(
'point',
this.layer(),
this.layer().renderer()
);
m_this.addChild(m_points);
if (arg.nodes) {
this.nodes(arg.nodes);
}
this._init(arg);
return this;
};
inherit(geo.graphFeature, geo.feature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class contourFeature
*
* @class
* @extends geo.feature
* @returns {geo.contourFeature}
*
*/
//////////////////////////////////////////////////////////////////////////////
geo.contourFeature = function (arg) {
'use strict';
if (!(this instanceof geo.contourFeature)) {
return new geo.contourFeature(arg);
}
arg = arg || {};
geo.feature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
m_contour = {},
s_init = this._init,
s_data = this.data;
if (arg.contour === undefined) {
m_contour = function (d) {
return d;
};
} else {
m_contour = arg.contour;
}
////////////////////////////////////////////////////////////////////////////
/**
* Override the parent data method to keep track of changes to the
* internal coordinates.
*/
////////////////////////////////////////////////////////////////////////////
this.data = function (arg) {
var ret = s_data(arg);
return ret;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set contour accessor
*
* @returns {geo.pointFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.contour = function (arg1, arg2) {
if (arg1 === undefined) {
return m_contour;
}
if (typeof arg1 === 'string' && arg2 === undefined) {
return m_contour[arg1];
}
if (arg2 === undefined) {
var contour = $.extend(
{},
{
gridWidth: function () {
if (arg1.gridHeight) {
return Math.floor(m_this.data().length / arg1.gridHeight);
}
return Math.floor(Math.sqrt(m_this.data().length));
},
gridHeight: function () {
if (arg1.gridWidth) {
return Math.floor(m_this.data().length / arg1.gridWidth);
}
return Math.floor(Math.sqrt(m_this.data().length));
},
minColor: 'black',
minOpacity: 0,
maxColor: 'black',
maxOpacity: 0,
/* 9-step based on paraview bwr colortable */
colorRange: [
{r: 0.07514311, g: 0.468049805, b: 1},
{r: 0.468487184, g: 0.588057293, b: 1},
{r: 0.656658579, g: 0.707001303, b: 1},
{r: 0.821573924, g: 0.837809045, b: 1},
{r: 0.943467973, g: 0.943498599, b: 0.943398095},
{r: 1, g: 0.788626485, b: 0.750707739},
{r: 1, g: 0.6289553, b: 0.568237474},
{r: 1, g: 0.472800903, b: 0.404551679},
{r: 0.916482116, g: 0.236630659, b: 0.209939162}
]
},
m_contour,
arg1
);
m_contour = contour;
} else {
m_contour[arg1] = arg2;
}
m_this.modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* A uniform getter that always returns a function even for constant values.
* If undefined input, return all the contour values as an object.
*
* @param {string|undefined} key
* @return {function}
*/
////////////////////////////////////////////////////////////////////////////
this.contour.get = function (key) {
if (key === undefined) {
var all = {}, k;
for (k in m_contour) {
if (m_contour.hasOwnProperty(k)) {
all[k] = m_this.contour.get(k);
}
}
return all;
}
return geo.util.ensureFunction(m_contour[key]);
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set position accessor
*
* @returns {geo.pointFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.position = function (val) {
if (val === undefined) {
return m_this.style('position');
} else {
m_this.style('position', val);
m_this.dataTime().modified();
m_this.modified();
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Create a set of vertices, values at the vertices, and opacities at the
* vertices. Create a set of triangles of indices into the vertex array.
* Create a color and opacity map corresponding to the values.
*
* @returns: an object with pos, value, opacity, elements, minValue,
* maxValue, minColor, maxColor, colorMap, factor. If there is no
* contour data that can be used, only elements is guaranteed to
* exist, and it will be a zero-length array.
*/
////////////////////////////////////////////////////////////////////////////
this.createContours = function () {
var i, i3, j, idx, k, val, numPts, usedPts = 0, usePos, item,
idxMap = {},
minval, maxval, range,
contour = m_this.contour,
data = m_this.data(),
posFunc = m_this.position(), posVal,
gridW = contour.get('gridWidth')(),
gridH = contour.get('gridHeight')(),
x0 = contour.get('x0')(),
y0 = contour.get('y0')(),
dx = contour.get('dx')(),
dy = contour.get('dy')(),
opacityFunc = m_this.style.get('opacity'),
opacityRange = contour.get('opacityRange')(),
rangeValues = contour.get('rangeValues')(),
valueFunc = m_this.style.get('value'), values = [],
stepped = contour.get('stepped')(),
wrapLong = contour.get('wrapLongitude')(),
calcX, skipColumn, x, origI, /* used for wrapping */
gridWorig = gridW, /* can be different when wrapping */
result = {
minValue: contour.get('min')(),
maxValue: contour.get('max')(),
stepped: stepped === undefined || stepped ? true : false,
wrapLongitude: wrapLong === undefined || wrapLong ? true : false,
colorMap: [],
elements: []
};
/* Create the min/max colors and the color array */
result.minColor = $.extend({a: contour.get('minOpacity')() || 0},
geo.util.convertColor(contour.get('minColor')()));
result.maxColor = $.extend({a: contour.get('maxOpacity')() || 0},
geo.util.convertColor(contour.get('maxColor')()));
contour.get('colorRange')().forEach(function (clr, idx) {
result.colorMap.push($.extend(
{a: opacityRange && opacityRange[idx] !== undefined ?
opacityRange[idx] : 1}, geo.util.convertColor(clr)));
});
/* Determine which values are usable */
if (gridW * gridH > data.length) {
gridH = Math.floor(data.length) / gridW;
}
/* If we are not using the position values (we are using x0, y0, dx, dy),
* and wrapLongitude is turned on, and the position spans 180 degrees,
* duplicate one or two columns of points at opposite ends of the map. */
usePos = (x0 === null || x0 === undefined || y0 === null ||
y0 === undefined || !dx || !dy);
if (!usePos && result.wrapLongitude && (x0 < -180 || x0 > 180 ||
x0 + dx * (gridW - 1) < -180 || x0 + dx * (gridW - 1) > 180) &&
dx > -180 && dx < 180) {
calcX = [];
for (i = 0; i < gridW; i += 1) {
x = x0 + i * dx;
while (x < -180) { x += 360; }
while (x > 180) { x -= 360; }
if (i && Math.abs(x - calcX[calcX.length - 1]) > 180) {
if (x > calcX[calcX.length - 1]) {
calcX.push(x - 360);
calcX.push(calcX[calcX.length - 2] + 360);
} else {
calcX.push(x + 360);
calcX.push(calcX[calcX.length - 2] - 360);
}
skipColumn = i;
}
calcX.push(x);
}
gridW += 2;
if (Math.abs(Math.abs(gridWorig * dx) - 360) < 0.01) {
gridW += 1;
x = x0 + gridWorig * dx;
while (x < -180) { x += 360; }
while (x > 180) { x -= 360; }
calcX.push(x);
}
}
/* Calculate the value for point */
numPts = gridW * gridH;
for (i = 0; i < numPts; i += 1) {
if (skipColumn === undefined) {
val = parseFloat(valueFunc(data[i]));
} else {
j = Math.floor(i / gridW);
origI = i - j * gridW;
origI += (origI > skipColumn ? -2 : 0);
if (origI >= gridWorig) {
origI -= gridWorig;
}
origI += j * gridWorig;
val = parseFloat(valueFunc(data[origI]));
}
values[i] = isNaN(val) ? null : val;
if (values[i] !== null) {
idxMap[i] = usedPts;
usedPts += 1;
if (minval === undefined) {
minval = maxval = values[i];
}
if (values[i] < minval) {
minval = values[i];
}
if (values[i] > maxval) {
maxval = values[i];
}
}
}
if (!usedPts) {
return result;
}
if (!$.isNumeric(result.minValue)) {
result.minValue = minval;
}
if (!$.isNumeric(result.maxValue)) {
result.maxValue = maxval;
}
if (!rangeValues || rangeValues.length !== result.colorMap.length + 1) {
rangeValues = null;
}
if (rangeValues) { /* ensure increasing monotonicity */
for (k = 1; k < rangeValues.length; k += 1) {
if (rangeValues[k] > rangeValues[k + 1]) {
rangeValues = null;
break;
}
}
}
if (rangeValues) {
result.minValue = rangeValues[0];
result.maxValue = rangeValues[rangeValues.length - 1];
}
range = result.maxValue - result.minValue;
if (!range) {
result.colorMap = result.colorMap.slice(0, 1);
range = 1;
rangeValues = null;
}
result.rangeValues = rangeValues;
result.factor = result.colorMap.length / range;
/* Create triangles */
for (j = idx = 0; j < gridH - 1; j += 1, idx += 1) {
for (i = 0; i < gridW - 1; i += 1, idx += 1) {
if (values[idx] !== null && values[idx + 1] !== null &&
values[idx + gridW] !== null &&
values[idx + gridW + 1] !== null && i !== skipColumn) {
result.elements.push(idxMap[idx]);
result.elements.push(idxMap[idx + 1]);
result.elements.push(idxMap[idx + gridW]);
result.elements.push(idxMap[idx + gridW + 1]);
result.elements.push(idxMap[idx + gridW]);
result.elements.push(idxMap[idx + 1]);
}
}
}
/* Only locate the points that are in use. */
result.pos = new Array(usedPts * 3);
result.value = new Array(usedPts);
result.opacity = new Array(usedPts);
for (j = i = i3 = 0; j < numPts; j += 1) {
val = values[j];
if (val !== null) {
item = data[j];
if (usePos) {
posVal = posFunc(item);
result.pos[i3] = posVal.x;
result.pos[i3 + 1] = posVal.y;
result.pos[i3 + 2] = posVal.z || 0;
} else {
if (skipColumn === undefined) {
result.pos[i3] = x0 + dx * (j % gridW);
} else {
result.pos[i3] = calcX[j % gridW];
}
result.pos[i3 + 1] = y0 + dy * Math.floor(j / gridW);
result.pos[i3 + 2] = 0;
}
result.opacity[i] = opacityFunc(item);
if (rangeValues && val >= result.minValue && val <= result.maxValue) {
for (k = 1; k < rangeValues.length; k += 1) {
if (val <= rangeValues[k]) {
result.value[i] = k - 1 + (val - rangeValues[k - 1]) /
(rangeValues[k] - rangeValues[k - 1]);
break;
}
}
} else {
result.value[i] = (val - result.minValue) * result.factor;
}
i += 1;
i3 += 3;
}
}
return result;
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
var defaultStyle = $.extend(
{},
{
opacity: 1.0,
position: function (d) {
return {x: d.x, y: d.y, z: d.z};
},
value: function (d) {
return m_this.position()(d).z;
}
},
arg.style === undefined ? {} : arg.style
);
m_this.style(defaultStyle);
if (m_contour) {
m_this.dataTime().modified();
}
};
this._init(arg);
return this;
};
inherit(geo.contourFeature, geo.feature);
/* Example:
layer.createFeature('contour', {
})
.data(<array with w x h elements>)
.position(function (d) {
return { x: <longitude>, y: <latitude>, z: <altitude>};
})
.style({
opacity: function (d) {
return <opacity of grid point>;
},
value: function (d) { // defaults to position().z
return <contour value>;
}
})
.contour({
gridWidth: <width of grid>,
gridHeight: <height of grid>,
x0: <the x coordinate of the 0th point in the value array>,
y0: <the y coordinate of the 0th point in the value array>,
dx: <the distance in the x direction between the 0th and 1st point in the
value array>,
dy: <the distance in the y direction between the 0th and (gridWidth)th point
in the value array>,
wrapLongitude: <boolean (default true). If true, AND the position array is
not used, assume the x coordinates is longitude and should be adjusted to
be within -180 to 180. If the data spans 180 degrees, the points or
squares will be duplicated to ensure that the map is covered from -180 to
180 as appropriate. Set this to false if using a non longitude x
coordinate. This is ignored if the position array is used.>,
min: <optional minimum contour value, otherwise taken from style.value>,
max: <optional maximum contour value, otherwise taken from style.value>,
minColor: <color for any value below the minimum>,
minOpacity: <opacity for any value below the minimum>,
maxColor: <color for any value above the maximum>,
maxOpacity: <opacity for any value above the maximum>,
stepped: <boolean (default true). If false, smooth transitions between
colors>,
colorRange: [<array of colors used for the contour>],
opacityRange: [<optional array of opacities used for the contour, expected to
be the same length as colorRange>],
rangeValues: [<if specified, instead of spacing the colors linearly, use this
spacing. Must be increasing monotonic and one value longer than the length
of colorRange>]
})
Notes:
* The position array is only used for position if not all of x0, y0, dx, and dy
are specified (not null or undefined). If a value array is not specified,
the position array could still be used for the value.
* If the value() of a grid point is null or undefined, that point will not be
included in the contour display. Since the values are on a grid, if this
point is in the interior of the grid, this can remove up to four squares.
* Only one of gridWidth and gridHeight needs to be specified. If both are
specified and gridWidth * gridHeight < data().length, not all the data will
be used. If neither are specified, floor(sqrt(data().length)) is used for
both.
*/
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class renderer
*
* @class
* @extends geo.object
* @returns {geo.renderer}
*/
//////////////////////////////////////////////////////////////////////////////
geo.renderer = function (arg) {
'use strict';
if (!(this instanceof geo.renderer)) {
return new geo.renderer(arg);
}
geo.object.call(this);
arg = arg || {};
var m_this = this,
m_layer = arg.layer === undefined ? null : arg.layer,
m_canvas = arg.canvas === undefined ? null : arg.canvas,
m_initialized = false;
////////////////////////////////////////////////////////////////////////////
/**
* Get layer of the renderer
*
* @returns {*}
*/
////////////////////////////////////////////////////////////////////////////
this.layer = function () {
return m_layer;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get canvas for the renderer
*/
////////////////////////////////////////////////////////////////////////////
this.canvas = function (val) {
if (val === undefined) {
return m_canvas;
} else {
m_canvas = val;
m_this.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get map that this renderer belongs to
*/
////////////////////////////////////////////////////////////////////////////
this.map = function () {
if (m_layer) {
return m_layer.map();
} else {
return null;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get base layer that belongs to this renderer
*/
////////////////////////////////////////////////////////////////////////////
this.baseLayer = function () {
if (m_this.map()) {
return m_this.map().baseLayer();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set if renderer has been initialized
*/
////////////////////////////////////////////////////////////////////////////
this.initialized = function (val) {
if (val === undefined) {
return m_initialized;
} else {
m_initialized = val;
return m_this;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get render API used by the renderer
*/
////////////////////////////////////////////////////////////////////////////
this.api = function () {
throw 'Should be implemented by derivied classes';
};
////////////////////////////////////////////////////////////////////////////
/**
* Reset to default
*/
////////////////////////////////////////////////////////////////////////////
this.reset = function () {
return true;
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function () {
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle resize event
*/
////////////////////////////////////////////////////////////////////////////
this._resize = function () {
};
////////////////////////////////////////////////////////////////////////////
/**
* Render
*/
////////////////////////////////////////////////////////////////////////////
this._render = function () {
};
return this;
};
inherit(geo.renderer, geo.object);
(function () {
'use strict';
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of osmLayer
*
* @class
* @extends geo.featureLayer
*
* @param {Object} arg - arg can contain following keys: baseUrl,
* imageFormat (such as png or jpeg), and displayLast
* (to decide whether or not render tiles from last zoom level).
*/
//////////////////////////////////////////////////////////////////////////////
geo.osmLayer = function (arg) {
if (!(this instanceof geo.osmLayer)) {
return new geo.osmLayer(arg);
}
if (arg.mapOpacity !== undefined && arg.opacity === undefined) {
arg.opacity = arg.mapOpacity;
}
geo.tileLayer.call(this, arg);
/* mapOpacity is just another name for the layer opacity. */
this.mapOpacity = this.opacity;
/**
* Returns an instantiated imageTile object with the given indices. This
* method always returns a new tile object. Use `_getTileCached`
* to use the caching layer.
* @param {Object} index The tile index
* @param {Number} index.x
* @param {Number} index.y
* @param {Number} index.level
* @param {Object} source The tile index used for constructing the url
* @param {Number} source.x
* @param {Number} source.y
* @param {Number} source.level
* @returns {geo.tile}
*/
this._getTile = function (index, source) {
var urlParams = source || index;
return geo.imageTile({
index: index,
size: {x: this._options.tileWidth, y: this._options.tileHeight},
queue: this._queue,
url: this._options.url(urlParams.x, urlParams.y, urlParams.level || 0,
this._options.subdomains)
});
}.bind(this);
};
/**
* This object contains the default options used to initialize the osmLayer.
*/
geo.osmLayer.defaults = $.extend({}, geo.tileLayer.defaults, {
minLevel: 0,
maxLevel: 18,
tileOverlap: 0,
tileWidth: 256,
tileHeight: 256,
tileOffset : function (level) {
var s = Math.pow(2, level - 1) * 256;
return {x: s, y: s};
},
wrapX: true,
wrapY: false,
url: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
attribution: 'Tile data © <a href="http://osm.org/copyright">' +
'OpenStreetMap</a> contributors'
});
inherit(geo.osmLayer, geo.tileLayer);
geo.registerLayer('osm', geo.osmLayer);
})();
geo.domRenderer = function (arg) {
'use strict';
if (!(this instanceof geo.domRenderer)) {
return new geo.domRenderer(arg);
}
geo.renderer.call(this, arg);
arg = arg || {};
var m_this = this;
this.api = function () {
return 'dom';
};
this._init = function () {
var layer = m_this.layer().node();
if (!m_this.canvas() && layer && layer.length) {
// The renderer and the UI Layer share the same canvas
// at least for now. This renderer is essentially a noop renderer
// designed for backwards compatibility
m_this.canvas(layer[0]);
}
};
this._init(arg);
return this;
};
inherit(geo.domRenderer, geo.renderer);
geo.registerRenderer('dom', geo.domRenderer);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class choroplethFeature
*
* @class
* @extends geo.feature
* @returns {geo.choroplethFeature}
*
*/
//////////////////////////////////////////////////////////////////////////////
geo.choroplethFeature = function (arg) {
'use strict';
if (!(this instanceof geo.choroplethFeature)) {
return new geo.choroplethFeature(arg);
}
arg = arg || {};
geo.feature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_init = this._init,
m_choropleth = $
.extend({},
{
/* 9-step based on paraview bwr colortable */
colorRange: [
{r: 0.07514311, g: 0.468049805, b: 1},
{r: 0.468487184, g: 0.588057293, b: 1},
{r: 0.656658579, g: 0.707001303, b: 1},
{r: 0.821573924, g: 0.837809045, b: 1},
{r: 0.943467973, g: 0.943498599, b: 0.943398095},
{r: 1, g: 0.788626485, b: 0.750707739},
{r: 1, g: 0.6289553, b: 0.568237474},
{r: 1, g: 0.472800903, b: 0.404551679},
{r: 0.916482116, g: 0.236630659, b: 0.209939162}
],
scale: d3.scale.quantize(),
accessors: {
//accessor for ID on geodata feature
geoId: function (geoFeature) {
return geoFeature.properties.GEO_ID;
},
//accessor for ID on scalar element
scalarId: function (scalarElement) {
return scalarElement.id;
},
//accessor for value on scalar element
scalarValue: function (scalarElement) {
return scalarElement.value;
}
}
},
arg.choropleth);
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set choropleth scalar data
*
* @returns {geo.feature.choropleth}
*/
////////////////////////////////////////////////////////////////////////////
this.scalar = function (data, aggregator) {
var scalarId, scalarValue;
if (data === undefined) {
return m_this.choropleth.get('scalar')();
} else {
scalarId = m_this.choropleth.get('accessors')().scalarId;
scalarValue = m_this.choropleth.get('accessors')().scalarValue;
m_choropleth.scalar = data;
m_choropleth.scalarAggregator = aggregator || d3.mean;
// we make internal dictionary from array for faster lookup
// when matching geojson features to scalar values,
// note that we also allow for multiple scalar elements
// for the same geo feature
m_choropleth.scalar._dictionary = data
.reduce(function (accumeDictionary, scalarElement) {
var id, value;
id = scalarId(scalarElement);
value = scalarValue(scalarElement);
accumeDictionary[id] =
accumeDictionary[id] ?
accumeDictionary[id].push(value) : [value];
return accumeDictionary;
}, {});
m_this.dataTime().modified();
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set choropleth accessor
*
* @returns {geo.feature.choropleth}
*/
////////////////////////////////////////////////////////////////////////////
this.choropleth = function (arg1, arg2) {
var choropleth;
if (arg1 === undefined) {
return m_choropleth;
}
if (typeof arg1 === 'string' && arg2 === undefined) {
return m_choropleth[arg1];
}
if (arg2 === undefined) {
choropleth = $.extend(
{},
m_choropleth,
arg1
);
m_choropleth = choropleth;
} else {
m_choropleth[arg1] = arg2; //if you pass in accessor for prop
}
m_this.modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* A uniform getter that always returns a function even for constant values.
* If undefined input, return all the choropleth values as an object.
*
* @param {string|undefined} key
* @return {function}
*/
////////////////////////////////////////////////////////////////////////////
this.choropleth.get = function (key) {
var all = {}, k;
if (key === undefined) {
for (k in m_choropleth) {
if (m_choropleth.hasOwnProperty(k)) {
all[k] = m_this.choropleth.get(k);
}
}
return all;
}
return geo.util.ensureFunction(m_choropleth[key]);
};
////////////////////////////////////////////////////////////////////////////
/**
* A method that adds a polygon feature to the current layer.
*
* @param {array} coordinateArray
* @param {geo.color} fillColor
* @return {geo.feature}
*/
////////////////////////////////////////////////////////////////////////////
this._addPolygonFeature = function (feature, fillColor) {
var newFeature = m_this.layer()
.createFeature('polygon', {});
if (feature.geometry.type === 'Polygon') {
newFeature.data([{
type: 'Polygon',
coordinates: feature.geometry.coordinates
}]);
} else if (feature.geometry.type === 'MultiPolygon') {
newFeature.data(feature.geometry.coordinates.map(function (coordinateMap) {
return {
type: 'Polygon',
coordinates: coordinateMap
};
}));
}
newFeature
.polygon(function (d) {
return {
'outer': d.coordinates[0],
'inner': d.coordinates[1] // undefined but ok
};
})
.position(function (d) {
return {
x: d[0],
y: d[1]
};
})
.style({
'fillColor': fillColor
});
return newFeature;
};
////////////////////////////////////////////////////////////////////////////
/**
* A method that adds polygons from a given feature to the current layer.
*
* @param {} geoJsonFeature
* @param geo.color
* @return [{geo.feature}]
*/
////////////////////////////////////////////////////////////////////////////
this._featureToPolygons = function (feature, fillValue) {
return m_this
._addPolygonFeature(feature, fillValue);
};
////////////////////////////////////////////////////////////////////////////
/**
* A method that sets a choropleth scale's domain and range.
*
* @param {undefined | function({})} valueAccessor
* @return {geo.feature.choropleth}
*/
////////////////////////////////////////////////////////////////////////////
this._generateScale = function (valueAccessor) {
var extent =
d3.extent(m_this.scalar(), valueAccessor || undefined);
m_this.choropleth()
.scale
.domain(extent)
.range(m_this.choropleth().colorRange);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**sr
* Generate scale for choropleth.data(), make polygons from features.
* @returns: [ [geo.feature.polygon, ...] , ... ]
*/
////////////////////////////////////////////////////////////////////////////
this.createChoropleth = function () {
var choropleth = m_this.choropleth,
data = m_this.data(),
scalars = m_this.scalar(),
valueFunc = choropleth.get('accessors')().scalarValue,
getFeatureId = choropleth.get('accessors')().geoId;
m_this._generateScale(valueFunc);
return data
.map(function (feature) {
var id = getFeatureId(feature);
var valueArray = scalars._dictionary[id];
var accumulatedScalarValue = choropleth().scalarAggregator(valueArray);
// take average of this array of values
// which allows for non-bijective correspondance
// between geo data and scalar data
var fillColor =
m_this
.choropleth()
.scale(accumulatedScalarValue);
return m_this
._featureToPolygons(feature, fillColor);
});
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
if (m_choropleth) {
m_this.dataTime().modified();
}
};
this._init(arg);
return this;
};
inherit(geo.choroplethFeature, geo.feature);
/* Example:
*/
/**
* @namespace
*/
geo.gl = {};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of lineFeature
*
* @class
* @extends geo.lineFeature
* @returns {geo.gl.lineFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gl.lineFeature = function (arg) {
'use strict';
if (!(this instanceof geo.gl.lineFeature)) {
return new geo.gl.lineFeature(arg);
}
arg = arg || {};
geo.lineFeature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_exit = this._exit,
m_actor = null,
m_mapper = null,
m_material = null,
m_pixelWidthUnif = null,
m_aspectUniform = null,
m_dynamicDraw = arg.dynamicDraw === undefined ? false : arg.dynamicDraw,
s_init = this._init,
s_update = this._update;
function createVertexShader() {
var vertexShaderSource = [
'#ifdef GL_ES',
' precision highp float;',
'#endif',
'attribute vec3 pos;',
'attribute vec3 prev;',
'attribute vec3 next;',
'attribute float offset;',
'attribute vec3 strokeColor;',
'attribute float strokeOpacity;',
'attribute float strokeWidth;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'uniform float pixelWidth;',
'uniform float aspect;',
'varying vec3 strokeColorVar;',
'varying float strokeWidthVar;',
'varying float strokeOpacityVar;',
'void main(void)',
'{',
/* If any vertex has been deliberately set to a negative opacity,
* skip doing computations on it. */
' if (strokeOpacity < 0.0) {',
' gl_Position = vec4(2, 2, 0, 1);',
' return;',
' }',
' const float PI = 3.14159265358979323846264;',
' vec4 worldPos = projectionMatrix * modelViewMatrix * vec4(pos.xyz, 1);',
' if (worldPos.w != 0.0) {',
' worldPos = worldPos/worldPos.w;',
' }',
' vec4 worldNext = projectionMatrix * modelViewMatrix * vec4(next.xyz, 1);',
' if (worldNext.w != 0.0) {',
' worldNext = worldNext/worldNext.w;',
' }',
' vec4 worldPrev = projectionMatrix* modelViewMatrix * vec4(prev.xyz, 1);',
' if (worldPrev.w != 0.0) {',
' worldPrev = worldPrev/worldPrev.w;',
' }',
' strokeColorVar = strokeColor;',
' strokeWidthVar = strokeWidth;',
' strokeOpacityVar = strokeOpacity;',
' vec2 deltaNext = worldNext.xy - worldPos.xy;',
' vec2 deltaPrev = worldPos.xy - worldPrev.xy;',
' float angleNext = 0.0, anglePrev = 0.0;',
' if (deltaNext.xy != vec2(0.0, 0.0))',
' angleNext = atan(deltaNext.y / aspect, deltaNext.x);',
' if (deltaPrev.xy == vec2(0.0, 0.0)) anglePrev = angleNext;',
' else anglePrev = atan(deltaPrev.y / aspect, deltaPrev.x);',
' if (deltaNext.xy == vec2(0.0, 0.0)) angleNext = anglePrev;',
' float angle = (anglePrev + angleNext) / 2.0;',
' float cosAngle = cos(anglePrev - angle);',
' if (cosAngle < 0.1) { cosAngle = sign(cosAngle) * 1.0; angle = 0.0; }',
' float distance = (offset * strokeWidth * pixelWidth) /',
' cosAngle;',
' worldPos.x += distance * sin(angle);',
' worldPos.y -= distance * cos(angle) * aspect;',
' gl_Position = worldPos;',
'}'
].join('\n'),
shader = new vgl.shader(vgl.GL.VERTEX_SHADER);
shader.setShaderSource(vertexShaderSource);
return shader;
}
function createFragmentShader() {
var fragmentShaderSource = [
'#ifdef GL_ES',
' precision highp float;',
'#endif',
'varying vec3 strokeColorVar;',
'varying float strokeWidthVar;',
'varying float strokeOpacityVar;',
'void main () {',
' gl_FragColor = vec4 (strokeColorVar, strokeOpacityVar);',
'}'
].join('\n'),
shader = new vgl.shader(vgl.GL.FRAGMENT_SHADER);
shader.setShaderSource(fragmentShaderSource);
return shader;
}
function createGLLines() {
var data = m_this.data(),
i, j, k, v,
numSegments = 0, len,
lineItem, lineItemData,
vert = [{}, {}], vertTemp,
pos, posIdx3,
position = [],
posFunc = m_this.position(),
strkWidthFunc = m_this.style.get('strokeWidth'),
strkColorFunc = m_this.style.get('strokeColor'),
strkOpacityFunc = m_this.style.get('strokeOpacity'),
order = m_this.featureVertices(),
posBuf, nextBuf, prevBuf, offsetBuf, indicesBuf,
strokeWidthBuf, strokeColorBuf, strokeOpacityBuf,
dest, dest3,
geom = m_mapper.geometryData();
for (i = 0; i < data.length; i += 1) {
lineItem = m_this.line()(data[i], i);
numSegments += lineItem.length - 1;
for (j = 0; j < lineItem.length; j += 1) {
pos = posFunc(lineItem[j], j, lineItem, i);
position.push(pos.x);
position.push(pos.y);
position.push(pos.z || 0.0);
}
}
position = geo.transform.transformCoordinates(
m_this.gcs(), m_this.layer().map().gcs(),
position, 3);
len = numSegments * order.length;
posBuf = geo.util.getGeomBuffer(geom, 'pos', len * 3);
nextBuf = geo.util.getGeomBuffer(geom, 'next', len * 3);
prevBuf = geo.util.getGeomBuffer(geom, 'prev', len * 3);
offsetBuf = geo.util.getGeomBuffer(geom, 'offset', len);
strokeWidthBuf = geo.util.getGeomBuffer(geom, 'strokeWidth', len);
strokeColorBuf = geo.util.getGeomBuffer(geom, 'strokeColor', len * 3);
strokeOpacityBuf = geo.util.getGeomBuffer(geom, 'strokeOpacity', len);
indicesBuf = geom.primitive(0).indices();
if (!(indicesBuf instanceof Uint16Array) || indicesBuf.length !== len) {
indicesBuf = new Uint16Array(len);
geom.primitive(0).setIndices(indicesBuf);
}
for (i = posIdx3 = dest = dest3 = 0; i < data.length; i += 1) {
lineItem = m_this.line()(data[i], i);
for (j = 0; j < lineItem.length; j += 1, posIdx3 += 3) {
lineItemData = lineItem[j];
/* swap entries in vert so that vert[0] is the first vertex, and
* vert[1] will be reused for the second vertex */
if (j) {
vertTemp = vert[0];
vert[0] = vert[1];
vert[1] = vertTemp;
}
vert[1].pos = posIdx3;
vert[1].prev = posIdx3 - (j ? 3 : 0);
vert[1].next = posIdx3 + (j + 1 < lineItem.length ? 3 : 0);
vert[1].strokeWidth = strkWidthFunc(lineItemData, j, lineItem, i);
vert[1].strokeColor = strkColorFunc(lineItemData, j, lineItem, i);
vert[1].strokeOpacity = strkOpacityFunc(lineItemData, j, lineItem, i);
if (j) {
for (k = 0; k < order.length; k += 1, dest += 1, dest3 += 3) {
v = vert[order[k][0]];
posBuf[dest3] = position[v.pos];
posBuf[dest3 + 1] = position[v.pos + 1];
posBuf[dest3 + 2] = position[v.pos + 2];
prevBuf[dest3] = position[v.prev];
prevBuf[dest3 + 1] = position[v.prev + 1];
prevBuf[dest3 + 2] = position[v.prev + 2];
nextBuf[dest3] = position[v.next];
nextBuf[dest3 + 1] = position[v.next + 1];
nextBuf[dest3 + 2] = position[v.next + 2];
offsetBuf[dest] = order[k][1];
/* We can ignore the indicies (they will all be zero) */
strokeWidthBuf[dest] = v.strokeWidth;
strokeColorBuf[dest3] = v.strokeColor.r;
strokeColorBuf[dest3 + 1] = v.strokeColor.g;
strokeColorBuf[dest3 + 2] = v.strokeColor.b;
strokeOpacityBuf[dest] = v.strokeOpacity;
}
}
}
}
geom.boundsDirty(true);
m_mapper.modified();
m_mapper.boundsDirtyTimestamp().modified();
}
////////////////////////////////////////////////////////////////////////////
/**
* Return the arrangement of vertices used for each line segment.
*
* @returns {Number}
*/
////////////////////////////////////////////////////////////////////////////
this.featureVertices = function () {
return [[0, 1], [1, -1], [0, -1], [0, 1], [1, 1], [1, -1]];
};
////////////////////////////////////////////////////////////////////////////
/**
* Return the number of vertices used for each line segment.
*
* @returns {Number}
*/
////////////////////////////////////////////////////////////////////////////
this.verticesPerFeature = function () {
return m_this.featureVertices().length;
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
var prog = vgl.shaderProgram(),
vs = createVertexShader(),
fs = createFragmentShader(),
// Vertex attributes
posAttr = vgl.vertexAttribute('pos'),
prvAttr = vgl.vertexAttribute('prev'),
nxtAttr = vgl.vertexAttribute('next'),
offAttr = vgl.vertexAttribute('offset'),
strkWidthAttr = vgl.vertexAttribute('strokeWidth'),
strkColorAttr = vgl.vertexAttribute('strokeColor'),
strkOpacityAttr = vgl.vertexAttribute('strokeOpacity'),
// Shader uniforms
mviUnif = new vgl.modelViewUniform('modelViewMatrix'),
prjUnif = new vgl.projectionUniform('projectionMatrix'),
geom = vgl.geometryData(),
// Sources
posData = vgl.sourceDataP3fv({'name': 'pos'}),
prvPosData = vgl.sourceDataAnyfv(
3, vgl.vertexAttributeKeysIndexed.Four, {'name': 'prev'}),
nxtPosData = vgl.sourceDataAnyfv(
3, vgl.vertexAttributeKeysIndexed.Five, {'name': 'next'}),
offPosData = vgl.sourceDataAnyfv(
1, vgl.vertexAttributeKeysIndexed.Six, {'name': 'offset'}),
strkWidthData = vgl.sourceDataAnyfv(
1, vgl.vertexAttributeKeysIndexed.One, {'name': 'strokeWidth'}),
strkColorData = vgl.sourceDataAnyfv(
3, vgl.vertexAttributeKeysIndexed.Two, {'name': 'strokeColor'}),
strkOpacityData = vgl.sourceDataAnyfv(
1, vgl.vertexAttributeKeysIndexed.Three,
{'name': 'strokeOpacity'}),
// Primitive indices
triangles = vgl.triangles();
m_pixelWidthUnif = new vgl.floatUniform('pixelWidth',
1.0 / m_this.renderer().width());
m_aspectUniform = new vgl.floatUniform('aspect',
m_this.renderer().width() / m_this.renderer().height());
s_init.call(m_this, arg);
m_material = vgl.material();
m_mapper = vgl.mapper({dynamicDraw: m_dynamicDraw});
prog.addVertexAttribute(posAttr, vgl.vertexAttributeKeys.Position);
prog.addVertexAttribute(strkWidthAttr, vgl.vertexAttributeKeysIndexed.One);
prog.addVertexAttribute(strkColorAttr, vgl.vertexAttributeKeysIndexed.Two);
prog.addVertexAttribute(strkOpacityAttr, vgl.vertexAttributeKeysIndexed.Three);
prog.addVertexAttribute(prvAttr, vgl.vertexAttributeKeysIndexed.Four);
prog.addVertexAttribute(nxtAttr, vgl.vertexAttributeKeysIndexed.Five);
prog.addVertexAttribute(offAttr, vgl.vertexAttributeKeysIndexed.Six);
prog.addUniform(mviUnif);
prog.addUniform(prjUnif);
prog.addUniform(m_pixelWidthUnif);
prog.addUniform(m_aspectUniform);
prog.addShader(fs);
prog.addShader(vs);
m_material.addAttribute(prog);
m_material.addAttribute(vgl.blend());
m_actor = vgl.actor();
m_actor.setMaterial(m_material);
m_actor.setMapper(m_mapper);
geom.addSource(posData);
geom.addSource(prvPosData);
geom.addSource(nxtPosData);
geom.addSource(strkWidthData);
geom.addSource(strkColorData);
geom.addSource(strkOpacityData);
geom.addSource(offPosData);
geom.addPrimitive(triangles);
m_mapper.setGeometryData(geom);
};
////////////////////////////////////////////////////////////////////////////
/**
* Return list of actors
*
* @returns {vgl.actor[]}
*/
////////////////////////////////////////////////////////////////////////////
this.actors = function () {
if (!m_actor) {
return [];
}
return [m_actor];
};
////////////////////////////////////////////////////////////////////////////
/**
* Build
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
if (m_actor) {
m_this.renderer().contextRenderer().removeActor(m_actor);
}
createGLLines();
m_this.renderer().contextRenderer().addActor(m_actor);
m_this.buildTime().modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
s_update.call(m_this);
if (m_this.dataTime().getMTime() >= m_this.buildTime().getMTime() ||
m_this.updateTime().getMTime() <= m_this.getMTime()) {
m_this._build();
}
m_pixelWidthUnif.set(1.0 / m_this.renderer().width());
m_aspectUniform.set(m_this.renderer().width() /
m_this.renderer().height());
m_actor.setVisible(m_this.visible());
m_actor.material().setBinNumber(m_this.bin());
m_this.updateTime().modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Destroy
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.renderer().contextRenderer().removeActor(m_actor);
s_exit();
};
this._init(arg);
return this;
};
inherit(geo.gl.lineFeature, geo.lineFeature);
// Now register it
geo.registerFeature('vgl', 'line', geo.gl.lineFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of pointFeature
*
* @class
* @extends geo.pointFeature
* @returns {geo.gl.pointFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gl.pointFeature = function (arg) {
'use strict';
if (!(this instanceof geo.gl.pointFeature)) {
return new geo.gl.pointFeature(arg);
}
arg = arg || {};
geo.pointFeature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_exit = this._exit,
m_actor = null,
m_mapper = null,
m_pixelWidthUniform = null,
m_aspectUniform = null,
m_dynamicDraw = arg.dynamicDraw === undefined ? false : arg.dynamicDraw,
m_primitiveShape = 'sprite', // arg can change this, below
s_init = this._init,
s_update = this._update,
vertexShaderSource = null,
fragmentShaderSource = null;
if (arg.primitiveShape === 'triangle' ||
arg.primitiveShape === 'square' ||
arg.primitiveShape === 'sprite') {
m_primitiveShape = arg.primitiveShape;
}
vertexShaderSource = [
'#ifdef GL_ES',
' precision highp float;',
'#endif',
'attribute vec3 pos;',
'attribute float rad;',
'attribute vec3 fillColor;',
'attribute vec3 strokeColor;',
'attribute float fillOpacity;',
'attribute float strokeWidth;',
'attribute float strokeOpacity;',
'attribute float fill;',
'attribute float stroke;',
'uniform float pixelWidth;',
'uniform float aspect;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'varying vec4 fillColorVar;',
'varying vec4 strokeColorVar;',
'varying float radiusVar;',
'varying float strokeWidthVar;',
'varying float fillVar;',
'varying float strokeVar;'
];
if (m_primitiveShape !== 'sprite') {
vertexShaderSource = vertexShaderSource.concat([
'attribute vec2 unit;',
'varying vec3 unitVar;'
]);
}
vertexShaderSource.push.apply(vertexShaderSource, [
'void main(void)',
'{',
' strokeWidthVar = strokeWidth;',
' // No stroke or fill implies nothing to draw',
' if (stroke < 1.0 || strokeWidth <= 0.0 || strokeOpacity <= 0.0) {',
' strokeVar = 0.0;',
' strokeWidthVar = 0.0;',
' }',
' else',
' strokeVar = 1.0;',
' if (fill < 1.0 || rad <= 0.0 || fillOpacity <= 0.0)',
' fillVar = 0.0;',
' else',
' fillVar = 1.0;',
/* If the point has no visible pixels, skip doing computations on it. */
' if (fillVar == 0.0 && strokeVar == 0.0) {',
' gl_Position = vec4(2, 2, 0, 1);',
' return;',
' }',
' fillColorVar = vec4 (fillColor, fillOpacity);',
' strokeColorVar = vec4 (strokeColor, strokeOpacity);',
' radiusVar = rad;'
]);
if (m_primitiveShape === 'sprite') {
vertexShaderSource.push.apply(vertexShaderSource, [
' gl_Position = (projectionMatrix * modelViewMatrix * vec4(pos, 1.0)).xyzw;',
' gl_PointSize = 2.0 * (rad + strokeWidthVar); ',
'}'
]);
} else {
vertexShaderSource.push.apply(vertexShaderSource, [
' unitVar = vec3 (unit, 1.0);',
' vec4 p = (projectionMatrix * modelViewMatrix * vec4(pos, 1.0)).xyzw;',
' if (p.w != 0.0) {',
' p = p / p.w;',
' }',
' p += (rad + strokeWidthVar) * ',
' vec4 (unit.x * pixelWidth, unit.y * pixelWidth * aspect, 0.0, 1.0);',
' gl_Position = vec4(p.xyz, 1.0);',
'}'
]);
}
vertexShaderSource = vertexShaderSource.join('\n');
fragmentShaderSource = [
'#ifdef GL_ES',
' precision highp float;',
'#endif',
'uniform float aspect;',
'varying vec4 fillColorVar;',
'varying vec4 strokeColorVar;',
'varying float radiusVar;',
'varying float strokeWidthVar;',
'varying float fillVar;',
'varying float strokeVar;'
];
if (m_primitiveShape !== 'sprite') {
fragmentShaderSource.push('varying vec3 unitVar;');
}
fragmentShaderSource.push.apply(fragmentShaderSource, [
'void main () {',
' vec4 strokeColor, fillColor;',
' float endStep;',
' // No stroke or fill implies nothing to draw',
' if (fillVar == 0.0 && strokeVar == 0.0)',
' discard;'
]);
if (m_primitiveShape === 'sprite') {
fragmentShaderSource.push(
' float rad = 2.0 * length (gl_PointCoord - vec2(0.5));');
} else {
fragmentShaderSource.push(
' float rad = length (unitVar.xy);');
}
fragmentShaderSource.push.apply(fragmentShaderSource, [
' if (rad > 1.0)',
' discard;',
' // If there is no stroke, the fill region should transition to nothing',
' if (strokeVar == 0.0) {',
' strokeColor = vec4 (fillColorVar.rgb, 0.0);',
' endStep = 1.0;',
' } else {',
' strokeColor = strokeColorVar;',
' endStep = radiusVar / (radiusVar + strokeWidthVar);',
' }',
' // Likewise, if there is no fill, the stroke should transition to nothing',
' if (fillVar == 0.0)',
' fillColor = vec4 (strokeColor.rgb, 0.0);',
' else',
' fillColor = fillColorVar;',
' // Distance to antialias over',
' float antialiasDist = 3.0 / (2.0 * radiusVar);',
' if (rad < endStep) {',
' float step = smoothstep (endStep - antialiasDist, endStep, rad);',
' gl_FragColor = mix (fillColor, strokeColor, step);',
' } else {',
' float step = smoothstep (1.0 - antialiasDist, 1.0, rad);',
' gl_FragColor = mix (strokeColor, vec4 (strokeColor.rgb, 0.0), step);',
' }',
'}'
]);
fragmentShaderSource = fragmentShaderSource.join('\n');
function createVertexShader() {
var shader = new vgl.shader(vgl.GL.VERTEX_SHADER);
shader.setShaderSource(vertexShaderSource);
return shader;
}
function createFragmentShader() {
var shader = new vgl.shader(vgl.GL.FRAGMENT_SHADER);
shader.setShaderSource(fragmentShaderSource);
return shader;
}
function pointPolygon(x, y, w, h) {
var verts;
switch (m_primitiveShape) {
case 'triangle':
/* Use an equilateral triangle. While this has 30% more area than a
* square, the reduction in vertices should help more than the
* processing the additional fragments. */
verts = [
x, y - h * 2,
x - w * Math.sqrt(3.0), y + h,
x + w * Math.sqrt(3.0), y + h
];
break;
case 'sprite':
/* Point sprite uses only one vertex per point. */
verts = [x, y];
break;
default: // "square"
/* Use a surrounding square split diagonally into two triangles. */
verts = [
x - w, y + h,
x - w, y - h,
x + w, y + h,
x - w, y - h,
x + w, y - h,
x + w, y + h
];
break;
}
return verts;
}
function createGLPoints() {
// unit and associated data is not used when drawing sprite
var i, j, numPts = m_this.data().length,
unit = pointPolygon(0, 0, 1, 1),
position = new Array(numPts * 3), posBuf, posVal, posFunc,
unitBuf, indices,
radius, radiusVal, radFunc,
stroke, strokeVal, strokeFunc,
strokeWidth, strokeWidthVal, strokeWidthFunc,
strokeOpacity, strokeOpacityVal, strokeOpacityFunc,
strokeColor, strokeColorVal, strokeColorFunc,
fill, fillVal, fillFunc,
fillOpacity, fillOpacityVal, fillOpacityFunc,
fillColor, fillColorVal, fillColorFunc,
vpf = m_this.verticesPerFeature(),
data = m_this.data(),
item, ivpf, ivpf3, iunit, i3,
geom = m_mapper.geometryData(), nonzeroZ;
posFunc = m_this.position();
radFunc = m_this.style.get('radius');
strokeFunc = m_this.style.get('stroke');
strokeWidthFunc = m_this.style.get('strokeWidth');
strokeOpacityFunc = m_this.style.get('strokeOpacity');
strokeColorFunc = m_this.style.get('strokeColor');
fillFunc = m_this.style.get('fill');
fillOpacityFunc = m_this.style.get('fillOpacity');
fillColorFunc = m_this.style.get('fillColor');
/* It is more efficient to do a transform on a single array rather than on
* an array of arrays or an array of objects. */
for (i = i3 = 0; i < numPts; i += 1, i3 += 3) {
posVal = posFunc(data[i]);
position[i3] = posVal.x;
position[i3 + 1] = posVal.y;
position[i3 + 2] = posVal.z || 0;
nonzeroZ = nonzeroZ || position[i3 + 2];
}
position = geo.transform.transformCoordinates(
m_this.gcs(), m_this.layer().map().gcs(),
position, 3);
/* Some transforms modify the z-coordinate. If we started with all zero z
* coordinates, don't modify them. This could be changed if the
* z-coordinate space of the gl cube is scaled appropriately. */
if (!nonzeroZ && m_this.gcs() !== m_this.layer().map().gcs()) {
for (i = i3 = 0; i < numPts; i += 1, i3 += 3) {
position[i3 + 2] = 0;
}
}
posBuf = geo.util.getGeomBuffer(geom, 'pos', vpf * numPts * 3);
if (m_primitiveShape !== 'sprite') {
unitBuf = geo.util.getGeomBuffer(geom, 'unit', vpf * numPts * 2);
}
radius = geo.util.getGeomBuffer(geom, 'rad', vpf * numPts);
stroke = geo.util.getGeomBuffer(geom, 'stroke', vpf * numPts);
strokeWidth = geo.util.getGeomBuffer(geom, 'strokeWidth', vpf * numPts);
strokeOpacity = geo.util.getGeomBuffer(geom, 'strokeOpacity', vpf * numPts);
strokeColor = geo.util.getGeomBuffer(geom, 'strokeColor', vpf * numPts * 3);
fill = geo.util.getGeomBuffer(geom, 'fill', vpf * numPts);
fillOpacity = geo.util.getGeomBuffer(geom, 'fillOpacity', vpf * numPts);
fillColor = geo.util.getGeomBuffer(geom, 'fillColor', vpf * numPts * 3);
indices = geom.primitive(0).indices();
if (!(indices instanceof Uint16Array) || indices.length !== vpf * numPts) {
indices = new Uint16Array(vpf * numPts);
geom.primitive(0).setIndices(indices);
}
for (i = ivpf = ivpf3 = iunit = i3 = 0; i < numPts; i += 1, i3 += 3) {
item = data[i];
if (m_primitiveShape !== 'sprite') {
for (j = 0; j < unit.length; j += 1, iunit += 1) {
unitBuf[iunit] = unit[j];
}
}
/* We can ignore the indicies (they will all be zero) */
radiusVal = radFunc(item);
strokeVal = strokeFunc(item) ? 1.0 : 0.0;
strokeWidthVal = strokeWidthFunc(item);
strokeOpacityVal = strokeOpacityFunc(item);
strokeColorVal = strokeColorFunc(item);
fillVal = fillFunc(item) ? 1.0 : 0.0;
fillOpacityVal = fillOpacityFunc(item);
fillColorVal = fillColorFunc(item);
for (j = 0; j < vpf; j += 1, ivpf += 1, ivpf3 += 3) {
posBuf[ivpf3] = position[i3];
posBuf[ivpf3 + 1] = position[i3 + 1];
posBuf[ivpf3 + 2] = position[i3 + 2];
radius[ivpf] = radiusVal;
stroke[ivpf] = strokeVal;
strokeWidth[ivpf] = strokeWidthVal;
strokeOpacity[ivpf] = strokeOpacityVal;
strokeColor[ivpf3] = strokeColorVal.r;
strokeColor[ivpf3 + 1] = strokeColorVal.g;
strokeColor[ivpf3 + 2] = strokeColorVal.b;
fill[ivpf] = fillVal;
fillOpacity[ivpf] = fillOpacityVal;
fillColor[ivpf3] = fillColorVal.r;
fillColor[ivpf3 + 1] = fillColorVal.g;
fillColor[ivpf3 + 2] = fillColorVal.b;
}
}
geom.boundsDirty(true);
m_mapper.modified();
m_mapper.boundsDirtyTimestamp().modified();
}
////////////////////////////////////////////////////////////////////////////
/**
* Return list of actors
*
* @returns {vgl.actor[]}
*/
////////////////////////////////////////////////////////////////////////////
this.actors = function () {
if (!m_actor) {
return [];
}
return [m_actor];
};
////////////////////////////////////////////////////////////////////////////
/**
* Return the number of vertices used for each point.
*
* @returns {Number}
*/
////////////////////////////////////////////////////////////////////////////
this.verticesPerFeature = function () {
var unit = pointPolygon(0, 0, 1, 1);
return unit.length / 2;
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function () {
var prog = vgl.shaderProgram(),
vertexShader = createVertexShader(),
fragmentShader = createFragmentShader(),
posAttr = vgl.vertexAttribute('pos'),
unitAttr = vgl.vertexAttribute('unit'),
radAttr = vgl.vertexAttribute('rad'),
strokeWidthAttr = vgl.vertexAttribute('strokeWidth'),
fillColorAttr = vgl.vertexAttribute('fillColor'),
fillAttr = vgl.vertexAttribute('fill'),
strokeColorAttr = vgl.vertexAttribute('strokeColor'),
strokeAttr = vgl.vertexAttribute('stroke'),
fillOpacityAttr = vgl.vertexAttribute('fillOpacity'),
strokeOpacityAttr = vgl.vertexAttribute('strokeOpacity'),
modelViewUniform = new vgl.modelViewUniform('modelViewMatrix'),
projectionUniform = new vgl.projectionUniform('projectionMatrix'),
mat = vgl.material(),
blend = vgl.blend(),
geom = vgl.geometryData(),
sourcePositions = vgl.sourceDataP3fv({'name': 'pos'}),
sourceUnits = vgl.sourceDataAnyfv(
2, vgl.vertexAttributeKeysIndexed.One, {'name': 'unit'}),
sourceRadius = vgl.sourceDataAnyfv(
1, vgl.vertexAttributeKeysIndexed.Two, {'name': 'rad'}),
sourceStrokeWidth = vgl.sourceDataAnyfv(
1, vgl.vertexAttributeKeysIndexed.Three, {'name': 'strokeWidth'}),
sourceFillColor = vgl.sourceDataAnyfv(
3, vgl.vertexAttributeKeysIndexed.Four, {'name': 'fillColor'}),
sourceFill = vgl.sourceDataAnyfv(
1, vgl.vertexAttributeKeysIndexed.Five, {'name': 'fill'}),
sourceStrokeColor = vgl.sourceDataAnyfv(
3, vgl.vertexAttributeKeysIndexed.Six, {'name': 'strokeColor'}),
sourceStroke = vgl.sourceDataAnyfv(
1, vgl.vertexAttributeKeysIndexed.Seven, {'name': 'stroke'}),
sourceAlpha = vgl.sourceDataAnyfv(
1, vgl.vertexAttributeKeysIndexed.Eight, {'name': 'fillOpacity'}),
sourceStrokeOpacity = vgl.sourceDataAnyfv(
1, vgl.vertexAttributeKeysIndexed.Nine, {'name': 'strokeOpacity'}),
primitive = new vgl.triangles();
if (m_primitiveShape === 'sprite') {
primitive = new vgl.points();
}
m_pixelWidthUniform = new vgl.floatUniform('pixelWidth',
2.0 / m_this.renderer().width());
m_aspectUniform = new vgl.floatUniform('aspect',
m_this.renderer().width() / m_this.renderer().height());
s_init.call(m_this, arg);
m_mapper = vgl.mapper({dynamicDraw: m_dynamicDraw});
// TODO: Right now this is ugly but we will fix it.
prog.addVertexAttribute(posAttr, vgl.vertexAttributeKeys.Position);
if (m_primitiveShape !== 'sprite') {
prog.addVertexAttribute(unitAttr, vgl.vertexAttributeKeysIndexed.One);
}
prog.addVertexAttribute(radAttr, vgl.vertexAttributeKeysIndexed.Two);
prog.addVertexAttribute(strokeWidthAttr, vgl.vertexAttributeKeysIndexed.Three);
prog.addVertexAttribute(fillColorAttr, vgl.vertexAttributeKeysIndexed.Four);
prog.addVertexAttribute(fillAttr, vgl.vertexAttributeKeysIndexed.Five);
prog.addVertexAttribute(strokeColorAttr, vgl.vertexAttributeKeysIndexed.Six);
prog.addVertexAttribute(strokeAttr, vgl.vertexAttributeKeysIndexed.Seven);
prog.addVertexAttribute(fillOpacityAttr, vgl.vertexAttributeKeysIndexed.Eight);
prog.addVertexAttribute(strokeOpacityAttr, vgl.vertexAttributeKeysIndexed.Nine);
prog.addUniform(m_pixelWidthUniform);
prog.addUniform(m_aspectUniform);
prog.addUniform(modelViewUniform);
prog.addUniform(projectionUniform);
prog.addShader(fragmentShader);
prog.addShader(vertexShader);
mat.addAttribute(prog);
mat.addAttribute(blend);
m_actor = vgl.actor();
m_actor.setMaterial(mat);
m_actor.setMapper(m_mapper);
geom.addSource(sourcePositions);
geom.addSource(sourceUnits);
geom.addSource(sourceRadius);
geom.addSource(sourceStrokeWidth);
geom.addSource(sourceFillColor);
geom.addSource(sourceFill);
geom.addSource(sourceStrokeColor);
geom.addSource(sourceStroke);
geom.addSource(sourceAlpha);
geom.addSource(sourceStrokeOpacity);
geom.addPrimitive(primitive);
m_mapper.setGeometryData(geom);
};
////////////////////////////////////////////////////////////////////////////
/**
* Build
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
if (m_actor) {
m_this.renderer().contextRenderer().removeActor(m_actor);
}
createGLPoints();
m_this.renderer().contextRenderer().addActor(m_actor);
m_this.renderer().contextRenderer().render();
m_this.buildTime().modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
s_update.call(m_this);
// For now build if the data or style changes. In the future we may
// we able to partially update the data using dynamic gl buffers.
if (m_this.dataTime().getMTime() >= m_this.buildTime().getMTime() ||
m_this.updateTime().getMTime() < m_this.getMTime()) {
m_this._build();
}
// Update uniforms
m_pixelWidthUniform.set(2.0 / m_this.renderer().width());
m_aspectUniform.set(m_this.renderer().width() /
m_this.renderer().height());
m_actor.setVisible(m_this.visible());
m_actor.material().setBinNumber(m_this.bin());
m_this.updateTime().modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Destroy
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.renderer().contextRenderer().removeActor(m_actor);
s_exit();
};
m_this._init();
return this;
};
inherit(geo.gl.pointFeature, geo.pointFeature);
// Now register it
geo.registerFeature('vgl', 'point', geo.gl.pointFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of geomFeature
*
* @class
* @extends geo.geomFeature
* @param {vgl.geometryData} arg
* @returns {geo.gl.geomFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gl.geomFeature = function (arg) {
'use strict';
if (!(this instanceof geo.gl.geomFeature)) {
return new geo.gl.geomFeature(arg);
}
arg = arg || {};
geo.geomFeature.call(this, arg);
// Initialize
var m_this = this,
m_geom = arg.geom || null,
m_actor = vgl.actor(),
m_mapper = vgl.mapper(),
m_material = null,
m_scalar = null,
m_color = arg.color || [1.0, 1.0, 1.0],
m_buildTime = null,
m_noOfPrimitives = 0;
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
var style = m_this.style();
// Vertex color gets the preference
if (m_geom !== null) {
m_scalar = m_geom.sourceData(vgl.vertexAttributeKeys.Scalar);
m_color = m_geom.sourceData(vgl.vertexAttributeKeys.Color);
m_mapper.setGeometryData(m_geom);
}
m_this.setMapper(m_mapper);
if (style.point_sprites !== undefined && style.point_sprites &&
style.point_sprites_image !== undefined &&
style.point_sprites_image !== null &&
m_noOfPrimitives === 1 &&
m_geom.primitive(0).primitiveType() === gl.POINTS) {
m_material = vgl.utils.createPointSpritesMaterial(
style.point_sprites_image);
} else if (m_scalar) {
if (m_color instanceof vgl.lookupTable) {
m_color.updateRange(m_scalar.scalarRange());
m_material = vgl.utils.createColorMappedMaterial(m_color);
} else {
m_color = vgl.lookupTable();
m_color.updateRange(m_scalar.scalarRange());
m_material = vgl.utils.createColorMappedMaterial(m_color);
}
} else if (m_color) {
m_material = vgl.utils.createColorMaterial();
} else {
m_material = vgl.utils.createSolidColorMaterial();
}
m_actor.setMaterial(m_material);
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
*
* @private
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
if (m_buildTime &&
m_buildTime.getMTime() < m_this.getMTime()) {
if (m_color instanceof vgl.lookupTable) {
vgl.utils.updateColorMappedMaterial(m_this.material(),
m_this.style.color);
}/* else {
// TODO
}*/
} else {
m_buildTime = vgl.timestamp();
m_buildTime.modified();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set geometry
*
* @returns {geo.gl.geomFeature}
*/
////////////////////////////////////////////////////////////////////////////
this.geometry = function (val) {
if (val === undefined) {
return m_geom;
} else {
m_geom = val;
m_this.modified();
return m_this;
}
};
return this;
};
inherit(geo.gl.geomFeature, geo.geomFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a plane feature given a lower left corner point
* and and upper right corner point
* @class
* @extends geo.planeFeature
* @param lowerleft
* @param upperright
* @returns {geo.gl.planeFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gl.planeFeature = function (arg) {
'use strict';
if (!(this instanceof geo.gl.planeFeature)) {
return new geo.gl.planeFeature(arg);
}
geo.planeFeature.call(this, arg);
var m_this = this,
s_exit = this._exit,
m_actor = null,
m_onloadCallback = arg.onload === undefined ? null : arg.onload;
////////////////////////////////////////////////////////////////////////////
/**
* Gets the coordinates for this plane
*
* @returns {Array} [[origin], [upper left] [lower right]]
*/
////////////////////////////////////////////////////////////////////////////
this.coords = function () {
return [m_this.origin(), m_this.upperLeft(), m_this.lowerRight()];
};
////////////////////////////////////////////////////////////////////////////
/**
* Build this feature
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
var or = m_this.origin(),
ul = m_this.upperLeft(),
lr = m_this.lowerRight(),
/// img could be a source or an Image
img = m_this.style().image,
image = null,
texture = null,
gcs = m_this.gcs(),
map_gcs = m_this.layer().map().gcs();
if (gcs !== map_gcs) {
or = geo.transform.transformCoordinates(gcs, map_gcs, or);
ul = geo.transform.transformCoordinates(gcs, map_gcs, ul);
lr = geo.transform.transformCoordinates(gcs, map_gcs, lr);
}
m_this.buildTime().modified();
if (m_actor) {
m_this.renderer().contextRenderer().removeActor(m_actor);
}
if (img && img instanceof Image) {
image = img;
} else if (img) {
image = new Image();
image.src = img;
}
if (!image) {
m_actor = vgl.utils.createPlane(or[0], or[1], or[2],
ul[0], ul[1], ul[2],
lr[0], lr[1], lr[2]);
m_actor.material().shaderProgram().uniform('opacity').set(
m_this.style().opacity !== undefined ? m_this.style().opacity : 1);
m_this.renderer().contextRenderer().addActor(m_actor);
} else {
m_actor = vgl.utils.createTexturePlane(or[0], or[1], or[2],
lr[0], lr[1], lr[2],
ul[0], ul[1], ul[2], true);
m_actor.material().shaderProgram().uniform('opacity').set(
m_this.style().opacity !== undefined ? m_this.style().opacity : 1);
texture = vgl.texture();
m_this.visible(false);
m_this.renderer().contextRenderer().addActor(m_actor);
/* An image is already loaded if .complete is true and .naturalWidth
* and .naturalHeight are defined and non-zero (not falsy seems to be
* sufficient). */
if (image.complete && image.naturalWidth && image.naturalHeight) {
texture.setImage(image);
m_actor.material().addAttribute(texture);
/// NOTE Currently we assume that we want to show the feature as
/// soon as the image gets loaded. However, there might be a case
/// where we want to lock down the visibility. We will deal with that
/// later.
m_this.visible(true);
if (m_onloadCallback) {
m_onloadCallback.call(m_this);
}
} else {
image.onload = function () {
texture.setImage(image);
m_actor.material().addAttribute(texture);
/// NOTE Currently we assume that we want to show the feature as
/// soon as the image gets loaded. However, there might be a case
/// where we want to lock down the visibility. We will deal with that
/// later.
m_this.visible(true);
if (m_onloadCallback) {
m_onloadCallback.call(m_this);
}
if (m_this.drawOnAsyncResourceLoad()) {
m_this._update();
m_this.layer().draw();
}
};
}
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
if (m_this.buildTime().getMTime() <= m_this.dataTime().getMTime()) {
m_this._build();
}
if (m_this.updateTime().getMTime() <= m_this.getMTime()) {
m_actor.setVisible(m_this.visible());
m_actor.material().setBinNumber(m_this.bin());
m_actor.material().shaderProgram().uniform('opacity').set(
m_this.style().opacity !== undefined ? m_this.style().opacity : 1);
}
m_this.updateTime().modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Destroy
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.renderer().contextRenderer().removeActor(m_actor);
s_exit();
};
return this;
};
inherit(geo.gl.planeFeature, geo.planeFeature);
// Now register it
geo.registerFeature('vgl', 'plane', geo.gl.planeFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class quadFeature
*
* @class
* @param {Object} arg Options object
* @extends geo.quadFeature
* @returns {geo.gl.quadFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gl.quadFeature = function (arg) {
'use strict';
if (!(this instanceof geo.gl.quadFeature)) {
return new geo.gl.quadFeature(arg);
}
geo.quadFeature.call(this, arg);
var m_this = this,
s_exit = this._exit,
s_init = this._init,
s_update = this._update,
m_modelViewUniform,
m_actor_image, m_actor_color, m_glBuffers = {}, m_imgposbuf,
m_clrposbuf, m_clrModelViewUniform,
m_glCompileTimestamp = vgl.timestamp(),
m_glColorCompileTimestamp = vgl.timestamp(),
m_quads;
var vertexShaderImageSource = [
'attribute vec3 vertexPosition;',
'attribute vec3 textureCoord;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'varying highp vec3 iTextureCoord;',
'void main(void) {',
' gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);',
' iTextureCoord = textureCoord;',
'}'].join('\n');
var vertexShaderColorSource = [
'attribute vec3 vertexPosition;',
'uniform vec3 vertexColor;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'varying mediump vec3 iVertexColor;',
'varying highp vec3 iTextureCoord;',
'void main(void) {',
' gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);',
' iVertexColor = vertexColor;',
'}'].join('\n');
/**
* Allocate buffers that we need to control for image quads. This mimics
* the actions from vgl.mapper to some degree.
*
* @private
*/
function setupDrawObjects(renderState) {
var context = renderState.m_context,
newbuf = false;
if (m_quads.imgQuads.length) {
if (!m_imgposbuf || m_imgposbuf.length < m_quads.imgQuads.length * 12 ||
!m_glBuffers.imgQuadsPosition) {
if (m_glBuffers.imgQuadsPosition) {
context.deleteBuffer(m_glBuffers.imgQuadsPosition);
}
m_glBuffers.imgQuadsPosition = context.createBuffer();
m_imgposbuf = new Float32Array(Math.max(
128, m_quads.imgQuads.length * 2) * 12);
newbuf = true;
}
$.each(m_quads.imgQuads, function (idx, quad) {
for (var i = 0; i < 12; i += 1) {
m_imgposbuf[idx * 12 + i] = quad.pos[i] - m_quads.origin[i % 3];
}
});
context.bindBuffer(vgl.GL.ARRAY_BUFFER, m_glBuffers.imgQuadsPosition);
if (newbuf) {
context.bufferData(vgl.GL.ARRAY_BUFFER, m_imgposbuf, vgl.GL.DYNAMIC_DRAW);
} else {
context.bufferSubData(vgl.GL.ARRAY_BUFFER, 0, m_imgposbuf);
}
}
m_glCompileTimestamp.modified();
}
/**
* Allocate buffers that we need to control for color quads. This mimics
* the actions from vgl.mapper to some degree.
*
* @private
*/
function setupColorDrawObjects(renderState) {
var context = renderState.m_context,
newbuf = false;
if (m_quads.clrQuads.length) {
if (!m_clrposbuf || m_clrposbuf.length < m_quads.clrQuads.length * 12 ||
!m_glBuffers.clrQuadsPosition) {
if (m_glBuffers.imgQuadsPosition) {
context.deleteBuffer(m_glBuffers.clrQuadsPosition);
}
m_glBuffers.clrQuadsPosition = context.createBuffer();
m_clrposbuf = new Float32Array(Math.max(
128, m_quads.clrQuads.length * 2) * 12);
newbuf = true;
}
$.each(m_quads.clrQuads, function (idx, quad) {
for (var i = 0; i < 12; i += 1) {
m_clrposbuf[idx * 12 + i] = quad.pos[i] - m_quads.origin[i % 3];
}
});
context.bindBuffer(vgl.GL.ARRAY_BUFFER, m_glBuffers.clrQuadsPosition);
if (newbuf) {
context.bufferData(vgl.GL.ARRAY_BUFFER, m_clrposbuf, vgl.GL.DYNAMIC_DRAW);
} else {
context.bufferSubData(vgl.GL.ARRAY_BUFFER, 0, m_clrposbuf);
}
}
m_glColorCompileTimestamp.modified();
}
////////////////////////////////////////////////////////////////////////////
/**
* Build this feature
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
var mapper, mat, prog, srctex, geom;
if (!m_this.position()) {
return;
}
m_quads = this._generateQuads();
/* Create an actor to render image quads */
if (m_quads.imgQuads.length && !m_actor_image) {
m_this.visible(false);
mapper = new vgl.mapper({dynamicDraw: true});
m_actor_image = new vgl.actor();
/* This is similar to vgl.utils.createTextureMaterial */
m_actor_image.setMapper(mapper);
mat = new vgl.material();
prog = new vgl.shaderProgram();
prog.addVertexAttribute(new vgl.vertexAttribute('vertexPosition'),
vgl.vertexAttributeKeys.Position);
prog.addVertexAttribute(new vgl.vertexAttribute('textureCoord'),
vgl.vertexAttributeKeys.TextureCoordinate);
m_modelViewUniform = new vgl.modelViewOriginUniform('modelViewMatrix',
m_quads.origin);
prog.addUniform(m_modelViewUniform);
prog.addUniform(new vgl.projectionUniform('projectionMatrix'));
prog.addUniform(new vgl.floatUniform('opacity', 1.0));
prog.addShader(vgl.getCachedShader(
vgl.GL.VERTEX_SHADER, vgl.GL, vertexShaderImageSource));
prog.addShader(vgl.utils.createRgbaTextureFragmentShader(vgl.GL));
mat.addAttribute(prog);
mat.addAttribute(new vgl.blend());
/* This is similar to vgl.planeSource */
geom = new vgl.geometryData();
m_imgposbuf = undefined;
srctex = new vgl.sourceDataT2fv();
srctex.pushBack([0, 0, 1, 0, 0, 1, 1, 1]);
geom.addSource(srctex);
/* We deliberately do not add a primitive to our geometry -- we take care
* of that ourselves. */
mapper.setGeometryData(geom);
m_actor_image.setMaterial(mat);
mapper.s_render = mapper.render;
mapper.render = m_this._renderImageQuads;
m_this.renderer().contextRenderer().addActor(m_actor_image);
m_this.visible(true);
}
/* Create an actor to render color quads */
if (m_quads.clrQuads.length && !m_actor_color) {
m_this.visible(false);
mapper = new vgl.mapper({dynamicDraw: true});
m_actor_color = new vgl.actor();
/* This is similar to vgl.utils.createTextureMaterial */
m_actor_color.setMapper(mapper);
mat = new vgl.material();
prog = new vgl.shaderProgram();
prog.addVertexAttribute(new vgl.vertexAttribute('vertexPosition'),
vgl.vertexAttributeKeys.Position);
m_clrModelViewUniform = new vgl.modelViewOriginUniform('modelViewMatrix',
m_quads.origin);
prog.addUniform(m_clrModelViewUniform);
prog.addUniform(new vgl.projectionUniform('projectionMatrix'));
prog.addUniform(new vgl.floatUniform('opacity', 1.0));
prog.addUniform(new vgl.uniform(vgl.GL.FLOAT_VEC3, 'vertexColor'));
prog.addShader(vgl.getCachedShader(
vgl.GL.VERTEX_SHADER, vgl.GL, vertexShaderColorSource));
prog.addShader(vgl.utils.createFragmentShader(vgl.GL));
mat.addAttribute(prog);
mat.addAttribute(new vgl.blend());
/* This is similar to vgl.planeSource */
geom = new vgl.geometryData();
m_clrposbuf = undefined;
/* We deliberately do not add a primitive to our geometry -- we take care
* of that ourselves. */
mapper.setGeometryData(geom);
m_actor_color.setMaterial(mat);
mapper.s_render = mapper.render;
mapper.render = m_this._renderColorQuads;
m_this.renderer().contextRenderer().addActor(m_actor_color);
m_this.visible(true);
}
if (m_modelViewUniform) {
m_modelViewUniform.setOrigin(m_quads.origin);
}
if (m_clrModelViewUniform) {
m_clrModelViewUniform.setOrigin(m_quads.origin);
}
m_this.buildTime().modified();
};
/**
* Check all of the image quads. If any do not have the correct texture,
* update them. */
this._updateTextures = function () {
var texture;
$.each(m_quads.imgQuads, function (idx, quad) {
if (!quad.image) {
return;
}
if (quad.image._texture) {
quad.texture = quad.image._texture;
} else {
texture = new vgl.texture();
texture.setImage(quad.image);
quad.texture = quad.image._texture = texture;
}
});
};
/**
* Render all of the color quads using a single mapper.
*
* @param renderState: the render state used for the render.
*/
this._renderColorQuads = function (renderState) {
if (!m_quads.clrQuads.length) {
return;
}
var mapper = this;
if (mapper.getMTime() > m_glColorCompileTimestamp.getMTime() ||
m_this.dataTime().getMTime() > m_glColorCompileTimestamp.getMTime() ||
renderState.m_contextChanged || !m_clrposbuf ||
m_quads.clrQuads.length * 12 > m_clrposbuf.length) {
setupColorDrawObjects(renderState);
}
mapper.s_render(renderState);
var context = renderState.m_context, opacity = 1, color;
context.bindBuffer(vgl.GL.ARRAY_BUFFER, m_glBuffers.clrQuadsPosition);
$.each(m_quads.clrQuads, function (idx, quad) {
if (quad.opacity !== opacity) {
opacity = quad.opacity;
context.uniform1fv(renderState.m_material.shaderProgram(
).uniformLocation('opacity'), new Float32Array([opacity]));
}
if (!color || color.r !== quad.color.r || color.g !== quad.color.g ||
color.b !== quad.color.b) {
color = quad.color;
context.uniform3fv(renderState.m_material.shaderProgram(
).uniformLocation('vertexColor'), new Float32Array([
color.r, color.g, color.b]));
}
context.bindBuffer(vgl.GL.ARRAY_BUFFER, m_glBuffers.clrQuadsPosition);
context.vertexAttribPointer(vgl.vertexAttributeKeys.Position, 3,
vgl.GL.FLOAT, false, 12, idx * 12 * 4);
context.enableVertexAttribArray(vgl.vertexAttributeKeys.Position);
context.drawArrays(vgl.GL.TRIANGLE_STRIP, 0, 4);
});
context.bindBuffer(vgl.GL.ARRAY_BUFFER, null);
};
/**
* Render all of the image quads using a single mapper.
*
* @param renderState: the render state used for the render.
*/
this._renderImageQuads = function (renderState) {
if (!m_quads.imgQuads.length) {
return;
}
var mapper = this;
if (mapper.getMTime() > m_glCompileTimestamp.getMTime() ||
m_this.dataTime().getMTime() > m_glCompileTimestamp.getMTime() ||
renderState.m_contextChanged || !m_imgposbuf ||
m_quads.imgQuads.length * 12 > m_imgposbuf.length) {
setupDrawObjects(renderState);
}
mapper.s_render(renderState);
var context = renderState.m_context, opacity = 1;
m_this._updateTextures();
context.bindBuffer(vgl.GL.ARRAY_BUFFER, m_glBuffers.imgQuadsPosition);
$.each(m_quads.imgQuads, function (idx, quad) {
if (!quad.image) {
return;
}
quad.texture.bind(renderState);
if (quad.opacity !== opacity) {
opacity = quad.opacity;
context.uniform1fv(renderState.m_material.shaderProgram(
).uniformLocation('opacity'), new Float32Array([opacity]));
}
context.bindBuffer(vgl.GL.ARRAY_BUFFER, m_glBuffers.imgQuadsPosition);
context.vertexAttribPointer(vgl.vertexAttributeKeys.Position, 3,
vgl.GL.FLOAT, false, 12, idx * 12 * 4);
context.enableVertexAttribArray(vgl.vertexAttributeKeys.Position);
context.drawArrays(vgl.GL.TRIANGLE_STRIP, 0, 4);
quad.texture.undoBind(renderState);
});
context.bindBuffer(vgl.GL.ARRAY_BUFFER, null);
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
s_update.call(m_this);
if (m_this.buildTime().getMTime() <= m_this.dataTime().getMTime() ||
m_this.updateTime().getMTime() < m_this.getMTime()) {
m_this._build();
}
if (m_actor_color) {
m_actor_color.setVisible(m_this.visible());
m_actor_color.material().setBinNumber(m_this.bin());
}
if (m_actor_image) {
m_actor_image.setVisible(m_this.visible());
m_actor_image.material().setBinNumber(m_this.bin());
}
m_this.updateTime().modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function () {
s_init.call(m_this, arg);
};
////////////////////////////////////////////////////////////////////////////
/**
* Destroy
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
if (m_actor_image) {
m_this.renderer().contextRenderer().removeActor(m_actor_image);
m_actor_image = null;
}
if (m_actor_color) {
m_this.renderer().contextRenderer().removeActor(m_actor_color);
m_actor_color = null;
}
s_exit.call(m_this);
};
m_this._init(arg);
return this;
};
inherit(geo.gl.quadFeature, geo.quadFeature);
// Now register it
geo.registerFeature('vgl', 'quad', geo.gl.quadFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of polygonFeature
*
* @class
* @extends geo.polygonFeature
* @returns {geo.gl.polygonFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gl.polygonFeature = function (arg) {
'use strict';
if (!(this instanceof geo.gl.polygonFeature)) {
return new geo.gl.polygonFeature(arg);
}
arg = arg || {};
geo.polygonFeature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_exit = this._exit,
m_actor = vgl.actor(),
m_mapper = vgl.mapper(),
m_material = vgl.material(),
s_init = this._init,
s_update = this._update;
function createVertexShader() {
var vertexShaderSource = [
'attribute vec3 pos;',
'attribute vec3 fillColor;',
'attribute float fillOpacity;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'uniform float pixelWidth;',
'varying vec3 fillColorVar;',
'varying float fillOpacityVar;',
'void main(void)',
'{',
' vec4 clipPos = projectionMatrix * modelViewMatrix * vec4(pos.xyz, 1);',
' if (clipPos.w != 0.0) {',
' clipPos = clipPos/clipPos.w;',
' }',
' fillColorVar = fillColor;',
' fillOpacityVar = fillOpacity;',
' gl_Position = clipPos;',
'}'
].join('\n'),
shader = new vgl.shader(vgl.GL.VERTEX_SHADER);
shader.setShaderSource(vertexShaderSource);
return shader;
}
function createFragmentShader() {
var fragmentShaderSource = [
'#ifdef GL_ES',
' precision highp float;',
'#endif',
'varying vec3 fillColorVar;',
'varying float fillOpacityVar;',
'void main () {',
' gl_FragColor = vec4 (fillColorVar, fillOpacityVar);',
'}'
].join('\n'),
shader = new vgl.shader(vgl.GL.FRAGMENT_SHADER);
shader.setShaderSource(fragmentShaderSource);
return shader;
}
function createGLPolygons() {
var i = null,
numPts = null,
start = null,
itemIndex = 0,
polygonItemCoordIndex = 0,
position = [],
fillColor = [],
fillOpacity = [],
fillColorNew = [],
fillOpacityNew = [],
posFunc = null,
fillColorFunc = null,
polygonItem = null,
fillOpacityFunc = null,
buffers = vgl.DataBuffers(1024),
sourcePositions = vgl.sourceDataP3fv(),
sourceFillColor =
vgl.sourceDataAnyfv(3, vgl.vertexAttributeKeysIndexed.Two),
sourceFillOpacity =
vgl.sourceDataAnyfv(1, vgl.vertexAttributeKeysIndexed.Three),
trianglePrimitive = vgl.triangles(),
geom = vgl.geometryData(),
polygon = null,
holes = null,
extRing = null,
extIndex = 0,
extLength = null,
intIndex = 0,
posInstance = null,
triangulator = new PNLTRI.Triangulator(),
triangList = null,
newTriangList = null,
fillColorInstance = null;
posFunc = m_this.position();
fillColorFunc = m_this.style.get('fillColor');
fillOpacityFunc = m_this.style.get('fillOpacity');
m_this.data().forEach(function (item) {
polygon = m_this.polygon()(item, itemIndex);
polygonItem = polygon.outer || [];
holes = polygon.inner || [];
polygonItemCoordIndex = 0;
extRing = [];
extIndex = 0;
extLength = polygonItem.length - 1;
extRing[0] = [];
intIndex = 0;
polygonItem.forEach(function (extRingCoords) {
if (extIndex !== extLength) {
//extRing = extRing.concat(extRingCoords);
posInstance = posFunc(extRingCoords,
polygonItemCoordIndex,
item, itemIndex);
extRing[0].push({
x: posInstance.x, y: posInstance.y, i: fillColor.length
});
fillColorInstance = fillColorFunc(extRingCoords,
polygonItemCoordIndex,
item, itemIndex);
fillColor.push([fillColorInstance.r,
fillColorInstance.g,
fillColorInstance.b]);
fillOpacity.push(fillOpacityFunc(extRingCoords,
polygonItemCoordIndex,
item,
itemIndex));
polygonItemCoordIndex += 1;
}
extIndex += 1;
});
polygonItemCoordIndex = 0;
holes.forEach(function (hole) {
extRing[intIndex + 1] = [];
hole.forEach(function (intRingCoords) {
posInstance = posFunc(intRingCoords, polygonItemCoordIndex,
item, itemIndex);
extRing[intIndex + 1].push({
x: posInstance.x, y: posInstance.y, i: fillColor.length
});
fillColorInstance = fillColorFunc(intRingCoords,
polygonItemCoordIndex,
item, itemIndex);
fillColor.push([fillColorInstance.r,
fillColorInstance.g,
fillColorInstance.b]);
fillOpacity.push(fillOpacityFunc(intRingCoords,
polygonItemCoordIndex,
item, itemIndex));
polygonItemCoordIndex += 1;
});
intIndex += 1;
});
//console.log("extRing ", extRing);
//console.log("result", PolyK.Triangulate(extRing));
triangList = triangulator.triangulate_polygon(extRing);
newTriangList = [];
triangList.forEach(function (newIndices) {
Array.prototype.push.apply(newTriangList, newIndices);
});
for (i = 1; i < extRing.length; i += 1) {
extRing[0] = extRing[0].concat(extRing[i]);
}
newTriangList.forEach(function (polygonIndex) {
var polygonItemCoords = extRing[0][polygonIndex];
position.push([polygonItemCoords.x,
polygonItemCoords.y,
polygonItemCoords.z || 0.0]);
fillColorNew.push(fillColor[polygonItemCoords.i]);
fillOpacityNew.push(fillOpacity[polygonItemCoords.i]);
});
itemIndex += 1;
});
position = geo.transform.transformCoordinates(
m_this.gcs(), m_this.layer().map().gcs(),
position, 3);
buffers.create('pos', 3);
buffers.create('indices', 1);
buffers.create('fillColor', 3);
buffers.create('fillOpacity', 1);
numPts = position.length;
start = buffers.alloc(numPts);
//console.log("numPts ", numPts);
for (i = 0; i < numPts; i += 1) {
buffers.write('pos', position[i], start + i, 1);
buffers.write('indices', [i], start + i, 1);
buffers.write('fillColor', fillColorNew[i], start + i, 1);
buffers.write('fillOpacity', [fillOpacityNew[i]], start + i, 1);
}
//console.log(buffers.get('fillColor'));
sourcePositions.pushBack(buffers.get('pos'));
geom.addSource(sourcePositions);
sourceFillColor.pushBack(buffers.get('fillColor'));
geom.addSource(sourceFillColor);
sourceFillOpacity.pushBack(buffers.get('fillOpacity'));
geom.addSource(sourceFillOpacity);
//console.log(buffers.get('indices'));
trianglePrimitive.setIndices(buffers.get('indices'));
geom.addPrimitive(trianglePrimitive);
m_mapper.setGeometryData(geom);
}
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
var blend = vgl.blend(),
prog = vgl.shaderProgram(),
posAttr = vgl.vertexAttribute('pos'),
fillColorAttr = vgl.vertexAttribute('fillColor'),
fillOpacityAttr = vgl.vertexAttribute('fillOpacity'),
modelViewUniform = new vgl.modelViewUniform('modelViewMatrix'),
projectionUniform = new vgl.projectionUniform('projectionMatrix'),
vertexShader = createVertexShader(),
fragmentShader = createFragmentShader();
s_init.call(m_this, arg);
prog.addVertexAttribute(posAttr, vgl.vertexAttributeKeys.Position);
prog.addVertexAttribute(fillColorAttr, vgl.vertexAttributeKeysIndexed.Two);
prog.addVertexAttribute(fillOpacityAttr, vgl.vertexAttributeKeysIndexed.Three);
prog.addUniform(modelViewUniform);
prog.addUniform(projectionUniform);
prog.addShader(fragmentShader);
prog.addShader(vertexShader);
m_material.addAttribute(prog);
m_material.addAttribute(blend);
m_actor.setMapper(m_mapper);
m_actor.setMaterial(m_material);
};
////////////////////////////////////////////////////////////////////////////
/**
* Build
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
if (m_actor) {
m_this.renderer().contextRenderer().removeActor(m_actor);
}
createGLPolygons();
m_this.renderer().contextRenderer().addActor(m_actor);
m_this.buildTime().modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
s_update.call(m_this);
if (m_this.dataTime().getMTime() >= m_this.buildTime().getMTime() ||
m_this.updateTime().getMTime() <= m_this.getMTime()) {
m_this._build();
}
m_actor.setVisible(m_this.visible());
m_actor.material().setBinNumber(m_this.bin());
m_this.updateTime().modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Destroy
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.renderer().contextRenderer().removeActor(m_actor);
s_exit();
};
this._init(arg);
return this;
};
inherit(geo.gl.polygonFeature, geo.polygonFeature);
// Now register it
geo.registerFeature('vgl', 'polygon', geo.gl.polygonFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of contourFeature
*
* @class
* @extends geo.contourFeature
* @returns {geo.gl.contourFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gl.contourFeature = function (arg) {
'use strict';
if (!(this instanceof geo.gl.contourFeature)) {
return new geo.gl.contourFeature(arg);
}
arg = arg || {};
geo.contourFeature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_exit = this._exit,
m_textureUnit = 7,
m_actor = null,
m_mapper = null,
m_material = null,
m_texture = null,
m_minColorUniform = null,
m_maxColorUniform = null,
m_stepsUniform = null,
m_steppedUniform = null,
m_dynamicDraw = arg.dynamicDraw === undefined ? false : arg.dynamicDraw,
s_init = this._init,
s_update = this._update;
function createVertexShader() {
var vertexShaderSource = [
'#ifdef GL_ES',
' precision highp float;',
'#endif',
'attribute vec3 pos;',
'attribute float value;',
'attribute float opacity;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'varying float valueVar;',
'varying float opacityVar;',
'void main(void)',
'{',
/* Don't use z values; something is rotten in one of our matrices */
' vec4 scrPos = projectionMatrix * modelViewMatrix * vec4(pos.xy, 0, 1);',
' if (scrPos.w != 0.0) {',
' scrPos = scrPos / scrPos.w;',
' }',
' valueVar = value;',
' opacityVar = opacity;',
' gl_Position = scrPos;',
'}'
].join('\n'),
shader = new vgl.shader(vgl.GL.VERTEX_SHADER);
shader.setShaderSource(vertexShaderSource);
return shader;
}
function createFragmentShader() {
var fragmentShaderSource = [
'#ifdef GL_ES',
' precision highp float;',
'#endif',
'uniform vec4 minColor;',
'uniform vec4 maxColor;',
'uniform float steps;',
'uniform bool stepped;',
'uniform sampler2D sampler2d;',
'varying float valueVar;',
'varying float opacityVar;',
'void main () {',
' vec4 clr;',
' if (valueVar < 0.0) {',
' clr = minColor;',
' } else if (valueVar > steps) {',
' clr = maxColor;',
' } else {',
' float step;',
' if (stepped) {',
' step = floor(valueVar) + 0.5;',
' if (step > steps) {',
' step = steps - 0.5;',
' }',
' } else {',
' step = valueVar;',
' }',
' clr = texture2D(sampler2d, vec2(step / steps, 0.0));',
' }',
' gl_FragColor = vec4(clr.rgb, clr.a * opacityVar);',
'}'
].join('\n'),
shader = new vgl.shader(vgl.GL.FRAGMENT_SHADER);
shader.setShaderSource(fragmentShaderSource);
return shader;
}
/* Create the contours. This calls the base class to generate the geometry,
* color map, and other parameters. The generated geoemtry is then loaded
* into the various gl uniforms and buffers.
*/
function createGLContours() {
var contour = m_this.createContours(),
numPts = contour.elements.length,
colorTable = [],
i, i3, j, j3,
posBuf, opacityBuf, valueBuf, indicesBuf,
geom = m_mapper.geometryData();
m_minColorUniform.set([contour.minColor.r, contour.minColor.g,
contour.minColor.b, contour.minColor.a]);
m_maxColorUniform.set([contour.maxColor.r, contour.maxColor.g,
contour.maxColor.b, contour.maxColor.a]);
m_stepsUniform.set(contour.colorMap.length);
m_steppedUniform.set(contour.stepped);
for (i = 0; i < contour.colorMap.length; i += 1) {
colorTable.push(contour.colorMap[i].r * 255);
colorTable.push(contour.colorMap[i].g * 255);
colorTable.push(contour.colorMap[i].b * 255);
colorTable.push(contour.colorMap[i].a * 255);
}
m_texture.setColorTable(colorTable);
contour.pos = geo.transform.transformCoordinates(
m_this.gcs(), m_this.layer().map().gcs(), contour.pos, 3);
posBuf = geo.util.getGeomBuffer(geom, 'pos', numPts * 3);
opacityBuf = geo.util.getGeomBuffer(geom, 'opacity', numPts);
valueBuf = geo.util.getGeomBuffer(geom, 'value', numPts);
for (i = i3 = 0; i < numPts; i += 1, i3 += 3) {
j = contour.elements[i];
j3 = j * 3;
posBuf[i3] = contour.pos[j3];
posBuf[i3 + 1] = contour.pos[j3 + 1];
posBuf[i3 + 2] = contour.pos[j3 + 2];
opacityBuf[i] = contour.opacity[j];
valueBuf[i] = contour.value[j];
}
indicesBuf = geom.primitive(0).indices();
if (!(indicesBuf instanceof Uint16Array) || indicesBuf.length !== numPts) {
indicesBuf = new Uint16Array(numPts);
geom.primitive(0).setIndices(indicesBuf);
}
geom.boundsDirty(true);
m_mapper.modified();
m_mapper.boundsDirtyTimestamp().modified();
}
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
var blend = vgl.blend(),
prog = vgl.shaderProgram(),
mat = vgl.material(),
tex = vgl.lookupTable(),
geom = vgl.geometryData(),
modelViewUniform = new vgl.modelViewUniform('modelViewMatrix'),
projectionUniform = new vgl.projectionUniform('projectionMatrix'),
samplerUniform = new vgl.uniform(vgl.GL.INT, 'sampler2d'),
vertexShader = createVertexShader(),
fragmentShader = createFragmentShader(),
posAttr = vgl.vertexAttribute('pos'),
valueAttr = vgl.vertexAttribute('value'),
opacityAttr = vgl.vertexAttribute('opacity'),
sourcePositions = vgl.sourceDataP3fv({'name': 'pos'}),
sourceValues = vgl.sourceDataAnyfv(
1, vgl.vertexAttributeKeysIndexed.One, {'name': 'value'}),
sourceOpacity = vgl.sourceDataAnyfv(
1, vgl.vertexAttributeKeysIndexed.Two, {'name': 'opacity'}),
primitive = new vgl.triangles();
s_init.call(m_this, arg);
m_mapper = vgl.mapper({dynamicDraw: m_dynamicDraw});
prog.addVertexAttribute(posAttr, vgl.vertexAttributeKeys.Position);
prog.addVertexAttribute(valueAttr, vgl.vertexAttributeKeysIndexed.One);
prog.addVertexAttribute(opacityAttr, vgl.vertexAttributeKeysIndexed.Two);
prog.addUniform(modelViewUniform);
prog.addUniform(projectionUniform);
m_minColorUniform = new vgl.uniform(vgl.GL.FLOAT_VEC4, 'minColor');
prog.addUniform(m_minColorUniform);
m_maxColorUniform = new vgl.uniform(vgl.GL.FLOAT_VEC4, 'maxColor');
prog.addUniform(m_maxColorUniform);
/* steps is always an integer, but it is more efficient if we use a float
*/
m_stepsUniform = new vgl.uniform(vgl.GL.FLOAT, 'steps');
prog.addUniform(m_stepsUniform);
m_steppedUniform = new vgl.uniform(vgl.GL.BOOL, 'stepped');
prog.addUniform(m_steppedUniform);
prog.addShader(fragmentShader);
prog.addShader(vertexShader);
prog.addUniform(samplerUniform);
tex.setTextureUnit(m_textureUnit);
samplerUniform.set(m_textureUnit);
m_material = mat;
m_material.addAttribute(prog);
m_material.addAttribute(blend);
m_texture = tex;
m_material.addAttribute(m_texture);
m_actor = vgl.actor();
m_actor.setMaterial(m_material);
m_actor.setMapper(m_mapper);
geom.addSource(sourcePositions);
geom.addSource(sourceValues);
geom.addSource(sourceOpacity);
geom.addPrimitive(primitive);
m_mapper.setGeometryData(geom);
};
////////////////////////////////////////////////////////////////////////////
/**
* Build
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
if (m_actor) {
m_this.renderer().contextRenderer().removeActor(m_actor);
}
createGLContours();
m_this.renderer().contextRenderer().addActor(m_actor);
m_this.buildTime().modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
s_update.call(m_this);
if (m_this.dataTime().getMTime() >= m_this.buildTime().getMTime() ||
m_this.updateTime().getMTime() <= m_this.getMTime()) {
m_this._build();
}
m_actor.setVisible(m_this.visible());
m_actor.material().setBinNumber(m_this.bin());
m_this.updateTime().modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Destroy
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.renderer().contextRenderer().removeActor(m_actor);
s_exit();
};
this._init(arg);
return this;
};
inherit(geo.gl.contourFeature, geo.contourFeature);
// Now register it
geo.registerFeature('vgl', 'contour', geo.gl.contourFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class vglRenderer
*
* @class
* @extends geo.renderer
* @param canvas
* @returns {geo.gl.vglRenderer}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gl.vglRenderer = function (arg) {
'use strict';
if (!(this instanceof geo.gl.vglRenderer)) {
return new geo.gl.vglRenderer(arg);
}
arg = arg || {};
geo.renderer.call(this, arg);
var m_this = this,
m_contextRenderer = null,
m_viewer = null,
m_width = 0,
m_height = 0,
m_renderAnimFrameRef = null,
s_init = this._init,
s_exit = this._exit;
/// TODO: Move this API to the base class
////////////////////////////////////////////////////////////////////////////
/**
* Return width of the renderer
*/
////////////////////////////////////////////////////////////////////////////
this.width = function () {
return m_width;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return height of the renderer
*/
////////////////////////////////////////////////////////////////////////////
this.height = function () {
return m_height;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get context specific renderer
*/
////////////////////////////////////////////////////////////////////////////
this.contextRenderer = function () {
return m_contextRenderer;
};
////////////////////////////////////////////////////////////////////////////
/**
* Get API used by the renderer
*/
////////////////////////////////////////////////////////////////////////////
this.api = function () {
return 'vgl';
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function () {
if (m_this.initialized()) {
return m_this;
}
s_init.call(m_this);
var canvas = $(document.createElement('canvas'));
canvas.attr('class', 'webgl-canvas');
$(m_this.layer().node().get(0)).append(canvas);
m_viewer = vgl.viewer(canvas.get(0), arg.options);
m_viewer.init();
m_contextRenderer = m_viewer.renderWindow().activeRenderer();
m_contextRenderer.setResetScene(false);
if (m_viewer.renderWindow().renderers().length > 0) {
m_contextRenderer.setLayer(m_viewer.renderWindow().renderers().length);
}
m_this.canvas(canvas);
/* Initialize the size of the renderer */
var map = m_this.layer().map(),
mapSize = map.size();
m_this._resize(0, 0, mapSize.width, mapSize.height);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle resize event
*/
////////////////////////////////////////////////////////////////////////////
this._resize = function (x, y, w, h) {
var renderWindow = m_viewer.renderWindow();
m_width = w;
m_height = h;
m_this.canvas().attr('width', w);
m_this.canvas().attr('height', h);
renderWindow.positionAndResize(x, y, w, h);
m_this._updateRendererCamera();
m_this._render();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Render
*/
////////////////////////////////////////////////////////////////////////////
this._render = function () {
if (m_renderAnimFrameRef === null) {
m_renderAnimFrameRef = window.requestAnimationFrame(function () {
m_renderAnimFrameRef = null;
m_viewer.render();
});
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Exit
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.canvas().remove();
m_viewer.exit();
s_exit();
};
this._updateRendererCamera = function () {
var renderWindow = m_viewer.renderWindow(),
map = m_this.layer().map(),
camera = map.camera(),
rotation = map.rotation() || 0,
view = camera.view,
proj = camera.projectionMatrix;
if (proj[15]) {
/* we want positive z to be closer to the camera, but webGL does the
* converse, so reverse the z coordinates. */
proj = mat4.scale(geo.util.mat4AsArray(), proj, [1, 1, -1]);
}
/* A similar kluge as in the base camera class worldToDisplay4. With this,
* we can show z values from 0 to 1. */
proj = mat4.translate(geo.util.mat4AsArray(), proj,
[0, 0, camera.constructor.bounds.far]);
/* Check if the rotation is a multiple of 90 */
var basis = Math.PI / 2,
angle = rotation % basis, // move to range (-pi/2, pi/2)
ortho = (Math.min(Math.abs(angle), Math.abs(angle - basis)) < 0.00001);
renderWindow.renderers().forEach(function (renderer) {
var cam = renderer.camera();
if (geo.util.compareArrays(view, cam.viewMatrix()) &&
geo.util.compareArrays(proj, cam.projectionMatrix())) {
return;
}
cam.setViewMatrix(view, true);
cam.setProjectionMatrix(proj);
if (proj[1] || proj[2] || proj[3] || proj[4] || proj[6] || proj[7] ||
proj[8] || proj[9] || proj[11] || proj[15] !== 1 || !ortho ||
(parseFloat(map.zoom().toFixed(6)) !==
parseFloat(map.zoom().toFixed(0)))) {
/* Don't align texels */
cam.viewAlignment = function () {
return null;
};
} else {
/* Set information for texel alignment. The rounding factors should
* probably be divided by window.devicePixelRatio. */
cam.viewAlignment = function () {
var align = {
roundx: 2.0 / camera.viewport.width,
roundy: 2.0 / camera.viewport.height
};
align.dx = (camera.viewport.width % 2) ? align.roundx * 0.5 : 0;
align.dy = (camera.viewport.height % 2) ? align.roundy * 0.5 : 0;
return align;
};
}
});
};
// Connect to interactor events
// Connect to pan event
m_this.layer().geoOn(geo.event.pan, function (evt) {
void (evt);
m_this._updateRendererCamera();
});
// Connect to zoom event
m_this.layer().geoOn(geo.event.zoom, function (evt) {
void (evt);
m_this._updateRendererCamera();
});
// Connect to rotation event
m_this.layer().geoOn(geo.event.rotate, function (evt) {
void (evt);
m_this._updateRendererCamera();
});
// Connect to parallelprojection event
m_this.layer().geoOn(geo.event.parallelprojection, function (evt) {
var vglRenderer = m_this.contextRenderer(),
camera,
layer = m_this.layer();
if (evt.geo && evt.geo._triggeredBy !== layer) {
if (!vglRenderer || !vglRenderer.camera()) {
console.log('Parallel projection event triggered on unconnected VGL ' +
'renderer.');
}
camera = vglRenderer.camera();
camera.setEnableParallelProjection(evt.parallelProjection);
m_this._updateRendererCamera();
}
});
return this;
};
inherit(geo.gl.vglRenderer, geo.renderer);
geo.registerRenderer('vgl', geo.gl.vglRenderer);
(function () {
'use strict';
var checkedWebGL;
/**
* Report if the vgl renderer is supported. This is just a check if webGL is
* supported and available.
*
* @returns {boolean} true if available.
*/
geo.gl.vglRenderer.supported = function () {
if (checkedWebGL === undefined) {
/* This is extracted from what Modernizr uses. */
var canvas, ctx, exts; // eslint-disable-line no-unused-vars
try {
canvas = document.createElement('canvas');
ctx = (canvas.getContext('webgl') ||
canvas.getContext('experimental-webgl'));
exts = ctx.getSupportedExtensions();
checkedWebGL = true;
} catch (e) {
console.error('No webGL support');
checkedWebGL = false;
}
canvas = undefined;
ctx = undefined;
exts = undefined;
}
return checkedWebGL;
};
/**
* If the vgl renderer is not supported, supply the name of a renderer that
* should be used instead. This asks for the null renderer.
*
* @returns null for the null renderer.
*/
geo.gl.vglRenderer.fallback = function () {
return null;
};
})();
geo.gl.tileLayer = function () {
'use strict';
var m_this = this,
s_init = this._init,
s_exit = this._exit,
m_quadFeature,
m_nextTileId = 0,
m_tiles = [];
/* Add a tile to the list of quads */
this._drawTile = function (tile) {
if (!m_quadFeature) {
return;
}
var bounds = this._tileBounds(tile),
level = tile.index.level || 0,
to = this._tileOffset(level),
quad = {};
quad.ul = this.fromLocal(this.fromLevel({
x: bounds.left - to.x, y: bounds.top - to.y
}, level), 0);
quad.ll = this.fromLocal(this.fromLevel({
x: bounds.left - to.x, y: bounds.bottom - to.y
}, level), 0);
quad.ur = this.fromLocal(this.fromLevel({
x: bounds.right - to.x, y: bounds.top - to.y
}, level), 0);
quad.lr = this.fromLocal(this.fromLevel({
x: bounds.right - to.x, y: bounds.bottom - to.y
}, level), 0);
quad.ul.z = quad.ll.z = quad.ur.z = quad.lr.z = level * 1e-5;
m_nextTileId += 1;
quad.id = m_nextTileId;
tile.quadId = quad.id;
quad.image = tile.image;
m_tiles.push(quad);
m_quadFeature.data(m_tiles);
m_quadFeature._update();
m_this.draw();
};
/* Remove the tile feature. */
this._remove = function (tile) {
if (tile.quadId !== undefined && m_quadFeature) {
for (var i = 0; i < m_tiles.length; i += 1) {
if (m_tiles[i].id === tile.quadId) {
m_tiles.splice(i, 1);
break;
}
}
m_quadFeature.data(m_tiles);
m_quadFeature._update();
m_this.draw();
}
};
/**
* Clean up the layer.
*/
this._exit = function () {
m_this.deleteFeature(m_quadFeature);
m_quadFeature = null;
m_tiles = [];
s_exit.apply(m_this, arguments);
};
/**
* Initialize after the layer is added to the map.
*/
this._init = function () {
s_init.apply(m_this, arguments);
m_quadFeature = this.createFeature('quad', {
previewColor: m_this._options.previewColor,
previewImage: m_this._options.previewImage
});
m_quadFeature.geoTrigger = undefined;
m_quadFeature.gcs(m_this._options.gcs || m_this.map().gcs());
m_quadFeature.data(m_tiles);
m_quadFeature._update();
};
/* These functions don't need to do anything. */
this._getSubLayer = function () {};
this._updateSubLayer = undefined;
};
geo.registerLayerAdjustment('vgl', 'tile', geo.gl.tileLayer);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of choroplethFeature
*
* @class
* @extends geo.choroplethFeature
* @returns {geo.gl.choroplethFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gl.choroplethFeature = function (arg) {
'use strict';
if (!(this instanceof geo.gl.choroplethFeature)) {
return new geo.gl.choroplethFeature(arg);
}
arg = arg || {};
geo.choroplethFeature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
m_gl_polygons = null,
s_exit = this._exit,
s_init = this._init,
s_update = this._update;
/* Create the choropleth. This calls the base class to generate the contours,
* into the various gl uniforms and buffers.
*/
function createGLChoropleth() {
return m_this.createChoropleth();
}
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
};
////////////////////////////////////////////////////////////////////////////
/**
* Build
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
m_this.buildTime().modified();
return (m_gl_polygons = createGLChoropleth());
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
s_update.call(m_this);
if (m_this.dataTime().getMTime() >= m_this.buildTime().getMTime() ||
m_this.updateTime().getMTime() <= m_this.getMTime()) {
m_this._wipePolygons();
m_this._build();
}
m_this.updateTime().modified();
};
////////////////////////////////////////////////////////////////////////////
/**
* Destroy Polygon Sub-Features
*/
////////////////////////////////////////////////////////////////////////////
this._wipePolygons = function () {
if (m_gl_polygons) {
m_gl_polygons.map(function (polygon) {
return polygon._exit();
});
}
m_gl_polygons = null;
};
////////////////////////////////////////////////////////////////////////////
/**
* Destroy
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this._wipePolygons();
s_exit();
};
this._init(arg);
return this;
};
inherit(geo.gl.choroplethFeature, geo.choroplethFeature);
// Now register it
geo.registerFeature('vgl', 'choropleth', geo.gl.choroplethFeature);
/** @namespace */
geo.d3 = {};
(function () {
'use strict';
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz',
strLength = 8;
//////////////////////////////////////////////////////////////////////////////
/**
* Get a random string to use as a div ID
* @returns {string}
*/
//////////////////////////////////////////////////////////////////////////////
geo.d3.uniqueID = function () {
var strArray = [],
i;
strArray.length = strLength;
for (i = 0; i < strLength; i += 1) {
strArray[i] = chars.charAt(Math.floor(Math.random() * chars.length));
}
return strArray.join('');
};
// event propagated when the d3 renderer rescales its group element
geo.event.d3Rescale = 'geo_d3_rescale';
}());
//////////////////////////////////////////////////////////////////////////////
/**
* D3 specific subclass of object which adds an id property for d3 selections
* on groups of objects by class id.
* @class
* @extends geo.sceneObject
*/
//////////////////////////////////////////////////////////////////////////////
geo.d3.object = function (arg) {
'use strict';
// this is used to extend other geojs classes, so only generate
// a new object when that is not the case... like if this === window
if (!(this instanceof geo.object)) {
return new geo.d3.object(arg);
}
geo.sceneObject.call(this);
var m_id = 'd3-' + geo.d3.uniqueID(),
s_exit = this._exit,
m_this = this,
s_draw = this.draw;
this._d3id = function () {
return m_id;
};
////////////////////////////////////////////////////////////////////////////
/**
* Returns a d3 selection for the feature elements
*/
////////////////////////////////////////////////////////////////////////////
this.select = function () {
return m_this.renderer().select(m_this._d3id());
};
////////////////////////////////////////////////////////////////////////////
/**
* Redraw the object.
*/
////////////////////////////////////////////////////////////////////////////
this.draw = function () {
m_this._update();
s_draw();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Removes the element from the svg and the renderer
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.renderer()._removeFeature(m_this._d3id());
s_exit();
};
return this;
};
inherit(geo.d3.object, geo.sceneObject);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class d3Renderer
*
* @class
* @extends geo.renderer
* @returns {geo.d3.d3Renderer}
*/
//////////////////////////////////////////////////////////////////////////////
geo.d3.d3Renderer = function (arg) {
'use strict';
if (!(this instanceof geo.d3.d3Renderer)) {
return new geo.d3.d3Renderer(arg);
}
geo.renderer.call(this, arg);
var s_exit = this._exit;
geo.d3.object.call(this, arg);
arg = arg || {};
var m_this = this,
m_sticky = null,
m_features = {},
m_corners = null,
m_width = null,
m_height = null,
m_diagonal = null,
m_scale = 1,
m_transform = {dx: 0, dy: 0, rx: 0, ry: 0, rotation: 0},
m_svg = null,
m_defs = null;
////////////////////////////////////////////////////////////////////////////
/**
* Set attributes to a d3 selection.
* @private
*/
////////////////////////////////////////////////////////////////////////////
function setAttrs(select, attrs) {
var key;
for (key in attrs) {
if (attrs.hasOwnProperty(key)) {
select.attr(key, attrs[key]);
}
}
}
////////////////////////////////////////////////////////////////////////////
/**
* Meta functions for converting from geojs styles to d3.
* @private
*/
////////////////////////////////////////////////////////////////////////////
this._convertColor = function (f, g) {
f = geo.util.ensureFunction(f);
g = g || function () { return true; };
return function () {
var c = 'none';
if (g.apply(m_this, arguments)) {
c = f.apply(m_this, arguments);
if (c.hasOwnProperty('r') &&
c.hasOwnProperty('g') &&
c.hasOwnProperty('b')) {
c = d3.rgb(255 * c.r, 255 * c.g, 255 * c.b);
}
}
return c;
};
};
this._convertPosition = function (f) {
f = geo.util.ensureFunction(f);
return function () {
return m_this.layer().map().worldToDisplay(f.apply(m_this, arguments));
};
};
this._convertScale = function (f) {
f = geo.util.ensureFunction(f);
return function () {
return f.apply(m_this, arguments) / m_scale;
};
};
////////////////////////////////////////////////////////////////////////////
/**
* Set styles to a d3 selection. Ignores unkown style keys.
* @private
*/
////////////////////////////////////////////////////////////////////////////
function setStyles(select, styles) {
var key, k, f;
function fillFunc() {
if (styles.fill.apply(m_this, arguments)) {
return null;
} else {
return 'none';
}
}
function strokeFunc() {
if (styles.stroke.apply(m_this, arguments)) {
return null;
} else {
return 'none';
}
}
for (key in styles) {
if (styles.hasOwnProperty(key)) {
f = null;
k = null;
if (key === 'strokeColor') {
k = 'stroke';
f = m_this._convertColor(styles[key], styles.stroke);
} else if (key === 'stroke' && styles[key] &&
!styles.hasOwnProperty('strokeColor')) {
k = 'stroke';
f = strokeFunc;
} else if (key === 'strokeWidth') {
k = 'stroke-width';
f = m_this._convertScale(styles[key]);
} else if (key === 'strokeOpacity') {
k = 'stroke-opacity';
f = styles[key];
} else if (key === 'fillColor') {
k = 'fill';
f = m_this._convertColor(styles[key], styles.fill);
} else if (key === 'fill' && !styles.hasOwnProperty('fillColor')) {
k = 'fill';
f = fillFunc;
} else if (key === 'fillOpacity') {
k = 'fill-opacity';
f = styles[key];
}
if (k) {
select.style(k, f);
}
}
}
}
////////////////////////////////////////////////////////////////////////////
/**
* Get the svg group element associated with this renderer instance, or of a
* group within the render instance.
*
* @private
*/
////////////////////////////////////////////////////////////////////////////
function getGroup(parentId) {
if (parentId) {
return m_svg.select('.group-' + parentId);
}
return m_svg.select('.group-' + m_this._d3id());
}
////////////////////////////////////////////////////////////////////////////
/**
* Set the initial lat-lon coordinates of the map view.
* @private
*/
////////////////////////////////////////////////////////////////////////////
function initCorners() {
var layer = m_this.layer(),
map = layer.map(),
width = map.size().width,
height = map.size().height;
m_width = width;
m_height = height;
if (!m_width || !m_height) {
throw 'Map layer has size 0';
}
m_diagonal = Math.pow(width * width + height * height, 0.5);
m_corners = {
upperLeft: map.displayToGcs({'x': 0, 'y': 0}, null),
lowerRight: map.displayToGcs({'x': width, 'y': height}, null),
center: map.displayToGcs({'x': width / 2, 'y': height / 2}, null)
};
}
////////////////////////////////////////////////////////////////////////////
/**
* Set the translation, scale, and zoom for the current view.
* @note rotation not yet supported
* @private
*/
////////////////////////////////////////////////////////////////////////////
this._setTransform = function () {
if (!m_corners) {
initCorners();
}
if (!m_sticky) {
return;
}
var layer = m_this.layer(),
map = layer.map(),
upperLeft = map.gcsToDisplay(m_corners.upperLeft, null),
lowerRight = map.gcsToDisplay(m_corners.lowerRight, null),
center = map.gcsToDisplay(m_corners.center, null),
group = getGroup(),
canvas = m_this.canvas(),
dx, dy, scale, rotation, rx, ry;
if (canvas.attr('scale') !== null) {
scale = parseFloat(canvas.attr('scale') || 1);
rx = (parseFloat(canvas.attr('dx') || 0) +
parseFloat(canvas.attr('offsetx') || 0));
ry = (parseFloat(canvas.attr('dy') || 0) +
parseFloat(canvas.attr('offsety') || 0));
rotation = parseFloat(canvas.attr('rotation') || 0);
dx = scale * rx + map.size().width / 2;
dy = scale * ry + map.size().height / 2;
} else {
scale = Math.sqrt(
Math.pow(lowerRight.y - upperLeft.y, 2) +
Math.pow(lowerRight.x - upperLeft.x, 2)) / m_diagonal;
// calculate the translation
rotation = map.rotation();
rx = -m_width / 2;
ry = -m_height / 2;
dx = scale * rx + center.x;
dy = scale * ry + center.y;
}
// set the group transform property
var transform = 'matrix(' + [scale, 0, 0, scale, dx, dy].join() + ')';
if (rotation) {
transform += ' rotate(' + [
rotation * 180 / Math.PI, -rx, -ry].join() + ')';
}
group.attr('transform', transform);
// set internal variables
m_scale = scale;
m_transform.dx = dx;
m_transform.dy = dy;
m_transform.rx = rx;
m_transform.ry = ry;
m_transform.rotation = rotation;
};
////////////////////////////////////////////////////////////////////////////
/**
* Convert from screen pixel coordinates to the local coordinate system
* in the SVG group element taking into account the transform.
* @private
*/
////////////////////////////////////////////////////////////////////////////
this.baseToLocal = function (pt) {
pt = {
x: (pt.x - m_transform.dx) / m_scale,
y: (pt.y - m_transform.dy) / m_scale
};
if (m_transform.rotation) {
var sinr = Math.sin(-m_transform.rotation),
cosr = Math.cos(-m_transform.rotation);
var x = pt.x + m_transform.rx, y = pt.y + m_transform.ry;
pt = {
x: x * cosr - y * sinr - m_transform.rx,
y: x * sinr + y * cosr - m_transform.ry
};
}
return pt;
};
////////////////////////////////////////////////////////////////////////////
/**
* Convert from the local coordinate system in the SVG group element
* to screen pixel coordinates.
* @private
*/
////////////////////////////////////////////////////////////////////////////
this.localToBase = function (pt) {
if (m_transform.rotation) {
var sinr = Math.sin(m_transform.rotation),
cosr = Math.cos(m_transform.rotation);
var x = pt.x + m_transform.rx, y = pt.y + m_transform.ry;
pt = {
x: x * cosr - y * sinr - m_transform.rx,
y: x * sinr + y * cosr - m_transform.ry
};
}
pt = {
x: pt.x * m_scale + m_transform.dx,
y: pt.y * m_scale + m_transform.dy
};
return pt;
};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
if (!m_this.canvas()) {
var canvas;
arg.widget = arg.widget || false;
if ('d3Parent' in arg) {
m_svg = d3.select(arg.d3Parent).append('svg');
} else {
m_svg = d3.select(m_this.layer().node().get(0)).append('svg');
}
// create a global svg definitions element
m_defs = m_svg.append('defs');
var shadow = m_defs
.append('filter')
.attr('id', 'geo-highlight')
.attr('x', '-100%')
.attr('y', '-100%')
.attr('width', '300%')
.attr('height', '300%');
shadow
.append('feMorphology')
.attr('operator', 'dilate')
.attr('radius', 2)
.attr('in', 'SourceAlpha')
.attr('result', 'dilateOut');
shadow
.append('feGaussianBlur')
.attr('stdDeviation', 5)
.attr('in', 'dilateOut')
.attr('result', 'blurOut');
shadow
.append('feColorMatrix')
.attr('type', 'matrix')
.attr('values', '-1 0 0 0 1 0 -1 0 0 1 0 0 -1 0 1 0 0 0 1 0')
.attr('in', 'blurOut')
.attr('result', 'invertOut');
shadow
.append('feBlend')
.attr('in', 'SourceGraphic')
.attr('in2', 'invertOut')
.attr('mode', 'normal');
if (!arg.widget) {
canvas = m_svg.append('g');
}
shadow = m_defs.append('filter')
.attr('id', 'geo-blur')
.attr('x', '-100%')
.attr('y', '-100%')
.attr('width', '300%')
.attr('height', '300%');
shadow
.append('feGaussianBlur')
.attr('stdDeviation', 20)
.attr('in', 'SourceGraphic');
m_sticky = m_this.layer().sticky();
m_svg.attr('class', m_this._d3id());
m_svg.attr('width', m_this.layer().node().width());
m_svg.attr('height', m_this.layer().node().height());
if (!arg.widget) {
canvas.attr('class', 'group-' + m_this._d3id());
m_this.canvas(canvas);
} else {
m_this.canvas(m_svg);
}
}
m_this._setTransform();
};
////////////////////////////////////////////////////////////////////////////
/**
* Get API used by the renderer
*/
////////////////////////////////////////////////////////////////////////////
this.api = function () {
return 'd3';
};
////////////////////////////////////////////////////////////////////////////
/**
* Return the current scaling factor to build features that shouldn't
* change size during zooms. For example:
*
* selection.append('circle')
* .attr('r', r0 / renderer.scaleFactor());
*
* This will create a circle element with radius r0 independent of the
* current zoom level.
*/
////////////////////////////////////////////////////////////////////////////
this.scaleFactor = function () {
return m_scale;
};
////////////////////////////////////////////////////////////////////////////
/**
* Handle resize event
*/
////////////////////////////////////////////////////////////////////////////
this._resize = function (x, y, w, h) {
if (!m_corners) {
initCorners();
}
m_svg.attr('width', w);
m_svg.attr('height', h);
m_this._setTransform();
m_this.layer().geoTrigger(geo.event.d3Rescale, { scale: m_scale }, true);
};
////////////////////////////////////////////////////////////////////////////
/**
* Update noop for geo.d3.object api.
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
};
////////////////////////////////////////////////////////////////////////////
/**
* Exit
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_features = {};
m_this.canvas().remove();
m_svg.remove();
m_svg = undefined;
m_defs.remove();
m_defs = undefined;
s_exit();
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the definitions dom element for the layer
* @protected
*/
////////////////////////////////////////////////////////////////////////////
this._definitions = function () {
return m_defs;
};
////////////////////////////////////////////////////////////////////////////
/**
* Create a new feature element from an object that describes the feature
* attributes. To be called from feature classes only.
*
* Input:
* {
* id: A unique string identifying the feature.
* data: Array of data objects used in a d3 data method.
* index: A function that returns a unique id for each data element.
* style: An object containing element CSS styles.
* attributes: An object containing element attributes.
* classes: An array of classes to add to the elements.
* append: The element type as used in d3 append methods.
* parentId: If set, the group ID of the parent element.
* }
*/
////////////////////////////////////////////////////////////////////////////
this._drawFeatures = function (arg) {
m_features[arg.id] = {
data: arg.data,
index: arg.dataIndex,
style: arg.style,
attributes: arg.attributes,
classes: arg.classes,
append: arg.append,
parentId: arg.parentId
};
return m_this.__render(arg.id, arg.parentId);
};
////////////////////////////////////////////////////////////////////////////
/**
* Updates a feature by performing a d3 data join. If no input id is
* provided then this method will update all features.
*/
////////////////////////////////////////////////////////////////////////////
this.__render = function (id, parentId) {
var key;
if (id === undefined) {
for (key in m_features) {
if (m_features.hasOwnProperty(key)) {
m_this.__render(key);
}
}
return m_this;
}
var data = m_features[id].data,
index = m_features[id].index,
style = m_features[id].style,
attributes = m_features[id].attributes,
classes = m_features[id].classes,
append = m_features[id].append,
selection = m_this.select(id, parentId).data(data, index);
selection.enter().append(append);
selection.exit().remove();
setAttrs(selection, attributes);
selection.attr('class', classes.concat([id]).join(' '));
setStyles(selection, style);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Returns a d3 selection for the given feature id.
*/
////////////////////////////////////////////////////////////////////////////
this.select = function (id, parentId) {
return getGroup(parentId).selectAll('.' + id);
};
////////////////////////////////////////////////////////////////////////////
/**
* Removes a feature from the layer.
*/
////////////////////////////////////////////////////////////////////////////
this._removeFeature = function (id) {
m_this.select(id).remove();
delete m_features[id];
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Override draw method to do nothing.
*/
////////////////////////////////////////////////////////////////////////////
this.draw = function () {
};
// connect to pan event
this.layer().geoOn(geo.event.pan, m_this._setTransform);
// connect to rotate event
this.layer().geoOn(geo.event.rotate, m_this._setTransform);
// connect to zoom event
this.layer().geoOn(geo.event.zoom, function () {
m_this._setTransform();
m_this.__render();
m_this.layer().geoTrigger(geo.event.d3Rescale, { scale: m_scale }, true);
});
this.layer().geoOn(geo.event.resize, function (event) {
m_this._resize(event.x, event.y, event.width, event.height);
});
this._init(arg);
return this;
};
inherit(geo.d3.d3Renderer, geo.renderer);
geo.registerRenderer('d3', geo.d3.d3Renderer);
(function () {
'use strict';
/**
* Report if the d3 renderer is supported. This is just a check if d3 is
* available.
*
* @returns {boolean} true if available.
*/
geo.d3.d3Renderer.supported = function () {
return (typeof d3 !== 'undefined');
};
/**
* If the d3 renderer is not supported, supply the name of a renderer that
* should be used instead. This asks for the null renderer.
*
* @returns null for the null renderer.
*/
geo.d3.d3Renderer.fallback = function () {
return null;
};
})();
geo.d3.tileLayer = function () {
'use strict';
var m_this = this,
s_update = this._update,
s_init = this._init;
this._drawTile = function (tile) {
var bounds = m_this._tileBounds(tile),
parentNode = m_this._getSubLayer(tile.index.level),
offsetx = parseInt(parentNode.attr('offsetx') || 0, 10),
offsety = parseInt(parentNode.attr('offsety') || 0, 10);
tile.feature = m_this.createFeature(
'plane', {drawOnAsyncResourceLoad: true})
.origin([bounds.left - offsetx, bounds.top - offsety])
.upperLeft([bounds.left - offsetx, bounds.top - offsety])
.lowerRight([bounds.right - offsetx, bounds.bottom - offsety])
.style({
image: tile._url,
opacity: 1,
reference: tile.toString(),
parentId: parentNode.attr('data-tile-layer-id')
});
/* Don't respond to geo events */
tile.feature.geoTrigger = undefined;
tile.feature._update();
m_this.draw();
};
/**
* Return the DOM element containing a level specific
* layer. This will create the element if it doesn't
* already exist.
* @param {number} level The zoom level of the layer to fetch
* @return {DOM}
*/
this._getSubLayer = function (level) {
var node = m_this.canvas().select(
'g[data-tile-layer="' + level.toFixed() + '"]');
if (node.empty()) {
node = m_this.canvas().append('g');
var id = geo.d3.uniqueID();
node.classed('group-' + id, true);
node.classed('geo-tile-layer', true);
node.attr('data-tile-layer', level.toFixed());
node.attr('data-tile-layer-id', id);
}
return node;
};
/**
* Set sublayer transforms to align them with the given zoom level.
* @param {number} level The target zoom level
* @param {object} view The view bounds. The top and left are used to
* adjust the offset of tile layers.
* @return {object} the x and y offsets for the current level.
*/
this._updateSubLayers = function (level, view) {
var canvas = m_this.canvas(),
lastlevel = parseInt(canvas.attr('lastlevel'), 10),
lastx = parseInt(canvas.attr('lastoffsetx') || 0, 10),
lasty = parseInt(canvas.attr('lastoffsety') || 0, 10);
if (lastlevel === level && Math.abs(lastx - view.left) < 65536 &&
Math.abs(lasty - view.top) < 65536) {
return {x: lastx, y: lasty};
}
var to = this._tileOffset(level),
x = parseInt(view.left, 10) + to.x,
y = parseInt(view.top, 10) + to.y;
var tileCache = m_this.cache._cache;
$.each(canvas.selectAll('.geo-tile-layer')[0], function (idx, el) {
var layer = parseInt($(el).attr('data-tile-layer'), 10),
scale = Math.pow(2, level - layer);
el = m_this._getSubLayer(layer);
el.attr('transform', 'matrix(' + [scale, 0, 0, scale, 0, 0].join() + ')');
/* x and y are the upper left of our view. This is the zero-point for
* offsets at the current level. Other tile layers' offsets are scaled
* by appropriate factors of 2. We need to shift the tiles of each
* layer by the appropriate amount (computed as dx and dy). */
var layerx = parseInt(x / Math.pow(2, level - layer), 10),
layery = parseInt(y / Math.pow(2, level - layer), 10),
dx = layerx - parseInt(el.attr('offsetx') || 0, 10),
dy = layery - parseInt(el.attr('offsety') || 0, 10);
el.attr({offsetx: layerx, offsety: layery});
/* We have to update the values stored in the tile features, too,
* otherwise when d3 regenerates these features, the offsets will be
* wrong. */
$.each(tileCache, function (idx, tile) {
if (tile._index.level === layer && tile.feature) {
var f = tile.feature,
o = f.origin(), ul = f.upperLeft(), lr = f.lowerRight();
f.origin([o[0] - dx, o[1] - dy, o[2]]);
f.upperLeft([ul[0] - dx, ul[1] - dy, ul[2]]);
f.lowerRight([lr[0] - dx, lr[1] - dy, lr[2]]);
f._update();
}
});
});
canvas.attr({lastoffsetx: x, lastoffsety: y, lastlevel: level});
return {x: x, y: y};
};
/* Initialize the tile layer. This creates a series of sublayers so that
* the different layers will stack in the proper order.
*/
this._init = function () {
var sublayer;
s_init.apply(m_this, arguments);
for (sublayer = 0; sublayer <= m_this._options.maxLevel; sublayer += 1) {
m_this._getSubLayer(sublayer);
}
};
/* When update is called, apply the transform to our renderer. */
this._update = function () {
s_update.apply(m_this, arguments);
m_this.renderer()._setTransform();
};
/* Remove both the tile feature and an internal image element. */
this._remove = function (tile) {
if (tile.feature) {
m_this.deleteFeature(tile.feature);
tile.feature = null;
}
if (tile.image) {
$(tile.image).remove();
}
};
};
geo.registerLayerAdjustment('d3', 'tile', geo.d3.tileLayer);
//////////////////////////////////////////////////////////////////////////////
/**
*
* Create a new instance of pointFeature
*
* @class
* @extends geo.pointFeature
* @extends geo.d3.object
* @returns {geo.d3.pointFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.d3.pointFeature = function (arg) {
'use strict';
if (!(this instanceof geo.d3.pointFeature)) {
return new geo.d3.pointFeature(arg);
}
arg = arg || {};
geo.pointFeature.call(this, arg);
geo.d3.object.call(this);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_init = this._init,
s_update = this._update,
m_buildTime = geo.timestamp(),
m_style = {};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Build
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
var data = m_this.data(),
s_style = m_this.style.get(),
m_renderer = m_this.renderer(),
pos_func = m_this.position();
// call super-method
s_update.call(m_this);
// default to empty data array
if (!data) { data = []; }
// fill in d3 renderer style object defaults
m_style.id = m_this._d3id();
m_style.data = data;
m_style.append = 'circle';
m_style.attributes = {
r: m_renderer._convertScale(s_style.radius),
cx: function (d) {
return m_this.featureGcsToDisplay(pos_func(d)).x;
},
cy: function (d) {
return m_this.featureGcsToDisplay(pos_func(d)).y;
}
};
m_style.style = s_style;
m_style.classes = ['d3PointFeature'];
// pass to renderer to draw
m_this.renderer()._drawFeatures(m_style);
// update time stamps
m_buildTime.modified();
m_this.updateTime().modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
s_update.call(m_this);
if (m_this.getMTime() >= m_buildTime.getMTime()) {
m_this._build();
}
return m_this;
};
this._init(arg);
return this;
};
inherit(geo.d3.pointFeature, geo.pointFeature);
// Now register it
geo.registerFeature('d3', 'point', geo.d3.pointFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class lineFeature
*
* @class
* @extends geo.lineFeature
* @extends geo.d3.object
* @returns {geo.d3.lineFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.d3.lineFeature = function (arg) {
'use strict';
if (!(this instanceof geo.d3.lineFeature)) {
return new geo.d3.lineFeature(arg);
}
arg = arg || {};
geo.lineFeature.call(this, arg);
geo.d3.object.call(this);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_init = this._init,
m_buildTime = geo.timestamp(),
s_update = this._update;
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Build
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
var data = m_this.data() || [],
s_style = m_this.style(),
m_renderer = m_this.renderer(),
pos_func = m_this.position(),
line = d3.svg.line()
.x(function (d) { return m_this.featureGcsToDisplay(d).x; })
.y(function (d) { return m_this.featureGcsToDisplay(d).y; });
s_update.call(m_this);
s_style.fill = function () { return false; };
data.forEach(function (item, idx) {
var m_style;
var ln = m_this.line()(item, idx);
var style = {}, key;
function wrapStyle(func) {
if (geo.util.isFunction(func)) {
return function () {
return func(ln[0], 0, item, idx);
};
} else {
return func;
}
}
for (key in s_style) {
if (s_style.hasOwnProperty(key)) {
style[key] = wrapStyle(s_style[key]);
}
}
// item is an object representing a single line
// m_this.line()(item) is an array of coordinates
m_style = {
data: [ln.map(function (d, i) { return pos_func(d, i, item, idx); })],
append: 'path',
attributes: {
d: line
},
id: m_this._d3id() + idx,
classes: ['d3LineFeature', 'd3SubLine-' + idx],
style: style
};
m_renderer._drawFeatures(m_style);
});
m_buildTime.modified();
m_this.updateTime().modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
s_update.call(m_this);
if (m_this.getMTime() >= m_buildTime.getMTime()) {
m_this._build();
}
return m_this;
};
this._init(arg);
return this;
};
inherit(geo.d3.lineFeature, geo.lineFeature);
geo.registerFeature('d3', 'line', geo.d3.lineFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class pathFeature
*
* @class
* @extends geo.pathFeature
* @extends geo.d3.object
* @returns {geo.d3.pathFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.d3.pathFeature = function (arg) {
'use strict';
if (!(this instanceof geo.d3.pathFeature)) {
return new geo.d3.pathFeature(arg);
}
arg = arg || {};
geo.pathFeature.call(this, arg);
geo.d3.object.call(this);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_init = this._init,
m_buildTime = geo.timestamp(),
s_update = this._update,
m_style = {};
m_style.style = {};
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Build
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
var data = m_this.data() || [],
s_style = m_this.style(),
tmp, diag;
s_update.call(m_this);
diag = function (d) {
var p = {
source: d.source,
target: d.target
};
return d3.svg.diagonal()(p);
};
tmp = [];
data.forEach(function (d, i) {
var src, trg;
if (i < data.length - 1) {
src = d;
trg = data[i + 1];
tmp.push({
source: m_this.featureGcsToDisplay(src),
target: m_this.featureGcsToDisplay(trg)
});
}
});
m_style.data = tmp;
m_style.attributes = {
d: diag
};
m_style.id = m_this._d3id();
m_style.append = 'path';
m_style.classes = ['d3PathFeature'];
m_style.style = $.extend({
'fill': function () { return false; },
'fillColor': function () { return { r: 0, g: 0, b: 0 }; }
}, s_style);
m_this.renderer()._drawFeatures(m_style);
m_buildTime.modified();
m_this.updateTime().modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
*
* @override
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
s_update.call(m_this);
if (m_this.dataTime().getMTime() >= m_buildTime.getMTime()) {
m_this._build();
}
return m_this;
};
this._init(arg);
return this;
};
inherit(geo.d3.pathFeature, geo.pathFeature);
geo.registerFeature('d3', 'path', geo.d3.pathFeature);
/**
* @class
* @extends geo.graphFeature
*/
geo.d3.graphFeature = function (arg) {
'use strict';
var m_this = this;
if (!(this instanceof geo.d3.graphFeature)) {
return new geo.d3.graphFeature(arg);
}
geo.graphFeature.call(this, arg);
////////////////////////////////////////////////////////////////////////////
/**
* Returns a d3 selection for the graph elements
*/
////////////////////////////////////////////////////////////////////////////
this.select = function () {
var renderer = m_this.renderer(),
selection = {},
node = m_this.nodeFeature(),
links = m_this.linkFeatures();
selection.nodes = renderer.select(node._d3id());
selection.links = links.map(function (link) {
return renderer.select(link._d3id());
});
return selection;
};
return this;
};
inherit(geo.d3.graphFeature, geo.graphFeature);
geo.registerFeature('d3', 'graph', geo.d3.graphFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a plane feature given a lower left corner point
* and and upper right corner point
*
* @class
* @extends geo.planeFeature
* @param lowerleft
* @param upperright
* @returns {geo.d3.planeFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.d3.planeFeature = function (arg) {
'use strict';
if (!(this instanceof geo.d3.planeFeature)) {
return new geo.d3.planeFeature(arg);
}
geo.planeFeature.call(this, arg);
geo.d3.object.call(this);
var m_this = this,
m_style = {},
s_update = this._update,
s_init = this._init,
m_buildTime = geo.timestamp();
//////////////////////////////////////////////////////////////////////////////
/**
* Normalize a coordinate as an object {x: ..., y: ...}
*
* @private
* @returns {Object}
*/
//////////////////////////////////////////////////////////////////////////////
function normalize(pt) {
if (Array.isArray(pt)) {
return {
x: pt[0],
y: pt[1]
};
}
return pt;
}
//////////////////////////////////////////////////////////////////////////////
/**
* Build the feature object and pass to the renderer for drawing.
*
* @private
* @returns {geo.d3.planeFeature}
*/
//////////////////////////////////////////////////////////////////////////////
this._build = function () {
var ul = normalize(m_this.upperLeft()),
lr = normalize(m_this.lowerRight()),
renderer = m_this.renderer(),
s = m_this.style();
delete s.fill_color;
delete s.color;
delete s.opacity;
/*
if (!s.screenCoordinates) {
origin = renderer.layer().map().worldToDisplay(origin);
ul = renderer.layer().map().worldToDisplay(ul);
lr = renderer.layer().map().worldToDisplay(lr);
}
*/
m_style.id = m_this._d3id();
m_style.style = s;
m_style.attributes = {
x: ul.x,
y: ul.y,
width: Math.abs(lr.x - ul.x),
height: Math.abs(lr.y - ul.y),
reference: s.reference
};
if (s.image) {
m_style.append = 'image';
m_style.attributes['xlink:href'] = s.image;
} else {
m_style.append = 'rect';
}
m_style.data = [0];
m_style.classes = ['d3PlaneFeature'];
if (s.parentId) {
m_style.parentId = s.parentId;
}
renderer._drawFeatures(m_style);
m_buildTime.modified();
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Redraw the plane feature if necessary.
*
* @private
* @returns {geo.d3.planeFeature}
*/
//////////////////////////////////////////////////////////////////////////////
this._update = function () {
s_update.call(m_this);
if (m_this.dataTime().getMTime() >= m_buildTime.getMTime()) {
m_this._build();
}
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Initializes the plane feature style (over-riding the parent default).
*
* @private
* @returns {geo.d3.planeFeature}
*/
//////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg || {});
m_this.style({
stroke: function () { return false; },
fill: function () { return true; },
fillColor: function () { return {r: 0.3, g: 0.3, b: 0.3}; },
fillOpacity: function () { return 0.5; }
});
return m_this;
};
this._init();
return this;
};
inherit(geo.d3.planeFeature, geo.planeFeature);
geo.registerFeature('d3', 'plane', geo.d3.planeFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of vectorFeature
*
* @class
* @extends geo.vectorFeature
* @extends geo.d3.object
* @returns {geo.d3.vectorFeature}
*/
//////////////////////////////////////////////////////////////////////////////
geo.d3.vectorFeature = function (arg) {
'use strict';
if (!(this instanceof geo.d3.vectorFeature)) {
return new geo.d3.vectorFeature(arg);
}
arg = arg || {};
geo.vectorFeature.call(this, arg);
geo.d3.object.call(this);
////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
////////////////////////////////////////////////////////////////////////////
var m_this = this,
s_init = this._init,
s_exit = this._exit,
s_update = this._update,
m_buildTime = geo.timestamp(),
m_style = {};
////////////////////////////////////////////////////////////////////////////
/**
* Generate a unique ID for a marker definition
* @private
* @param {object} d Unused datum (for d3 compat)
* @param {number} i The marker index
*/
////////////////////////////////////////////////////////////////////////////
function markerID(d, i) {
return m_this._d3id() + '_marker_' + i;
}
////////////////////////////////////////////////////////////////////////////
/**
* Add marker styles for vector arrows.
* @private
* @param {object[]} data The vector data array
* @param {function} stroke The stroke accessor
* @param {function} opacity The opacity accessor
*/
////////////////////////////////////////////////////////////////////////////
function updateMarkers(data, stroke, opacity) {
var renderer = m_this.renderer();
var sel = m_this.renderer()._definitions()
.selectAll('marker.geo-vector')
.data(data);
sel.enter()
.append('marker')
.attr('id', markerID)
.attr('class', 'geo-vector')
.attr('viewBox', '0 0 10 10')
.attr('refX', '1')
.attr('refY', '5')
.attr('markerWidth', '5')
.attr('markerHeight', '5')
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 0 0 L 10 5 L 0 10 z');
sel.exit().remove();
sel.style('stroke', renderer._convertColor(stroke))
.style('fill', renderer._convertColor(stroke))
.style('opacity', opacity);
}
////////////////////////////////////////////////////////////////////////////
/**
* Initialize
* @protected
*/
////////////////////////////////////////////////////////////////////////////
this._init = function (arg) {
s_init.call(m_this, arg);
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Build
* @protected
*/
////////////////////////////////////////////////////////////////////////////
this._build = function () {
var data = m_this.data(),
s_style = m_this.style.get(),
m_renderer = m_this.renderer(),
orig_func = m_this.origin(),
size_func = m_this.delta(),
cache = [],
scale = m_this.style('scale'),
max = Number.NEGATIVE_INFINITY;
// call super-method
s_update.call(m_this);
// default to empty data array
if (!data) { data = []; }
// cache the georeferencing
cache = data.map(function (d, i) {
var origin = m_this.featureGcsToDisplay(orig_func(d, i)),
delta = size_func(d, i);
max = Math.max(max, delta.x * delta.x + delta.y * delta.y);
return {
x1: origin.x,
y1: origin.y,
dx: delta.x,
dy: -delta.y
};
});
max = Math.sqrt(max);
if (!scale) {
scale = 75 / max;
}
function getScale() {
return scale / m_renderer.scaleFactor();
}
// fill in d3 renderer style object defaults
m_style.id = m_this._d3id();
m_style.data = data;
m_style.append = 'line';
m_style.attributes = {
x1: function (d, i) {
return cache[i].x1;
},
y1: function (d, i) {
return cache[i].y1;
},
x2: function (d, i) {
return cache[i].x1 + getScale() * cache[i].dx;
},
y2: function (d, i) {
return cache[i].y1 + getScale() * cache[i].dy;
},
'marker-end': function (d, i) {
return 'url(#' + markerID(d, i) + ')';
}
};
m_style.style = {
stroke: function () { return true; },
strokeColor: s_style.strokeColor,
strokeWidth: s_style.strokeWidth,
strokeOpacity: s_style.strokeOpacity
};
m_style.classes = ['d3VectorFeature'];
// Add markers to the defition list
updateMarkers(data, s_style.strokeColor, s_style.strokeOpacity);
// pass to renderer to draw
m_this.renderer()._drawFeatures(m_style);
// update time stamps
m_buildTime.modified();
m_this.updateTime().modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Update
* @protected
*/
////////////////////////////////////////////////////////////////////////////
this._update = function () {
s_update.call(m_this);
if (m_this.getMTime() >= m_buildTime.getMTime()) {
m_this._build();
} else {
updateMarkers(
m_style.data,
m_style.style.strokeColor,
m_style.style.strokeOpacity
);
}
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Exit
* @protected
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
s_exit.call(m_this);
m_style = {};
updateMarkers([], null, null);
};
this._init(arg);
return this;
};
inherit(geo.d3.vectorFeature, geo.vectorFeature);
// Now register it
geo.registerFeature('d3', 'vector', geo.d3.vectorFeature);
//////////////////////////////////////////////////////////////////////////////
/**
* @namespace
*/
//////////////////////////////////////////////////////////////////////////////
geo.gui = {};
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class uiLayer
*
* @class
* @extends {geo.layer}
* @returns {geo.gui.uiLayer}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gui.uiLayer = function (arg) {
'use strict';
// The widget stays fixed on the screen.
arg.renderer = 'dom';
arg.sticky = false;
if (!(this instanceof geo.gui.uiLayer)) {
return new geo.gui.uiLayer(arg);
}
geo.layer.call(this, arg);
var m_this = this,
s_exit = this._exit;
////////////////////////////////////////////////////////////////////////////
/**
* Create a new ui control
*
* @returns {geo.gui.Widget} Will return a new control widget
*/
////////////////////////////////////////////////////////////////////////////
this.createWidget = function (widgetName, arg) {
var newWidget = geo.createWidget(widgetName, m_this, arg);
// We only want top level widgets to be a child of the uiLayer
if (!(arg && 'parent' in arg)) {
m_this.addChild(newWidget);
}
newWidget._init(arg);
m_this.modified();
return newWidget;
};
////////////////////////////////////////////////////////////////////////////
/**
* Delete a ui control
*/
////////////////////////////////////////////////////////////////////////////
this.deleteWidget = function (widget) {
widget._exit();
m_this.removeChild(widget);
m_this.modified();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Free memory and destroy the layer.
*/
////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_this.children().forEach(function (child) {
m_this.deleteWidget(child);
});
s_exit();
};
};
inherit(geo.gui.uiLayer, geo.layer);
geo.registerLayer('ui', geo.gui.uiLayer);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class widget
*
* @class
* @extends {geo.sceneObject}
* @returns {geo.gui.widget}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gui.widget = function (arg) {
'use strict';
if (!(this instanceof geo.gui.widget)) {
return new geo.gui.widget(arg);
}
geo.sceneObject.call(this, arg);
var m_this = this,
s_exit = this._exit,
m_layer = arg.layer,
m_canvas = null;
arg.position = arg.position === undefined ? { left: 0, top: 0 } : arg.position;
if (arg.parent !== undefined && !(arg.parent instanceof geo.gui.widget)) {
throw 'Parent must be of type geo.gui.widget';
}
this._init = function () {
m_this.modified();
};
this._exit = function () {
m_this.children().forEach(function (child) {
m_this._deleteFeature(child);
});
m_this.layer().geoOff(geo.event.pan, m_this.repositionEvent);
m_this.parentCanvas().removeChild(m_this.canvas());
s_exit();
};
////////////////////////////////////////////////////////////////////////////
/**
* Create feature give a name
*
* @returns {geo.Feature} Will return a new feature
*/
////////////////////////////////////////////////////////////////////////////
this._createFeature = function (featureName, arg) {
var newFeature = geo.createFeature(
featureName, m_this, m_this.renderer(), arg);
m_this.addChild(newFeature);
m_this.modified();
return newFeature;
};
////////////////////////////////////////////////////////////////////////////
/**
* Delete feature
*/
////////////////////////////////////////////////////////////////////////////
this._deleteFeature = function (feature) {
m_this.removeChild(feature);
feature._exit();
return m_this;
};
////////////////////////////////////////////////////////////////////////////
/**
* Return the layer associated with this widget.
*/
////////////////////////////////////////////////////////////////////////////
this.layer = function () {
return m_layer;
};
////////////////////////////////////////////////////////////////////////////
/**
* Create the canvas this widget will operate on.
*/
////////////////////////////////////////////////////////////////////////////
this._createCanvas = function () {
throw 'Must be defined in derived classes';
};
////////////////////////////////////////////////////////////////////////////
/**
* Get/Set the canvas for the widget
*/
////////////////////////////////////////////////////////////////////////////
this.canvas = function (val) {
if (val === undefined) {
return m_canvas;
} else {
m_canvas = val;
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Appends a child to the widget
* The widget determines how to append itself to a parent, the parent can either
* be another widget, or the UI Layer.
*/
////////////////////////////////////////////////////////////////////////////
this._appendChild = function () {
m_this.parentCanvas().appendChild(m_this.canvas());
};
////////////////////////////////////////////////////////////////////////////
/**
* Get the parent canvas (top level widgets define their layer as their parent canvas)
*/
////////////////////////////////////////////////////////////////////////////
this.parentCanvas = function () {
if (m_this.parent === undefined) {
return m_this.layer().canvas();
} else {
return m_this.parent().canvas();
}
};
////////////////////////////////////////////////////////////////////////////
/**
* Gets the CSS positioning that a widget should be placed at.
* { top: 0, left: 0 } by default.
*/
////////////////////////////////////////////////////////////////////////////
this.position = function (pos) {
if (pos !== undefined) {
arg.position = pos;
this.reposition();
return this;
}
var position;
if (arg &&
arg.hasOwnProperty('position') &&
arg.position.hasOwnProperty('x') &&
arg.position.hasOwnProperty('y')) {
position = m_this.layer().map().gcsToDisplay(arg.position);
return {
left: position.x,
top: position.y
};
}
return arg.position;
};
////////////////////////////////////////////////////////////////////////////
/**
* Repositions a widget based on the argument passed, or calling position on
* the widget itself.
* @param {object} position A position with the form:
* { top: m, left: n }
*/
////////////////////////////////////////////////////////////////////////////
this.reposition = function (position) {
position = position || m_this.position();
m_this.canvas().style.position = 'absolute';
for (var cssAttr in position) {
if (position.hasOwnProperty(cssAttr)) {
m_this.canvas().style[cssAttr] = position[cssAttr] + 'px';
}
}
};
this.repositionEvent = function () {
return m_this.reposition();
};
////////////////////////////////////////////////////////////////////////////
/**
* Determines whether or not the widget is completely within the viewport.
*/
////////////////////////////////////////////////////////////////////////////
this.isInViewport = function () {
var position = m_this.position();
var layer = m_this.layer();
return ((position.left >= 0 && position.top >= 0) &&
(position.left <= layer.width() && position.top <= layer.height()));
};
if (arg &&
arg.hasOwnProperty('position') &&
arg.position.hasOwnProperty('x') &&
arg.position.hasOwnProperty('y')) {
this.layer().geoOn(geo.event.pan, m_this.repositionEvent);
}
};
inherit(geo.gui.widget, geo.sceneObject);
geo.gui.domWidget = function (arg) {
'use strict';
if (!(this instanceof geo.gui.domWidget)) {
return new geo.gui.domWidget(arg);
}
geo.gui.widget.call(this, arg);
var m_this = this,
m_default_canvas = 'div';
////////////////////////////////////////////////////////////////////////////
/**
* Initializes DOM Widget.
* Sets the canvas for the widget, does parent/child relationship management,
* appends it to it's parent and handles any positioning logic.
*/
////////////////////////////////////////////////////////////////////////////
this._init = function () {
if (arg.hasOwnProperty('parent')) {
arg.parent.addChild(m_this);
}
m_this._createCanvas();
m_this._appendChild();
m_this.canvas().addEventListener('mousedown', function (e) {
e.stopPropagation();
});
m_this.reposition();
};
////////////////////////////////////////////////////////////////////////////
/**
* Creates the widget canvas.
* This is just a simple DOM element (based on args.el, or defaults to a div)
*/
////////////////////////////////////////////////////////////////////////////
this._createCanvas = function () {
m_this.canvas(document.createElement(arg.el || m_default_canvas));
};
return this;
};
inherit(geo.gui.domWidget, geo.gui.widget);
geo.registerWidget('dom', 'dom', geo.gui.domWidget);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class geo.gui.svgWidget
*
* Due to the nature of d3 creating DOM elements as it inserts them, calls to appendChild
* don't appear in this widget.
*
* The canvas of an svgWidget always refers to the actual svg element.
* The parentCanvas can refer to another widgets svg element, dom element, or the
* UI layers dom element.
* See {@link geo.gui.widget#parentCanvas}.
*
* @class
* @extends geo.gui.domWidget
* @returns {geo.gui.svgWidget}
*
*/
//////////////////////////////////////////////////////////////////////////////
geo.gui.svgWidget = function (arg) {
'use strict';
if (!(this instanceof geo.gui.svgWidget)) {
return new geo.gui.svgWidget(arg);
}
geo.gui.domWidget.call(this, arg);
var m_this = this,
m_renderer = null;
this._init = function (arg) {
var d3Parent;
if (arg.hasOwnProperty('parent')) {
arg.parent.addChild(m_this);
// Tell the renderer there is an SVG element as a parent
d3Parent = arg.parent.canvas();
}
m_this._createCanvas(d3Parent);
m_this.canvas().addEventListener('mousedown', function (e) {
e.stopPropagation();
});
m_this.reposition();
};
////////////////////////////////////////////////////////////////////////////
/**
* Creates the canvas for the svg widget.
* This directly uses the {@link geo.d3.d3Renderer} as a helper to do all of the heavy
* lifting.
*/
////////////////////////////////////////////////////////////////////////////
this._createCanvas = function (d3Parent) {
var rendererOpts = {
layer: m_this.layer(),
widget: true
};
if (d3Parent) {
rendererOpts.d3Parent = d3Parent;
}
m_renderer = geo.d3.d3Renderer(rendererOpts);
m_this.canvas(m_renderer.canvas()[0][0]);
};
return this;
};
inherit(geo.gui.svgWidget, geo.gui.domWidget);
geo.registerWidget('dom', 'svg', geo.gui.svgWidget);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class sliderWidget
*
* @class
* @extends {geo.gui.svgWidget}
* @returns {geo.gui.sliderWidget}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gui.sliderWidget = function (arg) {
'use strict';
if (!(this instanceof geo.gui.sliderWidget)) {
return new geo.gui.sliderWidget(arg);
}
geo.gui.svgWidget.call(this, arg);
var m_this = this,
s_exit = this._exit,
s_createCanvas = this._createCanvas,
s_appendChild = this._appendChild,
m_xscale,
m_yscale,
m_plus,
m_minus,
m_nub,
m_width = 20, // Approximate size of the widget in pixels
m_height = 100,
m_nubSize = 10,
m_plusIcon,
m_minusIcon,
m_group,
m_lowContrast,
m_highlightDur = 100;
/* http://icomoon.io */
/* CC BY 3.0 http://creativecommons.org/licenses/by/3.0/ */
m_plusIcon = 'M512 81.92c-237.568 0-430.080 192.614-430.080 430.080 0 237.568 192.563 430.080 430.080 430.080s430.080-192.563 430.080-430.080c0-237.517-192.563-430.080-430.080-430.080zM564.326 564.326v206.182h-104.653v-206.182h-206.234v-104.653h206.182v-206.234h104.704v206.182h206.182v104.704h-206.182z';
m_minusIcon = 'M512 81.92c-237.568 0-430.080 192.614-430.080 430.080 0 237.568 192.563 430.080 430.080 430.080s430.080-192.563 430.080-430.080c0-237.517-192.563-430.080-430.080-430.080zM770.56 459.674v104.704h-517.12v-104.704h517.12z';
// Define off-white gray colors for low contrast ui (unselected).
m_lowContrast = {
white: '#f4f4f4',
black: '#505050'
};
//////////////////////////////////////////////////////////////////////////////
/**
* Add an icon from a path string. Returns a d3 group element.
*
* @function
* @argument {String} icon svg path string
* @argument {Array} base where to append the element (d3 selection)
* @argument {Number} cx Center x-coordinate
* @argument {Number} cy Center y-coordinate
* @argument {Number} size Icon size in pixels
* @returns {object}
* @private
*/
//////////////////////////////////////////////////////////////////////////////
function put_icon(icon, base, cx, cy, size) {
var g = base.append('g');
// the scale factor
var s = size / 1024;
g.append('g')
.append('g')
.attr(
'transform',
'translate(' + cx + ',' + cy + ') scale(' + s + ') translate(-512,-512)'
)
.append('path')
.attr('d', icon)
.attr('class', 'geo-glyphicon');
return g;
}
//////////////////////////////////////////////////////////////////////////////
/**
* Initialize the slider widget in the map.
*
* @function
* @returns {geo.gui.sliderWidget}
* @private
*/
//////////////////////////////////////////////////////////////////////////////
this._init = function () {
s_createCanvas();
s_appendChild();
m_this.reposition();
var svg = d3.select(m_this.canvas()),
x0 = 40,
y0 = 40 + m_width,
map = m_this.layer().map();
// create d3 scales for positioning
// TODO: make customizable and responsive
m_xscale = d3.scale.linear().domain([-4, 4]).range([x0, x0 + m_width]);
m_yscale = d3.scale.linear().domain([0, 1]).range([y0, y0 + m_height]);
// Create the main group element
svg = svg.append('g').classed('geo-ui-slider', true);
m_group = svg;
// Create + zoom button
m_plus = svg.append('g');
m_plus.append('circle')
.datum({
fill: 'white',
stroke: null
})
.classed('geo-zoom-in', true)
.attr('cx', m_xscale(0))
.attr('cy', m_yscale(0.0) - m_width + 2)
.attr('r', m_width / 2)
.style({
'cursor': 'pointer'
})
.on('click', function () {
var z = map.zoom();
map.transition({
zoom: z + 1,
ease: d3.ease('cubic-in-out'),
duration: 500
});
})
.on('mousedown', function () {
d3.event.stopPropagation();
});
put_icon(
m_plusIcon,
m_plus,
m_xscale(0),
m_yscale(0) - m_width + 2,
m_width + 5
).style('cursor', 'pointer')
.style('pointer-events', 'none')
.select('path')
.datum({
fill: 'black',
stroke: null
});
// Create the - zoom button
m_minus = svg.append('g');
m_minus.append('circle')
.datum({
fill: 'white',
stroke: null
})
.classed('geo-zoom-out', true)
.attr('cx', m_xscale(0))
.attr('cy', m_yscale(1.0) + m_width - 2)
.attr('r', m_width / 2)
.style({
'cursor': 'pointer'
})
.on('click', function () {
var z = map.zoom();
map.transition({
zoom: z - 1,
ease: d3.ease('cubic-in-out'),
duration: 500
});
})
.on('mousedown', function () {
d3.event.stopPropagation();
});
put_icon(
m_minusIcon,
m_minus,
m_xscale(0),
m_yscale(1) + m_width - 2,
m_width + 5
).style('cursor', 'pointer')
.style('pointer-events', 'none')
.select('path')
.datum({
fill: 'black',
stroke: null
});
// Respond to a mouse event on the widget
function respond(evt, trans) {
var z = m_yscale.invert(d3.mouse(m_this.layer().node()[0])[1]),
zrange = map.zoomRange();
z = (1 - z) * (zrange.max - zrange.min) + zrange.min;
if (trans) {
map.transition({
zoom: z,
ease: d3.ease('cubic-in-out'),
duration: 500,
done: m_this._update()
});
} else {
map.zoom(z);
m_this._update();
}
evt.stopPropagation();
}
// Create the track
svg.append('rect')
.datum({
fill: 'white',
stroke: 'black'
})
.classed('geo-zoom-track', true)
.attr('x', m_xscale(0) - m_width / 6)
.attr('y', m_yscale(0))
.attr('rx', m_width / 10)
.attr('ry', m_width / 10)
.attr('width', m_width / 3)
.attr('height', m_height)
.style({
'cursor': 'pointer'
})
.on('click', function () {
respond(d3.event, true);
});
// Create the nub
m_nub = svg.append('rect')
.datum({
fill: 'black',
stroke: null
})
.classed('geo-zoom-nub', true)
.attr('x', m_xscale(-4))
.attr('y', m_yscale(0.5) - m_nubSize / 2)
.attr('rx', 3)
.attr('ry', 3)
.attr('width', m_width)
.attr('height', m_nubSize)
.style({
'cursor': 'pointer'
})
.on('mousedown', function () {
d3.select(document).on('mousemove.geo.slider', function () {
respond(d3.event);
});
d3.select(document).on('mouseup.geo.slider', function () {
respond(d3.event);
d3.select(document).on('.geo.slider', null);
});
d3.event.stopPropagation();
});
var mouseOver = function () {
d3.select(this).attr('filter', 'url(#geo-highlight)');
m_group.selectAll('rect,path,circle').transition()
.duration(m_highlightDur)
.style('fill', function (d) {
return d.fill || null;
})
.style('stroke', function (d) {
return d.stroke || null;
});
};
var mouseOut = function () {
d3.select(this).attr('filter', null);
m_group.selectAll('circle,rect,path').transition()
.duration(m_highlightDur)
.style('fill', function (d) {
return m_lowContrast[d.fill] || null;
})
.style('stroke', function (d) {
return m_lowContrast[d.stroke] || null;
});
};
m_group.selectAll('*')
.on('mouseover', mouseOver)
.on('mouseout', mouseOut);
// Update the nub position on zoom
m_this.layer().geoOn(geo.event.zoom, function () {
m_this._update();
});
mouseOut();
m_this._update();
};
//////////////////////////////////////////////////////////////////////////////
/**
* Removes the slider element from the map and unbinds all handlers.
*
* @function
* @returns {geo.gui.sliderWidget}
* @private
*/
//////////////////////////////////////////////////////////////////////////////
this._exit = function () {
m_group.remove();
m_this.layer().geoOff(geo.event.zoom);
s_exit();
};
//////////////////////////////////////////////////////////////////////////////
/**
* Update the slider widget state in reponse to map changes. I.e. zoom
* range changes.
*
* @function
* @returns {geo.gui.sliderWidget}
* @private
*/
//////////////////////////////////////////////////////////////////////////////
this._update = function (obj) {
var map = m_this.layer().map(),
zoomRange = map.zoomRange(),
zoom = map.zoom(),
zoomScale = d3.scale.linear();
obj = obj || {};
zoom = obj.value || zoom;
zoomScale.domain([zoomRange.min, zoomRange.max])
.range([1, 0])
.clamp(true);
m_nub.attr('y', m_yscale(zoomScale(zoom)) - m_nubSize / 2);
};
};
inherit(geo.gui.sliderWidget, geo.gui.svgWidget);
geo.registerWidget('dom', 'slider', geo.gui.sliderWidget);
//////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance of class legendWidget
*
* @class
* @extends geo.gui.svgWidget
* @returns {geo.gui.legendWidget}
*/
//////////////////////////////////////////////////////////////////////////////
geo.gui.legendWidget = function (arg) {
'use strict';
if (!(this instanceof geo.gui.legendWidget)) {
return new geo.gui.legendWidget(arg);
}
geo.gui.svgWidget.call(this, arg);
/** @private */
var m_this = this,
m_categories = [],
m_top = null,
m_group = null,
m_border = null,
m_spacing = 20, // distance in pixels between lines
m_padding = 12, // padding in pixels inside the border
s_createCanvas = this._createCanvas,
s_appendChild = this._appendChild;
//////////////////////////////////////////////////////////////////////////////
/**
* Get or set the category array associated with
* the legend. Each element of this array is
* an object: ::
* {
* name: string,
* style: object,
* type: 'point' | 'line' | ...
* }
*
* The style property can contain the following feature styles:
* * fill: bool
* * fillColor: object | string
* * fillOpacity: number
* * stroke: bool
* * strokeColor: object | string
* * strokeWidth: number
* * strokeOpacity: number
*
* The type controls how the element is displayed, point as a circle,
* line as a line segment. Any other value will display as a rounded
* rectangle.
*
* @param {object[]?} categories The categories to display
*/
//////////////////////////////////////////////////////////////////////////////
this.categories = function (arg) {
if (arg === undefined) {
return m_categories.slice();
}
m_categories = arg.slice().map(function (d) {
if (d.type === 'line') {
d.style.fill = false;
d.style.stroke = true;
}
return d;
});
m_this.draw();
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Get the widget's size
* @return {{width: number, height: number}} The size in pixels
*/
//////////////////////////////////////////////////////////////////////////////
this.size = function () {
var width = 1, height;
var test = d3.select(m_this.canvas()).append('text')
.style('opacity', 1e-6);
m_categories.forEach(function (d) {
test.text(d.name);
width = Math.max(width, test.node().getBBox().width);
});
test.remove();
height = m_spacing * (m_categories.length + 1);
return {
width: width + 50,
height: height
};
};
//////////////////////////////////////////////////////////////////////////////
/**
* Redraw the legend
*/
//////////////////////////////////////////////////////////////////////////////
this.draw = function () {
m_this._init();
function applyColor(selection) {
selection.style('fill', function (d) {
if (d.style.fill || d.style.fill === undefined) {
return d.style.fillColor;
} else {
return 'none';
}
})
.style('fill-opacity', function (d) {
if (d.style.fillOpacity === undefined) {
return 1;
}
return d.style.fillOpacity;
})
.style('stroke', function (d) {
if (d.style.stroke || d.style.stroke === undefined) {
return d.style.strokeColor;
} else {
return 'none';
}
})
.style('stroke-opacity', function (d) {
if (d.style.strokeOpacity === undefined) {
return 1;
}
return d.style.strokeOpacity;
})
.style('stroke-width', function (d) {
if (d.style.strokeWidth === undefined) {
return 1.5;
}
return d.style.strokeWidth;
});
}
m_border.attr('height', m_this.size().height + 2 * m_padding)
.style('display', null);
var scale = m_this._scale();
var labels = m_group.selectAll('g.geo-label')
.data(m_categories, function (d) { return d.name; });
var g = labels.enter().append('g')
.attr('class', 'geo-label')
.attr('transform', function (d, i) {
return 'translate(0,' + scale.y(i) + ')';
});
applyColor(g.filter(function (d) {
return d.type !== 'point' && d.type !== 'line';
}).append('rect')
.attr('x', 0)
.attr('y', -6)
.attr('rx', 5)
.attr('ry', 5)
.attr('width', 40)
.attr('height', 12)
);
applyColor(g.filter(function (d) {
return d.type === 'point';
}).append('circle')
.attr('cx', 20)
.attr('cy', 0)
.attr('r', 6)
);
applyColor(g.filter(function (d) {
return d.type === 'line';
}).append('line')
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', 40)
.attr('y2', 0)
);
g.append('text')
.attr('x', '50px')
.attr('y', 0)
.attr('dy', '0.3em')
.text(function (d) {
return d.name;
});
m_this.reposition();
return m_this;
};
//////////////////////////////////////////////////////////////////////////////
/**
* Get scales for the x and y axis for the current size.
* @private
*/
//////////////////////////////////////////////////////////////////////////////
this._scale = function () {
return {
x: d3.scale.linear()
.domain([0, 1])
.range([0, m_this.size().width]),
y: d3.scale.linear()
.domain([0, m_categories.length - 1])
.range([m_padding / 2, m_this.size().height - m_padding / 2])
};
};
//////////////////////////////////////////////////////////////////////////////
/**
* Private initialization. Creates the widget's DOM container and internal
* variables.
* @private
*/
//////////////////////////////////////////////////////////////////////////////
this._init = function () {
// adding categories redraws the entire thing by calling _init, see
// the m_top.remove() line below
if (!m_top) {
s_createCanvas();
s_appendChild();
}
// total size = interior size + 2 * padding + 2 * width of the border
var w = m_this.size().width + 2 * m_padding + 4,
h = m_this.size().height + 2 * m_padding + 4;
// @todo - removing after creating to maintain the appendChild structure
if (m_top) {
m_top.remove();
}
d3.select(m_this.canvas()).attr('width', w).attr('height', h);
m_top = d3.select(m_this.canvas()).append('g');
m_group = m_top
.append('g')
.attr('transform', 'translate(' + [m_padding + 2, m_padding + 2] + ')');
m_border = m_group.append('rect')
.attr('x', -m_padding)
.attr('y', -m_padding)
.attr('width', w - 4)
.attr('height', h - 4)
.attr('rx', 3)
.attr('ry', 3)
.style({
'stroke': 'black',
'stroke-width': '1.5px',
'fill': 'white',
'fill-opacity': 0.75,
'display': 'none'
});
m_group.on('mousedown', function () {
d3.event.stopPropagation();
});
m_group.on('mouseover', function () {
m_border.transition()
.duration(250)
.style('fill-opacity', 1);
});
m_group.on('mouseout', function () {
m_border.transition()
.duration(250)
.style('fill-opacity', 0.75);
});
m_this.reposition();
};
this.geoOn(geo.event.resize, function () {
m_this.draw();
});
};
inherit(geo.gui.legendWidget, geo.gui.svgWidget);
geo.registerWidget('dom', 'legend', geo.gui.legendWidget);
(function ($, geo, d3) {
'use strict';
var load = function () {
// This requires jquery ui, which we don't want to make a
// hard requirement, so bail out here if the widget factory
// is not available and throw a helpful message when the
// tries to use it.
if (!$.widget) {
$.fn.geojsMap = function () {
throw new Error(
'The geojs jquery plugin requires jquery ui to be available.'
);
};
return;
}
/**
* Takes an option key and returns true if it should
* return a color accessor.
* @private
*/
function isColorKey(key) {
return key.slice(key.length - 5, key.length)
.toLowerCase() === 'color';
}
/**
* Take an array of data and an accessor for a color property
* and return a wrapped accessor mapping to actual color
* values. This allows users to pass arbitrary strings
* or numbers as any color property and this will wrap
* a categorical scale or linear scale.
*
* Requires d3
* @private
* @param {Object[]} data A data array
* @param {(string|number|function)} acc A color accessor
* @return {function}
*/
function makeColorScale(data, acc) {
if (!d3) {
console.warn('d3 is unavailable, cannot apply color scales.');
return acc;
}
var domain;
var cannotHandle = false;
var doNotHandle = true;
var categorical = false;
var min = Number.POSITIVE_INFINITY;
var max = Number.NEGATIVE_INFINITY;
function wrap(func) {
if (geo.util.isFunction(func)) {
return function () {
return func(acc.apply(this, arguments));
};
} else {
return func(acc);
}
}
if (geo.util.isFunction(acc)) {
domain = d3.set(data.map(acc)).values();
} else {
domain = [acc];
}
domain.forEach(function (v) {
if (!(typeof v === 'string' &&
typeof geo.util.convertColor(v) === 'object')) {
// This is to handle cases when values are css names or
// hex strings. We don't want to apply a categorical
// scale.
doNotHandle = false;
}
if (typeof v === 'string') {
categorical = true;
} else if (!isFinite(v)) {
cannotHandle = true;
} else if (+v > max) {
max = +v;
} else if (+v < min) {
min = +v;
}
});
if (cannotHandle) {
// At least one value is not a string or a numeric value.
// Pass the bare accessor back to geojs to handle it.
return acc;
}
if (doNotHandle) {
return acc;
}
if (categorical) {
if (domain.length <= 10) {
return wrap(d3.scale.category10().domain(domain));
} else if (domain.length <= 20) {
return wrap(d3.scale.category20().domain(domain));
} else {
// TODO: sort domain by most used and make an "other" category
return wrap(d3.scale.category20().domain(domain));
}
}
// http://colorbrewer2.org/?type=diverging&scheme=RdYlBu&n=3
return wrap(d3.scale.linear()
.range([
'rgb(252,141,89)',
'rgb(255,255,191)',
'rgb(145,191,219)'
])
.domain([
min,
(min + max) / 2,
max
]));
}
/**
* @class geojsMap
* @memberOf jQuery.fn
*
* @description Generates a geojs map inside an element.
*
*
* Due to current limitations in geojs, only a single map can be instantiated
* on a page. Trying to create a second map will throw an error
* (see issue
* <a href="https://github.com/OpenGeoscience/geojs/issues/154">#154</a>).
*
* @example <caption>Create a map with the default options.</caption>
* $("#map").geojsMap();
* @example <caption>Create a map with a given initial center and zoom</caption>
* $("#map").geojsMap({
* longitude: -125,
* latitude: 35,
* zoom: 5
* });
* @example <caption>Create a map with points</caption>
* $("#map").geojsMap({
* data: [...],
* layers: [{
* renderer: 'vgl',
* features: [{
* type: 'point',
* radius: 5,
* position: function (d) { return {x: d.geometry.x, y: d.geometry.y} },
* fillColor: function (d, i) { return i < 5 ? 'red' : 'blue' },
* stroke: false
* }]
* }]
* };
* @example <caption>Create a map with points, lines and multiple layers</caption>
* $("#map").geojsMap({
* center: { x: -130, y: 40 },
* zoom: 3,
* layers: [{
* renderer: 'vgl',
* features: [{
* data: [...],
* type: 'point',
* radius: 5,
* position: function (d) { return {x: d.geometry.x, y: d.geometry.y} },
* fillColor: function (d, i) { return i < 5 ? 'red' : 'blue' },
* stroke: false
* }]
* },
* {
* renderer: 'd3',
* features[{
* data: [...],
* type: 'line',
* position: function (d) { return { x: d[0], y: d[1] } },
* line: function (d) { return d.coordinates; },
* strokeWidth: 3,
* strokeColor: 'black',
* strokeOpacity: 0.5
* }]
* }]
* };
*/
$.widget('geojs.geojsMap', /** @lends jQuery.fn.geojsMap */{
/**
* A coordinate object as accepted by geojs to express positions in an
* arbitrary coordinate system (geographic, screen, etc). Coordinates returned by
* geojs methods are always expressed with "x" and "y" properties, but
* it will accept any of the aliased properties.
* @typedef coordinate
* @type {object}
* @property {number} longitude Alias: "x", "lng", or "lon"
* @property {number} latitude Alias: "y" or "lat"
* @property {number} [elevation=0] Alias: "z", "elev", or "height"
*/
/**
* Colors can be expressed in multiple ways:
* <ul>
* <li>css name (<code>"steelblue"</code>)</li>
* <li>24 bit hex value (<code>0xff0051</code>)</li>
* <li>25 bit hex string (<code>"#ff0051"</code>)</li>
* <li>rgb object (values from 0-1, <code>{r: 1, g: 0.5, b: 0}</code>)</li>
* </ul>
* @typedef color
* @type {*}
*/
/**
* Point feature options object. All styles can be
* given as accessor functions or constants. Accessor
* functions are called with the following signature:
* <pre>
* function func(d, i) {
* // d - data object
* // i - index of d in the data array
* // this - geo.pointFeature
* }
* </pre>
* Pass null to remove saved options from previous calls.
* @typedef pointOptions
* @type {Object}
* @property {Object[]} data Data array
* @property {coordinate} position Location of the point center
* @property {number} radius
* Radius of the circle in pixels (ignored when <code>size</code>
* is present)
* @property {function} size
* A function returning a numerical value
* @property {boolean} fill Presence or absence of the fill
* @property {color} fillColor Interior color
* @property {float} fillOpacity Opacity of the interior <code>[0,1]</code>
* @property {boolean} stroke Presence or absence of the stroke
* @property {color} strokeColor Stroke color
* @property {float} strokeOpacity Opacity of the stroke <code>[0,1]</code>
*/
/**
* @instance
* @description
* Map options (not fully implemented).
* @example <caption>Get the current map center</caption>
* var center=$("#map").geojsMap("center");
* @example <caption>Pan the map to a new center</caption>
* $("#map").geojsMap("center", {lat: 10, lng: -100});
* @property {object[]} [data=[]] The default data array used for
* features/layers not already containing data.
* @property {coordinate} [center={lat: 0, lng: 0}] The map center
* @property {number} [zoom=0] The zoom level (floating point >= 0)
* @property {(number|null)} [width=null]
* The width of the map in pixels or null for 100%
* @property {(number|null)} [height=null]
* The height of the map in pixels or null for 100%
* @property {geo.layer.spec[]} [layers=[]]
* Describes layers added to the map
* @property {boolean} [autoresize=true]
* Resize the map on <code>window.resize</code> (initialization only)
* @property {string} [url]
* The open street map tile server spec default:
* <code>http://tile.openstreetmap.org/<zoom>/<x>/<y>.png</code>
*/
options: {
center: {latitude: 0, longitude: 0},
zoom: 0,
width: null,
height: null,
layers: [],
data: [],
url: 'http://tile.openstreetmap.org/{z}/{x}/{y}.png',
attribution: undefined,
// These options are for future use, but shouldn't
// be changed at the moment, so they aren't documented.
baseLayer: 'osm',
baseRenderer: 'vgl'
},
/**
* Internal constructor
* @instance
* @protected
*/
_create: function () {
if (this._map || !this.element.length) {
// when called multiple times on a single element, do nothing
return;
}
// create the map
this._map = geo.map({
width: this.options.width,
height: this.options.height,
zoom: this.options.zoom,
center: this.options.center,
node: this.element.get(0)
});
// create the base layer
this._baseLayer = this._map.createLayer(
this.options.baseLayer,
{
renderer: this.options.baseRenderer,
url: this.options.url,
attribution: this.options.attribution
}
);
// Trigger a resize to a valid size before adding
// the feature layer to handle some of the bugs that
// occur when initializing onto a node of size 0.
this._resize({width: 800, height: 600});
this._layers = [];
this.update();
},
/**
* Update the layers and features using a new array of
* {@link geo.layer.spec} objects. All existing layers
* and features are deleted. If only the data has changed,
* you can usually just call {@link jQuery.fn.geojsMap#redraw redraw}.
* @instance
* @param {geo.layer.spec[]} [layers] New map layers
* @example <caption>Delete and recreate all existing layers</caption>
* $("#map").geojsMap("update");
* @example <caption>Remove all existing feature layers.</caption>
* $("#map").geojsMap("update", []);
*/
update: function (layers) {
var m_this = this;
this.options.layers = layers || this.options.layers || [];
// delete existing layers
this._layers.forEach(function (layer) {
layer.clear();
m_this._map.deleteLayer(layer);
});
// create new layers
this._layers = this.options.layers.map(function (layer) {
layer.data = layer.data || m_this.options.data;
// Until auto color scaling gets moved into geojs core, we will
// mutate the spec and replace the color and radius options.
(layer.features || []).forEach(function (feature) {
var data = feature.data || layer.data || [];
var scl;
if (feature.type === 'point') {
if (feature.size) {
feature._size = geo.util.ensureFunction(feature.size);
} else if (feature.size === null) {
delete feature._size;
}
if (data.length && feature._size) {
scl = d3.scale.linear()
.domain(
d3.extent(data, feature._size)
)
.range([5, 20]);
feature.radius = function () {
// TODO: wrong `this` (wait for style refactor)
return scl(feature._size.apply(this, arguments));
};
}
delete feature.size;
}
var key;
for (key in feature) {
if (feature.hasOwnProperty(key) &&
isColorKey(key)) {
feature[key] = makeColorScale(data, feature[key]);
}
}
});
return geo.layer.create(m_this._map, layer);
});
// trigger an initial draw
this.redraw();
return this;
},
/**
* Return the geojs map object.
* @instance
* @returns {geo.map}
*/
map: function () {
return this._map;
},
/**
* Set the tile server URL.
* @instance
* @param {string} url The url format string of an OSM tile server.
*/
url: function (url) {
this._baseLayer.url(url);
return this;
},
/**
* Resize the map canvas.
* @instance
* @protected
* @param {object?} size Explicit size or use this.options.
*/
_resize: function (size) {
var width = this.options.width,
height = this.options.height;
if (size) {
width = size.width;
height = size.height;
}
if (!width) {
width = this.element.width();
}
if (!height) {
height = this.element.height();
}
this._map.resize(0, 0, width, height);
},
/**
* Do a full redraw of the map. In general, users shouldn't need to
* call this method, but it could be useful when accessing lower
* level features of the mapping api.
* @todo This function may need to go through each feature and call
* {@link geo.feature#modified} to properly update.
* @instance
*/
redraw: function () {
this._resize();
return this;
}
});
// Some argument type definitions used only by this plugin:
/**
* A geojs renderer is one of the following:
* <ul>
* <li><code>"vgl"</code>: Uses webGL</li>
* <li><code>"d3"</code>: Uses svg</li>
* </ul>
* @typedef renderer
* @type {string}
*/
};
/* Provide a method to reload the plugin in case jquery-ui is loaded after
* the plugin. */
geo.jqueryPlugin = {reload: load};
$(load);
})(typeof $ !== 'undefined' ? $ : window.$,
typeof geo !== 'undefined' ? geo : window.geo,
typeof d3 !== 'undefined' ? d3 : window.d3);
|
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Factory class to create a rich autocomplete that will match
* from an array of data provided via ajax. The server returns a complex data
* structure that is used with client-side javascript functions to render the
* results.
*
* The server sends a list of the form:
* [["type1", {...}, {...}, ...], ["type2", {...}, {...}, ...], ...]
* The first element of each sublist is a string designating the type of the
* hashes in the sublist, each of which represents one match. The type string
* must be the name of a function(item) which converts the hash into a rich
* row that contains both a render(node, token) and a select(target) method.
* The render method is called by the renderer when rendering the rich row,
* and the select method is called by the RichInputHandler when the rich row is
* selected.
*
* @see ../../demos/autocompleterichremote.html
*/
goog.provide('goog.ui.AutoComplete.RichRemote');
goog.require('goog.ui.AutoComplete');
goog.require('goog.ui.AutoComplete.Remote');
goog.require('goog.ui.AutoComplete.Renderer');
goog.require('goog.ui.AutoComplete.RichInputHandler');
goog.require('goog.ui.AutoComplete.RichRemoteArrayMatcher');
/**
* Factory class to create a rich autocomplete widget that autocompletes an
* inputbox or textarea from data provided via ajax. The server returns a
* complex data structure that is used with client-side javascript functions to
* render the results.
* @param {string} url The Uri which generates the auto complete matches.
* @param {Element} input Input element or text area.
* @param {boolean=} opt_multi Whether to allow multiple entries; defaults
* to false.
* @param {boolean=} opt_useSimilar Whether to use similar matches; e.g.
* "gost" => "ghost".
* @constructor
* @extends {goog.ui.AutoComplete.Remote}
*/
goog.ui.AutoComplete.RichRemote = function(url, input, opt_multi,
opt_useSimilar) {
// Create a custom renderer that renders rich rows. The renderer calls
// row.render(node, token) for each row.
var customRenderer = {};
customRenderer.renderRow = function(row, token, node) {
return row.data.render(node, token);
};
/**
* A standard renderer that uses a custom row renderer to display the
* rich rows generated by this autocomplete widget.
* @type {goog.ui.AutoComplete.Renderer}
* @private
*/
var renderer = new goog.ui.AutoComplete.Renderer(null, customRenderer);
this.renderer_ = renderer;
/**
* A remote matcher that parses rich results returned by the server.
* @type {goog.ui.AutoComplete.RichRemoteArrayMatcher}
* @private
*/
var matcher = new goog.ui.AutoComplete.RichRemoteArrayMatcher(url,
!opt_useSimilar);
this.matcher_ = matcher;
/**
* An input handler that calls select on a row when it is selected.
* @type {goog.ui.AutoComplete.RichInputHandler}
* @private
*/
var inputhandler = new goog.ui.AutoComplete.RichInputHandler(null, null,
!!opt_multi, 300);
// Create the widget and connect it to the input handler.
goog.ui.AutoComplete.call(this, matcher, renderer, inputhandler);
inputhandler.attachAutoComplete(this);
inputhandler.attachInputs(input);
};
goog.inherits(goog.ui.AutoComplete.RichRemote, goog.ui.AutoComplete.Remote);
/**
* Set the filter that is called before the array matches are returned.
* @param {Function} rowFilter A function(rows) that returns an array of rows as
* a subset of the rows input array.
*/
goog.ui.AutoComplete.RichRemote.prototype.setRowFilter = function(rowFilter) {
this.matcher_.setRowFilter(rowFilter);
};
|
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
'use strict';
/**
* CSS properties which accept numbers but are not in units of "px".
*/
var isUnitlessNumber = {
animationIterationCount: true,
borderImageOutset: true,
borderImageSlice: true,
borderImageWidth: true,
boxFlex: true,
boxFlexGroup: true,
boxOrdinalGroup: true,
columnCount: true,
flex: true,
flexGrow: true,
flexPositive: true,
flexShrink: true,
flexNegative: true,
flexOrder: true,
gridRow: true,
gridRowEnd: true,
gridRowSpan: true,
gridRowStart: true,
gridColumn: true,
gridColumnEnd: true,
gridColumnSpan: true,
gridColumnStart: true,
fontWeight: true,
lineClamp: true,
lineHeight: true,
opacity: true,
order: true,
orphans: true,
tabSize: true,
widows: true,
zIndex: true,
zoom: true,
// SVG-related properties
fillOpacity: true,
floodOpacity: true,
stopOpacity: true,
strokeDasharray: true,
strokeDashoffset: true,
strokeMiterlimit: true,
strokeOpacity: true,
strokeWidth: true
};
/**
* @param {string} prefix vendor-specific prefix, eg: Webkit
* @param {string} key style name, eg: transitionDuration
* @return {string} style name prefixed with `prefix`, properly camelCased, eg:
* WebkitTransitionDuration
*/
function prefixKey(prefix, key) {
return prefix + key.charAt(0).toUpperCase() + key.substring(1);
}
/**
* Support style names that may come passed in prefixed by adding permutations
* of vendor prefixes.
*/
var prefixes = ['Webkit', 'ms', 'Moz', 'O'];
// Using Object.keys here, or else the vanilla for-in loop makes IE8 go into an
// infinite loop, because it iterates over the newly added props too.
Object.keys(isUnitlessNumber).forEach(function (prop) {
prefixes.forEach(function (prefix) {
isUnitlessNumber[prefixKey(prefix, prop)] = isUnitlessNumber[prop];
});
});
/**
* Most style properties can be unset by doing .style[prop] = '' but IE8
* doesn't like doing that with shorthand properties so for the properties that
* IE8 breaks on, which are listed here, we instead unset each of the
* individual properties. See http://bugs.jquery.com/ticket/12385.
* The 4-value 'clock' properties like margin, padding, border-width seem to
* behave without any problems. Curiously, list-style works too without any
* special prodding.
*/
var shorthandPropertyExpansions = {
background: {
backgroundAttachment: true,
backgroundColor: true,
backgroundImage: true,
backgroundPositionX: true,
backgroundPositionY: true,
backgroundRepeat: true
},
backgroundPosition: {
backgroundPositionX: true,
backgroundPositionY: true
},
border: {
borderWidth: true,
borderStyle: true,
borderColor: true
},
borderBottom: {
borderBottomWidth: true,
borderBottomStyle: true,
borderBottomColor: true
},
borderLeft: {
borderLeftWidth: true,
borderLeftStyle: true,
borderLeftColor: true
},
borderRight: {
borderRightWidth: true,
borderRightStyle: true,
borderRightColor: true
},
borderTop: {
borderTopWidth: true,
borderTopStyle: true,
borderTopColor: true
},
font: {
fontStyle: true,
fontVariant: true,
fontWeight: true,
fontSize: true,
lineHeight: true,
fontFamily: true
},
outline: {
outlineWidth: true,
outlineStyle: true,
outlineColor: true
}
};
var CSSProperty = {
isUnitlessNumber: isUnitlessNumber,
shorthandPropertyExpansions: shorthandPropertyExpansions
};
module.exports = CSSProperty; |
/*
Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.md or http://ckeditor.com/license
*/
CKEDITOR.dialog.add("cellProperties",function(g){function d(a){return function(b){for(var c=a(b[0]),d=1;d<b.length;d++)if(a(b[d])!==c){c=null;break}"undefined"!=typeof c&&(this.setValue(c),CKEDITOR.env.gecko&&("select"==this.type&&!c)&&(this.getInputElement().$.selectedIndex=-1))}}function j(a){if(a=l.exec(a.getStyle("width")||a.getAttribute("width")))return a[2]}var h=g.lang.table,c=h.cell,e=g.lang.common,i=CKEDITOR.dialog.validate,l=/^(\d+(?:\.\d+)?)(px|%)$/,f={type:"html",html:" "},m="rtl"==
g.lang.dir,k=g.plugins.colordialog;return{title:c.title,minWidth:CKEDITOR.env.ie&&CKEDITOR.env.quirks?450:410,minHeight:CKEDITOR.env.ie&&(CKEDITOR.env.ie7Compat||CKEDITOR.env.quirks)?230:220,contents:[{id:"info",label:c.title,accessKey:"I",elements:[{type:"hbox",widths:["40%","5%","40%"],children:[{type:"vbox",padding:0,children:[{type:"hbox",widths:["70%","30%"],children:[{type:"text",id:"width",width:"100px",label:e.width,validate:i.number(c.invalidWidth),onLoad:function(){var a=this.getDialog().getContentElement("info",
"widthType").getElement(),b=this.getInputElement(),c=b.getAttribute("aria-labelledby");b.setAttribute("aria-labelledby",[c,a.$.id].join(" "))},setup:d(function(a){var b=parseInt(a.getAttribute("width"),10),a=parseInt(a.getStyle("width"),10);return!isNaN(a)?a:!isNaN(b)?b:""}),commit:function(a){var b=parseInt(this.getValue(),10),c=this.getDialog().getValueOf("info","widthType")||j(a);isNaN(b)?a.removeStyle("width"):a.setStyle("width",b+c);a.removeAttribute("width")},"default":""},{type:"select",id:"widthType",
label:g.lang.table.widthUnit,labelStyle:"visibility:hidden","default":"px",items:[[h.widthPx,"px"],[h.widthPc,"%"]],setup:d(j)}]},{type:"hbox",widths:["70%","30%"],children:[{type:"text",id:"height",label:e.height,width:"100px","default":"",validate:i.number(c.invalidHeight),onLoad:function(){var a=this.getDialog().getContentElement("info","htmlHeightType").getElement(),b=this.getInputElement(),c=b.getAttribute("aria-labelledby");b.setAttribute("aria-labelledby",[c,a.$.id].join(" "))},setup:d(function(a){var b=
parseInt(a.getAttribute("height"),10),a=parseInt(a.getStyle("height"),10);return!isNaN(a)?a:!isNaN(b)?b:""}),commit:function(a){var b=parseInt(this.getValue(),10);isNaN(b)?a.removeStyle("height"):a.setStyle("height",CKEDITOR.tools.cssLength(b));a.removeAttribute("height")}},{id:"htmlHeightType",type:"html",html:"<br />"+h.widthPx}]},f,{type:"select",id:"wordWrap",label:c.wordWrap,"default":"yes",items:[[c.yes,"yes"],[c.no,"no"]],setup:d(function(a){var b=a.getAttribute("noWrap");if("nowrap"==a.getStyle("white-space")||
b)return"no"}),commit:function(a){"no"==this.getValue()?a.setStyle("white-space","nowrap"):a.removeStyle("white-space");a.removeAttribute("noWrap")}},f,{type:"select",id:"hAlign",label:c.hAlign,"default":"",items:[[e.notSet,""],[e.alignLeft,"left"],[e.alignCenter,"center"],[e.alignRight,"right"],[e.alignJustify,"justify"]],setup:d(function(a){var b=a.getAttribute("align");return a.getStyle("text-align")||b||""}),commit:function(a){var b=this.getValue();b?a.setStyle("text-align",b):a.removeStyle("text-align");
a.removeAttribute("align")}},{type:"select",id:"vAlign",label:c.vAlign,"default":"",items:[[e.notSet,""],[e.alignTop,"top"],[e.alignMiddle,"middle"],[e.alignBottom,"bottom"],[c.alignBaseline,"baseline"]],setup:d(function(a){var b=a.getAttribute("vAlign"),a=a.getStyle("vertical-align");switch(a){case "top":case "middle":case "bottom":case "baseline":break;default:a=""}return a||b||""}),commit:function(a){var b=this.getValue();b?a.setStyle("vertical-align",b):a.removeStyle("vertical-align");a.removeAttribute("vAlign")}}]},
f,{type:"vbox",padding:0,children:[{type:"select",id:"cellType",label:c.cellType,"default":"td",items:[[c.data,"td"],[c.header,"th"]],setup:d(function(a){return a.getName()}),commit:function(a){a.renameNode(this.getValue())}},f,{type:"text",id:"rowSpan",label:c.rowSpan,"default":"",validate:i.integer(c.invalidRowSpan),setup:d(function(a){if((a=parseInt(a.getAttribute("rowSpan"),10))&&1!=a)return a}),commit:function(a){var b=parseInt(this.getValue(),10);b&&1!=b?a.setAttribute("rowSpan",this.getValue()):
a.removeAttribute("rowSpan")}},{type:"text",id:"colSpan",label:c.colSpan,"default":"",validate:i.integer(c.invalidColSpan),setup:d(function(a){if((a=parseInt(a.getAttribute("colSpan"),10))&&1!=a)return a}),commit:function(a){var b=parseInt(this.getValue(),10);b&&1!=b?a.setAttribute("colSpan",this.getValue()):a.removeAttribute("colSpan")}},f,{type:"hbox",padding:0,widths:["60%","40%"],children:[{type:"text",id:"bgColor",label:c.bgColor,"default":"",setup:d(function(a){var b=a.getAttribute("bgColor");
return a.getStyle("background-color")||b}),commit:function(a){this.getValue()?a.setStyle("background-color",this.getValue()):a.removeStyle("background-color");a.removeAttribute("bgColor")}},k?{type:"button",id:"bgColorChoose","class":"colorChooser",label:c.chooseColor,onLoad:function(){this.getElement().getParent().setStyle("vertical-align","bottom")},onClick:function(){g.getColorFromDialog(function(a){a&&this.getDialog().getContentElement("info","bgColor").setValue(a);this.focus()},this)}}:f]},f,
{type:"hbox",padding:0,widths:["60%","40%"],children:[{type:"text",id:"borderColor",label:c.borderColor,"default":"",setup:d(function(a){var b=a.getAttribute("borderColor");return a.getStyle("border-color")||b}),commit:function(a){this.getValue()?a.setStyle("border-color",this.getValue()):a.removeStyle("border-color");a.removeAttribute("borderColor")}},k?{type:"button",id:"borderColorChoose","class":"colorChooser",label:c.chooseColor,style:(m?"margin-right":"margin-left")+": 10px",onLoad:function(){this.getElement().getParent().setStyle("vertical-align",
"bottom")},onClick:function(){g.getColorFromDialog(function(a){a&&this.getDialog().getContentElement("info","borderColor").setValue(a);this.focus()},this)}}:f]}]}]}]}],onShow:function(){this.cells=CKEDITOR.plugins.tabletools.getSelectedCells(this._.editor.getSelection());this.setupContent(this.cells)},onOk:function(){for(var a=this._.editor.getSelection(),b=a.createBookmarks(),c=this.cells,d=0;d<c.length;d++)this.commitContent(c[d]);this._.editor.forceNextSelectionCheck();a.selectBookmarks(b);this._.editor.selectionChange()},
onLoad:function(){var a={};this.foreach(function(b){b.setup&&b.commit&&(b.setup=CKEDITOR.tools.override(b.setup,function(c){return function(){c.apply(this,arguments);a[b.id]=b.getValue()}}),b.commit=CKEDITOR.tools.override(b.commit,function(c){return function(){a[b.id]!==b.getValue()&&c.apply(this,arguments)}}))})}}});
|
/*! places UNRELEASED | © Algolia | github.com/algolia/places */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["places"] = factory();
else
root["places"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _places = __webpack_require__(1);
var _places2 = _interopRequireDefault(_places);
var _version = __webpack_require__(119);
var _version2 = _interopRequireDefault(_version);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// must use module.exports to be commonJS compatible
module.exports = _places2.default;
module.exports.version = _version2.default;
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = places;
var _algoliasearch = __webpack_require__(2);
var _algoliasearch2 = _interopRequireDefault(_algoliasearch);
var _autocomplete = __webpack_require__(95);
var _autocomplete2 = _interopRequireDefault(_autocomplete);
var _createHitFormatter = __webpack_require__(112);
var _createHitFormatter2 = _interopRequireDefault(_createHitFormatter);
var _formatInputValue = __webpack_require__(114);
var _formatInputValue2 = _interopRequireDefault(_formatInputValue);
var _formatAutocompleteSuggestion = __webpack_require__(115);
var _formatAutocompleteSuggestion2 = _interopRequireDefault(_formatAutocompleteSuggestion);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var hitFormatter = (0, _createHitFormatter2.default)({
formatAutocompleteSuggestion: _formatAutocompleteSuggestion2.default,
formatInputValue: _formatInputValue2.default
}); /* eslint no-console:0 */
function places(_ref) {
var
// countries,
// language = navigator.language,
container = _ref.container;
var placesAPIClient = _algoliasearch2.default.initPlaces('places', '5c759b588c767287a3dca1e8e18232f8');
(0, _autocomplete2.default)(container, { debug: true, autoselect: true }, {
source: function source(query, cb) {
return placesAPIClient.search({ query: query }).then(function (_ref2) {
var hits = _ref2.hits;
return hits.slice(0, 5).map(hitFormatter);
}).then(function (hits) {
console.log(hits);return hits;
}).then(cb).catch(function (err) {
return console.error(err);
});
},
templates: {
suggestion: function suggestion(hit) {
return hit.suggestion;
}
}
});
}
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
// This is the standalone browser build entry point
// Browser implementation of the Algolia Search JavaScript client,
// using XMLHttpRequest, XDomainRequest and JSONP as fallback
module.exports = algoliasearch;
var inherits = __webpack_require__(3);
var Promise = window.Promise || __webpack_require__(4).Promise;
var AlgoliaSearch = __webpack_require__(9);
var errors = __webpack_require__(10);
var inlineHeaders = __webpack_require__(86);
var jsonpRequest = __webpack_require__(90);
var places = __webpack_require__(91);
if (({"NODE_ENV":"production"}).APP_ENV === 'development') {
__webpack_require__(38).enable('algoliasearch*');
}
function algoliasearch(applicationID, apiKey, opts) {
var cloneDeep = __webpack_require__(92);
var getDocumentProtocol = __webpack_require__(93);
opts = cloneDeep(opts || {});
if (opts.protocol === undefined) {
opts.protocol = getDocumentProtocol();
}
opts._ua = opts._ua || algoliasearch.ua;
return new AlgoliaSearchBrowser(applicationID, apiKey, opts);
}
algoliasearch.version = __webpack_require__(94);
algoliasearch.ua = 'Algolia for vanilla JavaScript ' + algoliasearch.version;
algoliasearch.initPlaces = places(algoliasearch);
// we expose into window no matter how we are used, this will allow
// us to easily debug any website running algolia
window.__algolia = {
debug: __webpack_require__(38),
algoliasearch: algoliasearch
};
var support = {
hasXMLHttpRequest: 'XMLHttpRequest' in window,
hasXDomainRequest: 'XDomainRequest' in window,
cors: 'withCredentials' in new XMLHttpRequest(),
timeout: 'timeout' in new XMLHttpRequest()
};
function AlgoliaSearchBrowser() {
// call AlgoliaSearch constructor
AlgoliaSearch.apply(this, arguments);
}
inherits(AlgoliaSearchBrowser, AlgoliaSearch);
AlgoliaSearchBrowser.prototype._request = function request(url, opts) {
return new Promise(function wrapRequest(resolve, reject) {
// no cors or XDomainRequest, no request
if (!support.cors && !support.hasXDomainRequest) {
// very old browser, not supported
reject(new errors.Network('CORS not supported'));
return;
}
url = inlineHeaders(url, opts.headers);
var body = opts.body;
var req = support.cors ? new XMLHttpRequest() : new XDomainRequest();
var ontimeout;
var timedOut;
// do not rely on default XHR async flag, as some analytics code like hotjar
// breaks it and set it to false by default
if (req instanceof XMLHttpRequest) {
req.open(opts.method, url, true);
} else {
req.open(opts.method, url);
}
if (support.cors) {
if (body) {
if (opts.method === 'POST') {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Simple_requests
req.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
} else {
req.setRequestHeader('content-type', 'application/json');
}
}
req.setRequestHeader('accept', 'application/json');
}
// we set an empty onprogress listener
// so that XDomainRequest on IE9 is not aborted
// refs:
// - https://github.com/algolia/algoliasearch-client-js/issues/76
// - https://social.msdn.microsoft.com/Forums/ie/en-US/30ef3add-767c-4436-b8a9-f1ca19b4812e/ie9-rtm-xdomainrequest-issued-requests-may-abort-if-all-event-handlers-not-specified?forum=iewebdevelopment
req.onprogress = function noop() {};
req.onload = load;
req.onerror = error;
if (support.timeout) {
// .timeout supported by both XHR and XDR,
// we do receive timeout event, tested
req.timeout = opts.timeout;
req.ontimeout = timeout;
} else {
ontimeout = setTimeout(timeout, opts.timeout);
}
req.send(body);
// event object not received in IE8, at least
// but we do not use it, still important to note
function load(/* event */) {
// When browser does not supports req.timeout, we can
// have both a load and timeout event, since handled by a dumb setTimeout
if (timedOut) {
return;
}
if (!support.timeout) {
clearTimeout(ontimeout);
}
var out;
try {
out = {
body: JSON.parse(req.responseText),
responseText: req.responseText,
statusCode: req.status,
// XDomainRequest does not have any response headers
headers: req.getAllResponseHeaders && req.getAllResponseHeaders() || {}
};
} catch (e) {
out = new errors.UnparsableJSON({
more: req.responseText
});
}
if (out instanceof errors.UnparsableJSON) {
reject(out);
} else {
resolve(out);
}
}
function error(event) {
if (timedOut) {
return;
}
if (!support.timeout) {
clearTimeout(ontimeout);
}
// error event is trigerred both with XDR/XHR on:
// - DNS error
// - unallowed cross domain request
reject(
new errors.Network({
more: event
})
);
}
function timeout() {
if (!support.timeout) {
timedOut = true;
req.abort();
}
reject(new errors.RequestTimeout());
}
});
};
AlgoliaSearchBrowser.prototype._request.fallback = function requestFallback(url, opts) {
url = inlineHeaders(url, opts.headers);
return new Promise(function wrapJsonpRequest(resolve, reject) {
jsonpRequest(url, opts, function jsonpRequestDone(err, content) {
if (err) {
reject(err);
return;
}
resolve(content);
});
});
};
AlgoliaSearchBrowser.prototype._promise = {
reject: function rejectPromise(val) {
return Promise.reject(val);
},
resolve: function resolvePromise(val) {
return Promise.resolve(val);
},
delay: function delayPromise(ms) {
return new Promise(function resolveOnTimeout(resolve/* , reject*/) {
setTimeout(resolve, ms);
});
}
};
/***/ },
/* 3 */
/***/ function(module, exports) {
if (typeof Object.create === 'function') {
// implementation from standard node.js 'util' module
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
} else {
// old school shim for old browsers
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
var TempCtor = function () {}
TempCtor.prototype = superCtor.prototype
ctor.prototype = new TempCtor()
ctor.prototype.constructor = ctor
}
}
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
var require;var __WEBPACK_AMD_DEFINE_RESULT__;/* WEBPACK VAR INJECTION */(function(process, global, module) {/*!
* @overview es6-promise - a tiny implementation of Promises/A+.
* @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)
* @license Licensed under MIT license
* See https://raw.githubusercontent.com/jakearchibald/es6-promise/master/LICENSE
* @version 3.1.2
*/
(function() {
"use strict";
function lib$es6$promise$utils$$objectOrFunction(x) {
return typeof x === 'function' || (typeof x === 'object' && x !== null);
}
function lib$es6$promise$utils$$isFunction(x) {
return typeof x === 'function';
}
function lib$es6$promise$utils$$isMaybeThenable(x) {
return typeof x === 'object' && x !== null;
}
var lib$es6$promise$utils$$_isArray;
if (!Array.isArray) {
lib$es6$promise$utils$$_isArray = function (x) {
return Object.prototype.toString.call(x) === '[object Array]';
};
} else {
lib$es6$promise$utils$$_isArray = Array.isArray;
}
var lib$es6$promise$utils$$isArray = lib$es6$promise$utils$$_isArray;
var lib$es6$promise$asap$$len = 0;
var lib$es6$promise$asap$$vertxNext;
var lib$es6$promise$asap$$customSchedulerFn;
var lib$es6$promise$asap$$asap = function asap(callback, arg) {
lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len] = callback;
lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len + 1] = arg;
lib$es6$promise$asap$$len += 2;
if (lib$es6$promise$asap$$len === 2) {
// If len is 2, that means that we need to schedule an async flush.
// If additional callbacks are queued before the queue is flushed, they
// will be processed by this flush that we are scheduling.
if (lib$es6$promise$asap$$customSchedulerFn) {
lib$es6$promise$asap$$customSchedulerFn(lib$es6$promise$asap$$flush);
} else {
lib$es6$promise$asap$$scheduleFlush();
}
}
}
function lib$es6$promise$asap$$setScheduler(scheduleFn) {
lib$es6$promise$asap$$customSchedulerFn = scheduleFn;
}
function lib$es6$promise$asap$$setAsap(asapFn) {
lib$es6$promise$asap$$asap = asapFn;
}
var lib$es6$promise$asap$$browserWindow = (typeof window !== 'undefined') ? window : undefined;
var lib$es6$promise$asap$$browserGlobal = lib$es6$promise$asap$$browserWindow || {};
var lib$es6$promise$asap$$BrowserMutationObserver = lib$es6$promise$asap$$browserGlobal.MutationObserver || lib$es6$promise$asap$$browserGlobal.WebKitMutationObserver;
var lib$es6$promise$asap$$isNode = typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';
// test for web worker but not in IE10
var lib$es6$promise$asap$$isWorker = typeof Uint8ClampedArray !== 'undefined' &&
typeof importScripts !== 'undefined' &&
typeof MessageChannel !== 'undefined';
// node
function lib$es6$promise$asap$$useNextTick() {
// node version 0.10.x displays a deprecation warning when nextTick is used recursively
// see https://github.com/cujojs/when/issues/410 for details
return function() {
process.nextTick(lib$es6$promise$asap$$flush);
};
}
// vertx
function lib$es6$promise$asap$$useVertxTimer() {
return function() {
lib$es6$promise$asap$$vertxNext(lib$es6$promise$asap$$flush);
};
}
function lib$es6$promise$asap$$useMutationObserver() {
var iterations = 0;
var observer = new lib$es6$promise$asap$$BrowserMutationObserver(lib$es6$promise$asap$$flush);
var node = document.createTextNode('');
observer.observe(node, { characterData: true });
return function() {
node.data = (iterations = ++iterations % 2);
};
}
// web worker
function lib$es6$promise$asap$$useMessageChannel() {
var channel = new MessageChannel();
channel.port1.onmessage = lib$es6$promise$asap$$flush;
return function () {
channel.port2.postMessage(0);
};
}
function lib$es6$promise$asap$$useSetTimeout() {
return function() {
setTimeout(lib$es6$promise$asap$$flush, 1);
};
}
var lib$es6$promise$asap$$queue = new Array(1000);
function lib$es6$promise$asap$$flush() {
for (var i = 0; i < lib$es6$promise$asap$$len; i+=2) {
var callback = lib$es6$promise$asap$$queue[i];
var arg = lib$es6$promise$asap$$queue[i+1];
callback(arg);
lib$es6$promise$asap$$queue[i] = undefined;
lib$es6$promise$asap$$queue[i+1] = undefined;
}
lib$es6$promise$asap$$len = 0;
}
function lib$es6$promise$asap$$attemptVertx() {
try {
var r = require;
var vertx = __webpack_require__(7);
lib$es6$promise$asap$$vertxNext = vertx.runOnLoop || vertx.runOnContext;
return lib$es6$promise$asap$$useVertxTimer();
} catch(e) {
return lib$es6$promise$asap$$useSetTimeout();
}
}
var lib$es6$promise$asap$$scheduleFlush;
// Decide what async method to use to triggering processing of queued callbacks:
if (lib$es6$promise$asap$$isNode) {
lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useNextTick();
} else if (lib$es6$promise$asap$$BrowserMutationObserver) {
lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useMutationObserver();
} else if (lib$es6$promise$asap$$isWorker) {
lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useMessageChannel();
} else if (lib$es6$promise$asap$$browserWindow === undefined && "function" === 'function') {
lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$attemptVertx();
} else {
lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useSetTimeout();
}
function lib$es6$promise$then$$then(onFulfillment, onRejection) {
var parent = this;
var state = parent._state;
if (state === lib$es6$promise$$internal$$FULFILLED && !onFulfillment || state === lib$es6$promise$$internal$$REJECTED && !onRejection) {
return this;
}
var child = new this.constructor(lib$es6$promise$$internal$$noop);
var result = parent._result;
if (state) {
var callback = arguments[state - 1];
lib$es6$promise$asap$$asap(function(){
lib$es6$promise$$internal$$invokeCallback(state, child, callback, result);
});
} else {
lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection);
}
return child;
}
var lib$es6$promise$then$$default = lib$es6$promise$then$$then;
function lib$es6$promise$promise$resolve$$resolve(object) {
/*jshint validthis:true */
var Constructor = this;
if (object && typeof object === 'object' && object.constructor === Constructor) {
return object;
}
var promise = new Constructor(lib$es6$promise$$internal$$noop);
lib$es6$promise$$internal$$resolve(promise, object);
return promise;
}
var lib$es6$promise$promise$resolve$$default = lib$es6$promise$promise$resolve$$resolve;
function lib$es6$promise$$internal$$noop() {}
var lib$es6$promise$$internal$$PENDING = void 0;
var lib$es6$promise$$internal$$FULFILLED = 1;
var lib$es6$promise$$internal$$REJECTED = 2;
var lib$es6$promise$$internal$$GET_THEN_ERROR = new lib$es6$promise$$internal$$ErrorObject();
function lib$es6$promise$$internal$$selfFulfillment() {
return new TypeError("You cannot resolve a promise with itself");
}
function lib$es6$promise$$internal$$cannotReturnOwn() {
return new TypeError('A promises callback cannot return that same promise.');
}
function lib$es6$promise$$internal$$getThen(promise) {
try {
return promise.then;
} catch(error) {
lib$es6$promise$$internal$$GET_THEN_ERROR.error = error;
return lib$es6$promise$$internal$$GET_THEN_ERROR;
}
}
function lib$es6$promise$$internal$$tryThen(then, value, fulfillmentHandler, rejectionHandler) {
try {
then.call(value, fulfillmentHandler, rejectionHandler);
} catch(e) {
return e;
}
}
function lib$es6$promise$$internal$$handleForeignThenable(promise, thenable, then) {
lib$es6$promise$asap$$asap(function(promise) {
var sealed = false;
var error = lib$es6$promise$$internal$$tryThen(then, thenable, function(value) {
if (sealed) { return; }
sealed = true;
if (thenable !== value) {
lib$es6$promise$$internal$$resolve(promise, value);
} else {
lib$es6$promise$$internal$$fulfill(promise, value);
}
}, function(reason) {
if (sealed) { return; }
sealed = true;
lib$es6$promise$$internal$$reject(promise, reason);
}, 'Settle: ' + (promise._label || ' unknown promise'));
if (!sealed && error) {
sealed = true;
lib$es6$promise$$internal$$reject(promise, error);
}
}, promise);
}
function lib$es6$promise$$internal$$handleOwnThenable(promise, thenable) {
if (thenable._state === lib$es6$promise$$internal$$FULFILLED) {
lib$es6$promise$$internal$$fulfill(promise, thenable._result);
} else if (thenable._state === lib$es6$promise$$internal$$REJECTED) {
lib$es6$promise$$internal$$reject(promise, thenable._result);
} else {
lib$es6$promise$$internal$$subscribe(thenable, undefined, function(value) {
lib$es6$promise$$internal$$resolve(promise, value);
}, function(reason) {
lib$es6$promise$$internal$$reject(promise, reason);
});
}
}
function lib$es6$promise$$internal$$handleMaybeThenable(promise, maybeThenable, then) {
if (maybeThenable.constructor === promise.constructor &&
then === lib$es6$promise$then$$default &&
constructor.resolve === lib$es6$promise$promise$resolve$$default) {
lib$es6$promise$$internal$$handleOwnThenable(promise, maybeThenable);
} else {
if (then === lib$es6$promise$$internal$$GET_THEN_ERROR) {
lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$GET_THEN_ERROR.error);
} else if (then === undefined) {
lib$es6$promise$$internal$$fulfill(promise, maybeThenable);
} else if (lib$es6$promise$utils$$isFunction(then)) {
lib$es6$promise$$internal$$handleForeignThenable(promise, maybeThenable, then);
} else {
lib$es6$promise$$internal$$fulfill(promise, maybeThenable);
}
}
}
function lib$es6$promise$$internal$$resolve(promise, value) {
if (promise === value) {
lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$selfFulfillment());
} else if (lib$es6$promise$utils$$objectOrFunction(value)) {
lib$es6$promise$$internal$$handleMaybeThenable(promise, value, lib$es6$promise$$internal$$getThen(value));
} else {
lib$es6$promise$$internal$$fulfill(promise, value);
}
}
function lib$es6$promise$$internal$$publishRejection(promise) {
if (promise._onerror) {
promise._onerror(promise._result);
}
lib$es6$promise$$internal$$publish(promise);
}
function lib$es6$promise$$internal$$fulfill(promise, value) {
if (promise._state !== lib$es6$promise$$internal$$PENDING) { return; }
promise._result = value;
promise._state = lib$es6$promise$$internal$$FULFILLED;
if (promise._subscribers.length !== 0) {
lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, promise);
}
}
function lib$es6$promise$$internal$$reject(promise, reason) {
if (promise._state !== lib$es6$promise$$internal$$PENDING) { return; }
promise._state = lib$es6$promise$$internal$$REJECTED;
promise._result = reason;
lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publishRejection, promise);
}
function lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection) {
var subscribers = parent._subscribers;
var length = subscribers.length;
parent._onerror = null;
subscribers[length] = child;
subscribers[length + lib$es6$promise$$internal$$FULFILLED] = onFulfillment;
subscribers[length + lib$es6$promise$$internal$$REJECTED] = onRejection;
if (length === 0 && parent._state) {
lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, parent);
}
}
function lib$es6$promise$$internal$$publish(promise) {
var subscribers = promise._subscribers;
var settled = promise._state;
if (subscribers.length === 0) { return; }
var child, callback, detail = promise._result;
for (var i = 0; i < subscribers.length; i += 3) {
child = subscribers[i];
callback = subscribers[i + settled];
if (child) {
lib$es6$promise$$internal$$invokeCallback(settled, child, callback, detail);
} else {
callback(detail);
}
}
promise._subscribers.length = 0;
}
function lib$es6$promise$$internal$$ErrorObject() {
this.error = null;
}
var lib$es6$promise$$internal$$TRY_CATCH_ERROR = new lib$es6$promise$$internal$$ErrorObject();
function lib$es6$promise$$internal$$tryCatch(callback, detail) {
try {
return callback(detail);
} catch(e) {
lib$es6$promise$$internal$$TRY_CATCH_ERROR.error = e;
return lib$es6$promise$$internal$$TRY_CATCH_ERROR;
}
}
function lib$es6$promise$$internal$$invokeCallback(settled, promise, callback, detail) {
var hasCallback = lib$es6$promise$utils$$isFunction(callback),
value, error, succeeded, failed;
if (hasCallback) {
value = lib$es6$promise$$internal$$tryCatch(callback, detail);
if (value === lib$es6$promise$$internal$$TRY_CATCH_ERROR) {
failed = true;
error = value.error;
value = null;
} else {
succeeded = true;
}
if (promise === value) {
lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$cannotReturnOwn());
return;
}
} else {
value = detail;
succeeded = true;
}
if (promise._state !== lib$es6$promise$$internal$$PENDING) {
// noop
} else if (hasCallback && succeeded) {
lib$es6$promise$$internal$$resolve(promise, value);
} else if (failed) {
lib$es6$promise$$internal$$reject(promise, error);
} else if (settled === lib$es6$promise$$internal$$FULFILLED) {
lib$es6$promise$$internal$$fulfill(promise, value);
} else if (settled === lib$es6$promise$$internal$$REJECTED) {
lib$es6$promise$$internal$$reject(promise, value);
}
}
function lib$es6$promise$$internal$$initializePromise(promise, resolver) {
try {
resolver(function resolvePromise(value){
lib$es6$promise$$internal$$resolve(promise, value);
}, function rejectPromise(reason) {
lib$es6$promise$$internal$$reject(promise, reason);
});
} catch(e) {
lib$es6$promise$$internal$$reject(promise, e);
}
}
function lib$es6$promise$promise$all$$all(entries) {
return new lib$es6$promise$enumerator$$default(this, entries).promise;
}
var lib$es6$promise$promise$all$$default = lib$es6$promise$promise$all$$all;
function lib$es6$promise$promise$race$$race(entries) {
/*jshint validthis:true */
var Constructor = this;
var promise = new Constructor(lib$es6$promise$$internal$$noop);
if (!lib$es6$promise$utils$$isArray(entries)) {
lib$es6$promise$$internal$$reject(promise, new TypeError('You must pass an array to race.'));
return promise;
}
var length = entries.length;
function onFulfillment(value) {
lib$es6$promise$$internal$$resolve(promise, value);
}
function onRejection(reason) {
lib$es6$promise$$internal$$reject(promise, reason);
}
for (var i = 0; promise._state === lib$es6$promise$$internal$$PENDING && i < length; i++) {
lib$es6$promise$$internal$$subscribe(Constructor.resolve(entries[i]), undefined, onFulfillment, onRejection);
}
return promise;
}
var lib$es6$promise$promise$race$$default = lib$es6$promise$promise$race$$race;
function lib$es6$promise$promise$reject$$reject(reason) {
/*jshint validthis:true */
var Constructor = this;
var promise = new Constructor(lib$es6$promise$$internal$$noop);
lib$es6$promise$$internal$$reject(promise, reason);
return promise;
}
var lib$es6$promise$promise$reject$$default = lib$es6$promise$promise$reject$$reject;
var lib$es6$promise$promise$$counter = 0;
function lib$es6$promise$promise$$needsResolver() {
throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
}
function lib$es6$promise$promise$$needsNew() {
throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
}
var lib$es6$promise$promise$$default = lib$es6$promise$promise$$Promise;
/**
Promise objects represent the eventual result of an asynchronous operation. The
primary way of interacting with a promise is through its `then` method, which
registers callbacks to receive either a promise's eventual value or the reason
why the promise cannot be fulfilled.
Terminology
-----------
- `promise` is an object or function with a `then` method whose behavior conforms to this specification.
- `thenable` is an object or function that defines a `then` method.
- `value` is any legal JavaScript value (including undefined, a thenable, or a promise).
- `exception` is a value that is thrown using the throw statement.
- `reason` is a value that indicates why a promise was rejected.
- `settled` the final resting state of a promise, fulfilled or rejected.
A promise can be in one of three states: pending, fulfilled, or rejected.
Promises that are fulfilled have a fulfillment value and are in the fulfilled
state. Promises that are rejected have a rejection reason and are in the
rejected state. A fulfillment value is never a thenable.
Promises can also be said to *resolve* a value. If this value is also a
promise, then the original promise's settled state will match the value's
settled state. So a promise that *resolves* a promise that rejects will
itself reject, and a promise that *resolves* a promise that fulfills will
itself fulfill.
Basic Usage:
------------
```js
var promise = new Promise(function(resolve, reject) {
// on success
resolve(value);
// on failure
reject(reason);
});
promise.then(function(value) {
// on fulfillment
}, function(reason) {
// on rejection
});
```
Advanced Usage:
---------------
Promises shine when abstracting away asynchronous interactions such as
`XMLHttpRequest`s.
```js
function getJSON(url) {
return new Promise(function(resolve, reject){
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onreadystatechange = handler;
xhr.responseType = 'json';
xhr.setRequestHeader('Accept', 'application/json');
xhr.send();
function handler() {
if (this.readyState === this.DONE) {
if (this.status === 200) {
resolve(this.response);
} else {
reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']'));
}
}
};
});
}
getJSON('/posts.json').then(function(json) {
// on fulfillment
}, function(reason) {
// on rejection
});
```
Unlike callbacks, promises are great composable primitives.
```js
Promise.all([
getJSON('/posts'),
getJSON('/comments')
]).then(function(values){
values[0] // => postsJSON
values[1] // => commentsJSON
return values;
});
```
@class Promise
@param {function} resolver
Useful for tooling.
@constructor
*/
function lib$es6$promise$promise$$Promise(resolver) {
this._id = lib$es6$promise$promise$$counter++;
this._state = undefined;
this._result = undefined;
this._subscribers = [];
if (lib$es6$promise$$internal$$noop !== resolver) {
typeof resolver !== 'function' && lib$es6$promise$promise$$needsResolver();
this instanceof lib$es6$promise$promise$$Promise ? lib$es6$promise$$internal$$initializePromise(this, resolver) : lib$es6$promise$promise$$needsNew();
}
}
lib$es6$promise$promise$$Promise.all = lib$es6$promise$promise$all$$default;
lib$es6$promise$promise$$Promise.race = lib$es6$promise$promise$race$$default;
lib$es6$promise$promise$$Promise.resolve = lib$es6$promise$promise$resolve$$default;
lib$es6$promise$promise$$Promise.reject = lib$es6$promise$promise$reject$$default;
lib$es6$promise$promise$$Promise._setScheduler = lib$es6$promise$asap$$setScheduler;
lib$es6$promise$promise$$Promise._setAsap = lib$es6$promise$asap$$setAsap;
lib$es6$promise$promise$$Promise._asap = lib$es6$promise$asap$$asap;
lib$es6$promise$promise$$Promise.prototype = {
constructor: lib$es6$promise$promise$$Promise,
/**
The primary way of interacting with a promise is through its `then` method,
which registers callbacks to receive either a promise's eventual value or the
reason why the promise cannot be fulfilled.
```js
findUser().then(function(user){
// user is available
}, function(reason){
// user is unavailable, and you are given the reason why
});
```
Chaining
--------
The return value of `then` is itself a promise. This second, 'downstream'
promise is resolved with the return value of the first promise's fulfillment
or rejection handler, or rejected if the handler throws an exception.
```js
findUser().then(function (user) {
return user.name;
}, function (reason) {
return 'default name';
}).then(function (userName) {
// If `findUser` fulfilled, `userName` will be the user's name, otherwise it
// will be `'default name'`
});
findUser().then(function (user) {
throw new Error('Found user, but still unhappy');
}, function (reason) {
throw new Error('`findUser` rejected and we're unhappy');
}).then(function (value) {
// never reached
}, function (reason) {
// if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'.
// If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'.
});
```
If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream.
```js
findUser().then(function (user) {
throw new PedagogicalException('Upstream error');
}).then(function (value) {
// never reached
}).then(function (value) {
// never reached
}, function (reason) {
// The `PedgagocialException` is propagated all the way down to here
});
```
Assimilation
------------
Sometimes the value you want to propagate to a downstream promise can only be
retrieved asynchronously. This can be achieved by returning a promise in the
fulfillment or rejection handler. The downstream promise will then be pending
until the returned promise is settled. This is called *assimilation*.
```js
findUser().then(function (user) {
return findCommentsByAuthor(user);
}).then(function (comments) {
// The user's comments are now available
});
```
If the assimliated promise rejects, then the downstream promise will also reject.
```js
findUser().then(function (user) {
return findCommentsByAuthor(user);
}).then(function (comments) {
// If `findCommentsByAuthor` fulfills, we'll have the value here
}, function (reason) {
// If `findCommentsByAuthor` rejects, we'll have the reason here
});
```
Simple Example
--------------
Synchronous Example
```javascript
var result;
try {
result = findResult();
// success
} catch(reason) {
// failure
}
```
Errback Example
```js
findResult(function(result, err){
if (err) {
// failure
} else {
// success
}
});
```
Promise Example;
```javascript
findResult().then(function(result){
// success
}, function(reason){
// failure
});
```
Advanced Example
--------------
Synchronous Example
```javascript
var author, books;
try {
author = findAuthor();
books = findBooksByAuthor(author);
// success
} catch(reason) {
// failure
}
```
Errback Example
```js
function foundBooks(books) {
}
function failure(reason) {
}
findAuthor(function(author, err){
if (err) {
failure(err);
// failure
} else {
try {
findBoooksByAuthor(author, function(books, err) {
if (err) {
failure(err);
} else {
try {
foundBooks(books);
} catch(reason) {
failure(reason);
}
}
});
} catch(error) {
failure(err);
}
// success
}
});
```
Promise Example;
```javascript
findAuthor().
then(findBooksByAuthor).
then(function(books){
// found books
}).catch(function(reason){
// something went wrong
});
```
@method then
@param {Function} onFulfilled
@param {Function} onRejected
Useful for tooling.
@return {Promise}
*/
then: lib$es6$promise$then$$default,
/**
`catch` is simply sugar for `then(undefined, onRejection)` which makes it the same
as the catch block of a try/catch statement.
```js
function findAuthor(){
throw new Error('couldn't find that author');
}
// synchronous
try {
findAuthor();
} catch(reason) {
// something went wrong
}
// async with promises
findAuthor().catch(function(reason){
// something went wrong
});
```
@method catch
@param {Function} onRejection
Useful for tooling.
@return {Promise}
*/
'catch': function(onRejection) {
return this.then(null, onRejection);
}
};
var lib$es6$promise$enumerator$$default = lib$es6$promise$enumerator$$Enumerator;
function lib$es6$promise$enumerator$$Enumerator(Constructor, input) {
this._instanceConstructor = Constructor;
this.promise = new Constructor(lib$es6$promise$$internal$$noop);
if (Array.isArray(input)) {
this._input = input;
this.length = input.length;
this._remaining = input.length;
this._result = new Array(this.length);
if (this.length === 0) {
lib$es6$promise$$internal$$fulfill(this.promise, this._result);
} else {
this.length = this.length || 0;
this._enumerate();
if (this._remaining === 0) {
lib$es6$promise$$internal$$fulfill(this.promise, this._result);
}
}
} else {
lib$es6$promise$$internal$$reject(this.promise, this._validationError());
}
}
lib$es6$promise$enumerator$$Enumerator.prototype._validationError = function() {
return new Error('Array Methods must be provided an Array');
};
lib$es6$promise$enumerator$$Enumerator.prototype._enumerate = function() {
var length = this.length;
var input = this._input;
for (var i = 0; this._state === lib$es6$promise$$internal$$PENDING && i < length; i++) {
this._eachEntry(input[i], i);
}
};
lib$es6$promise$enumerator$$Enumerator.prototype._eachEntry = function(entry, i) {
var c = this._instanceConstructor;
var resolve = c.resolve;
if (resolve === lib$es6$promise$promise$resolve$$default) {
var then = lib$es6$promise$$internal$$getThen(entry);
if (then === lib$es6$promise$then$$default &&
entry._state !== lib$es6$promise$$internal$$PENDING) {
this._settledAt(entry._state, i, entry._result);
} else if (typeof then !== 'function') {
this._remaining--;
this._result[i] = entry;
} else if (c === lib$es6$promise$promise$$default) {
var promise = new c(lib$es6$promise$$internal$$noop);
lib$es6$promise$$internal$$handleMaybeThenable(promise, entry, then);
this._willSettleAt(promise, i);
} else {
this._willSettleAt(new c(function(resolve) { resolve(entry); }), i);
}
} else {
this._willSettleAt(resolve(entry), i);
}
};
lib$es6$promise$enumerator$$Enumerator.prototype._settledAt = function(state, i, value) {
var promise = this.promise;
if (promise._state === lib$es6$promise$$internal$$PENDING) {
this._remaining--;
if (state === lib$es6$promise$$internal$$REJECTED) {
lib$es6$promise$$internal$$reject(promise, value);
} else {
this._result[i] = value;
}
}
if (this._remaining === 0) {
lib$es6$promise$$internal$$fulfill(promise, this._result);
}
};
lib$es6$promise$enumerator$$Enumerator.prototype._willSettleAt = function(promise, i) {
var enumerator = this;
lib$es6$promise$$internal$$subscribe(promise, undefined, function(value) {
enumerator._settledAt(lib$es6$promise$$internal$$FULFILLED, i, value);
}, function(reason) {
enumerator._settledAt(lib$es6$promise$$internal$$REJECTED, i, reason);
});
};
function lib$es6$promise$polyfill$$polyfill() {
var local;
if (typeof global !== 'undefined') {
local = global;
} else if (typeof self !== 'undefined') {
local = self;
} else {
try {
local = Function('return this')();
} catch (e) {
throw new Error('polyfill failed because global object is unavailable in this environment');
}
}
var P = local.Promise;
if (P && Object.prototype.toString.call(P.resolve()) === '[object Promise]' && !P.cast) {
return;
}
local.Promise = lib$es6$promise$promise$$default;
}
var lib$es6$promise$polyfill$$default = lib$es6$promise$polyfill$$polyfill;
var lib$es6$promise$umd$$ES6Promise = {
'Promise': lib$es6$promise$promise$$default,
'polyfill': lib$es6$promise$polyfill$$default
};
/* global define:true module:true window: true */
if ("function" === 'function' && __webpack_require__(8)['amd']) {
!(__WEBPACK_AMD_DEFINE_RESULT__ = function() { return lib$es6$promise$umd$$ES6Promise; }.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
} else if (typeof module !== 'undefined' && module['exports']) {
module['exports'] = lib$es6$promise$umd$$ES6Promise;
} else if (typeof this !== 'undefined') {
this['ES6Promise'] = lib$es6$promise$umd$$ES6Promise;
}
lib$es6$promise$polyfill$$default();
}).call(this);
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(5), (function() { return this; }()), __webpack_require__(6)(module)))
/***/ },
/* 5 */
/***/ function(module, exports) {
// shim for using process in browser
var process = module.exports = {};
var queue = [];
var draining = false;
var currentQueue;
var queueIndex = -1;
function cleanUpNextTick() {
draining = false;
if (currentQueue.length) {
queue = currentQueue.concat(queue);
} else {
queueIndex = -1;
}
if (queue.length) {
drainQueue();
}
}
function drainQueue() {
if (draining) {
return;
}
var timeout = setTimeout(cleanUpNextTick);
draining = true;
var len = queue.length;
while(len) {
currentQueue = queue;
queue = [];
while (++queueIndex < len) {
if (currentQueue) {
currentQueue[queueIndex].run();
}
}
queueIndex = -1;
len = queue.length;
}
currentQueue = null;
draining = false;
clearTimeout(timeout);
}
process.nextTick = function (fun) {
var args = new Array(arguments.length - 1);
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
}
queue.push(new Item(fun, args));
if (queue.length === 1 && !draining) {
setTimeout(drainQueue, 0);
}
};
// v8 likes predictible objects
function Item(fun, array) {
this.fun = fun;
this.array = array;
}
Item.prototype.run = function () {
this.fun.apply(null, this.array);
};
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
process.version = ''; // empty string to avoid regexp issues
process.versions = {};
function noop() {}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
process.binding = function (name) {
throw new Error('process.binding is not supported');
};
process.cwd = function () { return '/' };
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
process.umask = function() { return 0; };
/***/ },
/* 6 */
/***/ function(module, exports) {
module.exports = function(module) {
if(!module.webpackPolyfill) {
module.deprecate = function() {};
module.paths = [];
// module.parent = undefined by default
module.children = [];
module.webpackPolyfill = 1;
}
return module;
}
/***/ },
/* 7 */
/***/ function(module, exports) {
/* (ignored) */
/***/ },
/* 8 */
/***/ function(module, exports) {
module.exports = function() { throw new Error("define cannot be used indirect"); };
/***/ },
/* 9 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
module.exports = AlgoliaSearch;
var errors = __webpack_require__(10);
var buildSearchMethod = __webpack_require__(37);
// We will always put the API KEY in the JSON body in case of too long API KEY
var MAX_API_KEY_LENGTH = 500;
/*
* Algolia Search library initialization
* https://www.algolia.com/
*
* @param {string} applicationID - Your applicationID, found in your dashboard
* @param {string} apiKey - Your API key, found in your dashboard
* @param {Object} [opts]
* @param {number} [opts.timeout=2000] - The request timeout set in milliseconds,
* another request will be issued after this timeout
* @param {string} [opts.protocol='http:'] - The protocol used to query Algolia Search API.
* Set to 'https:' to force using https.
* Default to document.location.protocol in browsers
* @param {Object|Array} [opts.hosts={
* read: [this.applicationID + '-dsn.algolia.net'].concat([
* this.applicationID + '-1.algolianet.com',
* this.applicationID + '-2.algolianet.com',
* this.applicationID + '-3.algolianet.com']
* ]),
* write: [this.applicationID + '.algolia.net'].concat([
* this.applicationID + '-1.algolianet.com',
* this.applicationID + '-2.algolianet.com',
* this.applicationID + '-3.algolianet.com']
* ]) - The hosts to use for Algolia Search API.
* If you provide them, you will less benefit from our HA implementation
*/
function AlgoliaSearch(applicationID, apiKey, opts) {
var debug = __webpack_require__(38)('algoliasearch');
var clone = __webpack_require__(41);
var isArray = __webpack_require__(30);
var map = __webpack_require__(51);
var usage = 'Usage: algoliasearch(applicationID, apiKey, opts)';
if (!applicationID) {
throw new errors.AlgoliaSearchError('Please provide an application ID. ' + usage);
}
if (!apiKey) {
throw new errors.AlgoliaSearchError('Please provide an API key. ' + usage);
}
this.applicationID = applicationID;
this.apiKey = apiKey;
var defaultHosts = [
this.applicationID + '-1.algolianet.com',
this.applicationID + '-2.algolianet.com',
this.applicationID + '-3.algolianet.com'
];
this.hosts = {
read: [],
write: []
};
this.hostIndex = {
read: 0,
write: 0
};
opts = opts || {};
var protocol = opts.protocol || 'https:';
var timeout = opts.timeout === undefined ? 2000 : opts.timeout;
// while we advocate for colon-at-the-end values: 'http:' for `opts.protocol`
// we also accept `http` and `https`. It's a common error.
if (!/:$/.test(protocol)) {
protocol = protocol + ':';
}
if (opts.protocol !== 'http:' && opts.protocol !== 'https:') {
throw new errors.AlgoliaSearchError('protocol must be `http:` or `https:` (was `' + opts.protocol + '`)');
}
// no hosts given, add defaults
if (!opts.hosts) {
this.hosts.read = [this.applicationID + '-dsn.algolia.net'].concat(defaultHosts);
this.hosts.write = [this.applicationID + '.algolia.net'].concat(defaultHosts);
} else if (isArray(opts.hosts)) {
this.hosts.read = clone(opts.hosts);
this.hosts.write = clone(opts.hosts);
} else {
this.hosts.read = clone(opts.hosts.read);
this.hosts.write = clone(opts.hosts.write);
}
// add protocol and lowercase hosts
this.hosts.read = map(this.hosts.read, prepareHost(protocol));
this.hosts.write = map(this.hosts.write, prepareHost(protocol));
this.requestTimeout = timeout;
this.extraHeaders = [];
// In some situations you might want to warm the cache
this.cache = opts._cache || {};
this._ua = opts._ua;
this._useCache = opts._useCache === undefined || opts._cache ? true : opts._useCache;
this._useFallback = opts.useFallback === undefined ? true : opts.useFallback;
this._setTimeout = opts._setTimeout;
debug('init done, %j', this);
}
AlgoliaSearch.prototype = {
/*
* Delete an index
*
* @param indexName the name of index to delete
* @param callback the result callback called with two arguments
* error: null or Error('message')
* content: the server answer that contains the task ID
*/
deleteIndex: function(indexName, callback) {
return this._jsonRequest({
method: 'DELETE',
url: '/1/indexes/' + encodeURIComponent(indexName),
hostType: 'write',
callback: callback
});
},
/**
* Move an existing index.
* @param srcIndexName the name of index to copy.
* @param dstIndexName the new index name that will contains a copy of
* srcIndexName (destination will be overriten if it already exist).
* @param callback the result callback called with two arguments
* error: null or Error('message')
* content: the server answer that contains the task ID
*/
moveIndex: function(srcIndexName, dstIndexName, callback) {
var postObj = {
operation: 'move', destination: dstIndexName
};
return this._jsonRequest({
method: 'POST',
url: '/1/indexes/' + encodeURIComponent(srcIndexName) + '/operation',
body: postObj,
hostType: 'write',
callback: callback
});
},
/**
* Copy an existing index.
* @param srcIndexName the name of index to copy.
* @param dstIndexName the new index name that will contains a copy
* of srcIndexName (destination will be overriten if it already exist).
* @param callback the result callback called with two arguments
* error: null or Error('message')
* content: the server answer that contains the task ID
*/
copyIndex: function(srcIndexName, dstIndexName, callback) {
var postObj = {
operation: 'copy', destination: dstIndexName
};
return this._jsonRequest({
method: 'POST',
url: '/1/indexes/' + encodeURIComponent(srcIndexName) + '/operation',
body: postObj,
hostType: 'write',
callback: callback
});
},
/**
* Return last log entries.
* @param offset Specify the first entry to retrieve (0-based, 0 is the most recent log entry).
* @param length Specify the maximum number of entries to retrieve starting
* at offset. Maximum allowed value: 1000.
* @param callback the result callback called with two arguments
* error: null or Error('message')
* content: the server answer that contains the task ID
*/
getLogs: function(offset, length, callback) {
if (arguments.length === 0 || typeof offset === 'function') {
// getLogs([cb])
callback = offset;
offset = 0;
length = 10;
} else if (arguments.length === 1 || typeof length === 'function') {
// getLogs(1, [cb)]
callback = length;
length = 10;
}
return this._jsonRequest({
method: 'GET',
url: '/1/logs?offset=' + offset + '&length=' + length,
hostType: 'read',
callback: callback
});
},
/*
* List all existing indexes (paginated)
*
* @param page The page to retrieve, starting at 0.
* @param callback the result callback called with two arguments
* error: null or Error('message')
* content: the server answer with index list
*/
listIndexes: function(page, callback) {
var params = '';
if (page === undefined || typeof page === 'function') {
callback = page;
} else {
params = '?page=' + page;
}
return this._jsonRequest({
method: 'GET',
url: '/1/indexes' + params,
hostType: 'read',
callback: callback
});
},
/*
* Get the index object initialized
*
* @param indexName the name of index
* @param callback the result callback with one argument (the Index instance)
*/
initIndex: function(indexName) {
return new this.Index(this, indexName);
},
/*
* List all existing user keys with their associated ACLs
*
* @param callback the result callback called with two arguments
* error: null or Error('message')
* content: the server answer with user keys list
*/
listUserKeys: function(callback) {
return this._jsonRequest({
method: 'GET',
url: '/1/keys',
hostType: 'read',
callback: callback
});
},
/*
* Get ACL of a user key
*
* @param key
* @param callback the result callback called with two arguments
* error: null or Error('message')
* content: the server answer with user keys list
*/
getUserKeyACL: function(key, callback) {
return this._jsonRequest({
method: 'GET',
url: '/1/keys/' + key,
hostType: 'read',
callback: callback
});
},
/*
* Delete an existing user key
* @param key
* @param callback the result callback called with two arguments
* error: null or Error('message')
* content: the server answer with user keys list
*/
deleteUserKey: function(key, callback) {
return this._jsonRequest({
method: 'DELETE',
url: '/1/keys/' + key,
hostType: 'write',
callback: callback
});
},
/*
* Add a new global API key
*
* @param {string[]} acls - The list of ACL for this key. Defined by an array of strings that
* can contains the following values:
* - search: allow to search (https and http)
* - addObject: allows to add/update an object in the index (https only)
* - deleteObject : allows to delete an existing object (https only)
* - deleteIndex : allows to delete index content (https only)
* - settings : allows to get index settings (https only)
* - editSettings : allows to change index settings (https only)
* @param {Object} [params] - Optionnal parameters to set for the key
* @param {number} params.validity - Number of seconds after which the key will be automatically removed (0 means no time limit for this key)
* @param {number} params.maxQueriesPerIPPerHour - Number of API calls allowed from an IP address per hour
* @param {number} params.maxHitsPerQuery - Number of hits this API key can retrieve in one call
* @param {string[]} params.indexes - Allowed targeted indexes for this key
* @param {string} params.description - A description for your key
* @param {string[]} params.referers - A list of authorized referers
* @param {Object} params.queryParameters - Force the key to use specific query parameters
* @param {Function} callback - The result callback called with two arguments
* error: null or Error('message')
* content: the server answer with user keys list
* @return {Promise|undefined} Returns a promise if no callback given
* @example
* client.addUserKey(['search'], {
* validity: 300,
* maxQueriesPerIPPerHour: 2000,
* maxHitsPerQuery: 3,
* indexes: ['fruits'],
* description: 'Eat three fruits',
* referers: ['*.algolia.com'],
* queryParameters: {
* tagFilters: ['public'],
* }
* })
* @see {@link https://www.algolia.com/doc/rest_api#AddKey|Algolia REST API Documentation}
*/
addUserKey: function(acls, params, callback) {
var isArray = __webpack_require__(30);
var usage = 'Usage: client.addUserKey(arrayOfAcls[, params, callback])';
if (!isArray(acls)) {
throw new Error(usage);
}
if (arguments.length === 1 || typeof params === 'function') {
callback = params;
params = null;
}
var postObj = {
acl: acls
};
if (params) {
postObj.validity = params.validity;
postObj.maxQueriesPerIPPerHour = params.maxQueriesPerIPPerHour;
postObj.maxHitsPerQuery = params.maxHitsPerQuery;
postObj.indexes = params.indexes;
postObj.description = params.description;
if (params.queryParameters) {
postObj.queryParameters = this._getSearchParams(params.queryParameters, '');
}
postObj.referers = params.referers;
}
return this._jsonRequest({
method: 'POST',
url: '/1/keys',
body: postObj,
hostType: 'write',
callback: callback
});
},
/**
* Add a new global API key
* @deprecated Please use client.addUserKey()
*/
addUserKeyWithValidity: deprecate(function(acls, params, callback) {
return this.addUserKey(acls, params, callback);
}, deprecatedMessage('client.addUserKeyWithValidity()', 'client.addUserKey()')),
/**
* Update an existing API key
* @param {string} key - The key to update
* @param {string[]} acls - The list of ACL for this key. Defined by an array of strings that
* can contains the following values:
* - search: allow to search (https and http)
* - addObject: allows to add/update an object in the index (https only)
* - deleteObject : allows to delete an existing object (https only)
* - deleteIndex : allows to delete index content (https only)
* - settings : allows to get index settings (https only)
* - editSettings : allows to change index settings (https only)
* @param {Object} [params] - Optionnal parameters to set for the key
* @param {number} params.validity - Number of seconds after which the key will be automatically removed (0 means no time limit for this key)
* @param {number} params.maxQueriesPerIPPerHour - Number of API calls allowed from an IP address per hour
* @param {number} params.maxHitsPerQuery - Number of hits this API key can retrieve in one call
* @param {string[]} params.indexes - Allowed targeted indexes for this key
* @param {string} params.description - A description for your key
* @param {string[]} params.referers - A list of authorized referers
* @param {Object} params.queryParameters - Force the key to use specific query parameters
* @param {Function} callback - The result callback called with two arguments
* error: null or Error('message')
* content: the server answer with user keys list
* @return {Promise|undefined} Returns a promise if no callback given
* @example
* client.updateUserKey('APIKEY', ['search'], {
* validity: 300,
* maxQueriesPerIPPerHour: 2000,
* maxHitsPerQuery: 3,
* indexes: ['fruits'],
* description: 'Eat three fruits',
* referers: ['*.algolia.com'],
* queryParameters: {
* tagFilters: ['public'],
* }
* })
* @see {@link https://www.algolia.com/doc/rest_api#UpdateIndexKey|Algolia REST API Documentation}
*/
updateUserKey: function(key, acls, params, callback) {
var isArray = __webpack_require__(30);
var usage = 'Usage: client.updateUserKey(key, arrayOfAcls[, params, callback])';
if (!isArray(acls)) {
throw new Error(usage);
}
if (arguments.length === 2 || typeof params === 'function') {
callback = params;
params = null;
}
var putObj = {
acl: acls
};
if (params) {
putObj.validity = params.validity;
putObj.maxQueriesPerIPPerHour = params.maxQueriesPerIPPerHour;
putObj.maxHitsPerQuery = params.maxHitsPerQuery;
putObj.indexes = params.indexes;
putObj.description = params.description;
if (params.queryParameters) {
putObj.queryParameters = this._getSearchParams(params.queryParameters, '');
}
putObj.referers = params.referers;
}
return this._jsonRequest({
method: 'PUT',
url: '/1/keys/' + key,
body: putObj,
hostType: 'write',
callback: callback
});
},
/**
* Set the extra security tagFilters header
* @param {string|array} tags The list of tags defining the current security filters
*/
setSecurityTags: function(tags) {
if (Object.prototype.toString.call(tags) === '[object Array]') {
var strTags = [];
for (var i = 0; i < tags.length; ++i) {
if (Object.prototype.toString.call(tags[i]) === '[object Array]') {
var oredTags = [];
for (var j = 0; j < tags[i].length; ++j) {
oredTags.push(tags[i][j]);
}
strTags.push('(' + oredTags.join(',') + ')');
} else {
strTags.push(tags[i]);
}
}
tags = strTags.join(',');
}
this.securityTags = tags;
},
/**
* Set the extra user token header
* @param {string} userToken The token identifying a uniq user (used to apply rate limits)
*/
setUserToken: function(userToken) {
this.userToken = userToken;
},
/**
* Initialize a new batch of search queries
* @deprecated use client.search()
*/
startQueriesBatch: deprecate(function startQueriesBatchDeprecated() {
this._batch = [];
}, deprecatedMessage('client.startQueriesBatch()', 'client.search()')),
/**
* Add a search query in the batch
* @deprecated use client.search()
*/
addQueryInBatch: deprecate(function addQueryInBatchDeprecated(indexName, query, args) {
this._batch.push({
indexName: indexName,
query: query,
params: args
});
}, deprecatedMessage('client.addQueryInBatch()', 'client.search()')),
/**
* Clear all queries in client's cache
* @return undefined
*/
clearCache: function() {
this.cache = {};
},
/**
* Launch the batch of queries using XMLHttpRequest.
* @deprecated use client.search()
*/
sendQueriesBatch: deprecate(function sendQueriesBatchDeprecated(callback) {
return this.search(this._batch, callback);
}, deprecatedMessage('client.sendQueriesBatch()', 'client.search()')),
/**
* Set the number of milliseconds a request can take before automatically being terminated.
*
* @param {Number} milliseconds
*/
setRequestTimeout: function(milliseconds) {
if (milliseconds) {
this.requestTimeout = parseInt(milliseconds, 10);
}
},
/**
* Search through multiple indices at the same time
* @param {Object[]} queries An array of queries you want to run.
* @param {string} queries[].indexName The index name you want to target
* @param {string} [queries[].query] The query to issue on this index. Can also be passed into `params`
* @param {Object} queries[].params Any search param like hitsPerPage, ..
* @param {Function} callback Callback to be called
* @return {Promise|undefined} Returns a promise if no callback given
*/
search: function(queries, callback) {
var isArray = __webpack_require__(30);
var map = __webpack_require__(51);
var usage = 'Usage: client.search(arrayOfQueries[, callback])';
if (!isArray(queries)) {
throw new Error(usage);
}
var client = this;
var postObj = {
requests: map(queries, function prepareRequest(query) {
var params = '';
// allow query.query
// so we are mimicing the index.search(query, params) method
// {indexName:, query:, params:}
if (query.query !== undefined) {
params += 'query=' + encodeURIComponent(query.query);
}
return {
indexName: query.indexName,
params: client._getSearchParams(query.params, params)
};
})
};
var JSONPParams = map(postObj.requests, function prepareJSONPParams(request, requestId) {
return requestId + '=' +
encodeURIComponent(
'/1/indexes/' + encodeURIComponent(request.indexName) + '?' +
request.params
);
}).join('&');
return this._jsonRequest({
cache: this.cache,
method: 'POST',
url: '/1/indexes/*/queries',
body: postObj,
hostType: 'read',
fallback: {
method: 'GET',
url: '/1/indexes/*',
body: {
params: JSONPParams
}
},
callback: callback
});
},
/**
* Perform write operations accross multiple indexes.
*
* To reduce the amount of time spent on network round trips,
* you can create, update, or delete several objects in one call,
* using the batch endpoint (all operations are done in the given order).
*
* Available actions:
* - addObject
* - updateObject
* - partialUpdateObject
* - partialUpdateObjectNoCreate
* - deleteObject
*
* https://www.algolia.com/doc/rest_api#Indexes
* @param {Object[]} operations An array of operations to perform
* @return {Promise|undefined} Returns a promise if no callback given
* @example
* client.batch([{
* action: 'addObject',
* indexName: 'clients',
* body: {
* name: 'Bill'
* }
* }, {
* action: 'udpateObject',
* indexName: 'fruits',
* body: {
* objectID: '29138',
* name: 'banana'
* }
* }], cb)
*/
batch: function(operations, callback) {
var isArray = __webpack_require__(30);
var usage = 'Usage: client.batch(operations[, callback])';
if (!isArray(operations)) {
throw new Error(usage);
}
return this._jsonRequest({
method: 'POST',
url: '/1/indexes/*/batch',
body: {
requests: operations
},
hostType: 'write',
callback: callback
});
},
// environment specific methods
destroy: notImplemented,
enableRateLimitForward: notImplemented,
disableRateLimitForward: notImplemented,
useSecuredAPIKey: notImplemented,
disableSecuredAPIKey: notImplemented,
generateSecuredApiKey: notImplemented,
/*
* Index class constructor.
* You should not use this method directly but use initIndex() function
*/
Index: function(algoliasearch, indexName) {
this.indexName = indexName;
this.as = algoliasearch;
this.typeAheadArgs = null;
this.typeAheadValueOption = null;
// make sure every index instance has it's own cache
this.cache = {};
},
/**
* Add an extra field to the HTTP request
*
* @param name the header field name
* @param value the header field value
*/
setExtraHeader: function(name, value) {
this.extraHeaders.push({
name: name.toLowerCase(), value: value
});
},
/**
* Augment sent x-algolia-agent with more data, each agent part
* is automatically separated from the others by a semicolon;
*
* @param algoliaAgent the agent to add
*/
addAlgoliaAgent: function(algoliaAgent) {
this._ua += ';' + algoliaAgent;
},
/*
* Wrapper that try all hosts to maximize the quality of service
*/
_jsonRequest: function(initialOpts) {
var requestDebug = __webpack_require__(38)('algoliasearch:' + initialOpts.url);
var body;
var cache = initialOpts.cache;
var client = this;
var tries = 0;
var usingFallback = false;
var hasFallback = client._useFallback && client._request.fallback && initialOpts.fallback;
var headers;
if (this.apiKey.length > MAX_API_KEY_LENGTH && initialOpts.body !== undefined && initialOpts.body.params !== undefined) {
initialOpts.body.apiKey = this.apiKey;
headers = this._computeRequestHeaders(false);
} else {
headers = this._computeRequestHeaders();
}
if (initialOpts.body !== undefined) {
body = safeJSONStringify(initialOpts.body);
}
requestDebug('request start');
function doRequest(requester, reqOpts) {
var cacheID;
if (client._useCache) {
cacheID = initialOpts.url;
}
// as we sometime use POST requests to pass parameters (like query='aa'),
// the cacheID must also include the body to be different between calls
if (client._useCache && body) {
cacheID += '_body_' + reqOpts.body;
}
// handle cache existence
if (client._useCache && cache && cache[cacheID] !== undefined) {
requestDebug('serving response from cache');
return client._promise.resolve(JSON.parse(cache[cacheID]));
}
// if we reached max tries
if (tries >= client.hosts[initialOpts.hostType].length) {
if (!hasFallback || usingFallback) {
requestDebug('could not get any response');
// then stop
return client._promise.reject(new errors.AlgoliaSearchError(
'Cannot connect to the AlgoliaSearch API.' +
' Send an email to support@algolia.com to report and resolve the issue.' +
' Application id was: ' + client.applicationID
));
}
requestDebug('switching to fallback');
// let's try the fallback starting from here
tries = 0;
// method, url and body are fallback dependent
reqOpts.method = initialOpts.fallback.method;
reqOpts.url = initialOpts.fallback.url;
reqOpts.jsonBody = initialOpts.fallback.body;
if (reqOpts.jsonBody) {
reqOpts.body = safeJSONStringify(reqOpts.jsonBody);
}
// re-compute headers, they could be omitting the API KEY
headers = client._computeRequestHeaders();
reqOpts.timeout = client.requestTimeout * (tries + 1);
client.hostIndex[initialOpts.hostType] = 0;
usingFallback = true; // the current request is now using fallback
return doRequest(client._request.fallback, reqOpts);
}
var url = client.hosts[initialOpts.hostType][client.hostIndex[initialOpts.hostType]] + reqOpts.url;
var options = {
body: reqOpts.body,
jsonBody: reqOpts.jsonBody,
method: reqOpts.method,
headers: headers,
timeout: reqOpts.timeout,
debug: requestDebug
};
requestDebug('method: %s, url: %s, headers: %j, timeout: %d',
options.method, url, options.headers, options.timeout);
if (requester === client._request.fallback) {
requestDebug('using fallback');
}
// `requester` is any of this._request or this._request.fallback
// thus it needs to be called using the client as context
return requester.call(client, url, options).then(success, tryFallback);
function success(httpResponse) {
// compute the status of the response,
//
// When in browser mode, using XDR or JSONP, we have no statusCode available
// So we rely on our API response `status` property.
// But `waitTask` can set a `status` property which is not the statusCode (it's the task status)
// So we check if there's a `message` along `status` and it means it's an error
//
// That's the only case where we have a response.status that's not the http statusCode
var status = httpResponse && httpResponse.body && httpResponse.body.message && httpResponse.body.status ||
// this is important to check the request statusCode AFTER the body eventual
// statusCode because some implementations (jQuery XDomainRequest transport) may
// send statusCode 200 while we had an error
httpResponse.statusCode ||
// When in browser mode, using XDR or JSONP
// we default to success when no error (no response.status && response.message)
// If there was a JSON.parse() error then body is null and it fails
httpResponse && httpResponse.body && 200;
requestDebug('received response: statusCode: %s, computed statusCode: %d, headers: %j',
httpResponse.statusCode, status, httpResponse.headers);
var ok = status === 200 || status === 201;
var retry = !ok && Math.floor(status / 100) !== 4 && Math.floor(status / 100) !== 1;
if (client._useCache && ok && cache) {
cache[cacheID] = httpResponse.responseText;
}
if (ok) {
return httpResponse.body;
}
if (retry) {
tries += 1;
return retryRequest();
}
var unrecoverableError = new errors.AlgoliaSearchError(
httpResponse.body && httpResponse.body.message
);
return client._promise.reject(unrecoverableError);
}
function tryFallback(err) {
// error cases:
// While not in fallback mode:
// - CORS not supported
// - network error
// While in fallback mode:
// - timeout
// - network error
// - badly formatted JSONP (script loaded, did not call our callback)
// In both cases:
// - uncaught exception occurs (TypeError)
requestDebug('error: %s, stack: %s', err.message, err.stack);
if (!(err instanceof errors.AlgoliaSearchError)) {
err = new errors.Unknown(err && err.message, err);
}
tries += 1;
// stop the request implementation when:
if (
// we did not generate this error,
// it comes from a throw in some other piece of code
err instanceof errors.Unknown ||
// server sent unparsable JSON
err instanceof errors.UnparsableJSON ||
// max tries and already using fallback or no fallback
tries >= client.hosts[initialOpts.hostType].length &&
(usingFallback || !hasFallback)) {
// stop request implementation for this command
return client._promise.reject(err);
}
client.hostIndex[initialOpts.hostType] = ++client.hostIndex[initialOpts.hostType] % client.hosts[initialOpts.hostType].length;
if (err instanceof errors.RequestTimeout) {
return retryRequest();
} else if (!usingFallback) {
// next request loop, force using fallback for this request
tries = Infinity;
}
return doRequest(requester, reqOpts);
}
function retryRequest() {
client.hostIndex[initialOpts.hostType] = ++client.hostIndex[initialOpts.hostType] % client.hosts[initialOpts.hostType].length;
reqOpts.timeout = client.requestTimeout * (tries + 1);
return doRequest(requester, reqOpts);
}
}
var promise = doRequest(
client._request, {
url: initialOpts.url,
method: initialOpts.method,
body: body,
jsonBody: initialOpts.body,
timeout: client.requestTimeout * (tries + 1)
}
);
// either we have a callback
// either we are using promises
if (initialOpts.callback) {
promise.then(function okCb(content) {
exitPromise(function() {
initialOpts.callback(null, content);
}, client._setTimeout || setTimeout);
}, function nookCb(err) {
exitPromise(function() {
initialOpts.callback(err);
}, client._setTimeout || setTimeout);
});
} else {
return promise;
}
},
/*
* Transform search param object in query string
*/
_getSearchParams: function(args, params) {
if (args === undefined || args === null) {
return params;
}
for (var key in args) {
if (key !== null && args[key] !== undefined && args.hasOwnProperty(key)) {
params += params === '' ? '' : '&';
params += key + '=' + encodeURIComponent(Object.prototype.toString.call(args[key]) === '[object Array]' ? safeJSONStringify(args[key]) : args[key]);
}
}
return params;
},
_computeRequestHeaders: function(withAPIKey) {
var forEach = __webpack_require__(11);
var requestHeaders = {
'x-algolia-agent': this._ua,
'x-algolia-application-id': this.applicationID
};
// browser will inline headers in the url, node.js will use http headers
// but in some situations, the API KEY will be too long (big secured API keys)
// so if the request is a POST and the KEY is very long, we will be asked to not put
// it into headers but in the JSON body
if (withAPIKey !== false) {
requestHeaders['x-algolia-api-key'] = this.apiKey;
}
if (this.userToken) {
requestHeaders['x-algolia-usertoken'] = this.userToken;
}
if (this.securityTags) {
requestHeaders['x-algolia-tagfilters'] = this.securityTags;
}
if (this.extraHeaders) {
forEach(this.extraHeaders, function addToRequestHeaders(header) {
requestHeaders[header.name] = header.value;
});
}
return requestHeaders;
}
};
/*
* Contains all the functions related to one index
* You should use AlgoliaSearch.initIndex(indexName) to retrieve this object
*/
AlgoliaSearch.prototype.Index.prototype = {
/*
* Clear all queries in cache
*/
clearCache: function() {
this.cache = {};
},
/*
* Add an object in this index
*
* @param content contains the javascript object to add inside the index
* @param objectID (optional) an objectID you want to attribute to this object
* (if the attribute already exist the old object will be overwrite)
* @param callback (optional) the result callback called with two arguments:
* error: null or Error('message')
* content: the server answer that contains 3 elements: createAt, taskId and objectID
*/
addObject: function(content, objectID, callback) {
var indexObj = this;
if (arguments.length === 1 || typeof objectID === 'function') {
callback = objectID;
objectID = undefined;
}
return this.as._jsonRequest({
method: objectID !== undefined ?
'PUT' : // update or create
'POST', // create (API generates an objectID)
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + // create
(objectID !== undefined ? '/' + encodeURIComponent(objectID) : ''), // update or create
body: content,
hostType: 'write',
callback: callback
});
},
/*
* Add several objects
*
* @param objects contains an array of objects to add
* @param callback (optional) the result callback called with two arguments:
* error: null or Error('message')
* content: the server answer that updateAt and taskID
*/
addObjects: function(objects, callback) {
var isArray = __webpack_require__(30);
var usage = 'Usage: index.addObjects(arrayOfObjects[, callback])';
if (!isArray(objects)) {
throw new Error(usage);
}
var indexObj = this;
var postObj = {
requests: []
};
for (var i = 0; i < objects.length; ++i) {
var request = {
action: 'addObject',
body: objects[i]
};
postObj.requests.push(request);
}
return this.as._jsonRequest({
method: 'POST',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/batch',
body: postObj,
hostType: 'write',
callback: callback
});
},
/*
* Get an object from this index
*
* @param objectID the unique identifier of the object to retrieve
* @param attrs (optional) if set, contains the array of attribute names to retrieve
* @param callback (optional) the result callback called with two arguments
* error: null or Error('message')
* content: the object to retrieve or the error message if a failure occured
*/
getObject: function(objectID, attrs, callback) {
var indexObj = this;
if (arguments.length === 1 || typeof attrs === 'function') {
callback = attrs;
attrs = undefined;
}
var params = '';
if (attrs !== undefined) {
params = '?attributes=';
for (var i = 0; i < attrs.length; ++i) {
if (i !== 0) {
params += ',';
}
params += attrs[i];
}
}
return this.as._jsonRequest({
method: 'GET',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/' + encodeURIComponent(objectID) + params,
hostType: 'read',
callback: callback
});
},
/*
* Get several objects from this index
*
* @param objectIDs the array of unique identifier of objects to retrieve
*/
getObjects: function(objectIDs, attributesToRetrieve, callback) {
var isArray = __webpack_require__(30);
var map = __webpack_require__(51);
var usage = 'Usage: index.getObjects(arrayOfObjectIDs[, callback])';
if (!isArray(objectIDs)) {
throw new Error(usage);
}
var indexObj = this;
if (arguments.length === 1 || typeof attributesToRetrieve === 'function') {
callback = attributesToRetrieve;
attributesToRetrieve = undefined;
}
var body = {
requests: map(objectIDs, function prepareRequest(objectID) {
var request = {
indexName: indexObj.indexName,
objectID: objectID
};
if (attributesToRetrieve) {
request.attributesToRetrieve = attributesToRetrieve.join(',');
}
return request;
})
};
return this.as._jsonRequest({
method: 'POST',
url: '/1/indexes/*/objects',
hostType: 'read',
body: body,
callback: callback
});
},
/*
* Update partially an object (only update attributes passed in argument)
*
* @param partialObject contains the javascript attributes to override, the
* object must contains an objectID attribute
* @param createIfNotExists (optional) if false, avoid an automatic creation of the object
* @param callback (optional) the result callback called with two arguments:
* error: null or Error('message')
* content: the server answer that contains 3 elements: createAt, taskId and objectID
*/
partialUpdateObject: function(partialObject, createIfNotExists, callback) {
if (arguments.length === 1 || typeof createIfNotExists === 'function') {
callback = createIfNotExists;
createIfNotExists = undefined;
}
var indexObj = this;
var url = '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/' + encodeURIComponent(partialObject.objectID) + '/partial';
if (createIfNotExists === false) {
url += '?createIfNotExists=false';
}
return this.as._jsonRequest({
method: 'POST',
url: url,
body: partialObject,
hostType: 'write',
callback: callback
});
},
/*
* Partially Override the content of several objects
*
* @param objects contains an array of objects to update (each object must contains a objectID attribute)
* @param callback (optional) the result callback called with two arguments:
* error: null or Error('message')
* content: the server answer that updateAt and taskID
*/
partialUpdateObjects: function(objects, callback) {
var isArray = __webpack_require__(30);
var usage = 'Usage: index.partialUpdateObjects(arrayOfObjects[, callback])';
if (!isArray(objects)) {
throw new Error(usage);
}
var indexObj = this;
var postObj = {
requests: []
};
for (var i = 0; i < objects.length; ++i) {
var request = {
action: 'partialUpdateObject',
objectID: objects[i].objectID,
body: objects[i]
};
postObj.requests.push(request);
}
return this.as._jsonRequest({
method: 'POST',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/batch',
body: postObj,
hostType: 'write',
callback: callback
});
},
/*
* Override the content of object
*
* @param object contains the javascript object to save, the object must contains an objectID attribute
* @param callback (optional) the result callback called with two arguments:
* error: null or Error('message')
* content: the server answer that updateAt and taskID
*/
saveObject: function(object, callback) {
var indexObj = this;
return this.as._jsonRequest({
method: 'PUT',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/' + encodeURIComponent(object.objectID),
body: object,
hostType: 'write',
callback: callback
});
},
/*
* Override the content of several objects
*
* @param objects contains an array of objects to update (each object must contains a objectID attribute)
* @param callback (optional) the result callback called with two arguments:
* error: null or Error('message')
* content: the server answer that updateAt and taskID
*/
saveObjects: function(objects, callback) {
var isArray = __webpack_require__(30);
var usage = 'Usage: index.saveObjects(arrayOfObjects[, callback])';
if (!isArray(objects)) {
throw new Error(usage);
}
var indexObj = this;
var postObj = {
requests: []
};
for (var i = 0; i < objects.length; ++i) {
var request = {
action: 'updateObject',
objectID: objects[i].objectID,
body: objects[i]
};
postObj.requests.push(request);
}
return this.as._jsonRequest({
method: 'POST',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/batch',
body: postObj,
hostType: 'write',
callback: callback
});
},
/*
* Delete an object from the index
*
* @param objectID the unique identifier of object to delete
* @param callback (optional) the result callback called with two arguments:
* error: null or Error('message')
* content: the server answer that contains 3 elements: createAt, taskId and objectID
*/
deleteObject: function(objectID, callback) {
if (typeof objectID === 'function' || typeof objectID !== 'string' && typeof objectID !== 'number') {
var err = new errors.AlgoliaSearchError('Cannot delete an object without an objectID');
callback = objectID;
if (typeof callback === 'function') {
return callback(err);
}
return this.as._promise.reject(err);
}
var indexObj = this;
return this.as._jsonRequest({
method: 'DELETE',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/' + encodeURIComponent(objectID),
hostType: 'write',
callback: callback
});
},
/*
* Delete several objects from an index
*
* @param objectIDs contains an array of objectID to delete
* @param callback (optional) the result callback called with two arguments:
* error: null or Error('message')
* content: the server answer that contains 3 elements: createAt, taskId and objectID
*/
deleteObjects: function(objectIDs, callback) {
var isArray = __webpack_require__(30);
var map = __webpack_require__(51);
var usage = 'Usage: index.deleteObjects(arrayOfObjectIDs[, callback])';
if (!isArray(objectIDs)) {
throw new Error(usage);
}
var indexObj = this;
var postObj = {
requests: map(objectIDs, function prepareRequest(objectID) {
return {
action: 'deleteObject',
objectID: objectID,
body: {
objectID: objectID
}
};
})
};
return this.as._jsonRequest({
method: 'POST',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/batch',
body: postObj,
hostType: 'write',
callback: callback
});
},
/*
* Delete all objects matching a query
*
* @param query the query string
* @param params the optional query parameters
* @param callback (optional) the result callback called with one argument
* error: null or Error('message')
*/
deleteByQuery: function(query, params, callback) {
var clone = __webpack_require__(41);
var map = __webpack_require__(51);
var indexObj = this;
var client = indexObj.as;
if (arguments.length === 1 || typeof params === 'function') {
callback = params;
params = {};
} else {
params = clone(params);
}
params.attributesToRetrieve = 'objectID';
params.hitsPerPage = 1000;
params.distinct = false;
// when deleting, we should never use cache to get the
// search results
this.clearCache();
// there's a problem in how we use the promise chain,
// see how waitTask is done
var promise = this
.search(query, params)
.then(stopOrDelete);
function stopOrDelete(searchContent) {
// stop here
if (searchContent.nbHits === 0) {
// return indexObj.as._request.resolve();
return searchContent;
}
// continue and do a recursive call
var objectIDs = map(searchContent.hits, function getObjectID(object) {
return object.objectID;
});
return indexObj
.deleteObjects(objectIDs)
.then(waitTask)
.then(doDeleteByQuery);
}
function waitTask(deleteObjectsContent) {
return indexObj.waitTask(deleteObjectsContent.taskID);
}
function doDeleteByQuery() {
return indexObj.deleteByQuery(query, params);
}
if (!callback) {
return promise;
}
promise.then(success, failure);
function success() {
exitPromise(function exit() {
callback(null);
}, client._setTimeout || setTimeout);
}
function failure(err) {
exitPromise(function exit() {
callback(err);
}, client._setTimeout || setTimeout);
}
},
/*
* Search inside the index using XMLHttpRequest request (Using a POST query to
* minimize number of OPTIONS queries: Cross-Origin Resource Sharing).
*
* @param query the full text query
* @param args (optional) if set, contains an object with query parameters:
* - page: (integer) Pagination parameter used to select the page to retrieve.
* Page is zero-based and defaults to 0. Thus,
* to retrieve the 10th page you need to set page=9
* - hitsPerPage: (integer) Pagination parameter used to select the number of hits per page. Defaults to 20.
* - attributesToRetrieve: a string that contains the list of object attributes
* you want to retrieve (let you minimize the answer size).
* Attributes are separated with a comma (for example "name,address").
* You can also use an array (for example ["name","address"]).
* By default, all attributes are retrieved. You can also use '*' to retrieve all
* values when an attributesToRetrieve setting is specified for your index.
* - attributesToHighlight: a string that contains the list of attributes you
* want to highlight according to the query.
* Attributes are separated by a comma. You can also use an array (for example ["name","address"]).
* If an attribute has no match for the query, the raw value is returned.
* By default all indexed text attributes are highlighted.
* You can use `*` if you want to highlight all textual attributes.
* Numerical attributes are not highlighted.
* A matchLevel is returned for each highlighted attribute and can contain:
* - full: if all the query terms were found in the attribute,
* - partial: if only some of the query terms were found,
* - none: if none of the query terms were found.
* - attributesToSnippet: a string that contains the list of attributes to snippet alongside
* the number of words to return (syntax is `attributeName:nbWords`).
* Attributes are separated by a comma (Example: attributesToSnippet=name:10,content:10).
* You can also use an array (Example: attributesToSnippet: ['name:10','content:10']).
* By default no snippet is computed.
* - minWordSizefor1Typo: the minimum number of characters in a query word to accept one typo in this word.
* Defaults to 3.
* - minWordSizefor2Typos: the minimum number of characters in a query word
* to accept two typos in this word. Defaults to 7.
* - getRankingInfo: if set to 1, the result hits will contain ranking
* information in _rankingInfo attribute.
* - aroundLatLng: search for entries around a given
* latitude/longitude (specified as two floats separated by a comma).
* For example aroundLatLng=47.316669,5.016670).
* You can specify the maximum distance in meters with the aroundRadius parameter (in meters)
* and the precision for ranking with aroundPrecision
* (for example if you set aroundPrecision=100, two objects that are distant of
* less than 100m will be considered as identical for "geo" ranking parameter).
* At indexing, you should specify geoloc of an object with the _geoloc attribute
* (in the form {"_geoloc":{"lat":48.853409, "lng":2.348800}})
* - insideBoundingBox: search entries inside a given area defined by the two extreme points
* of a rectangle (defined by 4 floats: p1Lat,p1Lng,p2Lat,p2Lng).
* For example insideBoundingBox=47.3165,4.9665,47.3424,5.0201).
* At indexing, you should specify geoloc of an object with the _geoloc attribute
* (in the form {"_geoloc":{"lat":48.853409, "lng":2.348800}})
* - numericFilters: a string that contains the list of numeric filters you want to
* apply separated by a comma.
* The syntax of one filter is `attributeName` followed by `operand` followed by `value`.
* Supported operands are `<`, `<=`, `=`, `>` and `>=`.
* You can have multiple conditions on one attribute like for example numericFilters=price>100,price<1000.
* You can also use an array (for example numericFilters: ["price>100","price<1000"]).
* - tagFilters: filter the query by a set of tags. You can AND tags by separating them by commas.
* To OR tags, you must add parentheses. For example, tags=tag1,(tag2,tag3) means tag1 AND (tag2 OR tag3).
* You can also use an array, for example tagFilters: ["tag1",["tag2","tag3"]]
* means tag1 AND (tag2 OR tag3).
* At indexing, tags should be added in the _tags** attribute
* of objects (for example {"_tags":["tag1","tag2"]}).
* - facetFilters: filter the query by a list of facets.
* Facets are separated by commas and each facet is encoded as `attributeName:value`.
* For example: `facetFilters=category:Book,author:John%20Doe`.
* You can also use an array (for example `["category:Book","author:John%20Doe"]`).
* - facets: List of object attributes that you want to use for faceting.
* Comma separated list: `"category,author"` or array `['category','author']`
* Only attributes that have been added in **attributesForFaceting** index setting
* can be used in this parameter.
* You can also use `*` to perform faceting on all attributes specified in **attributesForFaceting**.
* - queryType: select how the query words are interpreted, it can be one of the following value:
* - prefixAll: all query words are interpreted as prefixes,
* - prefixLast: only the last word is interpreted as a prefix (default behavior),
* - prefixNone: no query word is interpreted as a prefix. This option is not recommended.
* - optionalWords: a string that contains the list of words that should
* be considered as optional when found in the query.
* Comma separated and array are accepted.
* - distinct: If set to 1, enable the distinct feature (disabled by default)
* if the attributeForDistinct index setting is set.
* This feature is similar to the SQL "distinct" keyword: when enabled
* in a query with the distinct=1 parameter,
* all hits containing a duplicate value for the attributeForDistinct attribute are removed from results.
* For example, if the chosen attribute is show_name and several hits have
* the same value for show_name, then only the best
* one is kept and others are removed.
* - restrictSearchableAttributes: List of attributes you want to use for
* textual search (must be a subset of the attributesToIndex index setting)
* either comma separated or as an array
* @param callback the result callback called with two arguments:
* error: null or Error('message'). If false, the content contains the error.
* content: the server answer that contains the list of results.
*/
search: buildSearchMethod('query'),
/*
* -- BETA --
* Search a record similar to the query inside the index using XMLHttpRequest request (Using a POST query to
* minimize number of OPTIONS queries: Cross-Origin Resource Sharing).
*
* @param query the similar query
* @param args (optional) if set, contains an object with query parameters.
* All search parameters are supported (see search function), restrictSearchableAttributes and facetFilters
* are the two most useful to restrict the similar results and get more relevant content
*/
similarSearch: buildSearchMethod('similarQuery'),
/*
* Browse index content. The response content will have a `cursor` property that you can use
* to browse subsequent pages for this query. Use `index.browseFrom(cursor)` when you want.
*
* @param {string} query - The full text query
* @param {Object} [queryParameters] - Any search query parameter
* @param {Function} [callback] - The result callback called with two arguments
* error: null or Error('message')
* content: the server answer with the browse result
* @return {Promise|undefined} Returns a promise if no callback given
* @example
* index.browse('cool songs', {
* tagFilters: 'public,comments',
* hitsPerPage: 500
* }, callback);
* @see {@link https://www.algolia.com/doc/rest_api#Browse|Algolia REST API Documentation}
*/
// pre 3.5.0 usage, backward compatible
// browse: function(page, hitsPerPage, callback) {
browse: function(query, queryParameters, callback) {
var merge = __webpack_require__(76);
var indexObj = this;
var page;
var hitsPerPage;
// we check variadic calls that are not the one defined
// .browse()/.browse(fn)
// => page = 0
if (arguments.length === 0 || arguments.length === 1 && typeof arguments[0] === 'function') {
page = 0;
callback = arguments[0];
query = undefined;
} else if (typeof arguments[0] === 'number') {
// .browse(2)/.browse(2, 10)/.browse(2, fn)/.browse(2, 10, fn)
page = arguments[0];
if (typeof arguments[1] === 'number') {
hitsPerPage = arguments[1];
} else if (typeof arguments[1] === 'function') {
callback = arguments[1];
hitsPerPage = undefined;
}
query = undefined;
queryParameters = undefined;
} else if (typeof arguments[0] === 'object') {
// .browse(queryParameters)/.browse(queryParameters, cb)
if (typeof arguments[1] === 'function') {
callback = arguments[1];
}
queryParameters = arguments[0];
query = undefined;
} else if (typeof arguments[0] === 'string' && typeof arguments[1] === 'function') {
// .browse(query, cb)
callback = arguments[1];
queryParameters = undefined;
}
// otherwise it's a .browse(query)/.browse(query, queryParameters)/.browse(query, queryParameters, cb)
// get search query parameters combining various possible calls
// to .browse();
queryParameters = merge({}, queryParameters || {}, {
page: page,
hitsPerPage: hitsPerPage,
query: query
});
var params = this.as._getSearchParams(queryParameters, '');
return this.as._jsonRequest({
method: 'GET',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/browse?' + params,
hostType: 'read',
callback: callback
});
},
/*
* Continue browsing from a previous position (cursor), obtained via a call to `.browse()`.
*
* @param {string} query - The full text query
* @param {Object} [queryParameters] - Any search query parameter
* @param {Function} [callback] - The result callback called with two arguments
* error: null or Error('message')
* content: the server answer with the browse result
* @return {Promise|undefined} Returns a promise if no callback given
* @example
* index.browseFrom('14lkfsakl32', callback);
* @see {@link https://www.algolia.com/doc/rest_api#Browse|Algolia REST API Documentation}
*/
browseFrom: function(cursor, callback) {
return this.as._jsonRequest({
method: 'GET',
url: '/1/indexes/' + encodeURIComponent(this.indexName) + '/browse?cursor=' + encodeURIComponent(cursor),
hostType: 'read',
callback: callback
});
},
/*
* Browse all content from an index using events. Basically this will do
* .browse() -> .browseFrom -> .browseFrom -> .. until all the results are returned
*
* @param {string} query - The full text query
* @param {Object} [queryParameters] - Any search query parameter
* @return {EventEmitter}
* @example
* var browser = index.browseAll('cool songs', {
* tagFilters: 'public,comments',
* hitsPerPage: 500
* });
*
* browser.on('result', function resultCallback(content) {
* console.log(content.hits);
* });
*
* // if any error occurs, you get it
* browser.on('error', function(err) {
* throw err;
* });
*
* // when you have browsed the whole index, you get this event
* browser.on('end', function() {
* console.log('finished');
* });
*
* // at any point if you want to stop the browsing process, you can stop it manually
* // otherwise it will go on and on
* browser.stop();
*
* @see {@link https://www.algolia.com/doc/rest_api#Browse|Algolia REST API Documentation}
*/
browseAll: function(query, queryParameters) {
if (typeof query === 'object') {
queryParameters = query;
query = undefined;
}
var merge = __webpack_require__(76);
var IndexBrowser = __webpack_require__(84);
var browser = new IndexBrowser();
var client = this.as;
var index = this;
var params = client._getSearchParams(
merge({}, queryParameters || {}, {
query: query
}), ''
);
// start browsing
browseLoop();
function browseLoop(cursor) {
if (browser._stopped) {
return;
}
var queryString;
if (cursor !== undefined) {
queryString = 'cursor=' + encodeURIComponent(cursor);
} else {
queryString = params;
}
client._jsonRequest({
method: 'GET',
url: '/1/indexes/' + encodeURIComponent(index.indexName) + '/browse?' + queryString,
hostType: 'read',
callback: browseCallback
});
}
function browseCallback(err, content) {
if (browser._stopped) {
return;
}
if (err) {
browser._error(err);
return;
}
browser._result(content);
// no cursor means we are finished browsing
if (content.cursor === undefined) {
browser._end();
return;
}
browseLoop(content.cursor);
}
return browser;
},
/*
* Get a Typeahead.js adapter
* @param searchParams contains an object with query parameters (see search for details)
*/
ttAdapter: function(params) {
var self = this;
return function ttAdapter(query, syncCb, asyncCb) {
var cb;
if (typeof asyncCb === 'function') {
// typeahead 0.11
cb = asyncCb;
} else {
// pre typeahead 0.11
cb = syncCb;
}
self.search(query, params, function searchDone(err, content) {
if (err) {
cb(err);
return;
}
cb(content.hits);
});
};
},
/*
* Wait the publication of a task on the server.
* All server task are asynchronous and you can check with this method that the task is published.
*
* @param taskID the id of the task returned by server
* @param callback the result callback with with two arguments:
* error: null or Error('message')
* content: the server answer that contains the list of results
*/
waitTask: function(taskID, callback) {
// wait minimum 100ms before retrying
var baseDelay = 100;
// wait maximum 5s before retrying
var maxDelay = 5000;
var loop = 0;
// waitTask() must be handled differently from other methods,
// it's a recursive method using a timeout
var indexObj = this;
var client = indexObj.as;
var promise = retryLoop();
function retryLoop() {
return client._jsonRequest({
method: 'GET',
hostType: 'read',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/task/' + taskID
}).then(function success(content) {
loop++;
var delay = baseDelay * loop * loop;
if (delay > maxDelay) {
delay = maxDelay;
}
if (content.status !== 'published') {
return client._promise.delay(delay).then(retryLoop);
}
return content;
});
}
if (!callback) {
return promise;
}
promise.then(successCb, failureCb);
function successCb(content) {
exitPromise(function exit() {
callback(null, content);
}, client._setTimeout || setTimeout);
}
function failureCb(err) {
exitPromise(function exit() {
callback(err);
}, client._setTimeout || setTimeout);
}
},
/*
* This function deletes the index content. Settings and index specific API keys are kept untouched.
*
* @param callback (optional) the result callback called with two arguments
* error: null or Error('message')
* content: the settings object or the error message if a failure occured
*/
clearIndex: function(callback) {
var indexObj = this;
return this.as._jsonRequest({
method: 'POST',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/clear',
hostType: 'write',
callback: callback
});
},
/*
* Get settings of this index
*
* @param callback (optional) the result callback called with two arguments
* error: null or Error('message')
* content: the settings object or the error message if a failure occured
*/
getSettings: function(callback) {
var indexObj = this;
return this.as._jsonRequest({
method: 'GET',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/settings',
hostType: 'read',
callback: callback
});
},
/*
* Set settings for this index
*
* @param settigns the settings object that can contains :
* - minWordSizefor1Typo: (integer) the minimum number of characters to accept one typo (default = 3).
* - minWordSizefor2Typos: (integer) the minimum number of characters to accept two typos (default = 7).
* - hitsPerPage: (integer) the number of hits per page (default = 10).
* - attributesToRetrieve: (array of strings) default list of attributes to retrieve in objects.
* If set to null, all attributes are retrieved.
* - attributesToHighlight: (array of strings) default list of attributes to highlight.
* If set to null, all indexed attributes are highlighted.
* - attributesToSnippet**: (array of strings) default list of attributes to snippet alongside the number
* of words to return (syntax is attributeName:nbWords).
* By default no snippet is computed. If set to null, no snippet is computed.
* - attributesToIndex: (array of strings) the list of fields you want to index.
* If set to null, all textual and numerical attributes of your objects are indexed,
* but you should update it to get optimal results.
* This parameter has two important uses:
* - Limit the attributes to index: For example if you store a binary image in base64,
* you want to store it and be able to
* retrieve it but you don't want to search in the base64 string.
* - Control part of the ranking*: (see the ranking parameter for full explanation)
* Matches in attributes at the beginning of
* the list will be considered more important than matches in attributes further down the list.
* In one attribute, matching text at the beginning of the attribute will be
* considered more important than text after, you can disable
* this behavior if you add your attribute inside `unordered(AttributeName)`,
* for example attributesToIndex: ["title", "unordered(text)"].
* - attributesForFaceting: (array of strings) The list of fields you want to use for faceting.
* All strings in the attribute selected for faceting are extracted and added as a facet.
* If set to null, no attribute is used for faceting.
* - attributeForDistinct: (string) The attribute name used for the Distinct feature.
* This feature is similar to the SQL "distinct" keyword: when enabled
* in query with the distinct=1 parameter, all hits containing a duplicate
* value for this attribute are removed from results.
* For example, if the chosen attribute is show_name and several hits have
* the same value for show_name, then only the best one is kept and others are removed.
* - ranking: (array of strings) controls the way results are sorted.
* We have six available criteria:
* - typo: sort according to number of typos,
* - geo: sort according to decreassing distance when performing a geo-location based search,
* - proximity: sort according to the proximity of query words in hits,
* - attribute: sort according to the order of attributes defined by attributesToIndex,
* - exact:
* - if the user query contains one word: sort objects having an attribute
* that is exactly the query word before others.
* For example if you search for the "V" TV show, you want to find it
* with the "V" query and avoid to have all popular TV
* show starting by the v letter before it.
* - if the user query contains multiple words: sort according to the
* number of words that matched exactly (and not as a prefix).
* - custom: sort according to a user defined formula set in **customRanking** attribute.
* The standard order is ["typo", "geo", "proximity", "attribute", "exact", "custom"]
* - customRanking: (array of strings) lets you specify part of the ranking.
* The syntax of this condition is an array of strings containing attributes
* prefixed by asc (ascending order) or desc (descending order) operator.
* For example `"customRanking" => ["desc(population)", "asc(name)"]`
* - queryType: Select how the query words are interpreted, it can be one of the following value:
* - prefixAll: all query words are interpreted as prefixes,
* - prefixLast: only the last word is interpreted as a prefix (default behavior),
* - prefixNone: no query word is interpreted as a prefix. This option is not recommended.
* - highlightPreTag: (string) Specify the string that is inserted before
* the highlighted parts in the query result (default to "<em>").
* - highlightPostTag: (string) Specify the string that is inserted after
* the highlighted parts in the query result (default to "</em>").
* - optionalWords: (array of strings) Specify a list of words that should
* be considered as optional when found in the query.
* @param callback (optional) the result callback called with two arguments
* error: null or Error('message')
* content: the server answer or the error message if a failure occured
*/
setSettings: function(settings, callback) {
var indexObj = this;
return this.as._jsonRequest({
method: 'PUT',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/settings',
hostType: 'write',
body: settings,
callback: callback
});
},
/*
* List all existing user keys associated to this index
*
* @param callback the result callback called with two arguments
* error: null or Error('message')
* content: the server answer with user keys list
*/
listUserKeys: function(callback) {
var indexObj = this;
return this.as._jsonRequest({
method: 'GET',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/keys',
hostType: 'read',
callback: callback
});
},
/*
* Get ACL of a user key associated to this index
*
* @param key
* @param callback the result callback called with two arguments
* error: null or Error('message')
* content: the server answer with user keys list
*/
getUserKeyACL: function(key, callback) {
var indexObj = this;
return this.as._jsonRequest({
method: 'GET',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/keys/' + key,
hostType: 'read',
callback: callback
});
},
/*
* Delete an existing user key associated to this index
*
* @param key
* @param callback the result callback called with two arguments
* error: null or Error('message')
* content: the server answer with user keys list
*/
deleteUserKey: function(key, callback) {
var indexObj = this;
return this.as._jsonRequest({
method: 'DELETE',
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/keys/' + key,
hostType: 'write',
callback: callback
});
},
/*
* Add a new API key to this index
*
* @param {string[]} acls - The list of ACL for this key. Defined by an array of strings that
* can contains the following values:
* - search: allow to search (https and http)
* - addObject: allows to add/update an object in the index (https only)
* - deleteObject : allows to delete an existing object (https only)
* - deleteIndex : allows to delete index content (https only)
* - settings : allows to get index settings (https only)
* - editSettings : allows to change index settings (https only)
* @param {Object} [params] - Optionnal parameters to set for the key
* @param {number} params.validity - Number of seconds after which the key will
* be automatically removed (0 means no time limit for this key)
* @param {number} params.maxQueriesPerIPPerHour - Number of API calls allowed from an IP address per hour
* @param {number} params.maxHitsPerQuery - Number of hits this API key can retrieve in one call
* @param {string} params.description - A description for your key
* @param {string[]} params.referers - A list of authorized referers
* @param {Object} params.queryParameters - Force the key to use specific query parameters
* @param {Function} callback - The result callback called with two arguments
* error: null or Error('message')
* content: the server answer with user keys list
* @return {Promise|undefined} Returns a promise if no callback given
* @example
* index.addUserKey(['search'], {
* validity: 300,
* maxQueriesPerIPPerHour: 2000,
* maxHitsPerQuery: 3,
* description: 'Eat three fruits',
* referers: ['*.algolia.com'],
* queryParameters: {
* tagFilters: ['public'],
* }
* })
* @see {@link https://www.algolia.com/doc/rest_api#AddIndexKey|Algolia REST API Documentation}
*/
addUserKey: function(acls, params, callback) {
var isArray = __webpack_require__(30);
var usage = 'Usage: index.addUserKey(arrayOfAcls[, params, callback])';
if (!isArray(acls)) {
throw new Error(usage);
}
if (arguments.length === 1 || typeof params === 'function') {
callback = params;
params = null;
}
var postObj = {
acl: acls
};
if (params) {
postObj.validity = params.validity;
postObj.maxQueriesPerIPPerHour = params.maxQueriesPerIPPerHour;
postObj.maxHitsPerQuery = params.maxHitsPerQuery;
postObj.description = params.description;
if (params.queryParameters) {
postObj.queryParameters = this.as._getSearchParams(params.queryParameters, '');
}
postObj.referers = params.referers;
}
return this.as._jsonRequest({
method: 'POST',
url: '/1/indexes/' + encodeURIComponent(this.indexName) + '/keys',
body: postObj,
hostType: 'write',
callback: callback
});
},
/**
* Add an existing user key associated to this index
* @deprecated use index.addUserKey()
*/
addUserKeyWithValidity: deprecate(function deprecatedAddUserKeyWithValidity(acls, params, callback) {
return this.addUserKey(acls, params, callback);
}, deprecatedMessage('index.addUserKeyWithValidity()', 'index.addUserKey()')),
/**
* Update an existing API key of this index
* @param {string} key - The key to update
* @param {string[]} acls - The list of ACL for this key. Defined by an array of strings that
* can contains the following values:
* - search: allow to search (https and http)
* - addObject: allows to add/update an object in the index (https only)
* - deleteObject : allows to delete an existing object (https only)
* - deleteIndex : allows to delete index content (https only)
* - settings : allows to get index settings (https only)
* - editSettings : allows to change index settings (https only)
* @param {Object} [params] - Optionnal parameters to set for the key
* @param {number} params.validity - Number of seconds after which the key will
* be automatically removed (0 means no time limit for this key)
* @param {number} params.maxQueriesPerIPPerHour - Number of API calls allowed from an IP address per hour
* @param {number} params.maxHitsPerQuery - Number of hits this API key can retrieve in one call
* @param {string} params.description - A description for your key
* @param {string[]} params.referers - A list of authorized referers
* @param {Object} params.queryParameters - Force the key to use specific query parameters
* @param {Function} callback - The result callback called with two arguments
* error: null or Error('message')
* content: the server answer with user keys list
* @return {Promise|undefined} Returns a promise if no callback given
* @example
* index.updateUserKey('APIKEY', ['search'], {
* validity: 300,
* maxQueriesPerIPPerHour: 2000,
* maxHitsPerQuery: 3,
* description: 'Eat three fruits',
* referers: ['*.algolia.com'],
* queryParameters: {
* tagFilters: ['public'],
* }
* })
* @see {@link https://www.algolia.com/doc/rest_api#UpdateIndexKey|Algolia REST API Documentation}
*/
updateUserKey: function(key, acls, params, callback) {
var isArray = __webpack_require__(30);
var usage = 'Usage: index.updateUserKey(key, arrayOfAcls[, params, callback])';
if (!isArray(acls)) {
throw new Error(usage);
}
if (arguments.length === 2 || typeof params === 'function') {
callback = params;
params = null;
}
var putObj = {
acl: acls
};
if (params) {
putObj.validity = params.validity;
putObj.maxQueriesPerIPPerHour = params.maxQueriesPerIPPerHour;
putObj.maxHitsPerQuery = params.maxHitsPerQuery;
putObj.description = params.description;
if (params.queryParameters) {
putObj.queryParameters = this.as._getSearchParams(params.queryParameters, '');
}
putObj.referers = params.referers;
}
return this.as._jsonRequest({
method: 'PUT',
url: '/1/indexes/' + encodeURIComponent(this.indexName) + '/keys/' + key,
body: putObj,
hostType: 'write',
callback: callback
});
},
_search: function(params, url, callback) {
return this.as._jsonRequest({
cache: this.cache,
method: 'POST',
url: url || '/1/indexes/' + encodeURIComponent(this.indexName) + '/query',
body: {params: params},
hostType: 'read',
fallback: {
method: 'GET',
url: '/1/indexes/' + encodeURIComponent(this.indexName),
body: {params: params}
},
callback: callback
});
},
as: null,
indexName: null,
typeAheadArgs: null,
typeAheadValueOption: null
};
function prepareHost(protocol) {
return function prepare(host) {
return protocol + '//' + host.toLowerCase();
};
}
function notImplemented() {
var message = 'Not implemented in this environment.\n' +
'If you feel this is a mistake, write to support@algolia.com';
throw new errors.AlgoliaSearchError(message);
}
function deprecatedMessage(previousUsage, newUsage) {
var githubAnchorLink = previousUsage.toLowerCase()
.replace('.', '')
.replace('()', '');
return 'algoliasearch: `' + previousUsage + '` was replaced by `' + newUsage +
'`. Please see https://github.com/algolia/algoliasearch-client-js/wiki/Deprecated#' + githubAnchorLink;
}
// Parse cloud does not supports setTimeout
// We do not store a setTimeout reference in the client everytime
// We only fallback to a fake setTimeout when not available
// setTimeout cannot be override globally sadly
function exitPromise(fn, _setTimeout) {
_setTimeout(fn, 0);
}
function deprecate(fn, message) {
var warned = false;
function deprecated() {
if (!warned) {
/* eslint no-console:0 */
console.log(message);
warned = true;
}
return fn.apply(this, arguments);
}
return deprecated;
}
// Prototype.js < 1.7, a widely used library, defines a weird
// Array.prototype.toJSON function that will fail to stringify our content
// appropriately
// refs:
// - https://groups.google.com/forum/#!topic/prototype-core/E-SAVvV_V9Q
// - https://github.com/sstephenson/prototype/commit/038a2985a70593c1a86c230fadbdfe2e4898a48c
// - http://stackoverflow.com/a/3148441/147079
function safeJSONStringify(obj) {
/* eslint no-extend-native:0 */
if (Array.prototype.toJSON === undefined) {
return JSON.stringify(obj);
}
var toJSON = Array.prototype.toJSON;
delete Array.prototype.toJSON;
var out = JSON.stringify(obj);
Array.prototype.toJSON = toJSON;
return out;
}
/***/ },
/* 10 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
// This file hosts our error definitions
// We use custom error "types" so that we can act on them when we need it
// e.g.: if error instanceof errors.UnparsableJSON then..
var inherits = __webpack_require__(3);
function AlgoliaSearchError(message, extraProperties) {
var forEach = __webpack_require__(11);
var error = this;
// try to get a stacktrace
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
} else {
error.stack = (new Error()).stack || 'Cannot get a stacktrace, browser is too old';
}
this.name = this.constructor.name;
this.message = message || 'Unknown error';
if (extraProperties) {
forEach(extraProperties, function addToErrorObject(value, key) {
error[key] = value;
});
}
}
inherits(AlgoliaSearchError, Error);
function createCustomError(name, message) {
function AlgoliaSearchCustomError() {
var args = Array.prototype.slice.call(arguments, 0);
// custom message not set, use default
if (typeof args[0] !== 'string') {
args.unshift(message);
}
AlgoliaSearchError.apply(this, args);
this.name = 'AlgoliaSearch' + name + 'Error';
}
inherits(AlgoliaSearchCustomError, AlgoliaSearchError);
return AlgoliaSearchCustomError;
}
// late exports to let various fn defs and inherits take place
module.exports = {
AlgoliaSearchError: AlgoliaSearchError,
UnparsableJSON: createCustomError(
'UnparsableJSON',
'Could not parse the incoming response as JSON, see err.more for details'
),
RequestTimeout: createCustomError(
'RequestTimeout',
'Request timedout before getting a response'
),
Network: createCustomError(
'Network',
'Network issue, see err.more for details'
),
JSONPScriptFail: createCustomError(
'JSONPScriptFail',
'<script> was loaded but did not call our provided callback'
),
JSONPScriptError: createCustomError(
'JSONPScriptError',
'<script> unable to load due to an `error` event on it'
),
Unknown: createCustomError(
'Unknown',
'Unknown error occured'
)
};
/***/ },
/* 11 */
/***/ function(module, exports, __webpack_require__) {
var arrayEach = __webpack_require__(12),
baseEach = __webpack_require__(13),
createForEach = __webpack_require__(34);
/**
* Iterates over elements of `collection` invoking `iteratee` for each element.
* The `iteratee` is bound to `thisArg` and invoked with three arguments:
* (value, index|key, collection). Iteratee functions may exit iteration early
* by explicitly returning `false`.
*
* **Note:** As with other "Collections" methods, objects with a "length" property
* are iterated like arrays. To avoid this behavior `_.forIn` or `_.forOwn`
* may be used for object iteration.
*
* @static
* @memberOf _
* @alias each
* @category Collection
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function} [iteratee=_.identity] The function invoked per iteration.
* @param {*} [thisArg] The `this` binding of `iteratee`.
* @returns {Array|Object|string} Returns `collection`.
* @example
*
* _([1, 2]).forEach(function(n) {
* console.log(n);
* }).value();
* // => logs each value from left to right and returns the array
*
* _.forEach({ 'a': 1, 'b': 2 }, function(n, key) {
* console.log(n, key);
* });
* // => logs each value-key pair and returns the object (iteration order is not guaranteed)
*/
var forEach = createForEach(arrayEach, baseEach);
module.exports = forEach;
/***/ },
/* 12 */
/***/ function(module, exports) {
/**
* A specialized version of `_.forEach` for arrays without support for callback
* shorthands and `this` binding.
*
* @private
* @param {Array} array The array to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @returns {Array} Returns `array`.
*/
function arrayEach(array, iteratee) {
var index = -1,
length = array.length;
while (++index < length) {
if (iteratee(array[index], index, array) === false) {
break;
}
}
return array;
}
module.exports = arrayEach;
/***/ },
/* 13 */
/***/ function(module, exports, __webpack_require__) {
var baseForOwn = __webpack_require__(14),
createBaseEach = __webpack_require__(33);
/**
* The base implementation of `_.forEach` without support for callback
* shorthands and `this` binding.
*
* @private
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @returns {Array|Object|string} Returns `collection`.
*/
var baseEach = createBaseEach(baseForOwn);
module.exports = baseEach;
/***/ },
/* 14 */
/***/ function(module, exports, __webpack_require__) {
var baseFor = __webpack_require__(15),
keys = __webpack_require__(19);
/**
* The base implementation of `_.forOwn` without support for callback
* shorthands and `this` binding.
*
* @private
* @param {Object} object The object to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @returns {Object} Returns `object`.
*/
function baseForOwn(object, iteratee) {
return baseFor(object, iteratee, keys);
}
module.exports = baseForOwn;
/***/ },
/* 15 */
/***/ function(module, exports, __webpack_require__) {
var createBaseFor = __webpack_require__(16);
/**
* The base implementation of `baseForIn` and `baseForOwn` which iterates
* over `object` properties returned by `keysFunc` invoking `iteratee` for
* each property. Iteratee functions may exit iteration early by explicitly
* returning `false`.
*
* @private
* @param {Object} object The object to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @param {Function} keysFunc The function to get the keys of `object`.
* @returns {Object} Returns `object`.
*/
var baseFor = createBaseFor();
module.exports = baseFor;
/***/ },
/* 16 */
/***/ function(module, exports, __webpack_require__) {
var toObject = __webpack_require__(17);
/**
* Creates a base function for `_.forIn` or `_.forInRight`.
*
* @private
* @param {boolean} [fromRight] Specify iterating from right to left.
* @returns {Function} Returns the new base function.
*/
function createBaseFor(fromRight) {
return function(object, iteratee, keysFunc) {
var iterable = toObject(object),
props = keysFunc(object),
length = props.length,
index = fromRight ? length : -1;
while ((fromRight ? index-- : ++index < length)) {
var key = props[index];
if (iteratee(iterable[key], key, iterable) === false) {
break;
}
}
return object;
};
}
module.exports = createBaseFor;
/***/ },
/* 17 */
/***/ function(module, exports, __webpack_require__) {
var isObject = __webpack_require__(18);
/**
* Converts `value` to an object if it's not one.
*
* @private
* @param {*} value The value to process.
* @returns {Object} Returns the object.
*/
function toObject(value) {
return isObject(value) ? value : Object(value);
}
module.exports = toObject;
/***/ },
/* 18 */
/***/ function(module, exports) {
/**
* Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
* (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is an object, else `false`.
* @example
*
* _.isObject({});
* // => true
*
* _.isObject([1, 2, 3]);
* // => true
*
* _.isObject(1);
* // => false
*/
function isObject(value) {
// Avoid a V8 JIT bug in Chrome 19-20.
// See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
var type = typeof value;
return !!value && (type == 'object' || type == 'function');
}
module.exports = isObject;
/***/ },
/* 19 */
/***/ function(module, exports, __webpack_require__) {
var getNative = __webpack_require__(20),
isArrayLike = __webpack_require__(24),
isObject = __webpack_require__(18),
shimKeys = __webpack_require__(28);
/* Native method references for those with the same name as other `lodash` methods. */
var nativeKeys = getNative(Object, 'keys');
/**
* Creates an array of the own enumerable property names of `object`.
*
* **Note:** Non-object values are coerced to objects. See the
* [ES spec](http://ecma-international.org/ecma-262/6.0/#sec-object.keys)
* for more details.
*
* @static
* @memberOf _
* @category Object
* @param {Object} object The object to query.
* @returns {Array} Returns the array of property names.
* @example
*
* function Foo() {
* this.a = 1;
* this.b = 2;
* }
*
* Foo.prototype.c = 3;
*
* _.keys(new Foo);
* // => ['a', 'b'] (iteration order is not guaranteed)
*
* _.keys('hi');
* // => ['0', '1']
*/
var keys = !nativeKeys ? shimKeys : function(object) {
var Ctor = object == null ? undefined : object.constructor;
if ((typeof Ctor == 'function' && Ctor.prototype === object) ||
(typeof object != 'function' && isArrayLike(object))) {
return shimKeys(object);
}
return isObject(object) ? nativeKeys(object) : [];
};
module.exports = keys;
/***/ },
/* 20 */
/***/ function(module, exports, __webpack_require__) {
var isNative = __webpack_require__(21);
/**
* Gets the native function at `key` of `object`.
*
* @private
* @param {Object} object The object to query.
* @param {string} key The key of the method to get.
* @returns {*} Returns the function if it's native, else `undefined`.
*/
function getNative(object, key) {
var value = object == null ? undefined : object[key];
return isNative(value) ? value : undefined;
}
module.exports = getNative;
/***/ },
/* 21 */
/***/ function(module, exports, __webpack_require__) {
var isFunction = __webpack_require__(22),
isObjectLike = __webpack_require__(23);
/** Used to detect host constructors (Safari > 5). */
var reIsHostCtor = /^\[object .+?Constructor\]$/;
/** Used for native method references. */
var objectProto = Object.prototype;
/** Used to resolve the decompiled source of functions. */
var fnToString = Function.prototype.toString;
/** Used to check objects for own properties. */
var hasOwnProperty = objectProto.hasOwnProperty;
/** Used to detect if a method is native. */
var reIsNative = RegExp('^' +
fnToString.call(hasOwnProperty).replace(/[\\^$.*+?()[\]{}|]/g, '\\$&')
.replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
);
/**
* Checks if `value` is a native function.
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a native function, else `false`.
* @example
*
* _.isNative(Array.prototype.push);
* // => true
*
* _.isNative(_);
* // => false
*/
function isNative(value) {
if (value == null) {
return false;
}
if (isFunction(value)) {
return reIsNative.test(fnToString.call(value));
}
return isObjectLike(value) && reIsHostCtor.test(value);
}
module.exports = isNative;
/***/ },
/* 22 */
/***/ function(module, exports, __webpack_require__) {
var isObject = __webpack_require__(18);
/** `Object#toString` result references. */
var funcTag = '[object Function]';
/** Used for native method references. */
var objectProto = Object.prototype;
/**
* Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
* of values.
*/
var objToString = objectProto.toString;
/**
* Checks if `value` is classified as a `Function` object.
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
* @example
*
* _.isFunction(_);
* // => true
*
* _.isFunction(/abc/);
* // => false
*/
function isFunction(value) {
// The use of `Object#toString` avoids issues with the `typeof` operator
// in older versions of Chrome and Safari which return 'function' for regexes
// and Safari 8 which returns 'object' for typed array constructors.
return isObject(value) && objToString.call(value) == funcTag;
}
module.exports = isFunction;
/***/ },
/* 23 */
/***/ function(module, exports) {
/**
* Checks if `value` is object-like.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is object-like, else `false`.
*/
function isObjectLike(value) {
return !!value && typeof value == 'object';
}
module.exports = isObjectLike;
/***/ },
/* 24 */
/***/ function(module, exports, __webpack_require__) {
var getLength = __webpack_require__(25),
isLength = __webpack_require__(27);
/**
* Checks if `value` is array-like.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is array-like, else `false`.
*/
function isArrayLike(value) {
return value != null && isLength(getLength(value));
}
module.exports = isArrayLike;
/***/ },
/* 25 */
/***/ function(module, exports, __webpack_require__) {
var baseProperty = __webpack_require__(26);
/**
* Gets the "length" property value of `object`.
*
* **Note:** This function is used to avoid a [JIT bug](https://bugs.webkit.org/show_bug.cgi?id=142792)
* that affects Safari on at least iOS 8.1-8.3 ARM64.
*
* @private
* @param {Object} object The object to query.
* @returns {*} Returns the "length" value.
*/
var getLength = baseProperty('length');
module.exports = getLength;
/***/ },
/* 26 */
/***/ function(module, exports) {
/**
* The base implementation of `_.property` without support for deep paths.
*
* @private
* @param {string} key The key of the property to get.
* @returns {Function} Returns the new function.
*/
function baseProperty(key) {
return function(object) {
return object == null ? undefined : object[key];
};
}
module.exports = baseProperty;
/***/ },
/* 27 */
/***/ function(module, exports) {
/**
* Used as the [maximum length](http://ecma-international.org/ecma-262/6.0/#sec-number.max_safe_integer)
* of an array-like value.
*/
var MAX_SAFE_INTEGER = 9007199254740991;
/**
* Checks if `value` is a valid array-like length.
*
* **Note:** This function is based on [`ToLength`](http://ecma-international.org/ecma-262/6.0/#sec-tolength).
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
*/
function isLength(value) {
return typeof value == 'number' && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
}
module.exports = isLength;
/***/ },
/* 28 */
/***/ function(module, exports, __webpack_require__) {
var isArguments = __webpack_require__(29),
isArray = __webpack_require__(30),
isIndex = __webpack_require__(31),
isLength = __webpack_require__(27),
keysIn = __webpack_require__(32);
/** Used for native method references. */
var objectProto = Object.prototype;
/** Used to check objects for own properties. */
var hasOwnProperty = objectProto.hasOwnProperty;
/**
* A fallback implementation of `Object.keys` which creates an array of the
* own enumerable property names of `object`.
*
* @private
* @param {Object} object The object to query.
* @returns {Array} Returns the array of property names.
*/
function shimKeys(object) {
var props = keysIn(object),
propsLength = props.length,
length = propsLength && object.length;
var allowIndexes = !!length && isLength(length) &&
(isArray(object) || isArguments(object));
var index = -1,
result = [];
while (++index < propsLength) {
var key = props[index];
if ((allowIndexes && isIndex(key, length)) || hasOwnProperty.call(object, key)) {
result.push(key);
}
}
return result;
}
module.exports = shimKeys;
/***/ },
/* 29 */
/***/ function(module, exports, __webpack_require__) {
var isArrayLike = __webpack_require__(24),
isObjectLike = __webpack_require__(23);
/** Used for native method references. */
var objectProto = Object.prototype;
/** Used to check objects for own properties. */
var hasOwnProperty = objectProto.hasOwnProperty;
/** Native method references. */
var propertyIsEnumerable = objectProto.propertyIsEnumerable;
/**
* Checks if `value` is classified as an `arguments` object.
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
* @example
*
* _.isArguments(function() { return arguments; }());
* // => true
*
* _.isArguments([1, 2, 3]);
* // => false
*/
function isArguments(value) {
return isObjectLike(value) && isArrayLike(value) &&
hasOwnProperty.call(value, 'callee') && !propertyIsEnumerable.call(value, 'callee');
}
module.exports = isArguments;
/***/ },
/* 30 */
/***/ function(module, exports, __webpack_require__) {
var getNative = __webpack_require__(20),
isLength = __webpack_require__(27),
isObjectLike = __webpack_require__(23);
/** `Object#toString` result references. */
var arrayTag = '[object Array]';
/** Used for native method references. */
var objectProto = Object.prototype;
/**
* Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
* of values.
*/
var objToString = objectProto.toString;
/* Native method references for those with the same name as other `lodash` methods. */
var nativeIsArray = getNative(Array, 'isArray');
/**
* Checks if `value` is classified as an `Array` object.
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
* @example
*
* _.isArray([1, 2, 3]);
* // => true
*
* _.isArray(function() { return arguments; }());
* // => false
*/
var isArray = nativeIsArray || function(value) {
return isObjectLike(value) && isLength(value.length) && objToString.call(value) == arrayTag;
};
module.exports = isArray;
/***/ },
/* 31 */
/***/ function(module, exports) {
/** Used to detect unsigned integer values. */
var reIsUint = /^\d+$/;
/**
* Used as the [maximum length](http://ecma-international.org/ecma-262/6.0/#sec-number.max_safe_integer)
* of an array-like value.
*/
var MAX_SAFE_INTEGER = 9007199254740991;
/**
* Checks if `value` is a valid array-like index.
*
* @private
* @param {*} value The value to check.
* @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
* @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
*/
function isIndex(value, length) {
value = (typeof value == 'number' || reIsUint.test(value)) ? +value : -1;
length = length == null ? MAX_SAFE_INTEGER : length;
return value > -1 && value % 1 == 0 && value < length;
}
module.exports = isIndex;
/***/ },
/* 32 */
/***/ function(module, exports, __webpack_require__) {
var isArguments = __webpack_require__(29),
isArray = __webpack_require__(30),
isIndex = __webpack_require__(31),
isLength = __webpack_require__(27),
isObject = __webpack_require__(18);
/** Used for native method references. */
var objectProto = Object.prototype;
/** Used to check objects for own properties. */
var hasOwnProperty = objectProto.hasOwnProperty;
/**
* Creates an array of the own and inherited enumerable property names of `object`.
*
* **Note:** Non-object values are coerced to objects.
*
* @static
* @memberOf _
* @category Object
* @param {Object} object The object to query.
* @returns {Array} Returns the array of property names.
* @example
*
* function Foo() {
* this.a = 1;
* this.b = 2;
* }
*
* Foo.prototype.c = 3;
*
* _.keysIn(new Foo);
* // => ['a', 'b', 'c'] (iteration order is not guaranteed)
*/
function keysIn(object) {
if (object == null) {
return [];
}
if (!isObject(object)) {
object = Object(object);
}
var length = object.length;
length = (length && isLength(length) &&
(isArray(object) || isArguments(object)) && length) || 0;
var Ctor = object.constructor,
index = -1,
isProto = typeof Ctor == 'function' && Ctor.prototype === object,
result = Array(length),
skipIndexes = length > 0;
while (++index < length) {
result[index] = (index + '');
}
for (var key in object) {
if (!(skipIndexes && isIndex(key, length)) &&
!(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) {
result.push(key);
}
}
return result;
}
module.exports = keysIn;
/***/ },
/* 33 */
/***/ function(module, exports, __webpack_require__) {
var getLength = __webpack_require__(25),
isLength = __webpack_require__(27),
toObject = __webpack_require__(17);
/**
* Creates a `baseEach` or `baseEachRight` function.
*
* @private
* @param {Function} eachFunc The function to iterate over a collection.
* @param {boolean} [fromRight] Specify iterating from right to left.
* @returns {Function} Returns the new base function.
*/
function createBaseEach(eachFunc, fromRight) {
return function(collection, iteratee) {
var length = collection ? getLength(collection) : 0;
if (!isLength(length)) {
return eachFunc(collection, iteratee);
}
var index = fromRight ? length : -1,
iterable = toObject(collection);
while ((fromRight ? index-- : ++index < length)) {
if (iteratee(iterable[index], index, iterable) === false) {
break;
}
}
return collection;
};
}
module.exports = createBaseEach;
/***/ },
/* 34 */
/***/ function(module, exports, __webpack_require__) {
var bindCallback = __webpack_require__(35),
isArray = __webpack_require__(30);
/**
* Creates a function for `_.forEach` or `_.forEachRight`.
*
* @private
* @param {Function} arrayFunc The function to iterate over an array.
* @param {Function} eachFunc The function to iterate over a collection.
* @returns {Function} Returns the new each function.
*/
function createForEach(arrayFunc, eachFunc) {
return function(collection, iteratee, thisArg) {
return (typeof iteratee == 'function' && thisArg === undefined && isArray(collection))
? arrayFunc(collection, iteratee)
: eachFunc(collection, bindCallback(iteratee, thisArg, 3));
};
}
module.exports = createForEach;
/***/ },
/* 35 */
/***/ function(module, exports, __webpack_require__) {
var identity = __webpack_require__(36);
/**
* A specialized version of `baseCallback` which only supports `this` binding
* and specifying the number of arguments to provide to `func`.
*
* @private
* @param {Function} func The function to bind.
* @param {*} thisArg The `this` binding of `func`.
* @param {number} [argCount] The number of arguments to provide to `func`.
* @returns {Function} Returns the callback.
*/
function bindCallback(func, thisArg, argCount) {
if (typeof func != 'function') {
return identity;
}
if (thisArg === undefined) {
return func;
}
switch (argCount) {
case 1: return function(value) {
return func.call(thisArg, value);
};
case 3: return function(value, index, collection) {
return func.call(thisArg, value, index, collection);
};
case 4: return function(accumulator, value, index, collection) {
return func.call(thisArg, accumulator, value, index, collection);
};
case 5: return function(value, other, key, object, source) {
return func.call(thisArg, value, other, key, object, source);
};
}
return function() {
return func.apply(thisArg, arguments);
};
}
module.exports = bindCallback;
/***/ },
/* 36 */
/***/ function(module, exports) {
/**
* This method returns the first argument provided to it.
*
* @static
* @memberOf _
* @category Utility
* @param {*} value Any value.
* @returns {*} Returns `value`.
* @example
*
* var object = { 'user': 'fred' };
*
* _.identity(object) === object;
* // => true
*/
function identity(value) {
return value;
}
module.exports = identity;
/***/ },
/* 37 */
/***/ function(module, exports, __webpack_require__) {
module.exports = buildSearchMethod;
var errors = __webpack_require__(10);
function buildSearchMethod(queryParam, url) {
return function search(query, args, callback) {
// warn V2 users on how to search
if (typeof query === 'function' && typeof args === 'object' ||
typeof callback === 'object') {
// .search(query, params, cb)
// .search(cb, params)
throw new errors.AlgoliaSearchError('index.search usage is index.search(query, params, cb)');
}
if (arguments.length === 0 || typeof query === 'function') {
// .search(), .search(cb)
callback = query;
query = '';
} else if (arguments.length === 1 || typeof args === 'function') {
// .search(query/args), .search(query, cb)
callback = args;
args = undefined;
}
// .search(args), careful: typeof null === 'object'
if (typeof query === 'object' && query !== null) {
args = query;
query = undefined;
} else if (query === undefined || query === null) { // .search(undefined/null)
query = '';
}
var params = '';
if (query !== undefined) {
params += queryParam + '=' + encodeURIComponent(query);
}
if (args !== undefined) {
// `_getSearchParams` will augment params, do not be fooled by the = versus += from previous if
params = this.as._getSearchParams(args, params);
}
return this._search(params, url, callback);
};
}
/***/ },
/* 38 */
/***/ function(module, exports, __webpack_require__) {
/**
* This is the web browser implementation of `debug()`.
*
* Expose `debug()` as the module.
*/
exports = module.exports = __webpack_require__(39);
exports.log = log;
exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
exports.storage = 'undefined' != typeof chrome
&& 'undefined' != typeof chrome.storage
? chrome.storage.local
: localstorage();
/**
* Colors.
*/
exports.colors = [
'lightseagreen',
'forestgreen',
'goldenrod',
'dodgerblue',
'darkorchid',
'crimson'
];
/**
* Currently only WebKit-based Web Inspectors, Firefox >= v31,
* and the Firebug extension (any Firefox version) are known
* to support "%c" CSS customizations.
*
* TODO: add a `localStorage` variable to explicitly enable/disable colors
*/
function useColors() {
// is webkit? http://stackoverflow.com/a/16459606/376773
return ('WebkitAppearance' in document.documentElement.style) ||
// is firebug? http://stackoverflow.com/a/398120/376773
(window.console && (console.firebug || (console.exception && console.table))) ||
// is firefox >= v31?
// https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
(navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31);
}
/**
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
*/
exports.formatters.j = function(v) {
return JSON.stringify(v);
};
/**
* Colorize log arguments if enabled.
*
* @api public
*/
function formatArgs() {
var args = arguments;
var useColors = this.useColors;
args[0] = (useColors ? '%c' : '')
+ this.namespace
+ (useColors ? ' %c' : ' ')
+ args[0]
+ (useColors ? '%c ' : ' ')
+ '+' + exports.humanize(this.diff);
if (!useColors) return args;
var c = 'color: ' + this.color;
args = [args[0], c, 'color: inherit'].concat(Array.prototype.slice.call(args, 1));
// the final "%c" is somewhat tricky, because there could be other
// arguments passed either before or after the %c, so we need to
// figure out the correct index to insert the CSS into
var index = 0;
var lastC = 0;
args[0].replace(/%[a-z%]/g, function(match) {
if ('%%' === match) return;
index++;
if ('%c' === match) {
// we only are interested in the *last* %c
// (the user may have provided their own)
lastC = index;
}
});
args.splice(lastC, 0, c);
return args;
}
/**
* Invokes `console.log()` when available.
* No-op when `console.log` is not a "function".
*
* @api public
*/
function log() {
// this hackery is required for IE8/9, where
// the `console.log` function doesn't have 'apply'
return 'object' === typeof console
&& console.log
&& Function.prototype.apply.call(console.log, console, arguments);
}
/**
* Save `namespaces`.
*
* @param {String} namespaces
* @api private
*/
function save(namespaces) {
try {
if (null == namespaces) {
exports.storage.removeItem('debug');
} else {
exports.storage.debug = namespaces;
}
} catch(e) {}
}
/**
* Load `namespaces`.
*
* @return {String} returns the previously persisted debug modes
* @api private
*/
function load() {
var r;
try {
r = exports.storage.debug;
} catch(e) {}
return r;
}
/**
* Enable namespaces listed in `localStorage.debug` initially.
*/
exports.enable(load());
/**
* Localstorage attempts to return the localstorage.
*
* This is necessary because safari throws
* when a user disables cookies/localstorage
* and you attempt to access it.
*
* @return {LocalStorage}
* @api private
*/
function localstorage(){
try {
return window.localStorage;
} catch (e) {}
}
/***/ },
/* 39 */
/***/ function(module, exports, __webpack_require__) {
/**
* This is the common logic for both the Node.js and web browser
* implementations of `debug()`.
*
* Expose `debug()` as the module.
*/
exports = module.exports = debug;
exports.coerce = coerce;
exports.disable = disable;
exports.enable = enable;
exports.enabled = enabled;
exports.humanize = __webpack_require__(40);
/**
* The currently active debug mode names, and names to skip.
*/
exports.names = [];
exports.skips = [];
/**
* Map of special "%n" handling functions, for the debug "format" argument.
*
* Valid key names are a single, lowercased letter, i.e. "n".
*/
exports.formatters = {};
/**
* Previously assigned color.
*/
var prevColor = 0;
/**
* Previous log timestamp.
*/
var prevTime;
/**
* Select a color.
*
* @return {Number}
* @api private
*/
function selectColor() {
return exports.colors[prevColor++ % exports.colors.length];
}
/**
* Create a debugger with the given `namespace`.
*
* @param {String} namespace
* @return {Function}
* @api public
*/
function debug(namespace) {
// define the `disabled` version
function disabled() {
}
disabled.enabled = false;
// define the `enabled` version
function enabled() {
var self = enabled;
// set `diff` timestamp
var curr = +new Date();
var ms = curr - (prevTime || curr);
self.diff = ms;
self.prev = prevTime;
self.curr = curr;
prevTime = curr;
// add the `color` if not set
if (null == self.useColors) self.useColors = exports.useColors();
if (null == self.color && self.useColors) self.color = selectColor();
var args = Array.prototype.slice.call(arguments);
args[0] = exports.coerce(args[0]);
if ('string' !== typeof args[0]) {
// anything else let's inspect with %o
args = ['%o'].concat(args);
}
// apply any `formatters` transformations
var index = 0;
args[0] = args[0].replace(/%([a-z%])/g, function(match, format) {
// if we encounter an escaped % then don't increase the array index
if (match === '%%') return match;
index++;
var formatter = exports.formatters[format];
if ('function' === typeof formatter) {
var val = args[index];
match = formatter.call(self, val);
// now we need to remove `args[index]` since it's inlined in the `format`
args.splice(index, 1);
index--;
}
return match;
});
if ('function' === typeof exports.formatArgs) {
args = exports.formatArgs.apply(self, args);
}
var logFn = enabled.log || exports.log || console.log.bind(console);
logFn.apply(self, args);
}
enabled.enabled = true;
var fn = exports.enabled(namespace) ? enabled : disabled;
fn.namespace = namespace;
return fn;
}
/**
* Enables a debug mode by namespaces. This can include modes
* separated by a colon and wildcards.
*
* @param {String} namespaces
* @api public
*/
function enable(namespaces) {
exports.save(namespaces);
var split = (namespaces || '').split(/[\s,]+/);
var len = split.length;
for (var i = 0; i < len; i++) {
if (!split[i]) continue; // ignore empty strings
namespaces = split[i].replace(/\*/g, '.*?');
if (namespaces[0] === '-') {
exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
} else {
exports.names.push(new RegExp('^' + namespaces + '$'));
}
}
}
/**
* Disable debug output.
*
* @api public
*/
function disable() {
exports.enable('');
}
/**
* Returns true if the given mode name is enabled, false otherwise.
*
* @param {String} name
* @return {Boolean}
* @api public
*/
function enabled(name) {
var i, len;
for (i = 0, len = exports.skips.length; i < len; i++) {
if (exports.skips[i].test(name)) {
return false;
}
}
for (i = 0, len = exports.names.length; i < len; i++) {
if (exports.names[i].test(name)) {
return true;
}
}
return false;
}
/**
* Coerce `val`.
*
* @param {Mixed} val
* @return {Mixed}
* @api private
*/
function coerce(val) {
if (val instanceof Error) return val.stack || val.message;
return val;
}
/***/ },
/* 40 */
/***/ function(module, exports) {
/**
* Helpers.
*/
var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
var y = d * 365.25;
/**
* Parse or format the given `val`.
*
* Options:
*
* - `long` verbose formatting [false]
*
* @param {String|Number} val
* @param {Object} options
* @return {String|Number}
* @api public
*/
module.exports = function(val, options){
options = options || {};
if ('string' == typeof val) return parse(val);
// long, short were "future reserved words in js", YUI compressor fail on them
// https://github.com/algolia/algoliasearch-client-js/issues/113#issuecomment-111978606
// https://github.com/yui/yuicompressor/issues/47
// https://github.com/rauchg/ms.js/pull/40
return options['long']
? _long(val)
: _short(val);
};
/**
* Parse the given `str` and return milliseconds.
*
* @param {String} str
* @return {Number}
* @api private
*/
function parse(str) {
str = '' + str;
if (str.length > 10000) return;
var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(str);
if (!match) return;
var n = parseFloat(match[1]);
var type = (match[2] || 'ms').toLowerCase();
switch (type) {
case 'years':
case 'year':
case 'yrs':
case 'yr':
case 'y':
return n * y;
case 'days':
case 'day':
case 'd':
return n * d;
case 'hours':
case 'hour':
case 'hrs':
case 'hr':
case 'h':
return n * h;
case 'minutes':
case 'minute':
case 'mins':
case 'min':
case 'm':
return n * m;
case 'seconds':
case 'second':
case 'secs':
case 'sec':
case 's':
return n * s;
case 'milliseconds':
case 'millisecond':
case 'msecs':
case 'msec':
case 'ms':
return n;
}
}
/**
* Short format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
function _short(ms) {
if (ms >= d) return Math.round(ms / d) + 'd';
if (ms >= h) return Math.round(ms / h) + 'h';
if (ms >= m) return Math.round(ms / m) + 'm';
if (ms >= s) return Math.round(ms / s) + 's';
return ms + 'ms';
}
/**
* Long format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
function _long(ms) {
return plural(ms, d, 'day')
|| plural(ms, h, 'hour')
|| plural(ms, m, 'minute')
|| plural(ms, s, 'second')
|| ms + ' ms';
}
/**
* Pluralization helper.
*/
function plural(ms, n, name) {
if (ms < n) return;
if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name;
return Math.ceil(ms / n) + ' ' + name + 's';
}
/***/ },
/* 41 */
/***/ function(module, exports, __webpack_require__) {
var baseClone = __webpack_require__(42),
bindCallback = __webpack_require__(35),
isIterateeCall = __webpack_require__(50);
/**
* Creates a clone of `value`. If `isDeep` is `true` nested objects are cloned,
* otherwise they are assigned by reference. If `customizer` is provided it's
* invoked to produce the cloned values. If `customizer` returns `undefined`
* cloning is handled by the method instead. The `customizer` is bound to
* `thisArg` and invoked with up to three argument; (value [, index|key, object]).
*
* **Note:** This method is loosely based on the
* [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm).
* The enumerable properties of `arguments` objects and objects created by
* constructors other than `Object` are cloned to plain `Object` objects. An
* empty object is returned for uncloneable values such as functions, DOM nodes,
* Maps, Sets, and WeakMaps.
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to clone.
* @param {boolean} [isDeep] Specify a deep clone.
* @param {Function} [customizer] The function to customize cloning values.
* @param {*} [thisArg] The `this` binding of `customizer`.
* @returns {*} Returns the cloned value.
* @example
*
* var users = [
* { 'user': 'barney' },
* { 'user': 'fred' }
* ];
*
* var shallow = _.clone(users);
* shallow[0] === users[0];
* // => true
*
* var deep = _.clone(users, true);
* deep[0] === users[0];
* // => false
*
* // using a customizer callback
* var el = _.clone(document.body, function(value) {
* if (_.isElement(value)) {
* return value.cloneNode(false);
* }
* });
*
* el === document.body
* // => false
* el.nodeName
* // => BODY
* el.childNodes.length;
* // => 0
*/
function clone(value, isDeep, customizer, thisArg) {
if (isDeep && typeof isDeep != 'boolean' && isIterateeCall(value, isDeep, customizer)) {
isDeep = false;
}
else if (typeof isDeep == 'function') {
thisArg = customizer;
customizer = isDeep;
isDeep = false;
}
return typeof customizer == 'function'
? baseClone(value, isDeep, bindCallback(customizer, thisArg, 3))
: baseClone(value, isDeep);
}
module.exports = clone;
/***/ },
/* 42 */
/***/ function(module, exports, __webpack_require__) {
var arrayCopy = __webpack_require__(43),
arrayEach = __webpack_require__(12),
baseAssign = __webpack_require__(44),
baseForOwn = __webpack_require__(14),
initCloneArray = __webpack_require__(46),
initCloneByTag = __webpack_require__(47),
initCloneObject = __webpack_require__(49),
isArray = __webpack_require__(30),
isObject = __webpack_require__(18);
/** `Object#toString` result references. */
var argsTag = '[object Arguments]',
arrayTag = '[object Array]',
boolTag = '[object Boolean]',
dateTag = '[object Date]',
errorTag = '[object Error]',
funcTag = '[object Function]',
mapTag = '[object Map]',
numberTag = '[object Number]',
objectTag = '[object Object]',
regexpTag = '[object RegExp]',
setTag = '[object Set]',
stringTag = '[object String]',
weakMapTag = '[object WeakMap]';
var arrayBufferTag = '[object ArrayBuffer]',
float32Tag = '[object Float32Array]',
float64Tag = '[object Float64Array]',
int8Tag = '[object Int8Array]',
int16Tag = '[object Int16Array]',
int32Tag = '[object Int32Array]',
uint8Tag = '[object Uint8Array]',
uint8ClampedTag = '[object Uint8ClampedArray]',
uint16Tag = '[object Uint16Array]',
uint32Tag = '[object Uint32Array]';
/** Used to identify `toStringTag` values supported by `_.clone`. */
var cloneableTags = {};
cloneableTags[argsTag] = cloneableTags[arrayTag] =
cloneableTags[arrayBufferTag] = cloneableTags[boolTag] =
cloneableTags[dateTag] = cloneableTags[float32Tag] =
cloneableTags[float64Tag] = cloneableTags[int8Tag] =
cloneableTags[int16Tag] = cloneableTags[int32Tag] =
cloneableTags[numberTag] = cloneableTags[objectTag] =
cloneableTags[regexpTag] = cloneableTags[stringTag] =
cloneableTags[uint8Tag] = cloneableTags[uint8ClampedTag] =
cloneableTags[uint16Tag] = cloneableTags[uint32Tag] = true;
cloneableTags[errorTag] = cloneableTags[funcTag] =
cloneableTags[mapTag] = cloneableTags[setTag] =
cloneableTags[weakMapTag] = false;
/** Used for native method references. */
var objectProto = Object.prototype;
/**
* Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
* of values.
*/
var objToString = objectProto.toString;
/**
* The base implementation of `_.clone` without support for argument juggling
* and `this` binding `customizer` functions.
*
* @private
* @param {*} value The value to clone.
* @param {boolean} [isDeep] Specify a deep clone.
* @param {Function} [customizer] The function to customize cloning values.
* @param {string} [key] The key of `value`.
* @param {Object} [object] The object `value` belongs to.
* @param {Array} [stackA=[]] Tracks traversed source objects.
* @param {Array} [stackB=[]] Associates clones with source counterparts.
* @returns {*} Returns the cloned value.
*/
function baseClone(value, isDeep, customizer, key, object, stackA, stackB) {
var result;
if (customizer) {
result = object ? customizer(value, key, object) : customizer(value);
}
if (result !== undefined) {
return result;
}
if (!isObject(value)) {
return value;
}
var isArr = isArray(value);
if (isArr) {
result = initCloneArray(value);
if (!isDeep) {
return arrayCopy(value, result);
}
} else {
var tag = objToString.call(value),
isFunc = tag == funcTag;
if (tag == objectTag || tag == argsTag || (isFunc && !object)) {
result = initCloneObject(isFunc ? {} : value);
if (!isDeep) {
return baseAssign(result, value);
}
} else {
return cloneableTags[tag]
? initCloneByTag(value, tag, isDeep)
: (object ? value : {});
}
}
// Check for circular references and return its corresponding clone.
stackA || (stackA = []);
stackB || (stackB = []);
var length = stackA.length;
while (length--) {
if (stackA[length] == value) {
return stackB[length];
}
}
// Add the source value to the stack of traversed objects and associate it with its clone.
stackA.push(value);
stackB.push(result);
// Recursively populate clone (susceptible to call stack limits).
(isArr ? arrayEach : baseForOwn)(value, function(subValue, key) {
result[key] = baseClone(subValue, isDeep, customizer, key, value, stackA, stackB);
});
return result;
}
module.exports = baseClone;
/***/ },
/* 43 */
/***/ function(module, exports) {
/**
* Copies the values of `source` to `array`.
*
* @private
* @param {Array} source The array to copy values from.
* @param {Array} [array=[]] The array to copy values to.
* @returns {Array} Returns `array`.
*/
function arrayCopy(source, array) {
var index = -1,
length = source.length;
array || (array = Array(length));
while (++index < length) {
array[index] = source[index];
}
return array;
}
module.exports = arrayCopy;
/***/ },
/* 44 */
/***/ function(module, exports, __webpack_require__) {
var baseCopy = __webpack_require__(45),
keys = __webpack_require__(19);
/**
* The base implementation of `_.assign` without support for argument juggling,
* multiple sources, and `customizer` functions.
*
* @private
* @param {Object} object The destination object.
* @param {Object} source The source object.
* @returns {Object} Returns `object`.
*/
function baseAssign(object, source) {
return source == null
? object
: baseCopy(source, keys(source), object);
}
module.exports = baseAssign;
/***/ },
/* 45 */
/***/ function(module, exports) {
/**
* Copies properties of `source` to `object`.
*
* @private
* @param {Object} source The object to copy properties from.
* @param {Array} props The property names to copy.
* @param {Object} [object={}] The object to copy properties to.
* @returns {Object} Returns `object`.
*/
function baseCopy(source, props, object) {
object || (object = {});
var index = -1,
length = props.length;
while (++index < length) {
var key = props[index];
object[key] = source[key];
}
return object;
}
module.exports = baseCopy;
/***/ },
/* 46 */
/***/ function(module, exports) {
/** Used for native method references. */
var objectProto = Object.prototype;
/** Used to check objects for own properties. */
var hasOwnProperty = objectProto.hasOwnProperty;
/**
* Initializes an array clone.
*
* @private
* @param {Array} array The array to clone.
* @returns {Array} Returns the initialized clone.
*/
function initCloneArray(array) {
var length = array.length,
result = new array.constructor(length);
// Add array properties assigned by `RegExp#exec`.
if (length && typeof array[0] == 'string' && hasOwnProperty.call(array, 'index')) {
result.index = array.index;
result.input = array.input;
}
return result;
}
module.exports = initCloneArray;
/***/ },
/* 47 */
/***/ function(module, exports, __webpack_require__) {
var bufferClone = __webpack_require__(48);
/** `Object#toString` result references. */
var boolTag = '[object Boolean]',
dateTag = '[object Date]',
numberTag = '[object Number]',
regexpTag = '[object RegExp]',
stringTag = '[object String]';
var arrayBufferTag = '[object ArrayBuffer]',
float32Tag = '[object Float32Array]',
float64Tag = '[object Float64Array]',
int8Tag = '[object Int8Array]',
int16Tag = '[object Int16Array]',
int32Tag = '[object Int32Array]',
uint8Tag = '[object Uint8Array]',
uint8ClampedTag = '[object Uint8ClampedArray]',
uint16Tag = '[object Uint16Array]',
uint32Tag = '[object Uint32Array]';
/** Used to match `RegExp` flags from their coerced string values. */
var reFlags = /\w*$/;
/**
* Initializes an object clone based on its `toStringTag`.
*
* **Note:** This function only supports cloning values with tags of
* `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.
*
* @private
* @param {Object} object The object to clone.
* @param {string} tag The `toStringTag` of the object to clone.
* @param {boolean} [isDeep] Specify a deep clone.
* @returns {Object} Returns the initialized clone.
*/
function initCloneByTag(object, tag, isDeep) {
var Ctor = object.constructor;
switch (tag) {
case arrayBufferTag:
return bufferClone(object);
case boolTag:
case dateTag:
return new Ctor(+object);
case float32Tag: case float64Tag:
case int8Tag: case int16Tag: case int32Tag:
case uint8Tag: case uint8ClampedTag: case uint16Tag: case uint32Tag:
var buffer = object.buffer;
return new Ctor(isDeep ? bufferClone(buffer) : buffer, object.byteOffset, object.length);
case numberTag:
case stringTag:
return new Ctor(object);
case regexpTag:
var result = new Ctor(object.source, reFlags.exec(object));
result.lastIndex = object.lastIndex;
}
return result;
}
module.exports = initCloneByTag;
/***/ },
/* 48 */
/***/ function(module, exports) {
/* WEBPACK VAR INJECTION */(function(global) {/** Native method references. */
var ArrayBuffer = global.ArrayBuffer,
Uint8Array = global.Uint8Array;
/**
* Creates a clone of the given array buffer.
*
* @private
* @param {ArrayBuffer} buffer The array buffer to clone.
* @returns {ArrayBuffer} Returns the cloned array buffer.
*/
function bufferClone(buffer) {
var result = new ArrayBuffer(buffer.byteLength),
view = new Uint8Array(result);
view.set(new Uint8Array(buffer));
return result;
}
module.exports = bufferClone;
/* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }())))
/***/ },
/* 49 */
/***/ function(module, exports) {
/**
* Initializes an object clone.
*
* @private
* @param {Object} object The object to clone.
* @returns {Object} Returns the initialized clone.
*/
function initCloneObject(object) {
var Ctor = object.constructor;
if (!(typeof Ctor == 'function' && Ctor instanceof Ctor)) {
Ctor = Object;
}
return new Ctor;
}
module.exports = initCloneObject;
/***/ },
/* 50 */
/***/ function(module, exports, __webpack_require__) {
var isArrayLike = __webpack_require__(24),
isIndex = __webpack_require__(31),
isObject = __webpack_require__(18);
/**
* Checks if the provided arguments are from an iteratee call.
*
* @private
* @param {*} value The potential iteratee value argument.
* @param {*} index The potential iteratee index or key argument.
* @param {*} object The potential iteratee object argument.
* @returns {boolean} Returns `true` if the arguments are from an iteratee call, else `false`.
*/
function isIterateeCall(value, index, object) {
if (!isObject(object)) {
return false;
}
var type = typeof index;
if (type == 'number'
? (isArrayLike(object) && isIndex(index, object.length))
: (type == 'string' && index in object)) {
var other = object[index];
return value === value ? (value === other) : (other !== other);
}
return false;
}
module.exports = isIterateeCall;
/***/ },
/* 51 */
/***/ function(module, exports, __webpack_require__) {
var arrayMap = __webpack_require__(52),
baseCallback = __webpack_require__(53),
baseMap = __webpack_require__(75),
isArray = __webpack_require__(30);
/**
* Creates an array of values by running each element in `collection` through
* `iteratee`. The `iteratee` is bound to `thisArg` and invoked with three
* arguments: (value, index|key, collection).
*
* If a property name is provided for `iteratee` the created `_.property`
* style callback returns the property value of the given element.
*
* If a value is also provided for `thisArg` the created `_.matchesProperty`
* style callback returns `true` for elements that have a matching property
* value, else `false`.
*
* If an object is provided for `iteratee` the created `_.matches` style
* callback returns `true` for elements that have the properties of the given
* object, else `false`.
*
* Many lodash methods are guarded to work as iteratees for methods like
* `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`.
*
* The guarded methods are:
* `ary`, `callback`, `chunk`, `clone`, `create`, `curry`, `curryRight`,
* `drop`, `dropRight`, `every`, `fill`, `flatten`, `invert`, `max`, `min`,
* `parseInt`, `slice`, `sortBy`, `take`, `takeRight`, `template`, `trim`,
* `trimLeft`, `trimRight`, `trunc`, `random`, `range`, `sample`, `some`,
* `sum`, `uniq`, and `words`
*
* @static
* @memberOf _
* @alias collect
* @category Collection
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [iteratee=_.identity] The function invoked
* per iteration.
* @param {*} [thisArg] The `this` binding of `iteratee`.
* @returns {Array} Returns the new mapped array.
* @example
*
* function timesThree(n) {
* return n * 3;
* }
*
* _.map([1, 2], timesThree);
* // => [3, 6]
*
* _.map({ 'a': 1, 'b': 2 }, timesThree);
* // => [3, 6] (iteration order is not guaranteed)
*
* var users = [
* { 'user': 'barney' },
* { 'user': 'fred' }
* ];
*
* // using the `_.property` callback shorthand
* _.map(users, 'user');
* // => ['barney', 'fred']
*/
function map(collection, iteratee, thisArg) {
var func = isArray(collection) ? arrayMap : baseMap;
iteratee = baseCallback(iteratee, thisArg, 3);
return func(collection, iteratee);
}
module.exports = map;
/***/ },
/* 52 */
/***/ function(module, exports) {
/**
* A specialized version of `_.map` for arrays without support for callback
* shorthands and `this` binding.
*
* @private
* @param {Array} array The array to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @returns {Array} Returns the new mapped array.
*/
function arrayMap(array, iteratee) {
var index = -1,
length = array.length,
result = Array(length);
while (++index < length) {
result[index] = iteratee(array[index], index, array);
}
return result;
}
module.exports = arrayMap;
/***/ },
/* 53 */
/***/ function(module, exports, __webpack_require__) {
var baseMatches = __webpack_require__(54),
baseMatchesProperty = __webpack_require__(66),
bindCallback = __webpack_require__(35),
identity = __webpack_require__(36),
property = __webpack_require__(73);
/**
* The base implementation of `_.callback` which supports specifying the
* number of arguments to provide to `func`.
*
* @private
* @param {*} [func=_.identity] The value to convert to a callback.
* @param {*} [thisArg] The `this` binding of `func`.
* @param {number} [argCount] The number of arguments to provide to `func`.
* @returns {Function} Returns the callback.
*/
function baseCallback(func, thisArg, argCount) {
var type = typeof func;
if (type == 'function') {
return thisArg === undefined
? func
: bindCallback(func, thisArg, argCount);
}
if (func == null) {
return identity;
}
if (type == 'object') {
return baseMatches(func);
}
return thisArg === undefined
? property(func)
: baseMatchesProperty(func, thisArg);
}
module.exports = baseCallback;
/***/ },
/* 54 */
/***/ function(module, exports, __webpack_require__) {
var baseIsMatch = __webpack_require__(55),
getMatchData = __webpack_require__(63),
toObject = __webpack_require__(17);
/**
* The base implementation of `_.matches` which does not clone `source`.
*
* @private
* @param {Object} source The object of property values to match.
* @returns {Function} Returns the new function.
*/
function baseMatches(source) {
var matchData = getMatchData(source);
if (matchData.length == 1 && matchData[0][2]) {
var key = matchData[0][0],
value = matchData[0][1];
return function(object) {
if (object == null) {
return false;
}
return object[key] === value && (value !== undefined || (key in toObject(object)));
};
}
return function(object) {
return baseIsMatch(object, matchData);
};
}
module.exports = baseMatches;
/***/ },
/* 55 */
/***/ function(module, exports, __webpack_require__) {
var baseIsEqual = __webpack_require__(56),
toObject = __webpack_require__(17);
/**
* The base implementation of `_.isMatch` without support for callback
* shorthands and `this` binding.
*
* @private
* @param {Object} object The object to inspect.
* @param {Array} matchData The propery names, values, and compare flags to match.
* @param {Function} [customizer] The function to customize comparing objects.
* @returns {boolean} Returns `true` if `object` is a match, else `false`.
*/
function baseIsMatch(object, matchData, customizer) {
var index = matchData.length,
length = index,
noCustomizer = !customizer;
if (object == null) {
return !length;
}
object = toObject(object);
while (index--) {
var data = matchData[index];
if ((noCustomizer && data[2])
? data[1] !== object[data[0]]
: !(data[0] in object)
) {
return false;
}
}
while (++index < length) {
data = matchData[index];
var key = data[0],
objValue = object[key],
srcValue = data[1];
if (noCustomizer && data[2]) {
if (objValue === undefined && !(key in object)) {
return false;
}
} else {
var result = customizer ? customizer(objValue, srcValue, key) : undefined;
if (!(result === undefined ? baseIsEqual(srcValue, objValue, customizer, true) : result)) {
return false;
}
}
}
return true;
}
module.exports = baseIsMatch;
/***/ },
/* 56 */
/***/ function(module, exports, __webpack_require__) {
var baseIsEqualDeep = __webpack_require__(57),
isObject = __webpack_require__(18),
isObjectLike = __webpack_require__(23);
/**
* The base implementation of `_.isEqual` without support for `this` binding
* `customizer` functions.
*
* @private
* @param {*} value The value to compare.
* @param {*} other The other value to compare.
* @param {Function} [customizer] The function to customize comparing values.
* @param {boolean} [isLoose] Specify performing partial comparisons.
* @param {Array} [stackA] Tracks traversed `value` objects.
* @param {Array} [stackB] Tracks traversed `other` objects.
* @returns {boolean} Returns `true` if the values are equivalent, else `false`.
*/
function baseIsEqual(value, other, customizer, isLoose, stackA, stackB) {
if (value === other) {
return true;
}
if (value == null || other == null || (!isObject(value) && !isObjectLike(other))) {
return value !== value && other !== other;
}
return baseIsEqualDeep(value, other, baseIsEqual, customizer, isLoose, stackA, stackB);
}
module.exports = baseIsEqual;
/***/ },
/* 57 */
/***/ function(module, exports, __webpack_require__) {
var equalArrays = __webpack_require__(58),
equalByTag = __webpack_require__(60),
equalObjects = __webpack_require__(61),
isArray = __webpack_require__(30),
isTypedArray = __webpack_require__(62);
/** `Object#toString` result references. */
var argsTag = '[object Arguments]',
arrayTag = '[object Array]',
objectTag = '[object Object]';
/** Used for native method references. */
var objectProto = Object.prototype;
/** Used to check objects for own properties. */
var hasOwnProperty = objectProto.hasOwnProperty;
/**
* Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
* of values.
*/
var objToString = objectProto.toString;
/**
* A specialized version of `baseIsEqual` for arrays and objects which performs
* deep comparisons and tracks traversed objects enabling objects with circular
* references to be compared.
*
* @private
* @param {Object} object The object to compare.
* @param {Object} other The other object to compare.
* @param {Function} equalFunc The function to determine equivalents of values.
* @param {Function} [customizer] The function to customize comparing objects.
* @param {boolean} [isLoose] Specify performing partial comparisons.
* @param {Array} [stackA=[]] Tracks traversed `value` objects.
* @param {Array} [stackB=[]] Tracks traversed `other` objects.
* @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
*/
function baseIsEqualDeep(object, other, equalFunc, customizer, isLoose, stackA, stackB) {
var objIsArr = isArray(object),
othIsArr = isArray(other),
objTag = arrayTag,
othTag = arrayTag;
if (!objIsArr) {
objTag = objToString.call(object);
if (objTag == argsTag) {
objTag = objectTag;
} else if (objTag != objectTag) {
objIsArr = isTypedArray(object);
}
}
if (!othIsArr) {
othTag = objToString.call(other);
if (othTag == argsTag) {
othTag = objectTag;
} else if (othTag != objectTag) {
othIsArr = isTypedArray(other);
}
}
var objIsObj = objTag == objectTag,
othIsObj = othTag == objectTag,
isSameTag = objTag == othTag;
if (isSameTag && !(objIsArr || objIsObj)) {
return equalByTag(object, other, objTag);
}
if (!isLoose) {
var objIsWrapped = objIsObj && hasOwnProperty.call(object, '__wrapped__'),
othIsWrapped = othIsObj && hasOwnProperty.call(other, '__wrapped__');
if (objIsWrapped || othIsWrapped) {
return equalFunc(objIsWrapped ? object.value() : object, othIsWrapped ? other.value() : other, customizer, isLoose, stackA, stackB);
}
}
if (!isSameTag) {
return false;
}
// Assume cyclic values are equal.
// For more information on detecting circular references see https://es5.github.io/#JO.
stackA || (stackA = []);
stackB || (stackB = []);
var length = stackA.length;
while (length--) {
if (stackA[length] == object) {
return stackB[length] == other;
}
}
// Add `object` and `other` to the stack of traversed objects.
stackA.push(object);
stackB.push(other);
var result = (objIsArr ? equalArrays : equalObjects)(object, other, equalFunc, customizer, isLoose, stackA, stackB);
stackA.pop();
stackB.pop();
return result;
}
module.exports = baseIsEqualDeep;
/***/ },
/* 58 */
/***/ function(module, exports, __webpack_require__) {
var arraySome = __webpack_require__(59);
/**
* A specialized version of `baseIsEqualDeep` for arrays with support for
* partial deep comparisons.
*
* @private
* @param {Array} array The array to compare.
* @param {Array} other The other array to compare.
* @param {Function} equalFunc The function to determine equivalents of values.
* @param {Function} [customizer] The function to customize comparing arrays.
* @param {boolean} [isLoose] Specify performing partial comparisons.
* @param {Array} [stackA] Tracks traversed `value` objects.
* @param {Array} [stackB] Tracks traversed `other` objects.
* @returns {boolean} Returns `true` if the arrays are equivalent, else `false`.
*/
function equalArrays(array, other, equalFunc, customizer, isLoose, stackA, stackB) {
var index = -1,
arrLength = array.length,
othLength = other.length;
if (arrLength != othLength && !(isLoose && othLength > arrLength)) {
return false;
}
// Ignore non-index properties.
while (++index < arrLength) {
var arrValue = array[index],
othValue = other[index],
result = customizer ? customizer(isLoose ? othValue : arrValue, isLoose ? arrValue : othValue, index) : undefined;
if (result !== undefined) {
if (result) {
continue;
}
return false;
}
// Recursively compare arrays (susceptible to call stack limits).
if (isLoose) {
if (!arraySome(other, function(othValue) {
return arrValue === othValue || equalFunc(arrValue, othValue, customizer, isLoose, stackA, stackB);
})) {
return false;
}
} else if (!(arrValue === othValue || equalFunc(arrValue, othValue, customizer, isLoose, stackA, stackB))) {
return false;
}
}
return true;
}
module.exports = equalArrays;
/***/ },
/* 59 */
/***/ function(module, exports) {
/**
* A specialized version of `_.some` for arrays without support for callback
* shorthands and `this` binding.
*
* @private
* @param {Array} array The array to iterate over.
* @param {Function} predicate The function invoked per iteration.
* @returns {boolean} Returns `true` if any element passes the predicate check,
* else `false`.
*/
function arraySome(array, predicate) {
var index = -1,
length = array.length;
while (++index < length) {
if (predicate(array[index], index, array)) {
return true;
}
}
return false;
}
module.exports = arraySome;
/***/ },
/* 60 */
/***/ function(module, exports) {
/** `Object#toString` result references. */
var boolTag = '[object Boolean]',
dateTag = '[object Date]',
errorTag = '[object Error]',
numberTag = '[object Number]',
regexpTag = '[object RegExp]',
stringTag = '[object String]';
/**
* A specialized version of `baseIsEqualDeep` for comparing objects of
* the same `toStringTag`.
*
* **Note:** This function only supports comparing values with tags of
* `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.
*
* @private
* @param {Object} object The object to compare.
* @param {Object} other The other object to compare.
* @param {string} tag The `toStringTag` of the objects to compare.
* @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
*/
function equalByTag(object, other, tag) {
switch (tag) {
case boolTag:
case dateTag:
// Coerce dates and booleans to numbers, dates to milliseconds and booleans
// to `1` or `0` treating invalid dates coerced to `NaN` as not equal.
return +object == +other;
case errorTag:
return object.name == other.name && object.message == other.message;
case numberTag:
// Treat `NaN` vs. `NaN` as equal.
return (object != +object)
? other != +other
: object == +other;
case regexpTag:
case stringTag:
// Coerce regexes to strings and treat strings primitives and string
// objects as equal. See https://es5.github.io/#x15.10.6.4 for more details.
return object == (other + '');
}
return false;
}
module.exports = equalByTag;
/***/ },
/* 61 */
/***/ function(module, exports, __webpack_require__) {
var keys = __webpack_require__(19);
/** Used for native method references. */
var objectProto = Object.prototype;
/** Used to check objects for own properties. */
var hasOwnProperty = objectProto.hasOwnProperty;
/**
* A specialized version of `baseIsEqualDeep` for objects with support for
* partial deep comparisons.
*
* @private
* @param {Object} object The object to compare.
* @param {Object} other The other object to compare.
* @param {Function} equalFunc The function to determine equivalents of values.
* @param {Function} [customizer] The function to customize comparing values.
* @param {boolean} [isLoose] Specify performing partial comparisons.
* @param {Array} [stackA] Tracks traversed `value` objects.
* @param {Array} [stackB] Tracks traversed `other` objects.
* @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
*/
function equalObjects(object, other, equalFunc, customizer, isLoose, stackA, stackB) {
var objProps = keys(object),
objLength = objProps.length,
othProps = keys(other),
othLength = othProps.length;
if (objLength != othLength && !isLoose) {
return false;
}
var index = objLength;
while (index--) {
var key = objProps[index];
if (!(isLoose ? key in other : hasOwnProperty.call(other, key))) {
return false;
}
}
var skipCtor = isLoose;
while (++index < objLength) {
key = objProps[index];
var objValue = object[key],
othValue = other[key],
result = customizer ? customizer(isLoose ? othValue : objValue, isLoose? objValue : othValue, key) : undefined;
// Recursively compare objects (susceptible to call stack limits).
if (!(result === undefined ? equalFunc(objValue, othValue, customizer, isLoose, stackA, stackB) : result)) {
return false;
}
skipCtor || (skipCtor = key == 'constructor');
}
if (!skipCtor) {
var objCtor = object.constructor,
othCtor = other.constructor;
// Non `Object` object instances with different constructors are not equal.
if (objCtor != othCtor &&
('constructor' in object && 'constructor' in other) &&
!(typeof objCtor == 'function' && objCtor instanceof objCtor &&
typeof othCtor == 'function' && othCtor instanceof othCtor)) {
return false;
}
}
return true;
}
module.exports = equalObjects;
/***/ },
/* 62 */
/***/ function(module, exports, __webpack_require__) {
var isLength = __webpack_require__(27),
isObjectLike = __webpack_require__(23);
/** `Object#toString` result references. */
var argsTag = '[object Arguments]',
arrayTag = '[object Array]',
boolTag = '[object Boolean]',
dateTag = '[object Date]',
errorTag = '[object Error]',
funcTag = '[object Function]',
mapTag = '[object Map]',
numberTag = '[object Number]',
objectTag = '[object Object]',
regexpTag = '[object RegExp]',
setTag = '[object Set]',
stringTag = '[object String]',
weakMapTag = '[object WeakMap]';
var arrayBufferTag = '[object ArrayBuffer]',
float32Tag = '[object Float32Array]',
float64Tag = '[object Float64Array]',
int8Tag = '[object Int8Array]',
int16Tag = '[object Int16Array]',
int32Tag = '[object Int32Array]',
uint8Tag = '[object Uint8Array]',
uint8ClampedTag = '[object Uint8ClampedArray]',
uint16Tag = '[object Uint16Array]',
uint32Tag = '[object Uint32Array]';
/** Used to identify `toStringTag` values of typed arrays. */
var typedArrayTags = {};
typedArrayTags[float32Tag] = typedArrayTags[float64Tag] =
typedArrayTags[int8Tag] = typedArrayTags[int16Tag] =
typedArrayTags[int32Tag] = typedArrayTags[uint8Tag] =
typedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] =
typedArrayTags[uint32Tag] = true;
typedArrayTags[argsTag] = typedArrayTags[arrayTag] =
typedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] =
typedArrayTags[dateTag] = typedArrayTags[errorTag] =
typedArrayTags[funcTag] = typedArrayTags[mapTag] =
typedArrayTags[numberTag] = typedArrayTags[objectTag] =
typedArrayTags[regexpTag] = typedArrayTags[setTag] =
typedArrayTags[stringTag] = typedArrayTags[weakMapTag] = false;
/** Used for native method references. */
var objectProto = Object.prototype;
/**
* Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
* of values.
*/
var objToString = objectProto.toString;
/**
* Checks if `value` is classified as a typed array.
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
* @example
*
* _.isTypedArray(new Uint8Array);
* // => true
*
* _.isTypedArray([]);
* // => false
*/
function isTypedArray(value) {
return isObjectLike(value) && isLength(value.length) && !!typedArrayTags[objToString.call(value)];
}
module.exports = isTypedArray;
/***/ },
/* 63 */
/***/ function(module, exports, __webpack_require__) {
var isStrictComparable = __webpack_require__(64),
pairs = __webpack_require__(65);
/**
* Gets the propery names, values, and compare flags of `object`.
*
* @private
* @param {Object} object The object to query.
* @returns {Array} Returns the match data of `object`.
*/
function getMatchData(object) {
var result = pairs(object),
length = result.length;
while (length--) {
result[length][2] = isStrictComparable(result[length][1]);
}
return result;
}
module.exports = getMatchData;
/***/ },
/* 64 */
/***/ function(module, exports, __webpack_require__) {
var isObject = __webpack_require__(18);
/**
* Checks if `value` is suitable for strict equality comparisons, i.e. `===`.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` if suitable for strict
* equality comparisons, else `false`.
*/
function isStrictComparable(value) {
return value === value && !isObject(value);
}
module.exports = isStrictComparable;
/***/ },
/* 65 */
/***/ function(module, exports, __webpack_require__) {
var keys = __webpack_require__(19),
toObject = __webpack_require__(17);
/**
* Creates a two dimensional array of the key-value pairs for `object`,
* e.g. `[[key1, value1], [key2, value2]]`.
*
* @static
* @memberOf _
* @category Object
* @param {Object} object The object to query.
* @returns {Array} Returns the new array of key-value pairs.
* @example
*
* _.pairs({ 'barney': 36, 'fred': 40 });
* // => [['barney', 36], ['fred', 40]] (iteration order is not guaranteed)
*/
function pairs(object) {
object = toObject(object);
var index = -1,
props = keys(object),
length = props.length,
result = Array(length);
while (++index < length) {
var key = props[index];
result[index] = [key, object[key]];
}
return result;
}
module.exports = pairs;
/***/ },
/* 66 */
/***/ function(module, exports, __webpack_require__) {
var baseGet = __webpack_require__(67),
baseIsEqual = __webpack_require__(56),
baseSlice = __webpack_require__(68),
isArray = __webpack_require__(30),
isKey = __webpack_require__(69),
isStrictComparable = __webpack_require__(64),
last = __webpack_require__(70),
toObject = __webpack_require__(17),
toPath = __webpack_require__(71);
/**
* The base implementation of `_.matchesProperty` which does not clone `srcValue`.
*
* @private
* @param {string} path The path of the property to get.
* @param {*} srcValue The value to compare.
* @returns {Function} Returns the new function.
*/
function baseMatchesProperty(path, srcValue) {
var isArr = isArray(path),
isCommon = isKey(path) && isStrictComparable(srcValue),
pathKey = (path + '');
path = toPath(path);
return function(object) {
if (object == null) {
return false;
}
var key = pathKey;
object = toObject(object);
if ((isArr || !isCommon) && !(key in object)) {
object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
if (object == null) {
return false;
}
key = last(path);
object = toObject(object);
}
return object[key] === srcValue
? (srcValue !== undefined || (key in object))
: baseIsEqual(srcValue, object[key], undefined, true);
};
}
module.exports = baseMatchesProperty;
/***/ },
/* 67 */
/***/ function(module, exports, __webpack_require__) {
var toObject = __webpack_require__(17);
/**
* The base implementation of `get` without support for string paths
* and default values.
*
* @private
* @param {Object} object The object to query.
* @param {Array} path The path of the property to get.
* @param {string} [pathKey] The key representation of path.
* @returns {*} Returns the resolved value.
*/
function baseGet(object, path, pathKey) {
if (object == null) {
return;
}
if (pathKey !== undefined && pathKey in toObject(object)) {
path = [pathKey];
}
var index = 0,
length = path.length;
while (object != null && index < length) {
object = object[path[index++]];
}
return (index && index == length) ? object : undefined;
}
module.exports = baseGet;
/***/ },
/* 68 */
/***/ function(module, exports) {
/**
* The base implementation of `_.slice` without an iteratee call guard.
*
* @private
* @param {Array} array The array to slice.
* @param {number} [start=0] The start position.
* @param {number} [end=array.length] The end position.
* @returns {Array} Returns the slice of `array`.
*/
function baseSlice(array, start, end) {
var index = -1,
length = array.length;
start = start == null ? 0 : (+start || 0);
if (start < 0) {
start = -start > length ? 0 : (length + start);
}
end = (end === undefined || end > length) ? length : (+end || 0);
if (end < 0) {
end += length;
}
length = start > end ? 0 : ((end - start) >>> 0);
start >>>= 0;
var result = Array(length);
while (++index < length) {
result[index] = array[index + start];
}
return result;
}
module.exports = baseSlice;
/***/ },
/* 69 */
/***/ function(module, exports, __webpack_require__) {
var isArray = __webpack_require__(30),
toObject = __webpack_require__(17);
/** Used to match property names within property paths. */
var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\n\\]|\\.)*?\1)\]/,
reIsPlainProp = /^\w*$/;
/**
* Checks if `value` is a property name and not a property path.
*
* @private
* @param {*} value The value to check.
* @param {Object} [object] The object to query keys on.
* @returns {boolean} Returns `true` if `value` is a property name, else `false`.
*/
function isKey(value, object) {
var type = typeof value;
if ((type == 'string' && reIsPlainProp.test(value)) || type == 'number') {
return true;
}
if (isArray(value)) {
return false;
}
var result = !reIsDeepProp.test(value);
return result || (object != null && value in toObject(object));
}
module.exports = isKey;
/***/ },
/* 70 */
/***/ function(module, exports) {
/**
* Gets the last element of `array`.
*
* @static
* @memberOf _
* @category Array
* @param {Array} array The array to query.
* @returns {*} Returns the last element of `array`.
* @example
*
* _.last([1, 2, 3]);
* // => 3
*/
function last(array) {
var length = array ? array.length : 0;
return length ? array[length - 1] : undefined;
}
module.exports = last;
/***/ },
/* 71 */
/***/ function(module, exports, __webpack_require__) {
var baseToString = __webpack_require__(72),
isArray = __webpack_require__(30);
/** Used to match property names within property paths. */
var rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\n\\]|\\.)*?)\2)\]/g;
/** Used to match backslashes in property paths. */
var reEscapeChar = /\\(\\)?/g;
/**
* Converts `value` to property path array if it's not one.
*
* @private
* @param {*} value The value to process.
* @returns {Array} Returns the property path array.
*/
function toPath(value) {
if (isArray(value)) {
return value;
}
var result = [];
baseToString(value).replace(rePropName, function(match, number, quote, string) {
result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
});
return result;
}
module.exports = toPath;
/***/ },
/* 72 */
/***/ function(module, exports) {
/**
* Converts `value` to a string if it's not one. An empty string is returned
* for `null` or `undefined` values.
*
* @private
* @param {*} value The value to process.
* @returns {string} Returns the string.
*/
function baseToString(value) {
return value == null ? '' : (value + '');
}
module.exports = baseToString;
/***/ },
/* 73 */
/***/ function(module, exports, __webpack_require__) {
var baseProperty = __webpack_require__(26),
basePropertyDeep = __webpack_require__(74),
isKey = __webpack_require__(69);
/**
* Creates a function that returns the property value at `path` on a
* given object.
*
* @static
* @memberOf _
* @category Utility
* @param {Array|string} path The path of the property to get.
* @returns {Function} Returns the new function.
* @example
*
* var objects = [
* { 'a': { 'b': { 'c': 2 } } },
* { 'a': { 'b': { 'c': 1 } } }
* ];
*
* _.map(objects, _.property('a.b.c'));
* // => [2, 1]
*
* _.pluck(_.sortBy(objects, _.property(['a', 'b', 'c'])), 'a.b.c');
* // => [1, 2]
*/
function property(path) {
return isKey(path) ? baseProperty(path) : basePropertyDeep(path);
}
module.exports = property;
/***/ },
/* 74 */
/***/ function(module, exports, __webpack_require__) {
var baseGet = __webpack_require__(67),
toPath = __webpack_require__(71);
/**
* A specialized version of `baseProperty` which supports deep paths.
*
* @private
* @param {Array|string} path The path of the property to get.
* @returns {Function} Returns the new function.
*/
function basePropertyDeep(path) {
var pathKey = (path + '');
path = toPath(path);
return function(object) {
return baseGet(object, path, pathKey);
};
}
module.exports = basePropertyDeep;
/***/ },
/* 75 */
/***/ function(module, exports, __webpack_require__) {
var baseEach = __webpack_require__(13),
isArrayLike = __webpack_require__(24);
/**
* The base implementation of `_.map` without support for callback shorthands
* and `this` binding.
*
* @private
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @returns {Array} Returns the new mapped array.
*/
function baseMap(collection, iteratee) {
var index = -1,
result = isArrayLike(collection) ? Array(collection.length) : [];
baseEach(collection, function(value, key, collection) {
result[++index] = iteratee(value, key, collection);
});
return result;
}
module.exports = baseMap;
/***/ },
/* 76 */
/***/ function(module, exports, __webpack_require__) {
var baseMerge = __webpack_require__(77),
createAssigner = __webpack_require__(82);
/**
* Recursively merges own enumerable properties of the source object(s), that
* don't resolve to `undefined` into the destination object. Subsequent sources
* overwrite property assignments of previous sources. If `customizer` is
* provided it's invoked to produce the merged values of the destination and
* source properties. If `customizer` returns `undefined` merging is handled
* by the method instead. The `customizer` is bound to `thisArg` and invoked
* with five arguments: (objectValue, sourceValue, key, object, source).
*
* @static
* @memberOf _
* @category Object
* @param {Object} object The destination object.
* @param {...Object} [sources] The source objects.
* @param {Function} [customizer] The function to customize assigned values.
* @param {*} [thisArg] The `this` binding of `customizer`.
* @returns {Object} Returns `object`.
* @example
*
* var users = {
* 'data': [{ 'user': 'barney' }, { 'user': 'fred' }]
* };
*
* var ages = {
* 'data': [{ 'age': 36 }, { 'age': 40 }]
* };
*
* _.merge(users, ages);
* // => { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] }
*
* // using a customizer callback
* var object = {
* 'fruits': ['apple'],
* 'vegetables': ['beet']
* };
*
* var other = {
* 'fruits': ['banana'],
* 'vegetables': ['carrot']
* };
*
* _.merge(object, other, function(a, b) {
* if (_.isArray(a)) {
* return a.concat(b);
* }
* });
* // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] }
*/
var merge = createAssigner(baseMerge);
module.exports = merge;
/***/ },
/* 77 */
/***/ function(module, exports, __webpack_require__) {
var arrayEach = __webpack_require__(12),
baseMergeDeep = __webpack_require__(78),
isArray = __webpack_require__(30),
isArrayLike = __webpack_require__(24),
isObject = __webpack_require__(18),
isObjectLike = __webpack_require__(23),
isTypedArray = __webpack_require__(62),
keys = __webpack_require__(19);
/**
* The base implementation of `_.merge` without support for argument juggling,
* multiple sources, and `this` binding `customizer` functions.
*
* @private
* @param {Object} object The destination object.
* @param {Object} source The source object.
* @param {Function} [customizer] The function to customize merged values.
* @param {Array} [stackA=[]] Tracks traversed source objects.
* @param {Array} [stackB=[]] Associates values with source counterparts.
* @returns {Object} Returns `object`.
*/
function baseMerge(object, source, customizer, stackA, stackB) {
if (!isObject(object)) {
return object;
}
var isSrcArr = isArrayLike(source) && (isArray(source) || isTypedArray(source)),
props = isSrcArr ? undefined : keys(source);
arrayEach(props || source, function(srcValue, key) {
if (props) {
key = srcValue;
srcValue = source[key];
}
if (isObjectLike(srcValue)) {
stackA || (stackA = []);
stackB || (stackB = []);
baseMergeDeep(object, source, key, baseMerge, customizer, stackA, stackB);
}
else {
var value = object[key],
result = customizer ? customizer(value, srcValue, key, object, source) : undefined,
isCommon = result === undefined;
if (isCommon) {
result = srcValue;
}
if ((result !== undefined || (isSrcArr && !(key in object))) &&
(isCommon || (result === result ? (result !== value) : (value === value)))) {
object[key] = result;
}
}
});
return object;
}
module.exports = baseMerge;
/***/ },
/* 78 */
/***/ function(module, exports, __webpack_require__) {
var arrayCopy = __webpack_require__(43),
isArguments = __webpack_require__(29),
isArray = __webpack_require__(30),
isArrayLike = __webpack_require__(24),
isPlainObject = __webpack_require__(79),
isTypedArray = __webpack_require__(62),
toPlainObject = __webpack_require__(81);
/**
* A specialized version of `baseMerge` for arrays and objects which performs
* deep merges and tracks traversed objects enabling objects with circular
* references to be merged.
*
* @private
* @param {Object} object The destination object.
* @param {Object} source The source object.
* @param {string} key The key of the value to merge.
* @param {Function} mergeFunc The function to merge values.
* @param {Function} [customizer] The function to customize merged values.
* @param {Array} [stackA=[]] Tracks traversed source objects.
* @param {Array} [stackB=[]] Associates values with source counterparts.
* @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
*/
function baseMergeDeep(object, source, key, mergeFunc, customizer, stackA, stackB) {
var length = stackA.length,
srcValue = source[key];
while (length--) {
if (stackA[length] == srcValue) {
object[key] = stackB[length];
return;
}
}
var value = object[key],
result = customizer ? customizer(value, srcValue, key, object, source) : undefined,
isCommon = result === undefined;
if (isCommon) {
result = srcValue;
if (isArrayLike(srcValue) && (isArray(srcValue) || isTypedArray(srcValue))) {
result = isArray(value)
? value
: (isArrayLike(value) ? arrayCopy(value) : []);
}
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
result = isArguments(value)
? toPlainObject(value)
: (isPlainObject(value) ? value : {});
}
else {
isCommon = false;
}
}
// Add the source value to the stack of traversed objects and associate
// it with its merged value.
stackA.push(srcValue);
stackB.push(result);
if (isCommon) {
// Recursively merge objects and arrays (susceptible to call stack limits).
object[key] = mergeFunc(result, srcValue, customizer, stackA, stackB);
} else if (result === result ? (result !== value) : (value === value)) {
object[key] = result;
}
}
module.exports = baseMergeDeep;
/***/ },
/* 79 */
/***/ function(module, exports, __webpack_require__) {
var baseForIn = __webpack_require__(80),
isArguments = __webpack_require__(29),
isObjectLike = __webpack_require__(23);
/** `Object#toString` result references. */
var objectTag = '[object Object]';
/** Used for native method references. */
var objectProto = Object.prototype;
/** Used to check objects for own properties. */
var hasOwnProperty = objectProto.hasOwnProperty;
/**
* Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
* of values.
*/
var objToString = objectProto.toString;
/**
* Checks if `value` is a plain object, that is, an object created by the
* `Object` constructor or one with a `[[Prototype]]` of `null`.
*
* **Note:** This method assumes objects created by the `Object` constructor
* have no inherited enumerable properties.
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
* @example
*
* function Foo() {
* this.a = 1;
* }
*
* _.isPlainObject(new Foo);
* // => false
*
* _.isPlainObject([1, 2, 3]);
* // => false
*
* _.isPlainObject({ 'x': 0, 'y': 0 });
* // => true
*
* _.isPlainObject(Object.create(null));
* // => true
*/
function isPlainObject(value) {
var Ctor;
// Exit early for non `Object` objects.
if (!(isObjectLike(value) && objToString.call(value) == objectTag && !isArguments(value)) ||
(!hasOwnProperty.call(value, 'constructor') && (Ctor = value.constructor, typeof Ctor == 'function' && !(Ctor instanceof Ctor)))) {
return false;
}
// IE < 9 iterates inherited properties before own properties. If the first
// iterated property is an object's own property then there are no inherited
// enumerable properties.
var result;
// In most environments an object's own properties are iterated before
// its inherited properties. If the last iterated property is an object's
// own property then there are no inherited enumerable properties.
baseForIn(value, function(subValue, key) {
result = key;
});
return result === undefined || hasOwnProperty.call(value, result);
}
module.exports = isPlainObject;
/***/ },
/* 80 */
/***/ function(module, exports, __webpack_require__) {
var baseFor = __webpack_require__(15),
keysIn = __webpack_require__(32);
/**
* The base implementation of `_.forIn` without support for callback
* shorthands and `this` binding.
*
* @private
* @param {Object} object The object to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @returns {Object} Returns `object`.
*/
function baseForIn(object, iteratee) {
return baseFor(object, iteratee, keysIn);
}
module.exports = baseForIn;
/***/ },
/* 81 */
/***/ function(module, exports, __webpack_require__) {
var baseCopy = __webpack_require__(45),
keysIn = __webpack_require__(32);
/**
* Converts `value` to a plain object flattening inherited enumerable
* properties of `value` to own properties of the plain object.
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to convert.
* @returns {Object} Returns the converted plain object.
* @example
*
* function Foo() {
* this.b = 2;
* }
*
* Foo.prototype.c = 3;
*
* _.assign({ 'a': 1 }, new Foo);
* // => { 'a': 1, 'b': 2 }
*
* _.assign({ 'a': 1 }, _.toPlainObject(new Foo));
* // => { 'a': 1, 'b': 2, 'c': 3 }
*/
function toPlainObject(value) {
return baseCopy(value, keysIn(value));
}
module.exports = toPlainObject;
/***/ },
/* 82 */
/***/ function(module, exports, __webpack_require__) {
var bindCallback = __webpack_require__(35),
isIterateeCall = __webpack_require__(50),
restParam = __webpack_require__(83);
/**
* Creates a `_.assign`, `_.defaults`, or `_.merge` function.
*
* @private
* @param {Function} assigner The function to assign values.
* @returns {Function} Returns the new assigner function.
*/
function createAssigner(assigner) {
return restParam(function(object, sources) {
var index = -1,
length = object == null ? 0 : sources.length,
customizer = length > 2 ? sources[length - 2] : undefined,
guard = length > 2 ? sources[2] : undefined,
thisArg = length > 1 ? sources[length - 1] : undefined;
if (typeof customizer == 'function') {
customizer = bindCallback(customizer, thisArg, 5);
length -= 2;
} else {
customizer = typeof thisArg == 'function' ? thisArg : undefined;
length -= (customizer ? 1 : 0);
}
if (guard && isIterateeCall(sources[0], sources[1], guard)) {
customizer = length < 3 ? undefined : customizer;
length = 1;
}
while (++index < length) {
var source = sources[index];
if (source) {
assigner(object, source, customizer);
}
}
return object;
});
}
module.exports = createAssigner;
/***/ },
/* 83 */
/***/ function(module, exports) {
/** Used as the `TypeError` message for "Functions" methods. */
var FUNC_ERROR_TEXT = 'Expected a function';
/* Native method references for those with the same name as other `lodash` methods. */
var nativeMax = Math.max;
/**
* Creates a function that invokes `func` with the `this` binding of the
* created function and arguments from `start` and beyond provided as an array.
*
* **Note:** This method is based on the [rest parameter](https://developer.mozilla.org/Web/JavaScript/Reference/Functions/rest_parameters).
*
* @static
* @memberOf _
* @category Function
* @param {Function} func The function to apply a rest parameter to.
* @param {number} [start=func.length-1] The start position of the rest parameter.
* @returns {Function} Returns the new function.
* @example
*
* var say = _.restParam(function(what, names) {
* return what + ' ' + _.initial(names).join(', ') +
* (_.size(names) > 1 ? ', & ' : '') + _.last(names);
* });
*
* say('hello', 'fred', 'barney', 'pebbles');
* // => 'hello fred, barney, & pebbles'
*/
function restParam(func, start) {
if (typeof func != 'function') {
throw new TypeError(FUNC_ERROR_TEXT);
}
start = nativeMax(start === undefined ? (func.length - 1) : (+start || 0), 0);
return function() {
var args = arguments,
index = -1,
length = nativeMax(args.length - start, 0),
rest = Array(length);
while (++index < length) {
rest[index] = args[start + index];
}
switch (start) {
case 0: return func.call(this, rest);
case 1: return func.call(this, args[0], rest);
case 2: return func.call(this, args[0], args[1], rest);
}
var otherArgs = Array(start + 1);
index = -1;
while (++index < start) {
otherArgs[index] = args[index];
}
otherArgs[start] = rest;
return func.apply(this, otherArgs);
};
}
module.exports = restParam;
/***/ },
/* 84 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
// This is the object returned by the `index.browseAll()` method
module.exports = IndexBrowser;
var inherits = __webpack_require__(3);
var EventEmitter = __webpack_require__(85).EventEmitter;
function IndexBrowser() {
}
inherits(IndexBrowser, EventEmitter);
IndexBrowser.prototype.stop = function() {
this._stopped = true;
this._clean();
};
IndexBrowser.prototype._end = function() {
this.emit('end');
this._clean();
};
IndexBrowser.prototype._error = function(err) {
this.emit('error', err);
this._clean();
};
IndexBrowser.prototype._result = function(content) {
this.emit('result', content);
};
IndexBrowser.prototype._clean = function() {
this.removeAllListeners('stop');
this.removeAllListeners('end');
this.removeAllListeners('error');
this.removeAllListeners('result');
};
/***/ },
/* 85 */
/***/ function(module, exports) {
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
function EventEmitter() {
this._events = this._events || {};
this._maxListeners = this._maxListeners || undefined;
}
module.exports = EventEmitter;
// Backwards-compat with node 0.10.x
EventEmitter.EventEmitter = EventEmitter;
EventEmitter.prototype._events = undefined;
EventEmitter.prototype._maxListeners = undefined;
// By default EventEmitters will print a warning if more than 10 listeners are
// added to it. This is a useful default which helps finding memory leaks.
EventEmitter.defaultMaxListeners = 10;
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.
EventEmitter.prototype.setMaxListeners = function(n) {
if (!isNumber(n) || n < 0 || isNaN(n))
throw TypeError('n must be a positive number');
this._maxListeners = n;
return this;
};
EventEmitter.prototype.emit = function(type) {
var er, handler, len, args, i, listeners;
if (!this._events)
this._events = {};
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events.error ||
(isObject(this._events.error) && !this._events.error.length)) {
er = arguments[1];
if (er instanceof Error) {
throw er; // Unhandled 'error' event
}
throw TypeError('Uncaught, unspecified "error" event.');
}
}
handler = this._events[type];
if (isUndefined(handler))
return false;
if (isFunction(handler)) {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
args = Array.prototype.slice.call(arguments, 1);
handler.apply(this, args);
}
} else if (isObject(handler)) {
args = Array.prototype.slice.call(arguments, 1);
listeners = handler.slice();
len = listeners.length;
for (i = 0; i < len; i++)
listeners[i].apply(this, args);
}
return true;
};
EventEmitter.prototype.addListener = function(type, listener) {
var m;
if (!isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events)
this._events = {};
// To avoid recursion in the case that type === "newListener"! Before
// adding it to the listeners, first emit "newListener".
if (this._events.newListener)
this.emit('newListener', type,
isFunction(listener.listener) ?
listener.listener : listener);
if (!this._events[type])
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
else if (isObject(this._events[type]))
// If we've already got an array, just append.
this._events[type].push(listener);
else
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
// Check for listener leak
if (isObject(this._events[type]) && !this._events[type].warned) {
if (!isUndefined(this._maxListeners)) {
m = this._maxListeners;
} else {
m = EventEmitter.defaultMaxListeners;
}
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +
'leak detected. %d listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
this._events[type].length);
if (typeof console.trace === 'function') {
// not supported in IE 10
console.trace();
}
}
}
return this;
};
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
EventEmitter.prototype.once = function(type, listener) {
if (!isFunction(listener))
throw TypeError('listener must be a function');
var fired = false;
function g() {
this.removeListener(type, g);
if (!fired) {
fired = true;
listener.apply(this, arguments);
}
}
g.listener = listener;
this.on(type, g);
return this;
};
// emits a 'removeListener' event iff the listener was removed
EventEmitter.prototype.removeListener = function(type, listener) {
var list, position, length, i;
if (!isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events || !this._events[type])
return this;
list = this._events[type];
length = list.length;
position = -1;
if (list === listener ||
(isFunction(list.listener) && list.listener === listener)) {
delete this._events[type];
if (this._events.removeListener)
this.emit('removeListener', type, listener);
} else if (isObject(list)) {
for (i = length; i-- > 0;) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener)) {
position = i;
break;
}
}
if (position < 0)
return this;
if (list.length === 1) {
list.length = 0;
delete this._events[type];
} else {
list.splice(position, 1);
}
if (this._events.removeListener)
this.emit('removeListener', type, listener);
}
return this;
};
EventEmitter.prototype.removeAllListeners = function(type) {
var key, listeners;
if (!this._events)
return this;
// not listening for removeListener, no need to emit
if (!this._events.removeListener) {
if (arguments.length === 0)
this._events = {};
else if (this._events[type])
delete this._events[type];
return this;
}
// emit removeListener for all listeners on all events
if (arguments.length === 0) {
for (key in this._events) {
if (key === 'removeListener') continue;
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events = {};
return this;
}
listeners = this._events[type];
if (isFunction(listeners)) {
this.removeListener(type, listeners);
} else if (listeners) {
// LIFO order
while (listeners.length)
this.removeListener(type, listeners[listeners.length - 1]);
}
delete this._events[type];
return this;
};
EventEmitter.prototype.listeners = function(type) {
var ret;
if (!this._events || !this._events[type])
ret = [];
else if (isFunction(this._events[type]))
ret = [this._events[type]];
else
ret = this._events[type].slice();
return ret;
};
EventEmitter.prototype.listenerCount = function(type) {
if (this._events) {
var evlistener = this._events[type];
if (isFunction(evlistener))
return 1;
else if (evlistener)
return evlistener.length;
}
return 0;
};
EventEmitter.listenerCount = function(emitter, type) {
return emitter.listenerCount(type);
};
function isFunction(arg) {
return typeof arg === 'function';
}
function isNumber(arg) {
return typeof arg === 'number';
}
function isObject(arg) {
return typeof arg === 'object' && arg !== null;
}
function isUndefined(arg) {
return arg === void 0;
}
/***/ },
/* 86 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
module.exports = inlineHeaders;
var querystring = __webpack_require__(87);
function inlineHeaders(url, headers) {
if (/\?/.test(url)) {
url += '&';
} else {
url += '?';
}
return url + querystring.encode(headers);
}
/***/ },
/* 87 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
exports.decode = exports.parse = __webpack_require__(88);
exports.encode = exports.stringify = __webpack_require__(89);
/***/ },
/* 88 */
/***/ function(module, exports) {
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
// If obj.hasOwnProperty has been overridden, then calling
// obj.hasOwnProperty(prop) will break.
// See: https://github.com/joyent/node/issues/1707
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
module.exports = function(qs, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';
var obj = {};
if (typeof qs !== 'string' || qs.length === 0) {
return obj;
}
var regexp = /\+/g;
qs = qs.split(sep);
var maxKeys = 1000;
if (options && typeof options.maxKeys === 'number') {
maxKeys = options.maxKeys;
}
var len = qs.length;
// maxKeys <= 0 means that we should not limit keys count
if (maxKeys > 0 && len > maxKeys) {
len = maxKeys;
}
for (var i = 0; i < len; ++i) {
var x = qs[i].replace(regexp, '%20'),
idx = x.indexOf(eq),
kstr, vstr, k, v;
if (idx >= 0) {
kstr = x.substr(0, idx);
vstr = x.substr(idx + 1);
} else {
kstr = x;
vstr = '';
}
k = decodeURIComponent(kstr);
v = decodeURIComponent(vstr);
if (!hasOwnProperty(obj, k)) {
obj[k] = v;
} else if (Array.isArray(obj[k])) {
obj[k].push(v);
} else {
obj[k] = [obj[k], v];
}
}
return obj;
};
/***/ },
/* 89 */
/***/ function(module, exports) {
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
var stringifyPrimitive = function(v) {
switch (typeof v) {
case 'string':
return v;
case 'boolean':
return v ? 'true' : 'false';
case 'number':
return isFinite(v) ? v : '';
default:
return '';
}
};
module.exports = function(obj, sep, eq, name) {
sep = sep || '&';
eq = eq || '=';
if (obj === null) {
obj = undefined;
}
if (typeof obj === 'object') {
return Object.keys(obj).map(function(k) {
var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
if (Array.isArray(obj[k])) {
return obj[k].map(function(v) {
return ks + encodeURIComponent(stringifyPrimitive(v));
}).join(sep);
} else {
return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
}
}).join(sep);
}
if (!name) return '';
return encodeURIComponent(stringifyPrimitive(name)) + eq +
encodeURIComponent(stringifyPrimitive(obj));
};
/***/ },
/* 90 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
module.exports = jsonpRequest;
var errors = __webpack_require__(10);
var JSONPCounter = 0;
function jsonpRequest(url, opts, cb) {
if (opts.method !== 'GET') {
cb(new Error('Method ' + opts.method + ' ' + url + ' is not supported by JSONP.'));
return;
}
opts.debug('JSONP: start');
var cbCalled = false;
var timedOut = false;
JSONPCounter += 1;
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
var cbName = 'algoliaJSONP_' + JSONPCounter;
var done = false;
window[cbName] = function(data) {
try {
delete window[cbName];
} catch (e) {
window[cbName] = undefined;
}
if (timedOut) {
return;
}
cbCalled = true;
clean();
cb(null, {
body: data/* ,
// We do not send the statusCode, there's no statusCode in JSONP, it will be
// computed using data.status && data.message like with XDR
statusCode*/
});
};
// add callback by hand
url += '&callback=' + cbName;
// add body params manually
if (opts.jsonBody && opts.jsonBody.params) {
url += '&' + opts.jsonBody.params;
}
var ontimeout = setTimeout(timeout, opts.timeout);
// script onreadystatechange needed only for
// <= IE8
// https://github.com/angular/angular.js/issues/4523
script.onreadystatechange = readystatechange;
script.onload = success;
script.onerror = error;
script.async = true;
script.defer = true;
script.src = url;
head.appendChild(script);
function success() {
opts.debug('JSONP: success');
if (done || timedOut) {
return;
}
done = true;
// script loaded but did not call the fn => script loading error
if (!cbCalled) {
opts.debug('JSONP: Fail. Script loaded but did not call the callback');
clean();
cb(new errors.JSONPScriptFail());
}
}
function readystatechange() {
if (this.readyState === 'loaded' || this.readyState === 'complete') {
success();
}
}
function clean() {
clearTimeout(ontimeout);
script.onload = null;
script.onreadystatechange = null;
script.onerror = null;
head.removeChild(script);
try {
delete window[cbName];
delete window[cbName + '_loaded'];
} catch (e) {
window[cbName] = null;
window[cbName + '_loaded'] = null;
}
}
function timeout() {
opts.debug('JSONP: Script timeout');
timedOut = true;
clean();
cb(new errors.RequestTimeout());
}
function error() {
opts.debug('JSONP: Script error');
if (done || timedOut) {
return;
}
clean();
cb(new errors.JSONPScriptError());
}
}
/***/ },
/* 91 */
/***/ function(module, exports, __webpack_require__) {
module.exports = createPlacesClient;
var buildSearchMethod = __webpack_require__(37);
function createPlacesClient(algoliasearch) {
return function places(appID, apiKey, opts) {
var cloneDeep = __webpack_require__(92);
opts = opts && cloneDeep(opts) || {};
opts.hosts = opts.hosts || [
'places-dsn.algolia.net',
'places-1.algolianet.com',
'places-2.algolianet.com',
'places-3.algolianet.com'
];
var client = algoliasearch(appID, apiKey, opts);
var index = client.initIndex('places');
index.search = buildSearchMethod('query', '/1/places/query');
return index;
};
}
/***/ },
/* 92 */
/***/ function(module, exports, __webpack_require__) {
var baseClone = __webpack_require__(42),
bindCallback = __webpack_require__(35);
/**
* Creates a deep clone of `value`. If `customizer` is provided it's invoked
* to produce the cloned values. If `customizer` returns `undefined` cloning
* is handled by the method instead. The `customizer` is bound to `thisArg`
* and invoked with up to three argument; (value [, index|key, object]).
*
* **Note:** This method is loosely based on the
* [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm).
* The enumerable properties of `arguments` objects and objects created by
* constructors other than `Object` are cloned to plain `Object` objects. An
* empty object is returned for uncloneable values such as functions, DOM nodes,
* Maps, Sets, and WeakMaps.
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to deep clone.
* @param {Function} [customizer] The function to customize cloning values.
* @param {*} [thisArg] The `this` binding of `customizer`.
* @returns {*} Returns the deep cloned value.
* @example
*
* var users = [
* { 'user': 'barney' },
* { 'user': 'fred' }
* ];
*
* var deep = _.cloneDeep(users);
* deep[0] === users[0];
* // => false
*
* // using a customizer callback
* var el = _.cloneDeep(document.body, function(value) {
* if (_.isElement(value)) {
* return value.cloneNode(true);
* }
* });
*
* el === document.body
* // => false
* el.nodeName
* // => BODY
* el.childNodes.length;
* // => 20
*/
function cloneDeep(value, customizer, thisArg) {
return typeof customizer == 'function'
? baseClone(value, true, bindCallback(customizer, thisArg, 3))
: baseClone(value, true);
}
module.exports = cloneDeep;
/***/ },
/* 93 */
/***/ function(module, exports) {
'use strict';
module.exports = getDocumentProtocol;
function getDocumentProtocol() {
var protocol = window.document.location.protocol;
// when in `file:` mode (local html file), default to `http:`
if (protocol !== 'http:' && protocol !== 'https:') {
protocol = 'http:';
}
return protocol;
}
/***/ },
/* 94 */
/***/ function(module, exports) {
'use strict';
module.exports = '3.13.1';
/***/ },
/* 95 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
module.exports = __webpack_require__(96);
/***/ },
/* 96 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var current$ = window.$;
__webpack_require__(97);
var zepto = window.$;
window.$ = current$;
// setup DOM element
var DOM = __webpack_require__(98);
DOM.element = zepto;
// setup utils functions
var _ = __webpack_require__(99);
_.isArray = zepto.isArray;
_.isFunction = zepto.isFunction;
_.isObject = zepto.isPlainObject;
_.bind = zepto.proxy;
_.each = function(collection, cb) {
// stupid argument order for jQuery.each
zepto.each(collection, reverseArgs);
function reverseArgs(index, value) {
return cb(value, index);
}
};
_.map = zepto.map;
_.mixin = zepto.extend;
var typeaheadKey = 'aaAutocomplete';
var Typeahead = __webpack_require__(100);
var EventBus = __webpack_require__(101);
function autocomplete(selector, options, datasets, typeaheadObject) {
datasets = _.isArray(datasets) ? datasets : [].slice.call(arguments, 2);
var inputs = zepto(selector).each(function(i, input) {
var $input = zepto(input);
var eventBus = new EventBus({el: $input});
var typeahead = typeaheadObject || new Typeahead({
input: $input,
eventBus: eventBus,
dropdownMenuContainer: options.dropdownMenuContainer,
hint: options.hint === undefined ? true : !!options.hint,
minLength: options.minLength,
autoselect: options.autoselect,
openOnFocus: options.openOnFocus,
templates: options.templates,
debug: options.debug,
datasets: datasets
});
$input.data(typeaheadKey, typeahead);
});
// expose all methods in the `autocomplete` attribute
inputs.autocomplete = {};
_.each(['open', 'close', 'getVal', 'setVal', 'destroy'], function(method) {
inputs.autocomplete[method] = function() {
var methodArguments = arguments;
inputs.each(function(j, input) {
var typeahead = zepto(input).data(typeaheadKey);
typeahead[method].apply(typeahead, methodArguments);
});
};
});
return inputs;
}
autocomplete.sources = Typeahead.sources;
module.exports = autocomplete;
/***/ },
/* 97 */
/***/ function(module, exports) {
/*! Zepto 1.1.6 (generated with Zepto Builder) - zepto event assets data ie - zeptojs.com/license */
// Zepto.js
// (c) 2010-2016 Thomas Fuchs
// Zepto.js may be freely distributed under the MIT license.
var Zepto = (function() {
var undefined, key, $, classList, emptyArray = [], concat = emptyArray.concat, filter = emptyArray.filter, slice = emptyArray.slice,
document = window.document,
elementDisplay = {}, classCache = {},
cssNumber = { 'column-count': 1, 'columns': 1, 'font-weight': 1, 'line-height': 1,'opacity': 1, 'z-index': 1, 'zoom': 1 },
fragmentRE = /^\s*<(\w+|!)[^>]*>/,
singleTagRE = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,
tagExpanderRE = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig,
rootNodeRE = /^(?:body|html)$/i,
capitalRE = /([A-Z])/g,
// special attributes that should be get/set via method calls
methodAttributes = ['val', 'css', 'html', 'text', 'data', 'width', 'height', 'offset'],
adjacencyOperators = [ 'after', 'prepend', 'before', 'append' ],
table = document.createElement('table'),
tableRow = document.createElement('tr'),
containers = {
'tr': document.createElement('tbody'),
'tbody': table, 'thead': table, 'tfoot': table,
'td': tableRow, 'th': tableRow,
'*': document.createElement('div')
},
readyRE = /complete|loaded|interactive/,
simpleSelectorRE = /^[\w-]*$/,
class2type = {},
toString = class2type.toString,
zepto = {},
camelize, uniq,
tempParent = document.createElement('div'),
propMap = {
'tabindex': 'tabIndex',
'readonly': 'readOnly',
'for': 'htmlFor',
'class': 'className',
'maxlength': 'maxLength',
'cellspacing': 'cellSpacing',
'cellpadding': 'cellPadding',
'rowspan': 'rowSpan',
'colspan': 'colSpan',
'usemap': 'useMap',
'frameborder': 'frameBorder',
'contenteditable': 'contentEditable'
},
isArray = Array.isArray ||
function(object){ return object instanceof Array }
zepto.matches = function(element, selector) {
if (!selector || !element || element.nodeType !== 1) return false
var matchesSelector = element.webkitMatchesSelector || element.mozMatchesSelector ||
element.oMatchesSelector || element.matchesSelector
if (matchesSelector) return matchesSelector.call(element, selector)
// fall back to performing a selector:
var match, parent = element.parentNode, temp = !parent
if (temp) (parent = tempParent).appendChild(element)
match = ~zepto.qsa(parent, selector).indexOf(element)
temp && tempParent.removeChild(element)
return match
}
function type(obj) {
return obj == null ? String(obj) :
class2type[toString.call(obj)] || "object"
}
function isFunction(value) { return type(value) == "function" }
function isWindow(obj) { return obj != null && obj == obj.window }
function isDocument(obj) { return obj != null && obj.nodeType == obj.DOCUMENT_NODE }
function isObject(obj) { return type(obj) == "object" }
function isPlainObject(obj) {
return isObject(obj) && !isWindow(obj) && Object.getPrototypeOf(obj) == Object.prototype
}
function likeArray(obj) { return typeof obj.length == 'number' }
function compact(array) { return filter.call(array, function(item){ return item != null }) }
function flatten(array) { return array.length > 0 ? $.fn.concat.apply([], array) : array }
camelize = function(str){ return str.replace(/-+(.)?/g, function(match, chr){ return chr ? chr.toUpperCase() : '' }) }
function dasherize(str) {
return str.replace(/::/g, '/')
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
.replace(/([a-z\d])([A-Z])/g, '$1_$2')
.replace(/_/g, '-')
.toLowerCase()
}
uniq = function(array){ return filter.call(array, function(item, idx){ return array.indexOf(item) == idx }) }
function classRE(name) {
return name in classCache ?
classCache[name] : (classCache[name] = new RegExp('(^|\\s)' + name + '(\\s|$)'))
}
function maybeAddPx(name, value) {
return (typeof value == "number" && !cssNumber[dasherize(name)]) ? value + "px" : value
}
function defaultDisplay(nodeName) {
var element, display
if (!elementDisplay[nodeName]) {
element = document.createElement(nodeName)
document.body.appendChild(element)
display = getComputedStyle(element, '').getPropertyValue("display")
element.parentNode.removeChild(element)
display == "none" && (display = "block")
elementDisplay[nodeName] = display
}
return elementDisplay[nodeName]
}
function children(element) {
return 'children' in element ?
slice.call(element.children) :
$.map(element.childNodes, function(node){ if (node.nodeType == 1) return node })
}
function Z(dom, selector) {
var i, len = dom ? dom.length : 0
for (i = 0; i < len; i++) this[i] = dom[i]
this.length = len
this.selector = selector || ''
}
// `$.zepto.fragment` takes a html string and an optional tag name
// to generate DOM nodes from the given html string.
// The generated DOM nodes are returned as an array.
// This function can be overridden in plugins for example to make
// it compatible with browsers that don't support the DOM fully.
zepto.fragment = function(html, name, properties) {
var dom, nodes, container
// A special case optimization for a single tag
if (singleTagRE.test(html)) dom = $(document.createElement(RegExp.$1))
if (!dom) {
if (html.replace) html = html.replace(tagExpanderRE, "<$1></$2>")
if (name === undefined) name = fragmentRE.test(html) && RegExp.$1
if (!(name in containers)) name = '*'
container = containers[name]
container.innerHTML = '' + html
dom = $.each(slice.call(container.childNodes), function(){
container.removeChild(this)
})
}
if (isPlainObject(properties)) {
nodes = $(dom)
$.each(properties, function(key, value) {
if (methodAttributes.indexOf(key) > -1) nodes[key](value)
else nodes.attr(key, value)
})
}
return dom
}
// `$.zepto.Z` swaps out the prototype of the given `dom` array
// of nodes with `$.fn` and thus supplying all the Zepto functions
// to the array. This method can be overridden in plugins.
zepto.Z = function(dom, selector) {
return new Z(dom, selector)
}
// `$.zepto.isZ` should return `true` if the given object is a Zepto
// collection. This method can be overridden in plugins.
zepto.isZ = function(object) {
return object instanceof zepto.Z
}
// `$.zepto.init` is Zepto's counterpart to jQuery's `$.fn.init` and
// takes a CSS selector and an optional context (and handles various
// special cases).
// This method can be overridden in plugins.
zepto.init = function(selector, context) {
var dom
// If nothing given, return an empty Zepto collection
if (!selector) return zepto.Z()
// Optimize for string selectors
else if (typeof selector == 'string') {
selector = selector.trim()
// If it's a html fragment, create nodes from it
// Note: In both Chrome 21 and Firefox 15, DOM error 12
// is thrown if the fragment doesn't begin with <
if (selector[0] == '<' && fragmentRE.test(selector))
dom = zepto.fragment(selector, RegExp.$1, context), selector = null
// If there's a context, create a collection on that context first, and select
// nodes from there
else if (context !== undefined) return $(context).find(selector)
// If it's a CSS selector, use it to select nodes.
else dom = zepto.qsa(document, selector)
}
// If a function is given, call it when the DOM is ready
else if (isFunction(selector)) return $(document).ready(selector)
// If a Zepto collection is given, just return it
else if (zepto.isZ(selector)) return selector
else {
// normalize array if an array of nodes is given
if (isArray(selector)) dom = compact(selector)
// Wrap DOM nodes.
else if (isObject(selector))
dom = [selector], selector = null
// If it's a html fragment, create nodes from it
else if (fragmentRE.test(selector))
dom = zepto.fragment(selector.trim(), RegExp.$1, context), selector = null
// If there's a context, create a collection on that context first, and select
// nodes from there
else if (context !== undefined) return $(context).find(selector)
// And last but no least, if it's a CSS selector, use it to select nodes.
else dom = zepto.qsa(document, selector)
}
// create a new Zepto collection from the nodes found
return zepto.Z(dom, selector)
}
// `$` will be the base `Zepto` object. When calling this
// function just call `$.zepto.init, which makes the implementation
// details of selecting nodes and creating Zepto collections
// patchable in plugins.
$ = function(selector, context){
return zepto.init(selector, context)
}
function extend(target, source, deep) {
for (key in source)
if (deep && (isPlainObject(source[key]) || isArray(source[key]))) {
if (isPlainObject(source[key]) && !isPlainObject(target[key]))
target[key] = {}
if (isArray(source[key]) && !isArray(target[key]))
target[key] = []
extend(target[key], source[key], deep)
}
else if (source[key] !== undefined) target[key] = source[key]
}
// Copy all but undefined properties from one or more
// objects to the `target` object.
$.extend = function(target){
var deep, args = slice.call(arguments, 1)
if (typeof target == 'boolean') {
deep = target
target = args.shift()
}
args.forEach(function(arg){ extend(target, arg, deep) })
return target
}
// `$.zepto.qsa` is Zepto's CSS selector implementation which
// uses `document.querySelectorAll` and optimizes for some special cases, like `#id`.
// This method can be overridden in plugins.
zepto.qsa = function(element, selector){
var found,
maybeID = selector[0] == '#',
maybeClass = !maybeID && selector[0] == '.',
nameOnly = maybeID || maybeClass ? selector.slice(1) : selector, // Ensure that a 1 char tag name still gets checked
isSimple = simpleSelectorRE.test(nameOnly)
return (element.getElementById && isSimple && maybeID) ? // Safari DocumentFragment doesn't have getElementById
( (found = element.getElementById(nameOnly)) ? [found] : [] ) :
(element.nodeType !== 1 && element.nodeType !== 9 && element.nodeType !== 11) ? [] :
slice.call(
isSimple && !maybeID && element.getElementsByClassName ? // DocumentFragment doesn't have getElementsByClassName/TagName
maybeClass ? element.getElementsByClassName(nameOnly) : // If it's simple, it could be a class
element.getElementsByTagName(selector) : // Or a tag
element.querySelectorAll(selector) // Or it's not simple, and we need to query all
)
}
function filtered(nodes, selector) {
return selector == null ? $(nodes) : $(nodes).filter(selector)
}
$.contains = document.documentElement.contains ?
function(parent, node) {
return parent !== node && parent.contains(node)
} :
function(parent, node) {
while (node && (node = node.parentNode))
if (node === parent) return true
return false
}
function funcArg(context, arg, idx, payload) {
return isFunction(arg) ? arg.call(context, idx, payload) : arg
}
function setAttribute(node, name, value) {
value == null ? node.removeAttribute(name) : node.setAttribute(name, value)
}
// access className property while respecting SVGAnimatedString
function className(node, value){
var klass = node.className || '',
svg = klass && klass.baseVal !== undefined
if (value === undefined) return svg ? klass.baseVal : klass
svg ? (klass.baseVal = value) : (node.className = value)
}
// "true" => true
// "false" => false
// "null" => null
// "42" => 42
// "42.5" => 42.5
// "08" => "08"
// JSON => parse if valid
// String => self
function deserializeValue(value) {
try {
return value ?
value == "true" ||
( value == "false" ? false :
value == "null" ? null :
+value + "" == value ? +value :
/^[\[\{]/.test(value) ? $.parseJSON(value) :
value )
: value
} catch(e) {
return value
}
}
$.type = type
$.isFunction = isFunction
$.isWindow = isWindow
$.isArray = isArray
$.isPlainObject = isPlainObject
$.isEmptyObject = function(obj) {
var name
for (name in obj) return false
return true
}
$.inArray = function(elem, array, i){
return emptyArray.indexOf.call(array, elem, i)
}
$.camelCase = camelize
$.trim = function(str) {
return str == null ? "" : String.prototype.trim.call(str)
}
// plugin compatibility
$.uuid = 0
$.support = { }
$.expr = { }
$.noop = function() {}
$.map = function(elements, callback){
var value, values = [], i, key
if (likeArray(elements))
for (i = 0; i < elements.length; i++) {
value = callback(elements[i], i)
if (value != null) values.push(value)
}
else
for (key in elements) {
value = callback(elements[key], key)
if (value != null) values.push(value)
}
return flatten(values)
}
$.each = function(elements, callback){
var i, key
if (likeArray(elements)) {
for (i = 0; i < elements.length; i++)
if (callback.call(elements[i], i, elements[i]) === false) return elements
} else {
for (key in elements)
if (callback.call(elements[key], key, elements[key]) === false) return elements
}
return elements
}
$.grep = function(elements, callback){
return filter.call(elements, callback)
}
if (window.JSON) $.parseJSON = JSON.parse
// Populate the class2type map
$.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) {
class2type[ "[object " + name + "]" ] = name.toLowerCase()
})
// Define methods that will be available on all
// Zepto collections
$.fn = {
constructor: zepto.Z,
length: 0,
// Because a collection acts like an array
// copy over these useful array functions.
forEach: emptyArray.forEach,
reduce: emptyArray.reduce,
push: emptyArray.push,
sort: emptyArray.sort,
splice: emptyArray.splice,
indexOf: emptyArray.indexOf,
concat: function(){
var i, value, args = []
for (i = 0; i < arguments.length; i++) {
value = arguments[i]
args[i] = zepto.isZ(value) ? value.toArray() : value
}
return concat.apply(zepto.isZ(this) ? this.toArray() : this, args)
},
// `map` and `slice` in the jQuery API work differently
// from their array counterparts
map: function(fn){
return $($.map(this, function(el, i){ return fn.call(el, i, el) }))
},
slice: function(){
return $(slice.apply(this, arguments))
},
ready: function(callback){
// need to check if document.body exists for IE as that browser reports
// document ready when it hasn't yet created the body element
if (readyRE.test(document.readyState) && document.body) callback($)
else document.addEventListener('DOMContentLoaded', function(){ callback($) }, false)
return this
},
get: function(idx){
return idx === undefined ? slice.call(this) : this[idx >= 0 ? idx : idx + this.length]
},
toArray: function(){ return this.get() },
size: function(){
return this.length
},
remove: function(){
return this.each(function(){
if (this.parentNode != null)
this.parentNode.removeChild(this)
})
},
each: function(callback){
emptyArray.every.call(this, function(el, idx){
return callback.call(el, idx, el) !== false
})
return this
},
filter: function(selector){
if (isFunction(selector)) return this.not(this.not(selector))
return $(filter.call(this, function(element){
return zepto.matches(element, selector)
}))
},
add: function(selector,context){
return $(uniq(this.concat($(selector,context))))
},
is: function(selector){
return this.length > 0 && zepto.matches(this[0], selector)
},
not: function(selector){
var nodes=[]
if (isFunction(selector) && selector.call !== undefined)
this.each(function(idx){
if (!selector.call(this,idx)) nodes.push(this)
})
else {
var excludes = typeof selector == 'string' ? this.filter(selector) :
(likeArray(selector) && isFunction(selector.item)) ? slice.call(selector) : $(selector)
this.forEach(function(el){
if (excludes.indexOf(el) < 0) nodes.push(el)
})
}
return $(nodes)
},
has: function(selector){
return this.filter(function(){
return isObject(selector) ?
$.contains(this, selector) :
$(this).find(selector).size()
})
},
eq: function(idx){
return idx === -1 ? this.slice(idx) : this.slice(idx, + idx + 1)
},
first: function(){
var el = this[0]
return el && !isObject(el) ? el : $(el)
},
last: function(){
var el = this[this.length - 1]
return el && !isObject(el) ? el : $(el)
},
find: function(selector){
var result, $this = this
if (!selector) result = $()
else if (typeof selector == 'object')
result = $(selector).filter(function(){
var node = this
return emptyArray.some.call($this, function(parent){
return $.contains(parent, node)
})
})
else if (this.length == 1) result = $(zepto.qsa(this[0], selector))
else result = this.map(function(){ return zepto.qsa(this, selector) })
return result
},
closest: function(selector, context){
var node = this[0], collection = false
if (typeof selector == 'object') collection = $(selector)
while (node && !(collection ? collection.indexOf(node) >= 0 : zepto.matches(node, selector)))
node = node !== context && !isDocument(node) && node.parentNode
return $(node)
},
parents: function(selector){
var ancestors = [], nodes = this
while (nodes.length > 0)
nodes = $.map(nodes, function(node){
if ((node = node.parentNode) && !isDocument(node) && ancestors.indexOf(node) < 0) {
ancestors.push(node)
return node
}
})
return filtered(ancestors, selector)
},
parent: function(selector){
return filtered(uniq(this.pluck('parentNode')), selector)
},
children: function(selector){
return filtered(this.map(function(){ return children(this) }), selector)
},
contents: function() {
return this.map(function() { return this.contentDocument || slice.call(this.childNodes) })
},
siblings: function(selector){
return filtered(this.map(function(i, el){
return filter.call(children(el.parentNode), function(child){ return child!==el })
}), selector)
},
empty: function(){
return this.each(function(){ this.innerHTML = '' })
},
// `pluck` is borrowed from Prototype.js
pluck: function(property){
return $.map(this, function(el){ return el[property] })
},
show: function(){
return this.each(function(){
this.style.display == "none" && (this.style.display = '')
if (getComputedStyle(this, '').getPropertyValue("display") == "none")
this.style.display = defaultDisplay(this.nodeName)
})
},
replaceWith: function(newContent){
return this.before(newContent).remove()
},
wrap: function(structure){
var func = isFunction(structure)
if (this[0] && !func)
var dom = $(structure).get(0),
clone = dom.parentNode || this.length > 1
return this.each(function(index){
$(this).wrapAll(
func ? structure.call(this, index) :
clone ? dom.cloneNode(true) : dom
)
})
},
wrapAll: function(structure){
if (this[0]) {
$(this[0]).before(structure = $(structure))
var children
// drill down to the inmost element
while ((children = structure.children()).length) structure = children.first()
$(structure).append(this)
}
return this
},
wrapInner: function(structure){
var func = isFunction(structure)
return this.each(function(index){
var self = $(this), contents = self.contents(),
dom = func ? structure.call(this, index) : structure
contents.length ? contents.wrapAll(dom) : self.append(dom)
})
},
unwrap: function(){
this.parent().each(function(){
$(this).replaceWith($(this).children())
})
return this
},
clone: function(){
return this.map(function(){ return this.cloneNode(true) })
},
hide: function(){
return this.css("display", "none")
},
toggle: function(setting){
return this.each(function(){
var el = $(this)
;(setting === undefined ? el.css("display") == "none" : setting) ? el.show() : el.hide()
})
},
prev: function(selector){ return $(this.pluck('previousElementSibling')).filter(selector || '*') },
next: function(selector){ return $(this.pluck('nextElementSibling')).filter(selector || '*') },
html: function(html){
return 0 in arguments ?
this.each(function(idx){
var originHtml = this.innerHTML
$(this).empty().append( funcArg(this, html, idx, originHtml) )
}) :
(0 in this ? this[0].innerHTML : null)
},
text: function(text){
return 0 in arguments ?
this.each(function(idx){
var newText = funcArg(this, text, idx, this.textContent)
this.textContent = newText == null ? '' : ''+newText
}) :
(0 in this ? this.pluck('textContent').join("") : null)
},
attr: function(name, value){
var result
return (typeof name == 'string' && !(1 in arguments)) ?
(!this.length || this[0].nodeType !== 1 ? undefined :
(!(result = this[0].getAttribute(name)) && name in this[0]) ? this[0][name] : result
) :
this.each(function(idx){
if (this.nodeType !== 1) return
if (isObject(name)) for (key in name) setAttribute(this, key, name[key])
else setAttribute(this, name, funcArg(this, value, idx, this.getAttribute(name)))
})
},
removeAttr: function(name){
return this.each(function(){ this.nodeType === 1 && name.split(' ').forEach(function(attribute){
setAttribute(this, attribute)
}, this)})
},
prop: function(name, value){
name = propMap[name] || name
return (1 in arguments) ?
this.each(function(idx){
this[name] = funcArg(this, value, idx, this[name])
}) :
(this[0] && this[0][name])
},
data: function(name, value){
var attrName = 'data-' + name.replace(capitalRE, '-$1').toLowerCase()
var data = (1 in arguments) ?
this.attr(attrName, value) :
this.attr(attrName)
return data !== null ? deserializeValue(data) : undefined
},
val: function(value){
return 0 in arguments ?
this.each(function(idx){
this.value = funcArg(this, value, idx, this.value)
}) :
(this[0] && (this[0].multiple ?
$(this[0]).find('option').filter(function(){ return this.selected }).pluck('value') :
this[0].value)
)
},
offset: function(coordinates){
if (coordinates) return this.each(function(index){
var $this = $(this),
coords = funcArg(this, coordinates, index, $this.offset()),
parentOffset = $this.offsetParent().offset(),
props = {
top: coords.top - parentOffset.top,
left: coords.left - parentOffset.left
}
if ($this.css('position') == 'static') props['position'] = 'relative'
$this.css(props)
})
if (!this.length) return null
if (!$.contains(document.documentElement, this[0]))
return {top: 0, left: 0}
var obj = this[0].getBoundingClientRect()
return {
left: obj.left + window.pageXOffset,
top: obj.top + window.pageYOffset,
width: Math.round(obj.width),
height: Math.round(obj.height)
}
},
css: function(property, value){
if (arguments.length < 2) {
var computedStyle, element = this[0]
if(!element) return
computedStyle = getComputedStyle(element, '')
if (typeof property == 'string')
return element.style[camelize(property)] || computedStyle.getPropertyValue(property)
else if (isArray(property)) {
var props = {}
$.each(property, function(_, prop){
props[prop] = (element.style[camelize(prop)] || computedStyle.getPropertyValue(prop))
})
return props
}
}
var css = ''
if (type(property) == 'string') {
if (!value && value !== 0)
this.each(function(){ this.style.removeProperty(dasherize(property)) })
else
css = dasherize(property) + ":" + maybeAddPx(property, value)
} else {
for (key in property)
if (!property[key] && property[key] !== 0)
this.each(function(){ this.style.removeProperty(dasherize(key)) })
else
css += dasherize(key) + ':' + maybeAddPx(key, property[key]) + ';'
}
return this.each(function(){ this.style.cssText += ';' + css })
},
index: function(element){
return element ? this.indexOf($(element)[0]) : this.parent().children().indexOf(this[0])
},
hasClass: function(name){
if (!name) return false
return emptyArray.some.call(this, function(el){
return this.test(className(el))
}, classRE(name))
},
addClass: function(name){
if (!name) return this
return this.each(function(idx){
if (!('className' in this)) return
classList = []
var cls = className(this), newName = funcArg(this, name, idx, cls)
newName.split(/\s+/g).forEach(function(klass){
if (!$(this).hasClass(klass)) classList.push(klass)
}, this)
classList.length && className(this, cls + (cls ? " " : "") + classList.join(" "))
})
},
removeClass: function(name){
return this.each(function(idx){
if (!('className' in this)) return
if (name === undefined) return className(this, '')
classList = className(this)
funcArg(this, name, idx, classList).split(/\s+/g).forEach(function(klass){
classList = classList.replace(classRE(klass), " ")
})
className(this, classList.trim())
})
},
toggleClass: function(name, when){
if (!name) return this
return this.each(function(idx){
var $this = $(this), names = funcArg(this, name, idx, className(this))
names.split(/\s+/g).forEach(function(klass){
(when === undefined ? !$this.hasClass(klass) : when) ?
$this.addClass(klass) : $this.removeClass(klass)
})
})
},
scrollTop: function(value){
if (!this.length) return
var hasScrollTop = 'scrollTop' in this[0]
if (value === undefined) return hasScrollTop ? this[0].scrollTop : this[0].pageYOffset
return this.each(hasScrollTop ?
function(){ this.scrollTop = value } :
function(){ this.scrollTo(this.scrollX, value) })
},
scrollLeft: function(value){
if (!this.length) return
var hasScrollLeft = 'scrollLeft' in this[0]
if (value === undefined) return hasScrollLeft ? this[0].scrollLeft : this[0].pageXOffset
return this.each(hasScrollLeft ?
function(){ this.scrollLeft = value } :
function(){ this.scrollTo(value, this.scrollY) })
},
position: function() {
if (!this.length) return
var elem = this[0],
// Get *real* offsetParent
offsetParent = this.offsetParent(),
// Get correct offsets
offset = this.offset(),
parentOffset = rootNodeRE.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset()
// Subtract element margins
// note: when an element has margin: auto the offsetLeft and marginLeft
// are the same in Safari causing offset.left to incorrectly be 0
offset.top -= parseFloat( $(elem).css('margin-top') ) || 0
offset.left -= parseFloat( $(elem).css('margin-left') ) || 0
// Add offsetParent borders
parentOffset.top += parseFloat( $(offsetParent[0]).css('border-top-width') ) || 0
parentOffset.left += parseFloat( $(offsetParent[0]).css('border-left-width') ) || 0
// Subtract the two offsets
return {
top: offset.top - parentOffset.top,
left: offset.left - parentOffset.left
}
},
offsetParent: function() {
return this.map(function(){
var parent = this.offsetParent || document.body
while (parent && !rootNodeRE.test(parent.nodeName) && $(parent).css("position") == "static")
parent = parent.offsetParent
return parent
})
}
}
// for now
$.fn.detach = $.fn.remove
// Generate the `width` and `height` functions
;['width', 'height'].forEach(function(dimension){
var dimensionProperty =
dimension.replace(/./, function(m){ return m[0].toUpperCase() })
$.fn[dimension] = function(value){
var offset, el = this[0]
if (value === undefined) return isWindow(el) ? el['inner' + dimensionProperty] :
isDocument(el) ? el.documentElement['scroll' + dimensionProperty] :
(offset = this.offset()) && offset[dimension]
else return this.each(function(idx){
el = $(this)
el.css(dimension, funcArg(this, value, idx, el[dimension]()))
})
}
})
function traverseNode(node, fun) {
fun(node)
for (var i = 0, len = node.childNodes.length; i < len; i++)
traverseNode(node.childNodes[i], fun)
}
// Generate the `after`, `prepend`, `before`, `append`,
// `insertAfter`, `insertBefore`, `appendTo`, and `prependTo` methods.
adjacencyOperators.forEach(function(operator, operatorIndex) {
var inside = operatorIndex % 2 //=> prepend, append
$.fn[operator] = function(){
// arguments can be nodes, arrays of nodes, Zepto objects and HTML strings
var argType, nodes = $.map(arguments, function(arg) {
argType = type(arg)
return argType == "object" || argType == "array" || arg == null ?
arg : zepto.fragment(arg)
}),
parent, copyByClone = this.length > 1
if (nodes.length < 1) return this
return this.each(function(_, target){
parent = inside ? target : target.parentNode
// convert all methods to a "before" operation
target = operatorIndex == 0 ? target.nextSibling :
operatorIndex == 1 ? target.firstChild :
operatorIndex == 2 ? target :
null
var parentInDocument = $.contains(document.documentElement, parent)
nodes.forEach(function(node){
if (copyByClone) node = node.cloneNode(true)
else if (!parent) return $(node).remove()
parent.insertBefore(node, target)
if (parentInDocument) traverseNode(node, function(el){
if (el.nodeName != null && el.nodeName.toUpperCase() === 'SCRIPT' &&
(!el.type || el.type === 'text/javascript') && !el.src)
window['eval'].call(window, el.innerHTML)
})
})
})
}
// after => insertAfter
// prepend => prependTo
// before => insertBefore
// append => appendTo
$.fn[inside ? operator+'To' : 'insert'+(operatorIndex ? 'Before' : 'After')] = function(html){
$(html)[operator](this)
return this
}
})
zepto.Z.prototype = Z.prototype = $.fn
// Export internal API functions in the `$.zepto` namespace
zepto.uniq = uniq
zepto.deserializeValue = deserializeValue
$.zepto = zepto
return $
})()
// If `$` is not yet defined, point it to `Zepto`
window.Zepto = Zepto
window.$ === undefined && (window.$ = Zepto)
// Zepto.js
// (c) 2010-2016 Thomas Fuchs
// Zepto.js may be freely distributed under the MIT license.
;(function($){
var cache = [], timeout
$.fn.remove = function(){
return this.each(function(){
if(this.parentNode){
if(this.tagName === 'IMG'){
cache.push(this)
this.src = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
if (timeout) clearTimeout(timeout)
timeout = setTimeout(function(){ cache = [] }, 60000)
}
this.parentNode.removeChild(this)
}
})
}
})(Zepto)
// Zepto.js
// (c) 2010-2016 Thomas Fuchs
// Zepto.js may be freely distributed under the MIT license.
// The following code is heavily inspired by jQuery's $.fn.data()
;(function($){
var data = {}, dataAttr = $.fn.data, camelize = $.camelCase,
exp = $.expando = 'Zepto' + (+new Date()), emptyArray = []
// Get value from node:
// 1. first try key as given,
// 2. then try camelized key,
// 3. fall back to reading "data-*" attribute.
function getData(node, name) {
var id = node[exp], store = id && data[id]
if (name === undefined) return store || setData(node)
else {
if (store) {
if (name in store) return store[name]
var camelName = camelize(name)
if (camelName in store) return store[camelName]
}
return dataAttr.call($(node), name)
}
}
// Store value under camelized key on node
function setData(node, name, value) {
var id = node[exp] || (node[exp] = ++$.uuid),
store = data[id] || (data[id] = attributeData(node))
if (name !== undefined) store[camelize(name)] = value
return store
}
// Read all "data-*" attributes from a node
function attributeData(node) {
var store = {}
$.each(node.attributes || emptyArray, function(i, attr){
if (attr.name.indexOf('data-') == 0)
store[camelize(attr.name.replace('data-', ''))] =
$.zepto.deserializeValue(attr.value)
})
return store
}
$.fn.data = function(name, value) {
return value === undefined ?
// set multiple values via object
$.isPlainObject(name) ?
this.each(function(i, node){
$.each(name, function(key, value){ setData(node, key, value) })
}) :
// get value from first element
(0 in this ? getData(this[0], name) : undefined) :
// set value on all elements
this.each(function(){ setData(this, name, value) })
}
$.fn.removeData = function(names) {
if (typeof names == 'string') names = names.split(/\s+/)
return this.each(function(){
var id = this[exp], store = id && data[id]
if (store) $.each(names || store, function(key){
delete store[names ? camelize(this) : key]
})
})
}
// Generate extended `remove` and `empty` functions
;['remove', 'empty'].forEach(function(methodName){
var origFn = $.fn[methodName]
$.fn[methodName] = function() {
var elements = this.find('*')
if (methodName === 'remove') elements = elements.add(this)
elements.removeData()
return origFn.call(this)
}
})
})(Zepto)
// Zepto.js
// (c) 2010-2016 Thomas Fuchs
// Zepto.js may be freely distributed under the MIT license.
;(function($){
var _zid = 1, undefined,
slice = Array.prototype.slice,
isFunction = $.isFunction,
isString = function(obj){ return typeof obj == 'string' },
handlers = {},
specialEvents={},
focusinSupported = 'onfocusin' in window,
focus = { focus: 'focusin', blur: 'focusout' },
hover = { mouseenter: 'mouseover', mouseleave: 'mouseout' }
specialEvents.click = specialEvents.mousedown = specialEvents.mouseup = specialEvents.mousemove = 'MouseEvents'
function zid(element) {
return element._zid || (element._zid = _zid++)
}
function findHandlers(element, event, fn, selector) {
event = parse(event)
if (event.ns) var matcher = matcherFor(event.ns)
return (handlers[zid(element)] || []).filter(function(handler) {
return handler
&& (!event.e || handler.e == event.e)
&& (!event.ns || matcher.test(handler.ns))
&& (!fn || zid(handler.fn) === zid(fn))
&& (!selector || handler.sel == selector)
})
}
function parse(event) {
var parts = ('' + event).split('.')
return {e: parts[0], ns: parts.slice(1).sort().join(' ')}
}
function matcherFor(ns) {
return new RegExp('(?:^| )' + ns.replace(' ', ' .* ?') + '(?: |$)')
}
function eventCapture(handler, captureSetting) {
return handler.del &&
(!focusinSupported && (handler.e in focus)) ||
!!captureSetting
}
function realEvent(type) {
return hover[type] || (focusinSupported && focus[type]) || type
}
function add(element, events, fn, data, selector, delegator, capture){
var id = zid(element), set = (handlers[id] || (handlers[id] = []))
events.split(/\s/).forEach(function(event){
if (event == 'ready') return $(document).ready(fn)
var handler = parse(event)
handler.fn = fn
handler.sel = selector
// emulate mouseenter, mouseleave
if (handler.e in hover) fn = function(e){
var related = e.relatedTarget
if (!related || (related !== this && !$.contains(this, related)))
return handler.fn.apply(this, arguments)
}
handler.del = delegator
var callback = delegator || fn
handler.proxy = function(e){
e = compatible(e)
if (e.isImmediatePropagationStopped()) return
e.data = data
var result = callback.apply(element, e._args == undefined ? [e] : [e].concat(e._args))
if (result === false) e.preventDefault(), e.stopPropagation()
return result
}
handler.i = set.length
set.push(handler)
if ('addEventListener' in element)
element.addEventListener(realEvent(handler.e), handler.proxy, eventCapture(handler, capture))
})
}
function remove(element, events, fn, selector, capture){
var id = zid(element)
;(events || '').split(/\s/).forEach(function(event){
findHandlers(element, event, fn, selector).forEach(function(handler){
delete handlers[id][handler.i]
if ('removeEventListener' in element)
element.removeEventListener(realEvent(handler.e), handler.proxy, eventCapture(handler, capture))
})
})
}
$.event = { add: add, remove: remove }
$.proxy = function(fn, context) {
var args = (2 in arguments) && slice.call(arguments, 2)
if (isFunction(fn)) {
var proxyFn = function(){ return fn.apply(context, args ? args.concat(slice.call(arguments)) : arguments) }
proxyFn._zid = zid(fn)
return proxyFn
} else if (isString(context)) {
if (args) {
args.unshift(fn[context], fn)
return $.proxy.apply(null, args)
} else {
return $.proxy(fn[context], fn)
}
} else {
throw new TypeError("expected function")
}
}
$.fn.bind = function(event, data, callback){
return this.on(event, data, callback)
}
$.fn.unbind = function(event, callback){
return this.off(event, callback)
}
$.fn.one = function(event, selector, data, callback){
return this.on(event, selector, data, callback, 1)
}
var returnTrue = function(){return true},
returnFalse = function(){return false},
ignoreProperties = /^([A-Z]|returnValue$|layer[XY]$)/,
eventMethods = {
preventDefault: 'isDefaultPrevented',
stopImmediatePropagation: 'isImmediatePropagationStopped',
stopPropagation: 'isPropagationStopped'
}
function compatible(event, source) {
if (source || !event.isDefaultPrevented) {
source || (source = event)
$.each(eventMethods, function(name, predicate) {
var sourceMethod = source[name]
event[name] = function(){
this[predicate] = returnTrue
return sourceMethod && sourceMethod.apply(source, arguments)
}
event[predicate] = returnFalse
})
if (source.defaultPrevented !== undefined ? source.defaultPrevented :
'returnValue' in source ? source.returnValue === false :
source.getPreventDefault && source.getPreventDefault())
event.isDefaultPrevented = returnTrue
}
return event
}
function createProxy(event) {
var key, proxy = { originalEvent: event }
for (key in event)
if (!ignoreProperties.test(key) && event[key] !== undefined) proxy[key] = event[key]
return compatible(proxy, event)
}
$.fn.delegate = function(selector, event, callback){
return this.on(event, selector, callback)
}
$.fn.undelegate = function(selector, event, callback){
return this.off(event, selector, callback)
}
$.fn.live = function(event, callback){
$(document.body).delegate(this.selector, event, callback)
return this
}
$.fn.die = function(event, callback){
$(document.body).undelegate(this.selector, event, callback)
return this
}
$.fn.on = function(event, selector, data, callback, one){
var autoRemove, delegator, $this = this
if (event && !isString(event)) {
$.each(event, function(type, fn){
$this.on(type, selector, data, fn, one)
})
return $this
}
if (!isString(selector) && !isFunction(callback) && callback !== false)
callback = data, data = selector, selector = undefined
if (callback === undefined || data === false)
callback = data, data = undefined
if (callback === false) callback = returnFalse
return $this.each(function(_, element){
if (one) autoRemove = function(e){
remove(element, e.type, callback)
return callback.apply(this, arguments)
}
if (selector) delegator = function(e){
var evt, match = $(e.target).closest(selector, element).get(0)
if (match && match !== element) {
evt = $.extend(createProxy(e), {currentTarget: match, liveFired: element})
return (autoRemove || callback).apply(match, [evt].concat(slice.call(arguments, 1)))
}
}
add(element, event, callback, data, selector, delegator || autoRemove)
})
}
$.fn.off = function(event, selector, callback){
var $this = this
if (event && !isString(event)) {
$.each(event, function(type, fn){
$this.off(type, selector, fn)
})
return $this
}
if (!isString(selector) && !isFunction(callback) && callback !== false)
callback = selector, selector = undefined
if (callback === false) callback = returnFalse
return $this.each(function(){
remove(this, event, callback, selector)
})
}
$.fn.trigger = function(event, args){
event = (isString(event) || $.isPlainObject(event)) ? $.Event(event) : compatible(event)
event._args = args
return this.each(function(){
// handle focus(), blur() by calling them directly
if (event.type in focus && typeof this[event.type] == "function") this[event.type]()
// items in the collection might not be DOM elements
else if ('dispatchEvent' in this) this.dispatchEvent(event)
else $(this).triggerHandler(event, args)
})
}
// triggers event handlers on current element just as if an event occurred,
// doesn't trigger an actual event, doesn't bubble
$.fn.triggerHandler = function(event, args){
var e, result
this.each(function(i, element){
e = createProxy(isString(event) ? $.Event(event) : event)
e._args = args
e.target = element
$.each(findHandlers(element, event.type || event), function(i, handler){
result = handler.proxy(e)
if (e.isImmediatePropagationStopped()) return false
})
})
return result
}
// shortcut methods for `.bind(event, fn)` for each event type
;('focusin focusout focus blur load resize scroll unload click dblclick '+
'mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave '+
'change select keydown keypress keyup error').split(' ').forEach(function(event) {
$.fn[event] = function(callback) {
return (0 in arguments) ?
this.bind(event, callback) :
this.trigger(event)
}
})
$.Event = function(type, props) {
if (!isString(type)) props = type, type = props.type
var event = document.createEvent(specialEvents[type] || 'Events'), bubbles = true
if (props) for (var name in props) (name == 'bubbles') ? (bubbles = !!props[name]) : (event[name] = props[name])
event.initEvent(type, bubbles, true)
return compatible(event)
}
})(Zepto)
// Zepto.js
// (c) 2010-2016 Thomas Fuchs
// Zepto.js may be freely distributed under the MIT license.
;(function(){
// getComputedStyle shouldn't freak out when called
// without a valid element as argument
try {
getComputedStyle(undefined)
} catch(e) {
var nativeGetComputedStyle = getComputedStyle;
window.getComputedStyle = function(element){
try {
return nativeGetComputedStyle(element)
} catch(e) {
return null
}
}
}
})()
/***/ },
/* 98 */
/***/ function(module, exports) {
'use strict';
module.exports = {
element: null
};
/***/ },
/* 99 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var DOM = __webpack_require__(98);
module.exports = {
// those methods are implemented differently
// depending on which build it is, using
// $... or angular... or Zepto... or require(...)
isArray: null,
isFunction: null,
isObject: null,
bind: null,
each: null,
map: null,
mixin: null,
isMsie: function() {
// from https://github.com/ded/bowser/blob/master/bowser.js
return (/(msie|trident)/i).test(navigator.userAgent) ?
navigator.userAgent.match(/(msie |rv:)(\d+(.\d+)?)/i)[2] : false;
},
// http://stackoverflow.com/a/6969486
escapeRegExChars: function(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
},
isNumber: function(obj) { return typeof obj === 'number'; },
toStr: function toStr(s) {
return s === undefined || s === null ? '' : s + '';
},
cloneDeep: function cloneDeep(obj) {
var clone = this.mixin({}, obj);
var self = this;
this.each(clone, function(value, key) {
if (value) {
if (self.isArray(value)) {
clone[key] = [].concat(value);
} else if (self.isObject(value)) {
clone[key] = self.cloneDeep(value);
}
}
});
return clone;
},
error: function(msg) {
throw new Error(msg);
},
every: function(obj, test) {
var result = true;
if (!obj) {
return result;
}
this.each(obj, function(val, key) {
result = test.call(null, val, key, obj);
if (!result) {
return false;
}
});
return !!result;
},
getUniqueId: (function() {
var counter = 0;
return function() { return counter++; };
})(),
templatify: function templatify(obj) {
if (this.isFunction(obj)) {
return obj;
}
var $template = DOM.element(obj);
if ($template.prop('tagName') === 'SCRIPT') {
return function template() { return $template.text(); };
}
return function template() { return String(obj); };
},
defer: function(fn) { setTimeout(fn, 0); },
noop: function() {}
};
/***/ },
/* 100 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var attrsKey = 'aaAttrs';
var _ = __webpack_require__(99);
var DOM = __webpack_require__(98);
var EventBus = __webpack_require__(101);
var Input = __webpack_require__(102);
var Dropdown = __webpack_require__(105);
var html = __webpack_require__(107);
var css = __webpack_require__(108);
// constructor
// -----------
// THOUGHT: what if datasets could dynamically be added/removed?
function Typeahead(o) {
var $menu;
var $input;
var $hint;
o = o || {};
if (!o.input) {
_.error('missing input');
}
this.isActivated = false;
this.debug = !!o.debug;
this.autoselect = !!o.autoselect;
this.openOnFocus = !!o.openOnFocus;
this.minLength = _.isNumber(o.minLength) ? o.minLength : 1;
this.$node = buildDom(o);
$menu = this.$node.find('.aa-dropdown-menu');
$input = this.$node.find('.aa-input');
$hint = this.$node.find('.aa-hint');
if (o.dropdownMenuContainer) {
DOM.element(o.dropdownMenuContainer)
.css('position', 'relative') // ensure the container has a relative position
.append($menu.css('top', '0')); // override the top: 100%
}
// #705: if there's scrollable overflow, ie doesn't support
// blur cancellations when the scrollbar is clicked
//
// #351: preventDefault won't cancel blurs in ie <= 8
$input.on('blur.aa', function($e) {
var active = document.activeElement;
if (_.isMsie() && ($menu.is(active) || $menu.has(active).length > 0)) {
$e.preventDefault();
// stop immediate in order to prevent Input#_onBlur from
// getting exectued
$e.stopImmediatePropagation();
_.defer(function() { $input.focus(); });
}
});
// #351: prevents input blur due to clicks within dropdown menu
$menu.on('mousedown.aa', function($e) { $e.preventDefault(); });
this.eventBus = o.eventBus || new EventBus({el: $input});
this.dropdown = new Typeahead.Dropdown({menu: $menu, datasets: o.datasets, templates: o.templates})
.onSync('suggestionClicked', this._onSuggestionClicked, this)
.onSync('cursorMoved', this._onCursorMoved, this)
.onSync('cursorRemoved', this._onCursorRemoved, this)
.onSync('opened', this._onOpened, this)
.onSync('closed', this._onClosed, this)
.onSync('shown', this._onShown, this)
.onAsync('datasetRendered', this._onDatasetRendered, this);
this.input = new Typeahead.Input({input: $input, hint: $hint})
.onSync('focused', this._onFocused, this)
.onSync('blurred', this._onBlurred, this)
.onSync('enterKeyed', this._onEnterKeyed, this)
.onSync('tabKeyed', this._onTabKeyed, this)
.onSync('escKeyed', this._onEscKeyed, this)
.onSync('upKeyed', this._onUpKeyed, this)
.onSync('downKeyed', this._onDownKeyed, this)
.onSync('leftKeyed', this._onLeftKeyed, this)
.onSync('rightKeyed', this._onRightKeyed, this)
.onSync('queryChanged', this._onQueryChanged, this)
.onSync('whitespaceChanged', this._onWhitespaceChanged, this);
this._setLanguageDirection();
}
// instance methods
// ----------------
_.mixin(Typeahead.prototype, {
// ### private
_onSuggestionClicked: function onSuggestionClicked(type, $el) {
var datum;
if (datum = this.dropdown.getDatumForSuggestion($el)) {
this._select(datum);
}
},
_onCursorMoved: function onCursorMoved() {
var datum = this.dropdown.getDatumForCursor();
this.input.setInputValue(datum.value, true);
this.eventBus.trigger('cursorchanged', datum.raw, datum.datasetName);
},
_onCursorRemoved: function onCursorRemoved() {
this.input.resetInputValue();
this._updateHint();
},
_onDatasetRendered: function onDatasetRendered() {
this._updateHint();
this.eventBus.trigger('updated');
},
_onOpened: function onOpened() {
this._updateHint();
this.eventBus.trigger('opened');
},
_onShown: function onShown() {
this.eventBus.trigger('shown');
},
_onClosed: function onClosed() {
this.input.clearHint();
this.eventBus.trigger('closed');
},
_onFocused: function onFocused() {
this.isActivated = true;
if (this.openOnFocus) {
var query = this.input.getQuery();
if (query.length >= this.minLength) {
this.dropdown.update(query);
} else {
this.dropdown.empty();
}
this.dropdown.open();
}
},
_onBlurred: function onBlurred() {
if (!this.debug) {
this.isActivated = false;
this.dropdown.empty();
this.dropdown.close();
}
},
_onEnterKeyed: function onEnterKeyed(type, $e) {
var cursorDatum;
var topSuggestionDatum;
cursorDatum = this.dropdown.getDatumForCursor();
topSuggestionDatum = this.dropdown.getDatumForTopSuggestion();
if (cursorDatum) {
this._select(cursorDatum);
$e.preventDefault();
} else if (this.autoselect && topSuggestionDatum) {
this._select(topSuggestionDatum);
$e.preventDefault();
}
},
_onTabKeyed: function onTabKeyed(type, $e) {
var datum;
if (datum = this.dropdown.getDatumForCursor()) {
this._select(datum);
$e.preventDefault();
} else {
this._autocomplete(true);
}
},
_onEscKeyed: function onEscKeyed() {
this.dropdown.close();
this.input.resetInputValue();
},
_onUpKeyed: function onUpKeyed() {
var query = this.input.getQuery();
if (this.dropdown.isEmpty && query.length >= this.minLength) {
this.dropdown.update(query);
} else {
this.dropdown.moveCursorUp();
}
this.dropdown.open();
},
_onDownKeyed: function onDownKeyed() {
var query = this.input.getQuery();
if (this.dropdown.isEmpty && query.length >= this.minLength) {
this.dropdown.update(query);
} else {
this.dropdown.moveCursorDown();
}
this.dropdown.open();
},
_onLeftKeyed: function onLeftKeyed() {
if (this.dir === 'rtl') {
this._autocomplete();
}
},
_onRightKeyed: function onRightKeyed() {
if (this.dir === 'ltr') {
this._autocomplete();
}
},
_onQueryChanged: function onQueryChanged(e, query) {
this.input.clearHintIfInvalid();
if (query.length >= this.minLength) {
this.dropdown.update(query);
} else {
this.dropdown.empty();
}
this.dropdown.open();
this._setLanguageDirection();
},
_onWhitespaceChanged: function onWhitespaceChanged() {
this._updateHint();
this.dropdown.open();
},
_setLanguageDirection: function setLanguageDirection() {
var dir = this.input.getLanguageDirection();
if (this.dir !== dir) {
this.dir = dir;
this.$node.css('direction', dir);
this.dropdown.setLanguageDirection(dir);
}
},
_updateHint: function updateHint() {
var datum;
var val;
var query;
var escapedQuery;
var frontMatchRegEx;
var match;
datum = this.dropdown.getDatumForTopSuggestion();
if (datum && this.dropdown.isVisible() && !this.input.hasOverflow()) {
val = this.input.getInputValue();
query = Input.normalizeQuery(val);
escapedQuery = _.escapeRegExChars(query);
// match input value, then capture trailing text
frontMatchRegEx = new RegExp('^(?:' + escapedQuery + ')(.+$)', 'i');
match = frontMatchRegEx.exec(datum.value);
// clear hint if there's no trailing text
if (match) {
this.input.setHint(val + match[1]);
} else {
this.input.clearHint();
}
} else {
this.input.clearHint();
}
},
_autocomplete: function autocomplete(laxCursor) {
var hint;
var query;
var isCursorAtEnd;
var datum;
hint = this.input.getHint();
query = this.input.getQuery();
isCursorAtEnd = laxCursor || this.input.isCursorAtEnd();
if (hint && query !== hint && isCursorAtEnd) {
datum = this.dropdown.getDatumForTopSuggestion();
if (datum) {
this.input.setInputValue(datum.value);
}
this.eventBus.trigger('autocompleted', datum.raw, datum.datasetName);
}
},
_select: function select(datum) {
if (typeof datum.value !== 'undefined') {
this.input.setQuery(datum.value);
}
this.input.setInputValue(datum.value, true);
this._setLanguageDirection();
this.eventBus.trigger('selected', datum.raw, datum.datasetName);
this.dropdown.close();
// #118: allow click event to bubble up to the body before removing
// the suggestions otherwise we break event delegation
_.defer(_.bind(this.dropdown.empty, this.dropdown));
},
// ### public
open: function open() {
// if the menu is not activated yet, we need to update
// the underlying dropdown menu to trigger the search
// otherwise we're not gonna see anything
if (!this.isActivated) {
var query = this.input.getInputValue();
if (query.length >= this.minLength) {
this.dropdown.update(query);
} else {
this.dropdown.empty();
}
}
this.dropdown.open();
},
close: function close() {
this.dropdown.close();
},
setVal: function setVal(val) {
// expect val to be a string, so be safe, and coerce
val = _.toStr(val);
if (this.isActivated) {
this.input.setInputValue(val);
} else {
this.input.setQuery(val);
this.input.setInputValue(val, true);
}
this._setLanguageDirection();
},
getVal: function getVal() {
return this.input.getQuery();
},
destroy: function destroy() {
this.input.destroy();
this.dropdown.destroy();
destroyDomStructure(this.$node);
this.$node = null;
}
});
function buildDom(options) {
var $input;
var $wrapper;
var $dropdown;
var $hint;
$input = DOM.element(options.input);
$wrapper = DOM.element(html.wrapper).css(css.wrapper);
// override the display property with the table-cell value
// if the parent element is a table and the original input was a block
// -> https://github.com/algolia/autocomplete.js/issues/16
if ($input.css('display') === 'block' && $input.parent().css('display') === 'table') {
$wrapper.css('display', 'table-cell');
}
$dropdown = DOM.element(html.dropdown).css(css.dropdown);
if (options.templates && options.templates.dropdownMenu) {
$dropdown.html(_.templatify(options.templates.dropdownMenu)());
}
$hint = $input.clone().css(css.hint).css(getBackgroundStyles($input));
$hint
.val('')
.addClass('aa-hint')
.removeAttr('id name placeholder required')
.prop('readonly', true)
.attr({autocomplete: 'off', spellcheck: 'false', tabindex: -1});
if ($hint.removeData) {
$hint.removeData();
}
// store the original values of the attrs that get modified
// so modifications can be reverted on destroy
$input.data(attrsKey, {
dir: $input.attr('dir'),
autocomplete: $input.attr('autocomplete'),
spellcheck: $input.attr('spellcheck'),
style: $input.attr('style')
});
$input
.addClass('aa-input')
.attr({autocomplete: 'off', spellcheck: false})
.css(options.hint ? css.input : css.inputWithNoHint);
// ie7 does not like it when dir is set to auto
try {
if (!$input.attr('dir')) {
$input.attr('dir', 'auto');
}
} catch (e) {
// ignore
}
return $input
.wrap($wrapper)
.parent()
.prepend(options.hint ? $hint : null)
.append($dropdown);
}
function getBackgroundStyles($el) {
return {
backgroundAttachment: $el.css('background-attachment'),
backgroundClip: $el.css('background-clip'),
backgroundColor: $el.css('background-color'),
backgroundImage: $el.css('background-image'),
backgroundOrigin: $el.css('background-origin'),
backgroundPosition: $el.css('background-position'),
backgroundRepeat: $el.css('background-repeat'),
backgroundSize: $el.css('background-size')
};
}
function destroyDomStructure($node) {
var $input = $node.find('.aa-input');
// need to remove attrs that weren't previously defined and
// revert attrs that originally had a value
_.each($input.data(attrsKey), function(val, key) {
if (val === undefined) {
$input.removeAttr(key);
} else {
$input.attr(key, val);
}
});
$input
.detach()
.removeClass('aa-input')
.insertAfter($node);
if ($input.removeData) {
$input.removeData(attrsKey);
}
$node.remove();
}
Typeahead.Dropdown = Dropdown;
Typeahead.Input = Input;
Typeahead.sources = __webpack_require__(109);
module.exports = Typeahead;
/***/ },
/* 101 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var namespace = 'autocomplete:';
var _ = __webpack_require__(99);
var DOM = __webpack_require__(98);
// constructor
// -----------
function EventBus(o) {
if (!o || !o.el) {
_.error('EventBus initialized without el');
}
this.$el = DOM.element(o.el);
}
// instance methods
// ----------------
_.mixin(EventBus.prototype, {
// ### public
trigger: function(type) {
var args = [].slice.call(arguments, 1);
this.$el.trigger(namespace + type, args);
}
});
module.exports = EventBus;
/***/ },
/* 102 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var specialKeyCodeMap;
specialKeyCodeMap = {
9: 'tab',
27: 'esc',
37: 'left',
39: 'right',
13: 'enter',
38: 'up',
40: 'down'
};
var _ = __webpack_require__(99);
var DOM = __webpack_require__(98);
var EventEmitter = __webpack_require__(103);
// constructor
// -----------
function Input(o) {
var that = this;
var onBlur;
var onFocus;
var onKeydown;
var onInput;
o = o || {};
if (!o.input) {
_.error('input is missing');
}
// bound functions
onBlur = _.bind(this._onBlur, this);
onFocus = _.bind(this._onFocus, this);
onKeydown = _.bind(this._onKeydown, this);
onInput = _.bind(this._onInput, this);
this.$hint = DOM.element(o.hint);
this.$input = DOM.element(o.input)
.on('blur.aa', onBlur)
.on('focus.aa', onFocus)
.on('keydown.aa', onKeydown);
// if no hint, noop all the hint related functions
if (this.$hint.length === 0) {
this.setHint = this.getHint = this.clearHint = this.clearHintIfInvalid = _.noop;
}
// ie7 and ie8 don't support the input event
// ie9 doesn't fire the input event when characters are removed
// not sure if ie10 is compatible
if (!_.isMsie()) {
this.$input.on('input.aa', onInput);
} else {
this.$input.on('keydown.aa keypress.aa cut.aa paste.aa', function($e) {
// if a special key triggered this, ignore it
if (specialKeyCodeMap[$e.which || $e.keyCode]) {
return;
}
// give the browser a chance to update the value of the input
// before checking to see if the query changed
_.defer(_.bind(that._onInput, that, $e));
});
}
// the query defaults to whatever the value of the input is
// on initialization, it'll most likely be an empty string
this.query = this.$input.val();
// helps with calculating the width of the input's value
this.$overflowHelper = buildOverflowHelper(this.$input);
}
// static methods
// --------------
Input.normalizeQuery = function(str) {
// strips leading whitespace and condenses all whitespace
return (str || '').replace(/^\s*/g, '').replace(/\s{2,}/g, ' ');
};
// instance methods
// ----------------
_.mixin(Input.prototype, EventEmitter, {
// ### private
_onBlur: function onBlur() {
this.resetInputValue();
this.trigger('blurred');
},
_onFocus: function onFocus() {
this.trigger('focused');
},
_onKeydown: function onKeydown($e) {
// which is normalized and consistent (but not for ie)
var keyName = specialKeyCodeMap[$e.which || $e.keyCode];
this._managePreventDefault(keyName, $e);
if (keyName && this._shouldTrigger(keyName, $e)) {
this.trigger(keyName + 'Keyed', $e);
}
},
_onInput: function onInput() {
this._checkInputValue();
},
_managePreventDefault: function managePreventDefault(keyName, $e) {
var preventDefault;
var hintValue;
var inputValue;
switch (keyName) {
case 'tab':
hintValue = this.getHint();
inputValue = this.getInputValue();
preventDefault = hintValue &&
hintValue !== inputValue &&
!withModifier($e);
break;
case 'up':
case 'down':
preventDefault = !withModifier($e);
break;
default:
preventDefault = false;
}
if (preventDefault) {
$e.preventDefault();
}
},
_shouldTrigger: function shouldTrigger(keyName, $e) {
var trigger;
switch (keyName) {
case 'tab':
trigger = !withModifier($e);
break;
default:
trigger = true;
}
return trigger;
},
_checkInputValue: function checkInputValue() {
var inputValue;
var areEquivalent;
var hasDifferentWhitespace;
inputValue = this.getInputValue();
areEquivalent = areQueriesEquivalent(inputValue, this.query);
hasDifferentWhitespace = areEquivalent && this.query ?
this.query.length !== inputValue.length : false;
this.query = inputValue;
if (!areEquivalent) {
this.trigger('queryChanged', this.query);
} else if (hasDifferentWhitespace) {
this.trigger('whitespaceChanged', this.query);
}
},
// ### public
focus: function focus() {
this.$input.focus();
},
blur: function blur() {
this.$input.blur();
},
getQuery: function getQuery() {
return this.query;
},
setQuery: function setQuery(query) {
this.query = query;
},
getInputValue: function getInputValue() {
return this.$input.val();
},
setInputValue: function setInputValue(value, silent) {
if (typeof value === 'undefined') {
value = this.query;
}
this.$input.val(value);
// silent prevents any additional events from being triggered
if (silent) {
this.clearHint();
} else {
this._checkInputValue();
}
},
resetInputValue: function resetInputValue() {
this.setInputValue(this.query, true);
},
getHint: function getHint() {
return this.$hint.val();
},
setHint: function setHint(value) {
this.$hint.val(value);
},
clearHint: function clearHint() {
this.setHint('');
},
clearHintIfInvalid: function clearHintIfInvalid() {
var val;
var hint;
var valIsPrefixOfHint;
var isValid;
val = this.getInputValue();
hint = this.getHint();
valIsPrefixOfHint = val !== hint && hint.indexOf(val) === 0;
isValid = val !== '' && valIsPrefixOfHint && !this.hasOverflow();
if (!isValid) {
this.clearHint();
}
},
getLanguageDirection: function getLanguageDirection() {
return (this.$input.css('direction') || 'ltr').toLowerCase();
},
hasOverflow: function hasOverflow() {
// 2 is arbitrary, just picking a small number to handle edge cases
var constraint = this.$input.width() - 2;
this.$overflowHelper.text(this.getInputValue());
return this.$overflowHelper.width() >= constraint;
},
isCursorAtEnd: function() {
var valueLength;
var selectionStart;
var range;
valueLength = this.$input.val().length;
selectionStart = this.$input[0].selectionStart;
if (_.isNumber(selectionStart)) {
return selectionStart === valueLength;
} else if (document.selection) {
// NOTE: this won't work unless the input has focus, the good news
// is this code should only get called when the input has focus
range = document.selection.createRange();
range.moveStart('character', -valueLength);
return valueLength === range.text.length;
}
return true;
},
destroy: function destroy() {
this.$hint.off('.aa');
this.$input.off('.aa');
this.$hint = this.$input = this.$overflowHelper = null;
}
});
// helper functions
// ----------------
function buildOverflowHelper($input) {
return DOM.element('<pre aria-hidden="true"></pre>')
.css({
// position helper off-screen
position: 'absolute',
visibility: 'hidden',
// avoid line breaks and whitespace collapsing
whiteSpace: 'pre',
// use same font css as input to calculate accurate width
fontFamily: $input.css('font-family'),
fontSize: $input.css('font-size'),
fontStyle: $input.css('font-style'),
fontVariant: $input.css('font-variant'),
fontWeight: $input.css('font-weight'),
wordSpacing: $input.css('word-spacing'),
letterSpacing: $input.css('letter-spacing'),
textIndent: $input.css('text-indent'),
textRendering: $input.css('text-rendering'),
textTransform: $input.css('text-transform')
})
.insertAfter($input);
}
function areQueriesEquivalent(a, b) {
return Input.normalizeQuery(a) === Input.normalizeQuery(b);
}
function withModifier($e) {
return $e.altKey || $e.ctrlKey || $e.metaKey || $e.shiftKey;
}
module.exports = Input;
/***/ },
/* 103 */
/***/ function(module, exports, __webpack_require__) {
/* WEBPACK VAR INJECTION */(function(setImmediate) {'use strict';
var splitter = /\s+/;
var nextTick = getNextTick();
module.exports = {
onSync: onSync,
onAsync: onAsync,
off: off,
trigger: trigger
};
function on(method, types, cb, context) {
var type;
if (!cb) {
return this;
}
types = types.split(splitter);
cb = context ? bindContext(cb, context) : cb;
this._callbacks = this._callbacks || {};
while (type = types.shift()) {
this._callbacks[type] = this._callbacks[type] || {sync: [], async: []};
this._callbacks[type][method].push(cb);
}
return this;
}
function onAsync(types, cb, context) {
return on.call(this, 'async', types, cb, context);
}
function onSync(types, cb, context) {
return on.call(this, 'sync', types, cb, context);
}
function off(types) {
var type;
if (!this._callbacks) {
return this;
}
types = types.split(splitter);
while (type = types.shift()) {
delete this._callbacks[type];
}
return this;
}
function trigger(types) {
var type;
var callbacks;
var args;
var syncFlush;
var asyncFlush;
if (!this._callbacks) {
return this;
}
types = types.split(splitter);
args = [].slice.call(arguments, 1);
while ((type = types.shift()) && (callbacks = this._callbacks[type])) { // eslint-disable-line
syncFlush = getFlush(callbacks.sync, this, [type].concat(args));
asyncFlush = getFlush(callbacks.async, this, [type].concat(args));
if (syncFlush()) {
nextTick(asyncFlush);
}
}
return this;
}
function getFlush(callbacks, context, args) {
return flush;
function flush() {
var cancelled;
for (var i = 0, len = callbacks.length; !cancelled && i < len; i += 1) {
// only cancel if the callback explicitly returns false
cancelled = callbacks[i].apply(context, args) === false;
}
return !cancelled;
}
}
function getNextTick() {
var nextTickFn;
if (window.setImmediate) { // IE10+
nextTickFn = function nextTickSetImmediate(fn) {
setImmediate(function() { fn(); });
};
} else { // old browsers
nextTickFn = function nextTickSetTimeout(fn) {
setTimeout(function() { fn(); }, 0);
};
}
return nextTickFn;
}
function bindContext(fn, context) {
return fn.bind ?
fn.bind(context) :
function() { fn.apply(context, [].slice.call(arguments, 0)); };
}
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(104).setImmediate))
/***/ },
/* 104 */
/***/ function(module, exports, __webpack_require__) {
/* WEBPACK VAR INJECTION */(function(setImmediate, clearImmediate) {var nextTick = __webpack_require__(5).nextTick;
var apply = Function.prototype.apply;
var slice = Array.prototype.slice;
var immediateIds = {};
var nextImmediateId = 0;
// DOM APIs, for completeness
exports.setTimeout = function() {
return new Timeout(apply.call(setTimeout, window, arguments), clearTimeout);
};
exports.setInterval = function() {
return new Timeout(apply.call(setInterval, window, arguments), clearInterval);
};
exports.clearTimeout =
exports.clearInterval = function(timeout) { timeout.close(); };
function Timeout(id, clearFn) {
this._id = id;
this._clearFn = clearFn;
}
Timeout.prototype.unref = Timeout.prototype.ref = function() {};
Timeout.prototype.close = function() {
this._clearFn.call(window, this._id);
};
// Does not start the time, just sets up the members needed.
exports.enroll = function(item, msecs) {
clearTimeout(item._idleTimeoutId);
item._idleTimeout = msecs;
};
exports.unenroll = function(item) {
clearTimeout(item._idleTimeoutId);
item._idleTimeout = -1;
};
exports._unrefActive = exports.active = function(item) {
clearTimeout(item._idleTimeoutId);
var msecs = item._idleTimeout;
if (msecs >= 0) {
item._idleTimeoutId = setTimeout(function onTimeout() {
if (item._onTimeout)
item._onTimeout();
}, msecs);
}
};
// That's not how node.js implements it but the exposed api is the same.
exports.setImmediate = typeof setImmediate === "function" ? setImmediate : function(fn) {
var id = nextImmediateId++;
var args = arguments.length < 2 ? false : slice.call(arguments, 1);
immediateIds[id] = true;
nextTick(function onNextTick() {
if (immediateIds[id]) {
// fn.call() is faster so we optimize for the common use-case
// @see http://jsperf.com/call-apply-segu
if (args) {
fn.apply(null, args);
} else {
fn.call(null);
}
// Prevent ids from leaking
exports.clearImmediate(id);
}
});
return id;
};
exports.clearImmediate = typeof clearImmediate === "function" ? clearImmediate : function(id) {
delete immediateIds[id];
};
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(104).setImmediate, __webpack_require__(104).clearImmediate))
/***/ },
/* 105 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _ = __webpack_require__(99);
var DOM = __webpack_require__(98);
var EventEmitter = __webpack_require__(103);
var Dataset = __webpack_require__(106);
var css = __webpack_require__(108);
// constructor
// -----------
function Dropdown(o) {
var that = this;
var onSuggestionClick;
var onSuggestionMouseEnter;
var onSuggestionMouseLeave;
o = o || {};
if (!o.menu) {
_.error('menu is required');
}
if (!_.isArray(o.datasets) && !_.isObject(o.datasets)) {
_.error('1 or more datasets required');
}
if (!o.datasets) {
_.error('datasets is required');
}
this.isOpen = false;
this.isEmpty = true;
// bound functions
onSuggestionClick = _.bind(this._onSuggestionClick, this);
onSuggestionMouseEnter = _.bind(this._onSuggestionMouseEnter, this);
onSuggestionMouseLeave = _.bind(this._onSuggestionMouseLeave, this);
this.$menu = DOM.element(o.menu)
.on('click.aa', '.aa-suggestion', onSuggestionClick)
.on('mouseenter.aa', '.aa-suggestion', onSuggestionMouseEnter)
.on('mouseleave.aa', '.aa-suggestion', onSuggestionMouseLeave);
if (o.templates && o.templates.header) {
this.$menu.prepend(_.templatify(o.templates.header)());
}
this.datasets = _.map(o.datasets, function(oDataset) { return initializeDataset(that.$menu, oDataset); });
_.each(this.datasets, function(dataset) {
var root = dataset.getRoot();
if (root && root.parent().length === 0) {
that.$menu.append(root);
}
dataset.onSync('rendered', that._onRendered, that);
});
if (o.templates && o.templates.footer) {
this.$menu.append(_.templatify(o.templates.footer)());
}
}
// instance methods
// ----------------
_.mixin(Dropdown.prototype, EventEmitter, {
// ### private
_onSuggestionClick: function onSuggestionClick($e) {
this.trigger('suggestionClicked', DOM.element($e.currentTarget));
},
_onSuggestionMouseEnter: function onSuggestionMouseEnter($e) {
this._removeCursor();
this._setCursor(DOM.element($e.currentTarget), true);
},
_onSuggestionMouseLeave: function onSuggestionMouseLeave() {
this._removeCursor();
},
_onRendered: function onRendered() {
this.isEmpty = _.every(this.datasets, isDatasetEmpty);
if (this.isEmpty) {
this._hide();
} else if (this.isOpen) {
this._show();
}
this.trigger('datasetRendered');
function isDatasetEmpty(dataset) {
return dataset.isEmpty();
}
},
_hide: function() {
this.$menu.hide();
},
_show: function() {
// can't use jQuery#show because $menu is a span element we want
// display: block; not dislay: inline;
this.$menu.css('display', 'block');
this.trigger('shown');
},
_getSuggestions: function getSuggestions() {
return this.$menu.find('.aa-suggestion');
},
_getCursor: function getCursor() {
return this.$menu.find('.aa-cursor').first();
},
_setCursor: function setCursor($el, silent) {
$el.first().addClass('aa-cursor');
if (!silent) {
this.trigger('cursorMoved');
}
},
_removeCursor: function removeCursor() {
this._getCursor().removeClass('aa-cursor');
},
_moveCursor: function moveCursor(increment) {
var $suggestions;
var $oldCursor;
var newCursorIndex;
var $newCursor;
if (!this.isOpen) {
return;
}
$oldCursor = this._getCursor();
$suggestions = this._getSuggestions();
this._removeCursor();
// shifting before and after modulo to deal with -1 index
newCursorIndex = $suggestions.index($oldCursor) + increment;
newCursorIndex = (newCursorIndex + 1) % ($suggestions.length + 1) - 1;
if (newCursorIndex === -1) {
this.trigger('cursorRemoved');
return;
} else if (newCursorIndex < -1) {
newCursorIndex = $suggestions.length - 1;
}
this._setCursor($newCursor = $suggestions.eq(newCursorIndex));
// in the case of scrollable overflow
// make sure the cursor is visible in the menu
this._ensureVisible($newCursor);
},
_ensureVisible: function ensureVisible($el) {
var elTop;
var elBottom;
var menuScrollTop;
var menuHeight;
elTop = $el.position().top;
elBottom = elTop + $el.height() +
parseInt($el.css('margin-top'), 10) +
parseInt($el.css('margin-bottom'), 10);
menuScrollTop = this.$menu.scrollTop();
menuHeight = this.$menu.height() +
parseInt(this.$menu.css('paddingTop'), 10) +
parseInt(this.$menu.css('paddingBottom'), 10);
if (elTop < 0) {
this.$menu.scrollTop(menuScrollTop + elTop);
} else if (menuHeight < elBottom) {
this.$menu.scrollTop(menuScrollTop + (elBottom - menuHeight));
}
},
// ### public
close: function close() {
if (this.isOpen) {
this.isOpen = false;
this._removeCursor();
this._hide();
this.trigger('closed');
}
},
open: function open() {
if (!this.isOpen) {
this.isOpen = true;
if (!this.isEmpty) {
this._show();
}
this.trigger('opened');
}
},
setLanguageDirection: function setLanguageDirection(dir) {
this.$menu.css(dir === 'ltr' ? css.ltr : css.rtl);
},
moveCursorUp: function moveCursorUp() {
this._moveCursor(-1);
},
moveCursorDown: function moveCursorDown() {
this._moveCursor(+1);
},
getDatumForSuggestion: function getDatumForSuggestion($el) {
var datum = null;
if ($el.length) {
datum = {
raw: Dataset.extractDatum($el),
value: Dataset.extractValue($el),
datasetName: Dataset.extractDatasetName($el)
};
}
return datum;
},
getDatumForCursor: function getDatumForCursor() {
return this.getDatumForSuggestion(this._getCursor().first());
},
getDatumForTopSuggestion: function getDatumForTopSuggestion() {
return this.getDatumForSuggestion(this._getSuggestions().first());
},
update: function update(query) {
_.each(this.datasets, updateDataset);
function updateDataset(dataset) {
dataset.update(query);
}
},
empty: function empty() {
_.each(this.datasets, clearDataset);
this.isEmpty = true;
function clearDataset(dataset) {
dataset.clear();
}
},
isVisible: function isVisible() {
return this.isOpen && !this.isEmpty;
},
destroy: function destroy() {
this.$menu.off('.aa');
this.$menu = null;
_.each(this.datasets, destroyDataset);
function destroyDataset(dataset) {
dataset.destroy();
}
}
});
// helper functions
// ----------------
Dropdown.Dataset = Dataset;
function initializeDataset($menu, oDataset) {
return new Dropdown.Dataset(_.mixin({$menu: $menu}, oDataset));
}
module.exports = Dropdown;
/***/ },
/* 106 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var datasetKey = 'aaDataset';
var valueKey = 'aaValue';
var datumKey = 'aaDatum';
var _ = __webpack_require__(99);
var DOM = __webpack_require__(98);
var html = __webpack_require__(107);
var css = __webpack_require__(108);
var EventEmitter = __webpack_require__(103);
// constructor
// -----------
function Dataset(o) {
o = o || {};
o.templates = o.templates || {};
if (!o.source) {
_.error('missing source');
}
if (o.name && !isValidName(o.name)) {
_.error('invalid dataset name: ' + o.name);
}
// tracks the last query the dataset was updated for
this.query = null;
this.highlight = !!o.highlight;
this.name = typeof o.name === 'undefined' || o.name === null ? _.getUniqueId() : o.name;
this.source = o.source;
this.displayFn = getDisplayFn(o.display || o.displayKey);
this.templates = getTemplates(o.templates, this.displayFn);
this.$el = o.$menu && o.$menu.find('.aa-dataset-' + this.name).length > 0 ?
DOM.element(o.$menu.find('.aa-dataset-' + this.name)[0]) :
DOM.element(html.dataset.replace('%CLASS%', this.name));
this.$menu = o.$menu;
}
// static methods
// --------------
Dataset.extractDatasetName = function extractDatasetName(el) {
return DOM.element(el).data(datasetKey);
};
Dataset.extractValue = function extractValue(el) {
return DOM.element(el).data(valueKey);
};
Dataset.extractDatum = function extractDatum(el) {
var datum = DOM.element(el).data(datumKey);
if (typeof datum === 'string') {
// Zepto has an automatic deserialization of the
// JSON encoded data attribute
datum = JSON.parse(datum);
}
return datum;
};
// instance methods
// ----------------
_.mixin(Dataset.prototype, EventEmitter, {
// ### private
_render: function render(query, suggestions) {
if (!this.$el) {
return;
}
var that = this;
var hasSuggestions;
var renderArgs = [].slice.call(arguments, 2);
this.$el.empty();
hasSuggestions = suggestions && suggestions.length;
if (!hasSuggestions && this.templates.empty) {
this.$el
.html(getEmptyHtml.apply(this, renderArgs))
.prepend(that.templates.header ? getHeaderHtml.apply(this, renderArgs) : null)
.append(that.templates.footer ? getFooterHtml.apply(this, renderArgs) : null);
} else if (hasSuggestions) {
this.$el
.html(getSuggestionsHtml.apply(this, renderArgs))
.prepend(that.templates.header ? getHeaderHtml.apply(this, renderArgs) : null)
.append(that.templates.footer ? getFooterHtml.apply(this, renderArgs) : null);
}
if (this.$menu) {
this.$menu.addClass('aa-' + (hasSuggestions ? 'with' : 'without') + '-' + this.name)
.removeClass('aa-' + (hasSuggestions ? 'without' : 'with') + '-' + this.name);
}
this.trigger('rendered');
function getEmptyHtml() {
var args = [].slice.call(arguments, 0);
args = [{query: query, isEmpty: true}].concat(args);
return that.templates.empty.apply(this, args);
}
function getSuggestionsHtml() {
var args = [].slice.call(arguments, 0);
var $suggestions;
var nodes;
$suggestions = DOM.element(html.suggestions).css(css.suggestions);
// jQuery#append doesn't support arrays as the first argument
// until version 1.8, see http://bugs.jquery.com/ticket/11231
nodes = _.map(suggestions, getSuggestionNode);
$suggestions.append.apply($suggestions, nodes);
return $suggestions;
function getSuggestionNode(suggestion) {
var $el;
$el = DOM.element(html.suggestion)
.append(that.templates.suggestion.apply(this, [suggestion].concat(args)));
$el.data(datasetKey, that.name);
$el.data(valueKey, that.displayFn(suggestion) || undefined); // this led to undefined return value
$el.data(datumKey, JSON.stringify(suggestion));
$el.children().each(function() { DOM.element(this).css(css.suggestionChild); });
return $el;
}
}
function getHeaderHtml() {
var args = [].slice.call(arguments, 0);
args = [{query: query, isEmpty: !hasSuggestions}].concat(args);
return that.templates.header.apply(this, args);
}
function getFooterHtml() {
var args = [].slice.call(arguments, 0);
args = [{query: query, isEmpty: !hasSuggestions}].concat(args);
return that.templates.footer.apply(this, args);
}
},
// ### public
getRoot: function getRoot() {
return this.$el;
},
update: function update(query) {
var that = this;
this.query = query;
this.canceled = false;
this.source(query, render);
function render(suggestions) {
// if the update has been canceled or if the query has changed
// do not render the suggestions as they've become outdated
if (!that.canceled && query === that.query) {
// concat all the other arguments that could have been passed
// to the render function, and forward them to _render
var args = [].slice.call(arguments, 1);
args = [query, suggestions].concat(args);
that._render.apply(that, args);
}
}
},
cancel: function cancel() {
this.canceled = true;
},
clear: function clear() {
this.cancel();
this.$el.empty();
this.trigger('rendered');
},
isEmpty: function isEmpty() {
return this.$el.is(':empty');
},
destroy: function destroy() {
this.$el = null;
}
});
// helper functions
// ----------------
function getDisplayFn(display) {
display = display || 'value';
return _.isFunction(display) ? display : displayFn;
function displayFn(obj) {
return obj[display];
}
}
function getTemplates(templates, displayFn) {
return {
empty: templates.empty && _.templatify(templates.empty),
header: templates.header && _.templatify(templates.header),
footer: templates.footer && _.templatify(templates.footer),
suggestion: templates.suggestion || suggestionTemplate
};
function suggestionTemplate(context) {
return '<p>' + displayFn(context) + '</p>';
}
}
function isValidName(str) {
// dashes, underscores, letters, and numbers
return (/^[_a-zA-Z0-9-]+$/).test(str);
}
module.exports = Dataset;
/***/ },
/* 107 */
/***/ function(module, exports) {
'use strict';
module.exports = {
wrapper: '<span class="algolia-autocomplete"></span>',
dropdown: '<span class="aa-dropdown-menu"></span>',
dataset: '<div class="aa-dataset-%CLASS%"></div>',
suggestions: '<span class="aa-suggestions"></span>',
suggestion: '<div class="aa-suggestion"></div>'
};
/***/ },
/* 108 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _ = __webpack_require__(99);
var css = {
wrapper: {
position: 'relative',
display: 'inline-block'
},
hint: {
position: 'absolute',
top: '0',
left: '0',
borderColor: 'transparent',
boxShadow: 'none',
// #741: fix hint opacity issue on iOS
opacity: '1'
},
input: {
position: 'relative',
verticalAlign: 'top',
backgroundColor: 'transparent'
},
inputWithNoHint: {
position: 'relative',
verticalAlign: 'top'
},
dropdown: {
position: 'absolute',
top: '100%',
left: '0',
zIndex: '100',
display: 'none'
},
suggestions: {
display: 'block'
},
suggestion: {
whiteSpace: 'nowrap',
cursor: 'pointer'
},
suggestionChild: {
whiteSpace: 'normal'
},
ltr: {
left: '0',
right: 'auto'
},
rtl: {
left: 'auto',
right: '0'
}
};
// ie specific styling
if (_.isMsie()) {
// ie6-8 (and 9?) doesn't fire hover and click events for elements with
// transparent backgrounds, for a workaround, use 1x1 transparent gif
_.mixin(css.input, {
backgroundImage: 'url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)'
});
}
// ie7 and under specific styling
if (_.isMsie() && _.isMsie() <= 7) {
// if someone can tell me why this is necessary to align
// the hint with the query in ie7, i'll send you $5 - @JakeHarding
_.mixin(css.input, {marginTop: '-1px'});
}
module.exports = css;
/***/ },
/* 109 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
module.exports = {
hits: __webpack_require__(110),
popularIn: __webpack_require__(111)
};
/***/ },
/* 110 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _ = __webpack_require__(99);
module.exports = function search(index, params) {
return sourceFn;
function sourceFn(query, cb) {
index.search(query, params, function(error, content) {
if (error) {
_.error(error.message);
return;
}
cb(content.hits, content);
});
}
};
/***/ },
/* 111 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _ = __webpack_require__(99);
module.exports = function popularIn(index, params, details, options) {
if (!details.source) {
return _.error("Missing 'source' key");
}
var source = _.isFunction(details.source) ? details.source : function(hit) { return hit[details.source]; };
if (!details.index) {
return _.error("Missing 'index' key");
}
var detailsIndex = details.index;
options = options || {};
return sourceFn;
function sourceFn(query, cb) {
index.search(query, params, function(error, content) {
if (error) {
_.error(error.message);
return;
}
if (content.hits.length > 0) {
var first = content.hits[0];
var detailsParams = _.mixin({hitsPerPage: 0}, details);
delete detailsParams.source; // not a query parameter
delete detailsParams.index; // not a query parameter
detailsIndex.search(source(first), detailsParams, function(error2, content2) {
if (error2) {
_.error(error2.message);
return;
}
var suggestions = [];
// add the 'all department' entry before others
if (options.includeAll) {
var label = options.allTitle || 'All departments';
suggestions.push(_.mixin({
facet: {value: label, count: content2.nbHits}
}, _.cloneDeep(first)));
}
// enrich the first hit iterating over the facets
_.each(content2.facets, function(values, facet) {
_.each(values, function(count, value) {
suggestions.push(_.mixin({
facet: {facet: facet, value: value, count: count}
}, _.cloneDeep(first)));
});
});
// append all other hits
for (var i = 1; i < content.hits.length; ++i) {
suggestions.push(content.hits[i]);
}
cb(suggestions, content);
});
return;
}
cb([]);
});
}
};
/***/ },
/* 112 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports.default = createHitFormatter;
var _findCountryCode = __webpack_require__(113);
var _findCountryCode2 = _interopRequireDefault(_findCountryCode);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function createHitFormatter(_ref) {
var formatAutocompleteSuggestion = _ref.formatAutocompleteSuggestion;
var formatInputValue = _ref.formatInputValue;
return function (hit) {
var formatted = {
administrative: hit.administrative && hit.administrative[0],
city: hit.city && hit.city.default[0],
country: hit.country.default,
countryCode: (0, _findCountryCode2.default)(hit._tags),
isCity: hit.is_city
};
// this is the value to put inside the input.value
// autocomplete.js automatically takes hit.value as the underlying
// input value when a suggestion is validated
formatted.value = formatInputValue(_extends({}, formatted, {
name: hit.locale_names.default[0]
}));
// this is the value shown in suggestions, we highlight the name
formatted.suggestion = formatAutocompleteSuggestion(_extends({}, formatted, {
name: hit._highlightResult.locale_names.default[0].value
}));
return formatted;
};
}
/***/ },
/* 113 */
/***/ function(module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = findCountryCode;
function findCountryCode(tags) {
var countryCode = void 0;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = tags[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var tag = _step.value;
var find = tag.match(/country\/(.*)?/);
if (find) {
countryCode = find[1];
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return countryCode;
}
/***/ },
/* 114 */
/***/ function(module, exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = formatInputValue;
function formatInputValue(_ref) {
var administrative = _ref.administrative;
var city = _ref.city;
var country = _ref.country;
var isCity = _ref.isCity;
var name = _ref.name;
return name + '\n' + (isCity === false ? ' ' + city + ',' : '') + '\n ' + administrative + ',\n ' + country;
}
/***/ },
/* 115 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = formatAutocompleteSuggestion;
var _icons = __webpack_require__(116);
var _icons2 = _interopRequireDefault(_icons);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function formatAutocompleteSuggestion(_ref) {
var administrative = _ref.administrative;
var city = _ref.city;
var country = _ref.country;
var isCity = _ref.isCity;
var name = _ref.name;
return name + '\n' + (isCity === false ? ' ' + city + ',' : '') + '\n ' + administrative + ',\n ' + country;
} /* eslint no-unused-vars: 0 */
// this ^^ line can be removed, just so travis test works
/***/ },
/* 116 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _address = __webpack_require__(117);
var _address2 = _interopRequireDefault(_address);
var _city = __webpack_require__(118);
var _city2 = _interopRequireDefault(_city);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// svg will be inlined by the raw-loader (see /webpack.config.babel.js)
exports.default = {
address: _address2.default,
city: _city2.default
};
/***/ },
/* 117 */
/***/ function(module, exports) {
module.exports = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 14 20\"><g fill=\"none\" fill-rule=\"evenodd\"><path d=\"M-5-2h24v24H-5V-2z\"/><path fill=\"#000\" d=\"M7 0C3.13 0 0 3.13 0 7c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5C5.62 9.5 4.5 8.38 4.5 7S5.62 4.5 7 4.5 9.5 5.62 9.5 7 8.38 9.5 7 9.5z\" opacity=\".3\"/></g></svg>\n"
/***/ },
/* 118 */
/***/ function(module, exports) {
module.exports = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 18 19\"><g fill=\"none\" fill-rule=\"evenodd\"><path fill=\"#000\" d=\"M12 9V3L9 0 6 3v2H0v14h18V9h-6zm-8 8H2v-2h2v2zm0-4H2v-2h2v2zm0-4H2V7h2v2zm6 8H8v-2h2v2zm0-4H8v-2h2v2zm0-4H8V7h2v2zm0-4H8V3h2v2zm6 12h-2v-2h2v2zm0-4h-2v-2h2v2z\"/><path d=\"M-3-2h24v24H-3V-2z\"/></g></svg>\n"
/***/ },
/* 119 */
/***/ function(module, exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = '0.0.0';
/***/ }
/******/ ])
});
;
//# sourceMappingURL=places.js.map |
/**
* @preserve
* Project: Bootstrap Hover Dropdown
* Author: Cameron Spear
* Version: v2.2.0
* Contributors: Mattia Larentis
* Dependencies: Bootstrap's Dropdown plugin, jQuery
* Description: A simple plugin to enable Bootstrap dropdowns to active on hover and provide a nice user experience.
* License: MIT
* Homepage: http://cameronspear.com/blog/bootstrap-dropdown-on-hover-plugin/
*/
;(function ($, window, undefined) {
// outside the scope of the jQuery plugin to
// keep track of all dropdowns
var $allDropdowns = $();
// if instantlyCloseOthers is true, then it will instantly
// shut other nav items when a new one is hovered over
$.fn.dropdownHover = function (options) {
// don't do anything if touch is supported
// (plugin causes some issues on mobile)
if('ontouchstart' in document) return this; // don't want to affect chaining
// the element we really care about
// is the dropdown-toggle's parent
$allDropdowns = $allDropdowns.add(this.parent());
return this.each(function () {
var $this = $(this),
$parent = $this.parent(),
defaults = {
delay: 500,
hoverDelay: 0,
instantlyCloseOthers: true
},
data = {
delay: $(this).data('delay'),
hoverDelay: $(this).data('hover-delay'),
instantlyCloseOthers: $(this).data('close-others')
},
showEvent = 'show.bs.dropdown',
hideEvent = 'hide.bs.dropdown',
// shownEvent = 'shown.bs.dropdown',
// hiddenEvent = 'hidden.bs.dropdown',
settings = $.extend(true, {}, defaults, options, data),
timeout, timeoutHover;
$parent.hover(function (event) {
// so a neighbor can't open the dropdown
if(!$parent.hasClass('open') && !$this.is(event.target)) {
// stop this event, stop executing any code
// in this callback but continue to propagate
return true;
}
openDropdown(event);
}, function () {
// clear timer for hover event
window.clearTimeout(timeoutHover)
timeout = window.setTimeout(function () {
$this.attr('aria-expanded', 'false');
$parent.removeClass('open');
$this.trigger(hideEvent);
}, settings.delay);
});
// this helps with button groups!
$this.hover(function (event) {
// this helps prevent a double event from firing.
// see https://github.com/CWSpear/bootstrap-hover-dropdown/issues/55
if(!$parent.hasClass('open') && !$parent.is(event.target)) {
// stop this event, stop executing any code
// in this callback but continue to propagate
return true;
}
openDropdown(event);
});
// handle submenus
$parent.find('.dropdown-submenu').each(function (){
var $this = $(this);
var subTimeout;
$this.hover(function () {
window.clearTimeout(subTimeout);
$this.children('.dropdown-menu').show();
// always close submenu siblings instantly
$this.siblings().children('.dropdown-menu').hide();
}, function () {
var $submenu = $this.children('.dropdown-menu');
subTimeout = window.setTimeout(function () {
$submenu.hide();
}, settings.delay);
});
});
function openDropdown(event) {
if($this.parents(".navbar").find(".navbar-toggle").is(":visible")) {
// If we're inside a navbar, don't do anything when the
// navbar is collapsed, as it makes the navbar pretty unusable.
return;
}
// clear dropdown timeout here so it doesnt close before it should
window.clearTimeout(timeout);
// restart hover timer
window.clearTimeout(timeoutHover);
// delay for hover event.
timeoutHover = window.setTimeout(function () {
$allDropdowns.find(':focus').blur();
if(settings.instantlyCloseOthers === true)
$allDropdowns.removeClass('open');
// clear timer for hover event
window.clearTimeout(timeoutHover);
$this.attr('aria-expanded', 'true');
$parent.addClass('open');
$this.trigger(showEvent);
}, settings.hoverDelay);
}
});
};
$(document).ready(function () {
// apply dropdownHover to all elements with the data-hover="dropdown" attribute
$('[data-hover="dropdown"]').dropdownHover();
});
})(jQuery, window);
|
/*! UIkit 2.16.0 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
(function(addon) {
var component;
if (jQuery && UIkit) {
component = addon(jQuery, UIkit);
}
if (typeof define == "function" && define.amd) {
define("uikit-htmleditor", ["uikit"], function(){
return component || addon(jQuery, UIkit);
});
}
})(function($, UI) {
"use strict";
var editors = [];
UI.component('htmleditor', {
defaults: {
iframe : false,
mode : 'split',
markdown : false,
autocomplete : true,
height : 500,
maxsplitsize : 1000,
markedOptions: { gfm: true, tables: true, breaks: true, pedantic: true, sanitize: false, smartLists: true, smartypants: false, langPrefix: 'lang-'},
codemirror : { mode: 'htmlmixed', lineWrapping: true, dragDrop: false, autoCloseTags: true, matchTags: true, autoCloseBrackets: true, matchBrackets: true, indentUnit: 4, indentWithTabs: false, tabSize: 4, hintOptions: {completionSingle:false} },
toolbar : [ 'bold', 'italic', 'strike', 'link', 'image', 'blockquote', 'listUl', 'listOl' ],
lblPreview : 'Preview',
lblCodeview : 'HTML',
lblMarkedview: 'Markdown'
},
boot: function() {
// init code
UI.ready(function(context) {
UI.$('textarea[data-@-htmleditor]', context).each(function() {
var editor = UI.$(this), obj;
if (!editor.data('htmleditor')) {
obj = UI.htmleditor(editor, UI.Utils.options(editor.attr('data-@-htmleditor')));
}
});
});
},
init: function() {
var $this = this, tpl = UI.components.htmleditor.template;
this.CodeMirror = this.options.CodeMirror || CodeMirror;
this.buttons = {};
tpl = tpl.replace(/\{:lblPreview\}/g, this.options.lblPreview);
tpl = tpl.replace(/\{:lblCodeview\}/g, this.options.lblCodeview);
this.htmleditor = UI.$(tpl);
this.content = this.htmleditor.find('.@-htmleditor-content');
this.toolbar = this.htmleditor.find('.@-htmleditor-toolbar');
this.preview = this.htmleditor.find('.@-htmleditor-preview').children().eq(0);
this.code = this.htmleditor.find('.@-htmleditor-code');
this.element.before(this.htmleditor).appendTo(this.code);
this.editor = this.CodeMirror.fromTextArea(this.element[0], this.options.codemirror);
this.editor.htmleditor = this;
this.editor.on('change', UI.Utils.debounce(function() { $this.render(); }, 150));
this.editor.on('change', function() { $this.editor.save(); });
this.code.find('.CodeMirror').css('height', this.options.height);
// iframe mode?
if (this.options.iframe) {
this.iframe = UI.$('<iframe class="@-htmleditor-iframe" frameborder="0" scrolling="auto" height="100" width="100%"></iframe>');
this.preview.append(this.iframe);
// must open and close document object to start using it!
this.iframe[0].contentWindow.document.open();
this.iframe[0].contentWindow.document.close();
this.preview.container = $(this.iframe[0].contentWindow.document).find('body');
// append custom stylesheet
if (typeof(this.options.iframe) === 'string') {
this.preview.container.parent().append('<link rel="stylesheet" href="'+this.options.iframe+'">');
}
} else {
this.preview.container = this.preview;
}
UI.$win.on('resize', UI.Utils.debounce(function() { $this.fit(); }, 200));
var previewContainer = this.iframe ? this.preview.container:$this.preview.parent(),
codeContent = this.code.find('.CodeMirror-sizer'),
codeScroll = this.code.find('.CodeMirror-scroll').on('scroll', UI.Utils.debounce(function() {
if ($this.htmleditor.attr('data-mode') == 'tab') return;
// calc position
var codeHeight = codeContent.height() - codeScroll.height(),
previewHeight = previewContainer[0].scrollHeight - ($this.iframe ? $this.iframe.height() : previewContainer.height()),
ratio = previewHeight / codeHeight,
previewPostition = codeScroll.scrollTop() * ratio;
// apply new scroll
previewContainer.scrollTop(previewPostition);
}, 10));
this.htmleditor.on('click', '.@-htmleditor-button-code, .@-htmleditor-button-preview', function(e) {
e.preventDefault();
if ($this.htmleditor.attr('data-mode') == 'tab') {
$this.htmleditor.find('.@-htmleditor-button-code, .@-htmleditor-button-preview').removeClass('@-active').filter(this).addClass('@-active');
$this.activetab = UI.$(this).hasClass('@-htmleditor-button-code') ? 'code' : 'preview';
$this.htmleditor.attr('data-active-tab', $this.activetab);
$this.editor.refresh();
}
});
// toolbar actions
this.htmleditor.on('click', 'a[data-htmleditor-button]', function() {
if (!$this.code.is(':visible')) return;
$this.trigger('action.' + UI.$(this).data('htmleditor-button'), [$this.editor]);
});
this.preview.parent().css('height', this.code.height());
// autocomplete
if (this.options.autocomplete && this.CodeMirror.showHint && this.CodeMirror.hint && this.CodeMirror.hint.html) {
this.editor.on('inputRead', UI.Utils.debounce(function() {
var doc = $this.editor.getDoc(), POS = doc.getCursor(), mode = $this.CodeMirror.innerMode($this.editor.getMode(), $this.editor.getTokenAt(POS).state).mode.name;
if (mode == 'xml') { //html depends on xml
var cur = $this.editor.getCursor(), token = $this.editor.getTokenAt(cur);
if (token.string.charAt(0) == '<' || token.type == 'attribute') {
$this.CodeMirror.showHint($this.editor, $this.CodeMirror.hint.html, { completeSingle: false });
}
}
}, 100));
}
this.debouncedRedraw = UI.Utils.debounce(function () { $this.redraw(); }, 5);
this.on('init.uk.component', function() {
$this.redraw();
});
this.element.attr('data-@-check-display', 1).on('display.uk.check', function(e) {
if (this.htmleditor.is(":visible")) this.fit();
}.bind(this));
editors.push(this);
},
addButton: function(name, button) {
this.buttons[name] = button;
},
addButtons: function(buttons) {
$.extend(this.buttons, buttons);
},
replaceInPreview: function(regexp, callback) {
var editor = this.editor, results = [], value = editor.getValue(), offset = -1;
this.currentvalue = this.currentvalue.replace(regexp, function() {
offset = value.indexOf(arguments[0], ++offset);
var match = {
matches: arguments,
from : translateOffset(offset),
to : translateOffset(offset + arguments[0].length),
replace: function(value) {
editor.replaceRange(value, match.from, match.to);
},
inRange: function(cursor) {
if (cursor.line === match.from.line && cursor.line === match.to.line) {
return cursor.ch >= match.from.ch && cursor.ch < match.to.ch;
}
return (cursor.line === match.from.line && cursor.ch >= match.from.ch) ||
(cursor.line > match.from.line && cursor.line < match.to.line) ||
(cursor.line === match.to.line && cursor.ch < match.to.ch);
}
};
var result = callback(match);
if (!result) {
return arguments[0];
}
results.push(match);
return result;
});
function translateOffset(offset) {
var result = editor.getValue().substring(0, offset).split('\n');
return { line: result.length - 1, ch: result[result.length - 1].length }
}
return results;
},
_buildtoolbar: function() {
if (!(this.options.toolbar && this.options.toolbar.length)) return;
var $this = this, bar = [];
this.toolbar.empty();
this.options.toolbar.forEach(function(button) {
if (!$this.buttons[button]) return;
var title = $this.buttons[button].title ? $this.buttons[button].title : button;
bar.push('<li><a data-htmleditor-button="'+button+'" title="'+title+'" data-@-tooltip>'+$this.buttons[button].label+'</a></li>');
});
this.toolbar.html(UI.prefix(bar.join('\n')));
},
fit: function() {
var mode = this.options.mode;
if (mode == 'split' && this.htmleditor.width() < this.options.maxsplitsize) {
mode = 'tab';
}
if (mode == 'tab') {
if (!this.activetab) {
this.activetab = 'code';
this.htmleditor.attr('data-active-tab', this.activetab);
}
this.htmleditor.find('.@-htmleditor-button-code, .@-htmleditor-button-preview').removeClass('@-active')
.filter(this.activetab == 'code' ? '.@-htmleditor-button-code' : '.@-htmleditor-button-preview')
.addClass('@-active');
}
this.editor.refresh();
this.preview.parent().css('height', this.code.height());
this.htmleditor.attr('data-mode', mode);
},
redraw: function() {
this._buildtoolbar();
this.render();
this.fit();
},
getMode: function() {
return this.editor.getOption('mode');
},
getCursorMode: function() {
var param = { mode: 'html'};
this.trigger('cursorMode', [param]);
return param.mode;
},
render: function() {
this.currentvalue = this.editor.getValue();
// empty code
if (!this.currentvalue) {
this.element.val('');
this.preview.container.html('');
return;
}
this.trigger('render', [this]);
this.trigger('renderLate', [this]);
this.preview.container.html(this.currentvalue);
},
addShortcut: function(name, callback) {
var map = {};
if (!$.isArray(name)) {
name = [name];
}
name.forEach(function(key) {
map[key] = callback;
});
this.editor.addKeyMap(map);
return map;
},
addShortcutAction: function(action, shortcuts) {
var editor = this;
this.addShortcut(shortcuts, function() {
editor.element.trigger('action.' + action, [editor.editor]);
});
},
replaceSelection: function(replace) {
var text = this.editor.getSelection();
if (!text.length) {
var cur = this.editor.getCursor(),
curLine = this.editor.getLine(cur.line),
start = cur.ch,
end = start;
while (end < curLine.length && /[\w$]+/.test(curLine.charAt(end))) ++end;
while (start && /[\w$]+/.test(curLine.charAt(start - 1))) --start;
var curWord = start != end && curLine.slice(start, end);
if (curWord) {
this.editor.setSelection({ line: cur.line, ch: start}, { line: cur.line, ch: end });
text = curWord;
}
}
var html = replace.replace('$1', text);
this.editor.replaceSelection(html, 'end');
this.editor.focus();
},
replaceLine: function(replace) {
var pos = this.editor.getDoc().getCursor(),
text = this.editor.getLine(pos.line),
html = replace.replace('$1', text);
this.editor.replaceRange(html , { line: pos.line, ch: 0 }, { line: pos.line, ch: text.length });
this.editor.setCursor({ line: pos.line, ch: html.length });
this.editor.focus();
},
save: function() {
this.editor.save();
}
});
UI.components.htmleditor.template = [
'<div class="@-htmleditor @-clearfix" data-mode="split">',
'<div class="@-htmleditor-navbar">',
'<ul class="@-htmleditor-navbar-nav @-htmleditor-toolbar"></ul>',
'<div class="@-htmleditor-navbar-flip">',
'<ul class="@-htmleditor-navbar-nav">',
'<li class="@-htmleditor-button-code"><a>{:lblCodeview}</a></li>',
'<li class="@-htmleditor-button-preview"><a>{:lblPreview}</a></li>',
'<li><a data-htmleditor-button="fullscreen"><i class="@-icon-expand"></i></a></li>',
'</ul>',
'</div>',
'</div>',
'<div class="@-htmleditor-content">',
'<div class="@-htmleditor-code"></div>',
'<div class="@-htmleditor-preview"><div></div></div>',
'</div>',
'</div>'
].join('');
UI.plugin('htmleditor', 'base', {
init: function(editor) {
editor.addButtons({
fullscreen: {
title : 'Fullscreen',
label : '<i class="@-icon-expand"></i>'
},
bold : {
title : 'Bold',
label : '<i class="@-icon-bold"></i>'
},
italic : {
title : 'Italic',
label : '<i class="@-icon-italic"></i>'
},
strike : {
title : 'Strikethrough',
label : '<i class="@-icon-strikethrough"></i>'
},
blockquote : {
title : 'Blockquote',
label : '<i class="@-icon-quote-right"></i>'
},
link : {
title : 'Link',
label : '<i class="@-icon-link"></i>'
},
image : {
title : 'Image',
label : '<i class="@-icon-picture-o"></i>'
},
listUl : {
title : 'Unordered List',
label : '<i class="@-icon-list-ul"></i>'
},
listOl : {
title : 'Ordered List',
label : '<i class="@-icon-list-ol"></i>'
}
});
addAction('bold', '<strong>$1</strong>');
addAction('italic', '<em>$1</em>');
addAction('strike', '<del>$1</del>');
addAction('blockquote', '<blockquote><p>$1</p></blockquote>', 'replaceLine');
addAction('link', '<a href="http://">$1</a>');
addAction('image', '<img src="http://" alt="$1">');
var listfn = function() {
if (editor.getCursorMode() == 'html') {
var cm = editor.editor,
pos = cm.getDoc().getCursor(true),
posend = cm.getDoc().getCursor(false);
for (var i=pos.line; i<(posend.line+1);i++) {
cm.replaceRange('<li>'+cm.getLine(i)+'</li>', { line: i, ch: 0 }, { line: i, ch: cm.getLine(i).length });
}
cm.setCursor({ line: posend.line, ch: cm.getLine(posend.line).length });
cm.focus();
}
}
editor.on('action.listUl', function() {
listfn();
});
editor.on('action.listOl', function() {
listfn();
});
editor.htmleditor.on('click', 'a[data-htmleditor-button="fullscreen"]', function() {
editor.htmleditor.toggleClass('@-htmleditor-fullscreen');
var wrap = editor.editor.getWrapperElement();
if (editor.htmleditor.hasClass('@-htmleditor-fullscreen')) {
editor.editor.state.fullScreenRestore = {scrollTop: window.pageYOffset, scrollLeft: window.pageXOffset, width: wrap.style.width, height: wrap.style.height};
wrap.style.width = '';
wrap.style.height = editor.content.height()+'px';
document.documentElement.style.overflow = 'hidden';
} else {
document.documentElement.style.overflow = '';
var info = editor.editor.state.fullScreenRestore;
wrap.style.width = info.width; wrap.style.height = info.height;
window.scrollTo(info.scrollLeft, info.scrollTop);
}
setTimeout(function() {
editor.fit();
UI.$win.trigger('resize');
}, 50);
});
editor.addShortcut(['Ctrl-S', 'Cmd-S'], function() { editor.element.trigger('htmleditor-save', [editor]); });
editor.addShortcutAction('bold', ['Ctrl-B', 'Cmd-B']);
function addAction(name, replace, mode) {
editor.on('action.'+name, function() {
if (editor.getCursorMode() == 'html') {
editor[mode == 'replaceLine' ? 'replaceLine' : 'replaceSelection'](replace);
}
});
}
}
});
UI.plugin('htmleditor', 'markdown', {
init: function(editor) {
var parser = editor.options.marked || marked;
if (!parser) return;
parser.setOptions(editor.options.markedOptions);
if (editor.options.markdown) {
enableMarkdown()
}
addAction('bold', '**$1**');
addAction('italic', '*$1*');
addAction('strike', '~~$1~~');
addAction('blockquote', '> $1', 'replaceLine');
addAction('link', '[$1](http://)');
addAction('image', '');
editor.on('action.listUl', function() {
if (editor.getCursorMode() == 'markdown') {
var cm = editor.editor,
pos = cm.getDoc().getCursor(true),
posend = cm.getDoc().getCursor(false);
for (var i=pos.line; i<(posend.line+1);i++) {
cm.replaceRange('* '+cm.getLine(i), { line: i, ch: 0 }, { line: i, ch: cm.getLine(i).length });
}
cm.setCursor({ line: posend.line, ch: cm.getLine(posend.line).length });
cm.focus();
}
});
editor.on('action.listOl', function() {
if (editor.getCursorMode() == 'markdown') {
var cm = editor.editor,
pos = cm.getDoc().getCursor(true),
posend = cm.getDoc().getCursor(false),
prefix = 1;
if (pos.line > 0) {
var prevline = cm.getLine(pos.line-1), matches;
if(matches = prevline.match(/^(\d+)\./)) {
prefix = Number(matches[1])+1;
}
}
for (var i=pos.line; i<(posend.line+1);i++) {
cm.replaceRange(prefix+'. '+cm.getLine(i), { line: i, ch: 0 }, { line: i, ch: cm.getLine(i).length });
prefix++;
}
cm.setCursor({ line: posend.line, ch: cm.getLine(posend.line).length });
cm.focus();
}
});
editor.on('renderLate', function() {
if (editor.editor.options.mode == 'gfm') {
editor.currentvalue = parser(editor.currentvalue);
}
});
editor.on('cursorMode', function(e, param) {
if (editor.editor.options.mode == 'gfm') {
var pos = editor.editor.getDoc().getCursor();
if (!editor.editor.getTokenAt(pos).state.base.htmlState) {
param.mode = 'markdown';
}
}
});
$.extend(editor, {
enableMarkdown: function() {
enableMarkdown()
this.render();
},
disableMarkdown: function() {
this.editor.setOption('mode', 'htmlmixed');
this.htmleditor.find('.@-htmleditor-button-code a').html(this.options.lblCodeview);
this.render();
}
});
// switch markdown mode on event
editor.on({
enableMarkdown : function() { editor.enableMarkdown(); },
disableMarkdown : function() { editor.disableMarkdown(); }
});
function enableMarkdown() {
editor.editor.setOption('mode', 'gfm');
editor.htmleditor.find('.@-htmleditor-button-code a').html(editor.options.lblMarkedview);
}
function addAction(name, replace, mode) {
editor.on('action.'+name, function() {
if (editor.getCursorMode() == 'markdown') {
editor[mode == 'replaceLine' ? 'replaceLine' : 'replaceSelection'](replace);
}
});
}
}
});
return UI.htmleditor;
});
|
/*!
SerializeJSON jQuery plugin.
https://github.com/marioizquierdo/jquery.serializeJSON
version 2.6.2 (May, 2015)
Copyright (c) 2012, 2015 Mario Izquierdo
Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*/
(function (factory) {
if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') { // Node/CommonJS
var jQuery = require('jquery');
module.exports = factory(jQuery);
} else { // Browser globals (zepto supported)
factory(window.jQuery || window.Zepto || window.$); // Zepto supported on browsers as well
}
}(function ($) {
"use strict";
// jQuery('form').serializeJSON()
$.fn.serializeJSON = function (options) {
var serializedObject, formAsArray, keys, type, value, _ref, f, opts;
f = $.serializeJSON;
opts = f.setupOpts(options); // calculate values for options {parseNumbers, parseBoolens, parseNulls}
formAsArray = this.serializeArray(); // array of objects {name, value}
f.readCheckboxUncheckedValues(formAsArray, this, opts); // add {name, value} of unchecked checkboxes if needed
serializedObject = {};
$.each(formAsArray, function (i, input) {
keys = f.splitInputNameIntoKeysArray(input.name, opts);
type = keys.pop(); // the last element is always the type ("string" by default)
if (type !== 'skip') { // easy way to skip a value
value = f.parseValue(input.value, type, opts); // string, number, boolean or null
if (opts.parseWithFunction && type === '_') { // allow for custom parsing
value = opts.parseWithFunction(value, input.name);
}
f.deepSet(serializedObject, keys, value, opts);
}
});
return serializedObject;
};
// Use $.serializeJSON as namespace for the auxiliar functions
// and to define defaults
$.serializeJSON = {
defaultOptions: {
checkboxUncheckedValue: undefined, // to include that value for unchecked checkboxes (instead of ignoring them)
parseNumbers: false, // convert values like "1", "-2.33" to 1, -2.33
parseBooleans: false, // convert "true", "false" to true, false
parseNulls: false, // convert "null" to null
parseAll: false, // all of the above
parseWithFunction: null, // to use custom parser, a function like: function(val){ return parsed_val; }
customTypes: {}, // override defaultTypes
defaultTypes: {
"string": function(str) { return String(str); },
"number": function(str) { return Number(str); },
"boolean": function(str) { var falses = ["false", "null", "undefined", "", "0"]; return falses.indexOf(str) === -1; },
"null": function(str) { var falses = ["false", "null", "undefined", "", "0"]; return falses.indexOf(str) === -1 ? str : null; },
"array": function(str) { return JSON.parse(str); },
"object": function(str) { return JSON.parse(str); },
"auto": function(str) { return $.serializeJSON.parseValue(str, null, {parseNumbers: true, parseBooleans: true, parseNulls: true}); } // try again with something like "parseAll"
},
useIntKeysAsArrayIndex: false // name="foo[2]" value="v" => {foo: [null, null, "v"]}, instead of {foo: ["2": "v"]}
},
// Merge option defaults into the options
setupOpts: function(options) {
var opt, validOpts, defaultOptions, optWithDefault, parseAll, f;
f = $.serializeJSON;
if (options == null) { options = {}; } // options ||= {}
defaultOptions = f.defaultOptions || {}; // defaultOptions
// Make sure that the user didn't misspell an option
validOpts = ['checkboxUncheckedValue', 'parseNumbers', 'parseBooleans', 'parseNulls', 'parseAll', 'parseWithFunction', 'customTypes', 'defaultTypes', 'useIntKeysAsArrayIndex']; // re-define because the user may override the defaultOptions
for (opt in options) {
if (validOpts.indexOf(opt) === -1) {
throw new Error("serializeJSON ERROR: invalid option '" + opt + "'. Please use one of " + validOpts.join(', '));
}
}
// Helper to get the default value for this option if none is specified by the user
optWithDefault = function(key) { return (options[key] !== false) && (options[key] !== '') && (options[key] || defaultOptions[key]); };
// Return computed options (opts to be used in the rest of the script)
parseAll = optWithDefault('parseAll');
return {
checkboxUncheckedValue: optWithDefault('checkboxUncheckedValue'),
parseNumbers: parseAll || optWithDefault('parseNumbers'),
parseBooleans: parseAll || optWithDefault('parseBooleans'),
parseNulls: parseAll || optWithDefault('parseNulls'),
parseWithFunction: optWithDefault('parseWithFunction'),
typeFunctions: $.extend({}, optWithDefault('defaultTypes'), optWithDefault('customTypes')),
useIntKeysAsArrayIndex: optWithDefault('useIntKeysAsArrayIndex')
};
},
// Given a string, apply the type or the relevant "parse" options, to return the parsed value
parseValue: function(str, type, opts) {
var typeFunction, f;
f = $.serializeJSON;
// Parse with a type if available
typeFunction = opts.typeFunctions && opts.typeFunctions[type];
if (typeFunction) { return typeFunction(str); } // use specific type
// Otherwise, check if there is any auto-parse option enabled and use it.
if (opts.parseNumbers && f.isNumeric(str)) { return Number(str); } // auto: number
if (opts.parseBooleans && (str === "true" || str === "false")) { return str === "true"; } // auto: boolean
if (opts.parseNulls && str == "null") { return null; } // auto: null
// If none applies, just return the str
return str;
},
isObject: function(obj) { return obj === Object(obj); }, // is it an Object?
isUndefined: function(obj) { return obj === void 0; }, // safe check for undefined values
isValidArrayIndex: function(val) { return /^[0-9]+$/.test(String(val)); }, // 1,2,3,4 ... are valid array indexes
isNumeric: function(obj) { return obj - parseFloat(obj) >= 0; }, // taken from jQuery.isNumeric implementation. Not using jQuery.isNumeric to support old jQuery and Zepto versions
optionKeys: function(obj) { if (Object.keys) { return Object.keys(obj); } else { var key, keys = []; for(key in obj){ keys.push(key); } return keys;} }, // polyfill Object.keys to get option keys in IE<9
// Split the input name in programatically readable keys.
// The last element is always the type (default "_").
// Examples:
// "foo" => ['foo', '_']
// "foo:string" => ['foo', 'string']
// "foo:boolean" => ['foo', 'boolean']
// "[foo]" => ['foo', '_']
// "foo[inn][bar]" => ['foo', 'inn', 'bar', '_']
// "foo[inn[bar]]" => ['foo', 'inn', 'bar', '_']
// "foo[inn][arr][0]" => ['foo', 'inn', 'arr', '0', '_']
// "arr[][val]" => ['arr', '', 'val', '_']
// "arr[][val]:null" => ['arr', '', 'val', 'null']
splitInputNameIntoKeysArray: function(name, opts) {
var keys, nameWithoutType, type, _ref, f;
f = $.serializeJSON;
_ref = f.extractTypeFromInputName(name, opts); nameWithoutType = _ref[0]; type = _ref[1];
keys = nameWithoutType.split('['); // split string into array
keys = $.map(keys, function (key) { return key.replace(/\]/g, ''); }); // remove closing brackets
if (keys[0] === '') { keys.shift(); } // ensure no opening bracket ("[foo][inn]" should be same as "foo[inn]")
keys.push(type); // add type at the end
return keys;
},
// Returns [name-without-type, type] from name.
// "foo" => ["foo", '_']
// "foo:boolean" => ["foo", 'boolean']
// "foo[bar]:null" => ["foo[bar]", 'null']
extractTypeFromInputName: function(name, opts) {
var match, validTypes, f;
if (match = name.match(/(.*):([^:]+)$/)){
f = $.serializeJSON;
validTypes = f.optionKeys(opts ? opts.typeFunctions : f.defaultOptions.defaultTypes);
validTypes.push('skip'); // skip is a special type that makes it easy to remove
if (validTypes.indexOf(match[2]) !== -1) {
return [match[1], match[2]];
} else {
throw new Error("serializeJSON ERROR: Invalid type " + match[2] + " found in input name '" + name + "', please use one of " + validTypes.join(', '));
}
} else {
return [name, '_']; // no defined type, then use parse options
}
},
// Set a value in an object or array, using multiple keys to set in a nested object or array:
//
// deepSet(obj, ['foo'], v) // obj['foo'] = v
// deepSet(obj, ['foo', 'inn'], v) // obj['foo']['inn'] = v // Create the inner obj['foo'] object, if needed
// deepSet(obj, ['foo', 'inn', '123'], v) // obj['foo']['arr']['123'] = v //
//
// deepSet(obj, ['0'], v) // obj['0'] = v
// deepSet(arr, ['0'], v, {useIntKeysAsArrayIndex: true}) // arr[0] = v
// deepSet(arr, [''], v) // arr.push(v)
// deepSet(obj, ['arr', ''], v) // obj['arr'].push(v)
//
// arr = [];
// deepSet(arr, ['', v] // arr => [v]
// deepSet(arr, ['', 'foo'], v) // arr => [v, {foo: v}]
// deepSet(arr, ['', 'bar'], v) // arr => [v, {foo: v, bar: v}]
// deepSet(arr, ['', 'bar'], v) // arr => [v, {foo: v, bar: v}, {bar: v}]
//
deepSet: function (o, keys, value, opts) {
var key, nextKey, tail, lastIdx, lastVal, f;
if (opts == null) { opts = {}; }
f = $.serializeJSON;
if (f.isUndefined(o)) { throw new Error("ArgumentError: param 'o' expected to be an object or array, found undefined"); }
if (!keys || keys.length === 0) { throw new Error("ArgumentError: param 'keys' expected to be an array with least one element"); }
key = keys[0];
// Only one key, then it's not a deepSet, just assign the value.
if (keys.length === 1) {
if (key === '') {
o.push(value); // '' is used to push values into the array (assume o is an array)
} else {
o[key] = value; // other keys can be used as object keys or array indexes
}
// With more keys is a deepSet. Apply recursively.
} else {
nextKey = keys[1];
// '' is used to push values into the array,
// with nextKey, set the value into the same object, in object[nextKey].
// Covers the case of ['', 'foo'] and ['', 'var'] to push the object {foo, var}, and the case of nested arrays.
if (key === '') {
lastIdx = o.length - 1; // asume o is array
lastVal = o[lastIdx];
if (f.isObject(lastVal) && (f.isUndefined(lastVal[nextKey]) || keys.length > 2)) { // if nextKey is not present in the last object element, or there are more keys to deep set
key = lastIdx; // then set the new value in the same object element
} else {
key = lastIdx + 1; // otherwise, point to set the next index in the array
}
}
// '' is used to push values into the array "array[]"
if (nextKey === '') {
if (f.isUndefined(o[key]) || !$.isArray(o[key])) {
o[key] = []; // define (or override) as array to push values
}
} else {
if (opts.useIntKeysAsArrayIndex && f.isValidArrayIndex(nextKey)) { // if 1, 2, 3 ... then use an array, where nextKey is the index
if (f.isUndefined(o[key]) || !$.isArray(o[key])) {
o[key] = []; // define (or override) as array, to insert values using int keys as array indexes
}
} else { // for anything else, use an object, where nextKey is going to be the attribute name
if (f.isUndefined(o[key]) || !f.isObject(o[key])) {
o[key] = {}; // define (or override) as object, to set nested properties
}
}
}
// Recursively set the inner object
tail = keys.slice(1);
f.deepSet(o[key], tail, value, opts);
}
},
// Fill the formAsArray object with values for the unchecked checkbox inputs,
// using the same format as the jquery.serializeArray function.
// The value of the unchecked values is determined from the opts.checkboxUncheckedValue
// and/or the data-unchecked-value attribute of the inputs.
readCheckboxUncheckedValues: function (formAsArray, $form, opts) {
var selector, $uncheckedCheckboxes, $el, dataUncheckedValue, f;
if (opts == null) { opts = {}; }
f = $.serializeJSON;
selector = 'input[type=checkbox][name]:not(:checked):not([disabled])';
$uncheckedCheckboxes = $form.find(selector).add($form.filter(selector));
$uncheckedCheckboxes.each(function (i, el) {
$el = $(el);
dataUncheckedValue = $el.attr('data-unchecked-value');
if(dataUncheckedValue) { // data-unchecked-value has precedence over option opts.checkboxUncheckedValue
formAsArray.push({name: el.name, value: dataUncheckedValue});
} else {
if (!f.isUndefined(opts.checkboxUncheckedValue)) {
formAsArray.push({name: el.name, value: opts.checkboxUncheckedValue});
}
}
});
}
};
}));
|
/* ===========================================================
* es.js
* Spanish translation for Trumbowyg
* http://alex-d.github.com/Trumbowyg
* ===========================================================
* Author : Moisés Márquez
* Email : moises.marquez.g@gmail.com
*/
jQuery.trumbowyg.langs.es={viewHTML:"Ver HTML",formatting:"Formato",p:"Párrafo",blockquote:"Cita",code:"Código",header:"Título",bold:"Negrita",italic:"Cursiva",strikethrough:"Tachado",underline:"Subrayado",strong:"Negrita",em:"Énfasis",del:"Borrar",unorderedList:"Lista Desordenada",orderedList:"Lista Ordenada",insertImage:"Insertar una imagen",insertVideo:"Insertar un vídeo",link:"Enlace",createLink:"Insertar un enlace",unlink:"Suprimir un enlace",justifyLeft:"Izquierda",justifyCenter:"Centrar",justifyRight:"Derecha",justifyFull:"Justificado",horizontalRule:"Insertar separador horizontal",fullscreen:"Pantalla completa",close:"Cerrar",submit:"Enviar",reset:"Cancelar",required:"Obligatorio",description:"Descripción",title:"Título",text:"Texto"}; |
/**
* Module dependencies
*/
var should = require('should');
var $Sails = require('../helpers/sails');
describe('`sails.router`', function() {
var sails = $Sails.load.withAllHooksDisabled();
it('should be exposed on the `sails` global', function () {
sails
.router
._privateRouter
.routes
.should.be.ok;
});
});
|
/*!
* type-is
* Copyright(c) 2014 Jonathan Ong
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/
'use strict'
/**
* Module dependencies.
* @private
*/
var typer = require('media-typer')
var mime = require('mime-types')
/**
* Module exports.
* @public
*/
module.exports = typeofrequest
module.exports.is = typeis
module.exports.hasBody = hasbody
module.exports.normalize = normalize
module.exports.match = mimeMatch
/**
* Compare a `value` content-type with `types`.
* Each `type` can be an extension like `html`,
* a special shortcut like `multipart` or `urlencoded`,
* or a mime type.
*
* If no types match, `false` is returned.
* Otherwise, the first `type` that matches is returned.
*
* @param {String} value
* @param {Array} types
* @public
*/
function typeis(value, types_) {
var i
var types = types_
// remove parameters and normalize
var val = tryNormalizeType(value)
// no type or invalid
if (!val) {
return false
}
// support flattened arguments
if (types && !Array.isArray(types)) {
types = new Array(arguments.length - 1)
for (i = 0; i < types.length; i++) {
types[i] = arguments[i + 1]
}
}
// no types, return the content type
if (!types || !types.length) {
return val
}
var type
for (i = 0; i < types.length; i++) {
if (mimeMatch(normalize(type = types[i]), val)) {
return type[0] === '+' || type.indexOf('*') !== -1
? val
: type
}
}
// no matches
return false
}
/**
* Check if a request has a request body.
* A request with a body __must__ either have `transfer-encoding`
* or `content-length` headers set.
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3
*
* @param {Object} request
* @return {Boolean}
* @public
*/
function hasbody(req) {
return req.headers['transfer-encoding'] !== undefined
|| !isNaN(req.headers['content-length'])
}
/**
* Check if the incoming request contains the "Content-Type"
* header field, and it contains any of the give mime `type`s.
* If there is no request body, `null` is returned.
* If there is no content type, `false` is returned.
* Otherwise, it returns the first `type` that matches.
*
* Examples:
*
* // With Content-Type: text/html; charset=utf-8
* this.is('html'); // => 'html'
* this.is('text/html'); // => 'text/html'
* this.is('text/*', 'application/json'); // => 'text/html'
*
* // When Content-Type is application/json
* this.is('json', 'urlencoded'); // => 'json'
* this.is('application/json'); // => 'application/json'
* this.is('html', 'application/*'); // => 'application/json'
*
* this.is('html'); // => false
*
* @param {String|Array} types...
* @return {String|false|null}
* @public
*/
function typeofrequest(req, types_) {
var types = types_
// no body
if (!hasbody(req)) {
return null
}
// support flattened arguments
if (arguments.length > 2) {
types = new Array(arguments.length - 1)
for (var i = 0; i < types.length; i++) {
types[i] = arguments[i + 1]
}
}
// request content type
var value = req.headers['content-type']
return typeis(value, types)
}
/**
* Normalize a mime type.
* If it's a shorthand, expand it to a valid mime type.
*
* In general, you probably want:
*
* var type = is(req, ['urlencoded', 'json', 'multipart']);
*
* Then use the appropriate body parsers.
* These three are the most common request body types
* and are thus ensured to work.
*
* @param {String} type
* @private
*/
function normalize(type) {
switch (type) {
case 'urlencoded':
return 'application/x-www-form-urlencoded'
case 'multipart':
return 'multipart/*'
}
if (type[0] === '+') {
// "+json" -> "*/*+json" expando
return '*/*' + type
}
return type.indexOf('/') === -1
? mime.lookup(type)
: type
}
/**
* Check if `exected` mime type
* matches `actual` mime type with
* wildcard and +suffix support.
*
* @param {String} expected
* @param {String} actual
* @return {Boolean}
* @private
*/
function mimeMatch(expected, actual) {
// invalid type
if (expected === false) {
return false
}
// split types
var actualParts = actual.split('/')
var expectedParts = expected.split('/')
// invalid format
if (actualParts.length !== 2 || expectedParts.length !== 2) {
return false
}
// validate type
if (expectedParts[0] !== '*' && expectedParts[0] !== actualParts[0]) {
return false
}
// validate suffix wildcard
if (expectedParts[1].substr(0, 2) === '*+') {
return expectedParts[1].length <= actualParts[1].length + 1
&& expectedParts[1].substr(1) === actualParts[1].substr(1 - expectedParts[1].length)
}
// validate subtype
if (expectedParts[1] !== '*' && expectedParts[1] !== actualParts[1]) {
return false
}
return true
}
/**
* Normalize a type and remove parameters.
*
* @param {string} value
* @return {string}
* @private
*/
function normalizeType(value) {
// parse the type
var type = typer.parse(value)
// remove the parameters
type.parameters = undefined
// reformat it
return typer.format(type)
}
/**
* Try to normalize a type and remove parameters.
*
* @param {string} value
* @return {string}
* @private
*/
function tryNormalizeType(value) {
try {
return normalizeType(value)
} catch (err) {
return null
}
}
|
if (Meteor.isClient) {
// counter starts at 0
Session.setDefault('counter', 0);
Template.hello.helpers({
counter: function () {
return Session.get('counter');
}
});
Template.hello.events({
'click button': function () {
// increment the counter when button is clicked
Session.set('counter', Session.get('counter') + 1);
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
|
"use strict";!function(e){void 0===e.cs&&(e.cs={"mejs.plural-form":8,"mejs.download-file":"Stáhnout soubor","mejs.fullscreen":"Celá obrazovka","mejs.play":"Přehrát","mejs.pause":"Pozastavit","mejs.time-slider":"Posuvný běžec nastavení času","mejs.time-help-text":"Použijte tlačítka se šipkami doleva / doprava pro posun o jednu vteřinu, tlačítka se šipkami nahoru / dolů pro posun o deset vteřin.","mejs.volume-help-text":"Použijte tlačítka se šipkami nahoru / dolů pro zesílení nebo zeslabení hlasitosti.","mejs.unmute":"Zapnout zvuk","mejs.mute":"Vypnout zvuk","mejs.volume-slider":"Posuvný běžec nastavení hlasitosti","mejs.video-player":"Přehrávač videa","mejs.audio-player":"Přehrávač hudby","mejs.captions-subtitles":"Titulky","mejs.none":"Žádný"})}(mejs.i18n); |
CKEDITOR.plugins.setLang("div","sl",{IdInputLabel:"Id",advisoryTitleInputLabel:"Predlagani naslov",cssClassInputLabel:"Razredi slogovne predloge",edit:"Uredi div",inlineStyleInputLabel:"Slog v vrstici",langDirLTRLabel:"Od leve proti desni (LTR)",langDirLabel:"Smer jezika",langDirRTLLabel:"Od desne proti levi (RTL)",languageCodeInputLabel:"Koda jezika",remove:"Odstrani div",styleSelectLabel:"Slog",title:"Ustvari vsebnik div",toolbar:"Ustvari vsebnik div"}); |
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
/*global require*/
//Only Chrome uses this file.
var isChrome = window.webkitRequestFileSystem && window.webkitResolveLocalFileSystemURL;
if (!isChrome) {
return;
}
var channel = require('cordova/channel');
var FileError = require('./FileError');
var PERSISTENT_FS_QUOTA = 5 * 1024 * 1024;
var filePluginIsReadyEvent = new Event('filePluginIsReady');
var entryFunctionsCreated = false;
var quotaWasRequested = false;
var eventWasThrown = false;
if (!window.requestFileSystem) {
window.requestFileSystem = function(type, size, win, fail) {
if (fail) {
fail("Not supported");
}
};
} else {
window.requestFileSystem(window.TEMPORARY, 1, createFileEntryFunctions, function() {});
}
if (!window.resolveLocalFileSystemURL) {
window.resolveLocalFileSystemURL = function(url, win, fail) {
if(fail) {
fail("Not supported");
}
};
}
// Resolves a filesystem entry by its path - which is passed either in standard (filesystem:file://) or
// Cordova-specific (cdvfile://) universal way.
// Aligns with specification: http://www.w3.org/TR/2011/WD-file-system-api-20110419/#widl-LocalFileSystem-resolveLocalFileSystemURL
var nativeResolveLocalFileSystemURL = window.resolveLocalFileSystemURL || window.webkitResolveLocalFileSystemURL;
window.resolveLocalFileSystemURL = function(url, win, fail) {
/* If url starts with `cdvfile` then we need convert it to Chrome real url first:
cdvfile://localhost/persistent/path/to/file -> filesystem:file://persistent/path/to/file */
if (url.trim().substr(0,7) === "cdvfile") {
/* Quirk:
Plugin supports cdvfile://localhost (local resources) only.
I.e. external resources are not supported via cdvfile. */
if (url.indexOf("cdvfile://localhost") !== -1) {
// Browser supports temporary and persistent only
var indexPersistent = url.indexOf('persistent');
var indexTemporary = url.indexOf('temporary');
/* Chrome urls start with 'filesystem:' prefix. See quirk:
toURL function in Chrome returns filesystem:-prefixed path depending on application host.
For example, filesystem:file:///persistent/somefile.txt,
filesystem:http://localhost:8080/persistent/somefile.txt. */
var prefix = 'filesystem:file:///';
if (location.protocol !== 'file:') {
prefix = 'filesystem:' + location.origin + '/';
}
var result;
if (indexPersistent !== -1) {
// cdvfile://localhost/persistent/path/to/file -> filesystem:file://persistent/path/to/file
// or filesystem:http://localhost:8080/persistent/path/to/file
result = prefix + 'persistent' + url.substr(indexPersistent + 10);
nativeResolveLocalFileSystemURL(result, win, fail);
return;
}
if (indexTemporary !== -1) {
// cdvfile://localhost/temporary/path/to/file -> filesystem:file://temporary/path/to/file
// or filesystem:http://localhost:8080/temporary/path/to/file
result = prefix + 'temporary' + url.substr(indexTemporary + 9);
nativeResolveLocalFileSystemURL(result, win, fail);
return;
}
}
// cdvfile other than local file resource is not supported
fail && fail(new FileError(FileError.ENCODING_ERR));
} else {
nativeResolveLocalFileSystemURL(url, win, fail);
}
};
function createFileEntryFunctions(fs) {
fs.root.getFile('todelete_658674_833_4_cdv', {create: true}, function(fileEntry) {
var fileEntryType = Object.getPrototypeOf(fileEntry);
var entryType = Object.getPrototypeOf(fileEntryType);
// Save the original method
var origToURL = entryType.toURL;
entryType.toURL = function () {
var origURL = origToURL.call(this);
if (this.isDirectory && origURL.substr(-1) !== '/') {
return origURL + '/';
}
return origURL;
};
entryType.toNativeURL = function () {
console.warn("DEPRECATED: Update your code to use 'toURL'");
return this.toURL();
};
entryType.toInternalURL = function() {
if (this.toURL().indexOf("persistent") > -1) {
return "cdvfile://localhost/persistent" + this.fullPath;
}
if (this.toURL().indexOf("temporary") > -1) {
return "cdvfile://localhost/temporary" + this.fullPath;
}
};
entryType.setMetadata = function(win, fail /*, metadata*/) {
fail && fail("Not supported");
};
fileEntry.createWriter(function(writer) {
var originalWrite = writer.write;
var writerProto = Object.getPrototypeOf(writer);
writerProto.write = function(blob) {
if(blob instanceof Blob) {
originalWrite.apply(this, [blob]);
} else {
var realBlob = new Blob([blob]);
originalWrite.apply(this, [realBlob]);
}
};
fileEntry.remove(function(){ entryFunctionsCreated = true; }, function(){ /* empty callback */ });
});
});
}
window.initPersistentFileSystem = function(size, win, fail) {
if (navigator.webkitPersistentStorage) {
navigator.webkitPersistentStorage.requestQuota(size, win, fail);
return;
}
fail("This browser does not support this function");
};
window.isFilePluginReadyRaised = function () { return eventWasThrown; };
window.initPersistentFileSystem(PERSISTENT_FS_QUOTA, function() {
console.log('Persistent fs quota granted');
quotaWasRequested = true;
}, function(e){
console.log('Error occured while trying to request Persistent fs quota: ' + JSON.stringify(e));
});
channel.onCordovaReady.subscribe(function () {
function dispatchEventIfReady() {
if (entryFunctionsCreated && quotaWasRequested) {
window.dispatchEvent(filePluginIsReadyEvent);
eventWasThrown = true;
} else {
setTimeout(dispatchEventIfReady, 100);
}
}
dispatchEventIfReady();
}, false);
|
// Copyright 2010 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Displays and edits the value of a cookie.
* Intended only for debugging.
*/
goog.provide('goog.ui.CookieEditor');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.events.EventType');
goog.require('goog.net.cookies');
goog.require('goog.string');
goog.require('goog.style');
goog.require('goog.ui.Component');
/**
* Displays and edits the value of a cookie.
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
* @constructor
* @extends {goog.ui.Component}
*/
goog.ui.CookieEditor = function(opt_domHelper) {
goog.base(this, opt_domHelper);
};
goog.inherits(goog.ui.CookieEditor, goog.ui.Component);
/**
* Cookie key.
* @type {?string}
* @private
*/
goog.ui.CookieEditor.prototype.cookieKey_;
/**
* Text area.
* @type {HTMLTextAreaElement}
* @private
*/
goog.ui.CookieEditor.prototype.textAreaElem_;
/**
* Clear button.
* @type {HTMLButtonElement}
* @private
*/
goog.ui.CookieEditor.prototype.clearButtonElem_;
/**
* Invalid value warning text.
* @type {HTMLSpanElement}
* @private
*/
goog.ui.CookieEditor.prototype.valueWarningElem_;
/**
* Update button.
* @type {HTMLButtonElement}
* @private
*/
goog.ui.CookieEditor.prototype.updateButtonElem_;
// TODO(user): add combobox for user to select different cookies
/**
* Sets the cookie which this component will edit.
* @param {string} cookieKey Cookie key.
*/
goog.ui.CookieEditor.prototype.selectCookie = function(cookieKey) {
goog.asserts.assert(goog.net.cookies.isValidName(cookieKey));
this.cookieKey_ = cookieKey;
if (this.textAreaElem_) {
this.textAreaElem_.value = goog.net.cookies.get(cookieKey) || '';
}
};
/** @override */
goog.ui.CookieEditor.prototype.canDecorate = function() {
return false;
};
/** @override */
goog.ui.CookieEditor.prototype.createDom = function() {
// Debug-only, so we don't need i18n.
this.clearButtonElem_ = /** @type {HTMLButtonElement} */ (goog.dom.createDom(
goog.dom.TagName.BUTTON, /* attributes */ null, 'Clear'));
this.updateButtonElem_ = /** @type {HTMLButtonElement} */ (goog.dom.createDom(
goog.dom.TagName.BUTTON, /* attributes */ null, 'Update'));
var value = this.cookieKey_ && goog.net.cookies.get(this.cookieKey_);
this.textAreaElem_ = /** @type {HTMLTextAreaElement} */ (goog.dom.createDom(
goog.dom.TagName.TEXTAREA, /* attibutes */ null, value || ''));
this.valueWarningElem_ = /** @type {HTMLSpanElement} */ (goog.dom.createDom(
goog.dom.TagName.SPAN, /* attibutes */ {
'style': 'display:none;color:red'
}, 'Invalid cookie value.'));
this.setElementInternal(goog.dom.createDom(goog.dom.TagName.DIV,
/* attibutes */ null,
this.valueWarningElem_,
goog.dom.createDom(goog.dom.TagName.BR),
this.textAreaElem_,
goog.dom.createDom(goog.dom.TagName.BR),
this.clearButtonElem_,
this.updateButtonElem_));
};
/** @override */
goog.ui.CookieEditor.prototype.enterDocument = function() {
goog.base(this, 'enterDocument');
this.getHandler().listen(this.clearButtonElem_,
goog.events.EventType.CLICK,
this.handleClear_);
this.getHandler().listen(this.updateButtonElem_,
goog.events.EventType.CLICK,
this.handleUpdate_);
};
/**
* Handles user clicking clear button.
* @param {!goog.events.Event} e The click event.
* @private
*/
goog.ui.CookieEditor.prototype.handleClear_ = function(e) {
if (this.cookieKey_) {
goog.net.cookies.remove(this.cookieKey_);
}
this.textAreaElem_.value = '';
};
/**
* Handles user clicking update button.
* @param {!goog.events.Event} e The click event.
* @private
*/
goog.ui.CookieEditor.prototype.handleUpdate_ = function(e) {
if (this.cookieKey_) {
var value = this.textAreaElem_.value;
if (value) {
// Strip line breaks.
value = goog.string.stripNewlines(value);
}
if (goog.net.cookies.isValidValue(value)) {
goog.net.cookies.set(this.cookieKey_, value);
goog.style.showElement(this.valueWarningElem_, false);
} else {
goog.style.showElement(this.valueWarningElem_, true);
}
}
};
/** @override */
goog.ui.CookieEditor.prototype.disposeInternal = function() {
this.clearButtonElem_ = null;
this.cookieKey_ = null;
this.textAreaElem_ = null;
this.updateButtonElem_ = null;
this.valueWarningElem_ = null;
};
|
(function (api, window, document){
var
encode = window.encodeURIComponent,
FormData = window.FormData,
Form = function (){
this.items = [];
}
;
Form.prototype = {
append: function (name, blob, file, type){
this.items.push({
name: name
, blob: blob && blob.blob || (blob == void 0 ? '' : blob)
, file: blob && (file || blob.name)
, type: blob && (type || blob.type)
});
},
each: function (fn){
var i = 0, n = this.items.length;
for( ; i < n; i++ ){
fn.call(this, this.items[i]);
}
},
toData: function (fn, options){
// allow chunked transfer if we have only one file to send
// flag is used below and in XHR._send
options._chunked = api.support.chunked && options.chunkSize > 0 && api.filter(this.items, function (item){ return item.file; }).length == 1;
if( !api.support.html5 ){
api.log('FileAPI.Form.toHtmlData');
this.toHtmlData(fn);
}
else if( this.multipart || !FormData ){
api.log('FileAPI.Form.toMultipartData');
this.toMultipartData(fn);
}
else if( options._chunked ){
api.log('FileAPI.Form.toPlainData');
this.toPlainData(fn);
}
else {
api.log('FileAPI.Form.toFormData');
this.toFormData(fn);
}
},
_to: function (data, complete, next, arg){
var queue = api.queue(function (){
complete(data);
});
this.each(function (file){
next(file, data, queue, arg);
});
queue.check();
},
toHtmlData: function (fn){
this._to(document.createDocumentFragment(), fn, function (file, data/**DocumentFragment*/){
var blob = file.blob, hidden;
if( file.file ){
api.reset(blob);
// set new name
blob.name = file.name;
data.appendChild(blob);
}
else {
hidden = document.createElement('input');
hidden.name = file.name;
hidden.type = 'hidden';
hidden.value = blob;
data.appendChild(hidden);
}
});
},
toPlainData: function (fn){
this._to({}, fn, function (file, data, queue){
if( file.file ){
data.type = file.file;
}
if( file.blob.toBlob ){
// canvas
queue.inc();
file.blob.toBlob(function (blob){
data.name = file.name;
data.file = blob;
data.size = blob.length;
data.type = file.type;
queue.next();
}, 'image/png');
}
else if( file.file ){
//file
data.name = file.blob.name;
data.file = file.blob;
data.size = file.blob.size;
data.type = file.type;
}
else {
// additional data
if (!data.params) {
data.params = [];
}
data.params.push(encodeURIComponent(file.name) + "=" + encodeURIComponent(file.blob));
}
data.start = -1;
data.end = data.file.FileAPIReadPosition || -1;
data.retry = 0;
});
},
toFormData: function (fn){
this._to(new FormData, fn, function (file, data, queue){
if( file.file ){
data.append('_'+file.name, file.file);
}
if( file.blob && file.blob.toBlob ){
queue.inc();
file.blob.toBlob(function (blob){
data.append(file.name, blob, file.file);
queue.next();
}, 'image/png');
}
else if( file.file ){
data.append(file.name, file.blob, file.file);
}
else {
data.append(file.name, file.blob);
}
});
},
toMultipartData: function (fn){
this._to([], fn, function (file, data, queue, boundary){
var
isFile = !!file.file
, blob = file.blob
, done = function (blob){
data.push(
'--_' + boundary + ('\r\nContent-Disposition: form-data; name="'+ file.name +'"'+ (isFile ? '; filename="'+ encode(file.file) +'"' : '')
+ (isFile ? '\r\nContent-Type: '+ (file.type || 'application/octet-stream') : '')
+ '\r\n'
+ '\r\n'+ (isFile ? blob : encode(blob))
+ '\r\n')
);
queue.next();
}
;
queue.inc();
if( api.isFile(blob) ){
api.readAsBinaryString(blob, function (evt/**Object*/){
if( evt.type == 'load' ){
done(evt.result);
}
});
}
else {
done(blob);
}
}, api.expando);
}
};
// @export
api.Form = Form;
})(FileAPI, window, document);
|
/**
* @fileoverview An inherited `glob.GlobSync` to support .gitignore patterns.
* @author Kael Zhang
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const Sync = require("glob").GlobSync,
util = require("util");
//------------------------------------------------------------------------------
// Private
//------------------------------------------------------------------------------
const IGNORE = typeof Symbol === "function" ? Symbol("ignore") : "_shouldIgnore";
/**
* Subclass of `glob.GlobSync`
* @param {string} pattern Pattern to be matched.
* @param {Object} options `options` for `glob`
* @param {function()} shouldIgnore Method to check whether a directory should be ignored.
* @constructor
*/
function GlobSync(pattern, options, shouldIgnore) {
/**
* We don't put this thing to argument `options` to avoid
* further problems, such as `options` validation.
*
* Use `Symbol` as much as possible to avoid confliction.
*/
this[IGNORE] = shouldIgnore;
Sync.call(this, pattern, options);
}
util.inherits(GlobSync, Sync);
/* eslint no-underscore-dangle: ["error", { "allow": ["_readdir", "_mark"] }] */
GlobSync.prototype._readdir = function(abs, inGlobStar) {
/**
* `options.nodir` makes `options.mark` as `true`.
* Mark `abs` first
* to make sure `"node_modules"` will be ignored immediately with ignore pattern `"node_modules/"`.
* There is a built-in cache about marked `File.Stat` in `glob`, so that we could not worry about the extra invocation of `this._mark()`
*/
const marked = this._mark(abs);
if (this[IGNORE](marked)) {
return null;
}
return Sync.prototype._readdir.call(this, abs, inGlobStar);
};
module.exports = GlobSync;
|
//! moment.js locale configuration
//! locale : Welsh (cy)
//! author : Robert Allen
;(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined'
&& typeof require === 'function' ? factory(require('../moment')) :
typeof define === 'function' && define.amd ? define(['moment'], factory) :
factory(global.moment)
}(this, function (moment) { 'use strict';
var cy = moment.defineLocale('cy', {
months: 'Ionawr_Chwefror_Mawrth_Ebrill_Mai_Mehefin_Gorffennaf_Awst_Medi_Hydref_Tachwedd_Rhagfyr'.split('_'),
monthsShort: 'Ion_Chwe_Maw_Ebr_Mai_Meh_Gor_Aws_Med_Hyd_Tach_Rhag'.split('_'),
weekdays: 'Dydd Sul_Dydd Llun_Dydd Mawrth_Dydd Mercher_Dydd Iau_Dydd Gwener_Dydd Sadwrn'.split('_'),
weekdaysShort: 'Sul_Llun_Maw_Mer_Iau_Gwe_Sad'.split('_'),
weekdaysMin: 'Su_Ll_Ma_Me_Ia_Gw_Sa'.split('_'),
// time formats are the same as en-gb
longDateFormat: {
LT: 'HH:mm',
LTS : 'HH:mm:ss',
L: 'DD/MM/YYYY',
LL: 'D MMMM YYYY',
LLL: 'D MMMM YYYY HH:mm',
LLLL: 'dddd, D MMMM YYYY HH:mm'
},
calendar: {
sameDay: '[Heddiw am] LT',
nextDay: '[Yfory am] LT',
nextWeek: 'dddd [am] LT',
lastDay: '[Ddoe am] LT',
lastWeek: 'dddd [diwethaf am] LT',
sameElse: 'L'
},
relativeTime: {
future: 'mewn %s',
past: '%s yn ôl',
s: 'ychydig eiliadau',
m: 'munud',
mm: '%d munud',
h: 'awr',
hh: '%d awr',
d: 'diwrnod',
dd: '%d diwrnod',
M: 'mis',
MM: '%d mis',
y: 'blwyddyn',
yy: '%d flynedd'
},
ordinalParse: /\d{1,2}(fed|ain|af|il|ydd|ed|eg)/,
// traditional ordinal numbers above 31 are not commonly used in colloquial Welsh
ordinal: function (number) {
var b = number,
output = '',
lookup = [
'', 'af', 'il', 'ydd', 'ydd', 'ed', 'ed', 'ed', 'fed', 'fed', 'fed', // 1af to 10fed
'eg', 'fed', 'eg', 'eg', 'fed', 'eg', 'eg', 'fed', 'eg', 'fed' // 11eg to 20fed
];
if (b > 20) {
if (b === 40 || b === 50 || b === 60 || b === 80 || b === 100) {
output = 'fed'; // not 30ain, 70ain or 90ain
} else {
output = 'ain';
}
} else if (b > 0) {
output = lookup[b];
}
return number + output;
},
week : {
dow : 1, // Monday is the first day of the week.
doy : 4 // The week that contains Jan 4th is the first week of the year.
}
});
return cy;
})); |
/* version: 0.3.31, born: 17-10-2014 23:48 */
var Organic = (function(w){
var o = {
helpers: {},
lib: {
atoms: {},
molecules: {}
}
}
var require = function(v) {
if(v == "./helpers/extend") {
return o.helpers.extend;
} else if(v == "/helpers/snippets" || v == "../../helpers/snippets") {
return o.helpers.snippets;
} else if(v == "/lib/atoms/atoms" || v == "../../lib/atoms/atoms" || v == "../atoms/atoms.js") {
return o.lib.atoms.atoms;
} else if(v == "../../helpers/units") {
return o.helpers.units;
} else if(v == "../../helpers/args") {
return o.helpers.args;
} else if(v == "path") {
return {
basename: function(f) {
return f.split("/").pop();
}
}
} else {
var moduleParts = v.split("/");
return (function getModule(currentModule) {
var part = moduleParts.shift().replace(".js", "");
if(currentModule[part]) {
if(moduleParts.length == 0) {
return currentModule[part];
} else {
return getModule(currentModule[part])
}
}
})(o);
}
}
var __dirname = '';
var walkClientSide = function(res, obj, path) {
if(typeof res == 'undefined') res = [];
if(typeof obj == 'undefined') obj = o.lib;
if(typeof path == 'undefined') path = "lib/";
for(var part in obj) {
if(typeof obj[part] == 'function') {
res.push(path + part + ".js");
} else {
walkClientSide(res, obj[part], path + part + "/");
}
}
return res;
};o.helpers.args = function(value) {
value = value.toString().replace(/\/ /g, '/').split('/');
return value;
}
o.helpers.extend = function() {
var process = function(destination, source) {
for (var key in source) {
if (hasOwnProperty.call(source, key)) {
destination[key] = source[key];
}
}
return destination;
};
var result = arguments[0];
for(var i=1; i<arguments.length; i++) {
result = process(result, arguments[i]);
}
return result;
}
o.helpers.snippets = function() {
// http://peters-playground.com/Emmet-Css-Snippets-for-Sublime-Text-2/
return {
// Visual Formatting
"pos": "position",
"pos:s": "position:static",
"pos:a": "position:absolute",
"pos:r": "position:relative",
"pos:f": "position:fixed",
// "top": "top",
"t:a": "top:auto",
"rig": "right",
"r:a": "right:auto",
"bot": "bottom",
// "b:a": "bottom:auto", // breaks the multiple comma selectors
"lef": "left",
"l:a": "left:auto",
"zin": "z-index",
"z:a": "z-index:auto",
"fl": "float",
"fl:n": "float:none",
"fl:l": "float:left",
"fl:r": "float:right",
"cl": "clear",
"cl:n": "clear:none",
"cl:l": "clear:left",
"cl:r": "clear:right",
"cl:b": "clear:both",
"dis": "display",
"d:n": "display:none",
"d:b": "display:block",
"d:i": "display:inline",
"d:ib": "display:inline-block",
"d:li": "display:list-item",
"d:ri": "display:run-in",
"d:cp": "display:compact",
"d:tb": "display:table",
"d:itb": "display:inline-table",
"d:tbcp": "display:table-caption",
"d:tbcl": "display:table-column",
"d:tbclg": "display:table-column-group",
"d:tbhg": "display:table-header-group",
"d:tbfg": "display:table-footer-group",
"d:tbr": "display:table-row",
"d:tbrg": "display:table-row-group",
"d:tbc": "display:table-cell",
"d:rb": "display:ruby",
"d:rbb": "display:ruby-base",
"d:rbbg": "display:ruby-base-group",
"d:rbt": "display:ruby-text",
"d:rbtg": "display:ruby-text-group",
"vis": "visibility",
"v:v": "visibility:visible",
"v:h": "visibility:hidden",
"v:c": "visibility:collapse",
"ov": "overflow",
"ov:v": "overflow:visible",
"ov:h": "overflow:hidden",
"ov:s": "overflow:scroll",
"ov:a": "overflow:auto",
"ovx": "overflow-x",
"ovx:v": "overflow-x:visible",
"ovx:h": "overflow-x:hidden",
"ovx:s": "overflow-x:scroll",
"ovx:a": "overflow-x:auto",
"ovy": "overflow-y",
"ovy:v": "overflow-y:visible",
"ovy:h": "overflow-y:hidden",
"ovy:s": "overflow-y:scroll",
"ovy:a": "overflow-y:auto",
"ovs": "overflow-style",
"ovs:a": "overflow-style:auto",
"ovs:s": "overflow-style:scrollbar",
"ovs:p": "overflow-style:panner",
"ovs:m": "overflow-style:move",
"ovs:mq": "overflow-style:marquee",
"zoo": "zoom:1",
"cp": "clip",
"cp:a": "clip:auto",
"cp:r": "clip:rect()",
"rz": "resize",
"rz:n": "resize:none",
"rz:b": "resize:both",
"rz:h": "resize:horizontal",
"rz:v": "resize:vertical",
"cur": "cursor",
"cur:a": "cursor:auto",
"cur:d": "cursor:default",
"cur:c": "cursor:crosshair",
"cur:ha": "cursor:hand",
"cur:he": "cursor:help",
"cur:m": "cursor:move",
"cur:p": "cursor:pointer",
"cur:t": "cursor:text",
// Margin & Padding
"mar": "margin",
"m:au": "margin:0 auto",
"mt": "margin-top",
"mt:a": "margin-top:auto",
"mr": "margin-right",
"mr:a": "margin-right:auto",
"mb": "margin-bottom",
"mb:a": "margin-bottom:auto",
"ml": "margin-left",
"ml:a": "margin-left:auto",
"pad": "padding",
"pt": "padding-top",
"pr": "padding-right",
"pb": "padding-bottom",
"pl": "padding-left",
// Box Sizing
"bxz": "box-sizing",
"bxz:cb": "box-sizing:content-box",
"bxz:bb": "box-sizing:border-box",
"bxsh": "box-shadow",
"bxsh:n": "box-shadow:none",
"bxsh+": "box-shadow:0 0 0 #000",
"wid": "width",
"w:a": "width:auto",
"hei": "height",
"h:a": "height:auto",
"maw": "max-width",
"maw:n": "max-width:none",
"mah": "max-height",
"mah:n": "max-height:none",
"miw": "min-width",
"mih": "min-height",
// Font
"fon": "font",
"fon+": "font:1em Arial, sans-serif",
"fw": "font-weight",
"fw:n": "font-weight:normal",
"fw:b": "font-weight:bold",
"fw:br": "font-weight:bolder",
"fw:lr": "font-weight:lighter",
"fs": "font-style",
"fs:n": "font-style:normal",
"fs:i": "font-style:italic",
"fs:o": "font-style:oblique",
"fv": "font-variant",
"fv:n": "font-variant:normal",
"fv:sc": "font-variant:small-caps",
"fz": "font-size",
"fza": "font-size-adjust",
"fza:n": "font-size-adjust:none",
"ff": "font-family",
"ff:s": "font-family:serif",
"ff:ss": "font-family:sans-serif",
"ff:c": "font-family:cursive",
"ff:f": "font-family:fantasy",
"ff:m": "font-family:monospace",
"fef": "font-effect",
"fef:n": "font-effect:none",
"fef:eg": "font-effect:engrave",
"fef:eb": "font-effect:emboss",
"fef:o": "font-effect:outline",
"fem": "font-emphasize",
"femp": "font-emphasize-position",
"femp:b": "font-emphasize-position:before",
"femp:a": "font-emphasize-position:after",
"fems": "font-emphasize-style",
"fems:n": "font-emphasize-style:none",
"fems:ac": "font-emphasize-style:accent",
"fems:dt": "font-emphasize-style:dot",
"fems:c": "font-emphasize-style:circle",
"fems:ds": "font-emphasize-style:disc",
"fsm": "font-smooth",
"fsm:au": "font-smooth:auto",
"fsm:n": "font-smooth:never",
"fsm:al": "font-smooth:always",
"fst": "font-stretch",
"fst:n": "font-stretch:normal",
"fst:uc": "font-stretch:ultra-condensed",
"fst:ec": "font-stretch:extra-condensed",
"fst:c": "font-stretch:condensed",
"fst:sc": "font-stretch:semi-condensed",
"fst:se": "font-stretch:semi-expanded",
"fst:e": "font-stretch:expanded",
"fst:ee": "font-stretch:extra-expanded",
"fst:ue": "font-stretch:ultra-expanded",
// Text
"va": "vertical-align",
"va:sup": "vertical-align:super",
"va:t": "vertical-align:top",
"va:tt": "vertical-align:text-top",
"va:m": "vertical-align:middle",
"va:bl": "vertical-align:baseline",
"va:b": "vertical-align:bottom",
"va:tb": "vertical-align:text-bottom",
"va:sub": "vertical-align:sub",
"ta": "text-align",
"ta:le": "text-align:left",
"ta:c": "text-align:center",
"ta:r": "text-align:right",
"ta:j": "text-align:justify",
"tal": "text-align-last",
"tal:a": "text-align-last:auto",
"tal:l": "text-align-last:left",
"tal:c": "text-align-last:center",
"tal:r": "text-align-last:right",
"ted": "text-decoration",
"ted:n": "text-decoration:none",
"ted:u": "text-decoration:underline",
"ted:o": "text-decoration:overline",
"ted:l": "text-decoration:line-through",
"te": "text-emphasis",
"te:n": "text-emphasis:none",
"te:ac": "text-emphasis:accent",
"te:dt": "text-emphasis:dot",
"te:c": "text-emphasis:circle",
"te:ds": "text-emphasis:disc",
"te:b": "text-emphasis:before",
"te:a": "text-emphasis:after",
"teh": "text-height",
"teh:a": "text-height:auto",
"teh:f": "text-height:font-size",
"teh:t": "text-height:text-size",
"teh:m": "text-height:max-size",
"ti": "text-indent",
"ti:-": "text-indent:-9999px",
"tj": "text-justify",
"tj:a": "text-justify:auto",
"tj:iw": "text-justify:inter-word",
"tj:ii": "text-justify:inter-ideograph",
"tj:ic": "text-justify:inter-cluster",
"tj:d": "text-justify:distribute",
"tj:k": "text-justify:kashida",
"tj:t": "text-justify:tibetan",
"tol": "text-outline",
"tol+": "text-outline:0 0 #000",
"tol:n": "text-outline:none",
// "tr": "text-replace",
// "tr:n": "text-replace:none",
"tt": "text-transform",
"tt:n": "text-transform:none",
"tt:c": "text-transform:capitalize",
"tt:u": "text-transform:uppercase",
"tt:l": "text-transform:lowercase",
"tw": "text-wrap",
"tw:n": "text-wrap:normal",
"tw:no": "text-wrap:none",
"tw:u": "text-wrap:unrestricted",
"tw:s": "text-wrap:suppress",
"tsh": "text-shadow",
"tsh+": "text-shadow:0 0 0 #000",
"tsh:n": "text-shadow:none",
"lh": "line-height",
"lts": "letter-spacing",
"whs": "white-space",
"whs:n": "white-space:normal",
"whs:p": "white-space:pre",
"whs:nw": "white-space:nowrap",
"whs:pw": "white-space:pre-wrap",
"whs:pl": "white-space:pre-line",
"whsc": "white-space-collapse",
"whsc:n": "white-space-collapse:normal",
"whsc:k": "white-space-collapse:keep-all",
"whsc:l": "white-space-collapse:loose",
"whsc:bs": "white-space-collapse:break-strict",
"whsc:ba": "white-space-collapse:break-all",
"wob": "word-break",
"wob:n": "word-break:normal",
"wob:k": "word-break:keep-all",
"wob:l": "word-break:loose",
"wob:bs": "word-break:break-strict",
"wob:ba": "word-break:break-all",
"wos": "word-spacing",
"wow": "word-wrap",
"wow:nm": "word-wrap:normal",
"wow:n": "word-wrap:none",
"wow:u": "word-wrap:unrestricted",
"wow:s": "word-wrap:suppress",
// Background
"bg": "background",
"bg+": "background:#fff url() 0 0 no-repeat",
"bg:n": "background:none",
"bgc": "background-color:#fff",
"bgc:t": "background-color:transparent",
"bgi": "background-image:url()",
"bgi:n": "background-image:none",
"bgr": "background-repeat",
"bgr:r": "background-repeat:repeat",
"bgr:n": "background-repeat:no-repeat",
"bgr:x": "background-repeat:repeat-x",
"bgr:y": "background-repeat:repeat-y",
"bga": "background-attachment",
"bga:f": "background-attachment:fixed",
"bga:s": "background-attachment:scroll",
"bgp": "background-position:0 0",
"bgpx": "background-position-x",
"bgpy": "background-position-y",
"bgbk": "background-break",
"bgbk:bb": "background-break:bounding-box",
"bgbk:eb": "background-break:each-box",
"bgbk:c": "background-break:continuous",
"bgcp": "background-clip",
"bgcp:bb": "background-clip:border-box",
"bgcp:pb": "background-clip:padding-box",
"bgcp:cb": "background-clip:content-box",
"bgcp:nc": "background-clip:no-clip",
"bgo": "background-origin",
"bgo:pb": "background-origin:padding-box",
"bgo:bb": "background-origin:border-box",
"bgo:cb": "background-origin:content-box",
"bgz": "background-size",
"bgz:a": "background-size:auto",
"bgz:ct": "background-size:contain",
"bgz:cv": "background-size:cover",
// Color
"col": "color:#000",
"op": "opacity",
"hsl": "hsl(359,100%,100%)",
"hsla": "hsla(359,100%,100%,0.5)",
"rgb": "rgb(255,255,255)",
"rgba": "rgba(255,255,255,0.5)",
// Generated Content
"ct": "content",
"ct:n": "content:normal",
"ct:oq": "content:open-quote",
"ct:noq": "content:no-open-quote",
"ct:cq": "content:close-quote",
"ct:ncq": "content:no-close-quote",
"ct:a": "content:attr()",
"ct:c": "content:counter()",
"ct:cs": "content:counters()",
"quo": "quotes",
"q:n": "quotes:none",
"q:ru": "quotes:'\00AB' '\00BB' '\201E' '\201C'",
"q:en": "quotes:'\201C' '\201D' '\2018' '\2019'",
"coi": "counter-increment",
"cor": "counter-reset",
// Outline
"out": "outline",
"o:n": "outline:none",
"oo": "outline-offset",
"ow": "outline-width",
"os": "outline-style",
"oc": "outline-color:#000",
"oc:i": "outline-color:invert",
// Table
"tbl": "table-layout",
"tbl:a": "table-layout:auto",
"tbl:f": "table-layout:fixed",
"cps": "caption-side",
"cps:t": "caption-side:top",
"cps:b": "caption-side:bottom",
"ec": "empty-cells",
"ec:s": "empty-cells:show",
"ec:h": "empty-cells:hide",
// Border
"bd": "border",
"bd+": "border:1px solid #000",
"bd:n": "border:none",
"bdbk": "border-break",
"bdbk:c": "border-break:close",
"bdcl": "border-collapse",
"bdcl:c": "border-collapse:collapse",
"bdcl:s": "border-collapse:separate",
"bdc": "border-color:#000",
// "bdi": "border-image:url()",
// "bdi:n": "border-image:none",
"bdti": "border-top-image:url()",
"bdti:n": "border-top-image:none",
"bdri": "border-right-image:url()",
"bdri:n": "border-right-image:none",
"bdbi": "border-bottom-image:url()",
"bdbi:n": "border-bottom-image:none",
"bdli": "border-left-image:url()",
"bdli:n": "border-left-image:none",
"bdci": "border-corner-image:url()",
"bdci:n": "border-corner-image:none",
"bdci:c": "border-corner-image:continue",
"bdtli": "border-top-left-image:url()",
"bdtli:n": "border-top-left-image:none",
"bdtli:c": "border-top-left-image:continue",
"bdtri": "border-top-right-image:url()",
"bdtri:n": "border-top-right-image:none",
"bdtri:c": "border-top-right-image:continue",
"bdbri": "border-bottom-right-image:url()",
"bdbri:n": "border-bottom-right-image:none",
"bdbri:c": "border-bottom-right-image:continue",
"bdbli": "border-bottom-left-image:url()",
"bdbli:n": "border-bottom-left-image:none",
"bdbli:c": "border-bottom-left-image:continue",
"bdf": "border-fit",
"bdf:c": "border-fit:clip",
"bdf:r": "border-fit:repeat",
"bdf:sc": "border-fit:scale",
"bdf:st": "border-fit:stretch",
"bdf:ow": "border-fit:overwrite",
"bdf:of": "border-fit:overflow",
"bdf:sp": "border-fit:space",
"bdlt": "border-length",
"bdlt:a": "border-length:auto",
"bdsp": "border-spacing",
"bds": "border-style",
"bds:n": "border-style:none",
"bds:h": "border-style:hidden",
"bds:dt": "border-style:dotted",
"bds:ds": "border-style:dashed",
"bds:s": "border-style:solid",
"bds:db": "border-style:double",
"bds:dd": "border-style:dot-dash",
"bds:ddd": "border-style:dot-dot-dash",
"bds:w": "border-style:wave",
"bds:g": "border-style:groove",
"bds:r": "border-style:ridge",
"bds:i": "border-style:inset",
"bds:o": "border-style:outset",
"bdw": "border-width",
"bdt": "border-top",
"bdt+": "border-top:1px solid #000",
"bdt:n": "border-top:none",
"bdtw": "border-top-width",
"bdts": "border-top-style",
"bdts:n": "border-top-style:none",
"bdtc": "border-top-color:#000",
"bdr": "border-right",
"bdr+": "border-right:1px solid #000",
"bdr:n": "border-right:none",
"bdrw": "border-right-width",
"bdrs": "border-right-style",
"bdrs:n": "border-right-style:none",
"bdrc": "border-right-color:#000",
"bdb": "border-bottom",
"bdb+": "border-bottom:1px solid #000",
"bdb:n": "border-bottom:none",
"bdbw": "border-bottom-width",
"bdbs": "border-bottom-style",
"bdbs:n": "border-bottom-style:none",
"bdbc": "border-bottom-color:#000",
"bdl": "border-left",
"bdl+": "border-left:1px solid #000",
"bdl:n": "border-left:none",
"bdlw": "border-left-width",
"bdls": "border-left-style",
"bdls:n": "border-left-style:none",
"bdlc": "border-left-color:#000",
"bdrsa": "border-radius",
"bdtrrs": "border-top-right-radius",
"bdtlrs": "border-top-left-radius",
"bdbrrs": "border-bottom-right-radius",
"bdblrs": "border-bottom-left-radius",
// Lists
"lis": "list-style",
"lis:n": "list-style:none",
"lisp": "list-style-position",
"lisp:i": "list-style-position:inside",
"lisp:o": "list-style-position:outside",
"list": "list-style-type",
"list:n": "list-style-type:none",
"list:d": "list-style-type:disc",
"list:c": "list-style-type:circle",
"list:s": "list-style-type:square",
"list:dc": "list-style-type:decimal",
"list:dclz": "list-style-type:decimal-leading-zero",
"list:lr": "list-style-type:lower-roman",
"list:ur": "list-style-type:upper-roman",
"lisi": "list-style-image",
"lisi:n": "list-style-image:none",
// Print
"pgbb": "page-break-before",
"pgbb:au": "page-break-before:auto",
"pgbb:al": "page-break-before:always",
"pgbb:l": "page-break-before:left",
"pgbb:r": "page-break-before:right",
"pgbi": "page-break-inside",
"pgbi:au": "page-break-inside:auto",
"pgbi:av": "page-break-inside:avoid",
"pgba": "page-break-after",
"pgba:au": "page-break-after:auto",
"pgba:al": "page-break-after:always",
"pgba:l": "page-break-after:left",
"pgba:r": "page-break-after:right",
"orp": "orphans",
"widows": "widows",
// Others
"ipt": "!important",
"ffa": "@font-family {<br> font-family:;<br> src:url();<br>}",
"ffa+": "@font-family {<br> font-family: 'FontName';<br> src: url('FileName.eot');<br> src: url('FileName.eot?#iefix') format('embedded-opentype'),<br> url('FileName.woff') format('woff'),<br> url('FileName.ttf') format('truetype'),<br> url('FileName.svg#FontName') format('svg');<br> font-style: normal;<br> font-weight: normal;<br>}",
"imp": "@import url()",
"mp": "@media print {}",
"bg:ie": "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='x.png',sizingMethod='crop')",
"op:ie": "filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=100)",
"op:ms": "-ms-filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=100)'",
"trf": "transform",
"trf:r": "transform:rotate(90deg)",
"trf:sc": "transform:scale(x,y)",
"trf:scx": "transform:scaleX(x)",
"trf:scy": "transform:scaleY(y)",
"trf:skx": "transform:skewX(90deg)",
"trf:sky": "transform:skewY(90deg)",
"trf:t": "transform:translate(x,y)",
"trf:tx": "transform:translateX(x)",
"trf:ty": "transform:translateY(y)",
"trs": "transition",
"trsde": "transition-delay",
"trsdu": "transition-duration",
"trsp": "transition-property",
"trstf": "transition-timing-function",
"ani": "animation",
"ann": "animation-name",
"adu": "animation-duration",
"atf": "animation-timing-function",
"ade": "animation-delay",
"aic": "animation-iteration-count",
"adi": "animation-direction",
"aps": "animation-play-state",
"key": "@keyframes {}",
"ms": "@media screen and () {}",
"in": "inherit",
"tra": "transparent",
"beh": "behavior:url()",
"cha": "@charset''",
// Pseudo Class
"ac": " :active{}",
"ac:a": "&:active{}",
"af": " :after{}",
"af:a": "&:after{}",
"be": " :before{}",
"be:a": "&:before{}",
"ch": " :checked{}",
"ch:a": "&:checked{}",
"dsa": " :disabled{}<i>[da]</i>",
"dsa:a": "&:disabled{}<i>[da:a]</i>",
"en": " :enabled{}",
"en:a": "&:enabled{}",
"fc": " :first-child{}",
"fc:a": "&:first-child{}",
"fle": " :first-letter{}",
"fle:a": "&:first-letter{}",
"fli": " :first-line{}",
"fli:a": "&:first-line{}",
"foc": " :focus{}",
"foc:a": "&:focus{}",
"ho": " :hover{}",
"ho:a": "&:hover{}",
"ln": " :lang(){}",
"ln:a": "&:lang(){}",
"lc": " :last-child{}",
"lc:a": "&:last-child{}",
// "li": " :link{}",
// "li:a": "&:link{}",
"nc": " :nth-child(){}",
"nc:a": "&:nth-child(){}",
"vit": " :visited{}",
"vit:a": "&:visited{}",
"tgt": " :target{}",
"tgt:a": "&:target{}",
"fot": " :first-of-type{}",
"fot:a": "&:first-of-type{}",
"lot": " :last-of-type{}",
"lot:a": "&:last-of-type{}",
"not": " :nth-of-type(){}",
"not:a": "&:nth-of-type(){}",
// Scss & Sass
"ext": "@extend",
"inc": "@include",
"mix": "@mixin",
"ieh": "ie-hex-str()"
};
}
o.helpers.units = function(v, def) {
if(!v.toString().match(/[%|in|cm|mm|em|ex|pt|pc|px|deg|ms|s]/)) return v + (def || '%');
else return v;
}
var extend = require("./helpers/extend"),
fs = require('fs'),
path = require('path')
var walk = function(dir) {
return walkClientSide();
var results = [];
var list = fs.readdirSync(dir);
for(var i=0; i<list.length; i++) {
var file = dir + '/' + list[i];
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
results = results.concat(walk(file));
} else {
results.push(file);
}
}
return results;
};
o.index = {
absurd: null,
init: function(decoration) {
if(typeof decoration != 'undefined') {
this.absurd = decoration;
}
// getting atoms and molecules
var files = walk(__dirname + "/lib/"), self = this;
for(var i=0; i<files.length; i++) {
var file = path.basename(files[i]);
(function(m) {
var module = require(m);
self.absurd.plugin(file.replace(".js", ""), function(absurd, value) {
return module(value);
});
})(files[i]);
}
// converting snippets to plugins
var snippets = require(__dirname + "/helpers/snippets")();
for(var atom in snippets) {
atom = atom.split(":");
(function(pluginName) {
self.absurd.plugin(pluginName, function(absurd, value, prefixes) {
if(prefixes === false) { prefixes = ''; }
var s, r = {};
if(s = snippets[pluginName + ':' + value]) {
s = s.split(':');
r[prefixes + s[0]] = s[1] || '';
} else if(s = snippets[pluginName]){
r[prefixes + s] = value;
}
return r;
});
})(atom.shift());
}
return this;
}
}
o.lib.atoms.atoms = function(value) {
var toObj = function(value, r) {
value = value.replace(/( )?:( )?/, ':').split(':');
r = r || {};
r[value[0]] = value[1] || '';
return r;
}
var processArr = function(value) {
var r = {};
for(var i=0; i<value.length; i++) {
toObj(value[i], r);
}
return r;
}
if(typeof value == 'string') {
return processArr(value.replace(/( )?\/( )?/g, '/').split('/'));
} else if(typeof value == 'object') {
if(!(value instanceof Array)) {
return value;
} else {
return processArr(value);
}
}
}
/*!
Animate.css - http://daneden.me/animate
Licensed under the MIT license
Copyright (c) 2013 Daniel Eden
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
o.lib.molecules.animate = function(value) {
var r = {};
r['-wmso-animation-name'] = '';
r['-wmso-animation-duration'] = '1s',
name = '';
if(typeof value === 'string') {
r['-wmso-animation-name'] = value;
} else if(typeof value === 'object') {
if(value instanceof Array) {
if(value.length === 1) {
r['-wmso-animation-name'] = value[0];
} else if(value.length === 2) {
r = {
keyframes: {
name: value[0],
frames: value[1]
}
};
} else {
value = r = {};
}
} else {
r['-wmso-animation-name'] = value.name;
value.duration ? r['-wmso-animation-duration'] = value.duration : '';
value.fillMode ? r['-wmso-animation-fill-mode'] = value.fillMode : '';
value.timingFunction ? r['-wmso-animation-timing-function'] = value.timingFunction : '';
value.iterationCount ? r['-wmso-animation-iteration-count'] = value.iterationCount : '';
value.delay ? r['-wmso-animation-delay'] = value.delay : '';
value.direction ? r['-wmso-animation-direction'] = value.direction : '';
value.playState ? r['-wmso-animation-play-state'] = value.playState : '';
if(value.frames) {
r.keyframes = {
name: value.name,
frames: value.frames
}
}
}
}
switch(r['-wmso-animation-name']) {
case "blink":
r.keyframes = {
name: "blink",
frames: {
"0%, 100%": {
transparent: 0
},
"50%": {
transparent: 1
}
}
}
break;
case "bounce":
r.keyframes = {
name: "bounce",
frames: {
"0%, 20%, 50%, 80%, 100%": {
"-wmso-transform": "translateY(0)"
},
"40%": {
"-wmso-transform": "translateY(-30px)"
},
"60%": {
"-wmso-transform": "translateY(-15px)"
}
}
}
break;
case "flash":
r.keyframes = {
name: "flash",
frames: {
"0%, 50%, 100%": {
"opacity": "1"
},
"25%, 75%": {
"opacity": "0"
}
}
}
break;
case "pulse":
r.keyframes = {
name: "pulse",
frames: {
"0%": {
"-wmso-transform": "scale(1)"
},
"50%": {
"-wmso-transform": "scale(1.1)"
},
"100%": {
"-wmso-transform": "scale(1)"
}
}
}
break;
case "shake":
r.keyframes = {
name: "shake",
frames: {
"0%, 100%": {
"-wmso-transform": "translateX(0)"
},
"10%, 30%, 50%, 70%, 90%": {
"-wmso-transform": "translateX(-10px)"
},
"20%, 40%, 60%, 80%": {
"-wmso-transform": "translateX(10px)"
}
}
}
break;
case "swing":
r.keyframes = {
name: "swing",
frames: {
"20%": {
"-wmso-transform": "rotate(15deg)"
},
"40%": {
"-wmso-transform": "rotate(-10deg)"
},
"60%": {
"-wmso-transform": "rotate(5deg)"
},
"80%": {
"-wmso-transform": "rotate(-5deg)"
},
"100%": {
"-wmso-transform": "rotate(0deg)"
}
}
}
break;
case "tada":
r.keyframes = {
name: "tada",
frames: {
"0%": {
"-wmso-transform": "scale(1)"
},
"10%, 20%": {
"-wmso-transform": "scale(0.9) rotate(-3deg)"
},
"30%, 50%, 70%, 90%": {
"-wmso-transform": "scale(1.1) rotate(3deg)"
},
"40%, 60%, 80%": {
"-wmso-transform": "scale(1.1) rotate(-3deg)"
},
"100%": {
"-wmso-transform": "scale(1) rotate(0)"
}
}
}
break;
case "wobble":
r.keyframes = {
name: "wobble",
frames: {
"0%": {
"-wmso-transform": "translateX(0%)"
},
"15%": {
"-wmso-transform": "translateX(-25%) rotate(-5deg)"
},
"30%": {
"-wmso-transform": "translateX(20%) rotate(3deg)"
},
"45%": {
"-wmso-transform": "translateX(-15%) rotate(-3deg)"
},
"60%": {
"-wmso-transform": "translateX(10%) rotate(2deg)"
},
"75%": {
"-wmso-transform": "translateX(-5%) rotate(-1deg)"
},
"100%": {
"-wmso-transform": "translateX(0%)"
}
}
}
break;
case "bounceIn":
r.keyframes = {
name: "bounceIn",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "scale(.3)"
},
"50%": {
"opacity": "1",
"-wmso-transform": "scale(1.05)"
},
"70%": {
"-wmso-transform": "scale(.9)"
},
"100%": {
"-wmso-transform": "scale(1)"
}
}
}
break;
case "bounceInDown":
r.keyframes = {
name: "bounceInDown",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateY(-2000px)"
},
"60%": {
"opacity": "1",
"-wmso-transform": "translateY(30px)"
},
"80%": {
"-wmso-transform": "translateY(-10px)"
},
"100%": {
"-wmso-transform": "translateY(0)"
}
}
}
break;
case "bounceInLeft":
r.keyframes = {
name: "bounceInLeft",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateX(-2000px)"
},
"60%": {
"opacity": "1",
"-wmso-transform": "translateX(30px)"
},
"80%": {
"-wmso-transform": "translateX(-10px)"
},
"100%": {
"-wmso-transform": "translateX(0)"
}
}
}
break;
case "bounceInRight":
r.keyframes = {
name: "bounceInRight",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateX(2000px)"
},
"60%": {
"opacity": "1",
"-wmso-transform": "translateX(-30px)"
},
"80%": {
"-wmso-transform": "translateX(10px)"
},
"100%": {
"-wmso-transform": "translateX(0)"
}
}
}
break;
case "bounceInUp":
r.keyframes = {
name: "bounceInUp",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateY(2000px)"
},
"60%": {
"opacity": "1",
"-wmso-transform": "translateY(-30px)"
},
"80%": {
"-wmso-transform": "translateY(10px)"
},
"100%": {
"-wmso-transform": "translateY(0)"
}
}
}
break;
case "bounceOut":
r.keyframes = {
name: "bounceOut",
frames: {
"0%": {
"-wmso-transform": "scale(1)"
},
"25%": {
"-wmso-transform": "scale(.95)"
},
"50%": {
"opacity": "1",
"-wmso-transform": "scale(1.1)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "scale(.3)"
}
}
}
break;
case "bounceOutDown":
r.keyframes = {
name: "bounceOutDown",
frames: {
"0%": {
"-wmso-transform": "translateY(0)"
},
"20%": {
"opacity": "1",
"-wmso-transform": "translateY(-20px)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateY(2000px)"
}
}
}
break;
case "bounceOutLeft":
r.keyframes = {
name: "bounceOutLeft",
frames: {
"0%": {
"-wmso-transform": "translateX(0)"
},
"20%": {
"opacity": "1",
"-wmso-transform": "translateX(20px)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateX(-2000px)"
}
}
}
break;
case "bounceOutRight":
r.keyframes = {
name: "bounceOutRight",
frames: {
"0%": {
"-wmso-transform": "translateX(0)"
},
"20%": {
"opacity": "1",
"-wmso-transform": "translateX(-20px)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateX(2000px)"
}
}
}
break;
case "bounceOutUp":
r.keyframes = {
name: "bounceOutUp",
frames: {
"0%": {
"-wmso-transform": "translateY(0)"
},
"20%": {
"opacity": "1",
"-wmso-transform": "translateY(20px)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateY(-2000px)"
}
}
}
break;
case "fadeIn":
r.keyframes = {
name: "fadeIn",
frames: {
"0%": {
"opacity": "0"
},
"100%": {
"opacity": "1"
}
}
}
break;
case "fadeInDown":
r.keyframes = {
name: "fadeInDown",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateY(-20px)"
},
"100%": {
"opacity": "1",
"-wmso-transform": "translateY(0)"
}
}
}
break;
case "fadeInDownBig":
r.keyframes = {
name: "fadeInDownBig",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateY(-2000px)"
},
"100%": {
"opacity": "1",
"-wmso-transform": "translateY(0)"
}
}
}
break;
case "fadeInLeft":
r.keyframes = {
name: "fadeInLeft",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateX(-20px)"
},
"100%": {
"opacity": "1",
"-wmso-transform": "translateX(0)"
}
}
}
break;
case "fadeInLeftBig":
r.keyframes = {
name: "fadeInLeftBig",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateX(-2000px)"
},
"100%": {
"opacity": "1",
"-wmso-transform": "translateX(0)"
}
}
}
break;
case "fadeInRight":
r.keyframes = {
name: "fadeInRight",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateX(20px)"
},
"100%": {
"opacity": "1",
"-wmso-transform": "translateX(0)"
}
}
}
break;
case "fadeInRightBig":
r.keyframes = {
name: "fadeInRightBig",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateX(2000px)"
},
"100%": {
"opacity": "1",
"-wmso-transform": "translateX(0)"
}
}
}
break;
case "fadeInUp":
r.keyframes = {
name: "fadeInUp",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateY(20px)"
},
"100%": {
"opacity": "1",
"-wmso-transform": "translateY(0)"
}
}
}
break;
case "fadeInUpBig":
r.keyframes = {
name: "fadeInUpBig",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateY(2000px)"
},
"100%": {
"opacity": "1",
"-wmso-transform": "translateY(0)"
}
}
}
break;
case "fadeOut":
r.keyframes = {
name: "fadeOut",
frames: {
"0%": {
"opacity": "1"
},
"100%": {
"opacity": "0"
}
}
}
break;
case "fadeOutDown":
r.keyframes = {
name: "fadeOutDown",
frames: {
"0%": {
"opacity": "1",
"-wmso-transform": "translateY(0)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateY(20px)"
}
}
}
break;
case "fadeOutDownBig":
r.keyframes = {
name: "fadeOutDownBig",
frames: {
"0%": {
"opacity": "1",
"-wmso-transform": "translateY(0)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateY(2000px)"
}
}
}
break;
case "fadeOutLeft":
r.keyframes = {
name: "fadeOutLeft",
frames: {
"0%": {
"opacity": "1",
"-wmso-transform": "translateX(0)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateX(-20px)"
}
}
}
break;
case "fadeOutLeftBig":
r.keyframes = {
name: "fadeOutLeftBig",
frames: {
"0%": {
"opacity": "1",
"-wmso-transform": "translateX(0)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateX(-2000px)"
}
}
}
break;
case "fadeOutRight":
r.keyframes = {
name: "fadeOutRight",
frames: {
"0%": {
"opacity": "1",
"-wmso-transform": "translateX(0)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateX(20px)"
}
}
}
break;
case "fadeOutRightBig":
r.keyframes = {
name: "fadeOutRightBig",
frames: {
"0%": {
"opacity": "1",
"-wmso-transform": "translateX(0)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateX(2000px)"
}
}
}
break;
case "fadeOutUp":
r.keyframes = {
name: "fadeOutUp",
frames: {
"0%": {
"opacity": "1",
"-wmso-transform": "translateY(0)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateY(-20px)"
}
}
}
break;
case "fadeOutUpBig":
r.keyframes = {
name: "fadeOutUpBig",
frames: {
"0%": {
"opacity": "1",
"-wmso-transform": "translateY(0)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateY(-2000px)"
}
}
}
break;
case "flip":
r.keyframes = {
name: "flip",
frames: {
"0%": {
"-wmso-transform": "perspective(400px) translateZ(0) rotateY(0) scale(1)",
"animation-timing-function": "ease-out"
},
"40%": {
"-wmso-transform": "perspective(400px) translateZ(150px) rotateY(170deg) scale(1)",
"animation-timing-function": "ease-out"
},
"50%": {
"-wmso-transform": "perspective(400px) translateZ(150px) rotateY(190deg) scale(1)",
"animation-timing-function": "ease-in"
},
"80%": {
"-wmso-transform": "perspective(400px) translateZ(0) rotateY(360deg) scale(.95)",
"animation-timing-function": "ease-in"
},
"100%": {
"-wmso-transform": "perspective(400px) translateZ(0) rotateY(360deg) scale(1)",
"animation-timing-function": "ease-in"
}
}
}
break;
case "flipInX":
r.keyframes = {
name: "flipInX",
frames: {
"0%": {
"-wmso-transform": "perspective(400px) rotateX(90deg)",
"opacity": "0"
},
"40%": {
"-wmso-transform": "perspective(400px) rotateX(-10deg)"
},
"70%": {
"-wmso-transform": "perspective(400px) rotateX(10deg)"
},
"100%": {
"-wmso-transform": "perspective(400px) rotateX(0deg)",
"opacity": "1"
}
}
}
break;
case "flipInY":
r.keyframes = {
name: "flipInY",
frames: {
"0%": {
"-wmso-transform": "perspective(400px) rotateY(90deg)",
"opacity": "0"
},
"40%": {
"-wmso-transform": "perspective(400px) rotateY(-10deg)"
},
"70%": {
"-wmso-transform": "perspective(400px) rotateY(10deg)"
},
"100%": {
"-wmso-transform": "perspective(400px) rotateY(0deg)",
"opacity": "1"
}
}
}
break;
case "flipOutX":
r.keyframes = {
name: "flipOutX",
frames: {
"0%": {
"-wmso-transform": "perspective(400px) rotateX(0deg)",
"opacity": "1"
},
"100%": {
"-wmso-transform": "perspective(400px) rotateX(90deg)",
"opacity": "0"
}
}
}
break;
case "flipOutY":
r.keyframes = {
name: "flipOutY",
frames: {
"0%": {
"-wmso-transform": "perspective(400px) rotateY(0deg)",
"opacity": "1"
},
"100%": {
"-wmso-transform": "perspective(400px) rotateY(90deg)",
"opacity": "0"
}
}
}
break;
case "lightSpeedIn":
r.keyframes = {
name: "lightSpeedIn",
frames: {
"0%": {
"-wmso-transform": "translateX(100%) skewX(-30deg)",
"opacity": "0"
},
"60%": {
"-wmso-transform": "translateX(-20%) skewX(30deg)",
"opacity": "1"
},
"80%": {
"-wmso-transform": "translateX(0%) skewX(-15deg)",
"opacity": "1"
},
"100%": {
"-wmso-transform": "translateX(0%) skewX(0deg)",
"opacity": "1"
}
}
}
break;
case "lightSpeedOut":
r.keyframes = {
name: "lightSpeedOut",
frames: {
"0%": {
"-wmso-transform": "translateX(0%) skewX(0deg)",
"opacity": "1"
},
"100%": {
"-wmso-transform": "translateX(100%) skewX(-30deg)",
"opacity": "0"
}
}
}
break;
case "rotateIn":
r.keyframes = {
name: "rotateIn",
frames: {
"0%": {
"transform-origin": "center center",
"-wmso-transform": "rotate(-200deg)",
"opacity": "0"
},
"100%": {
"transform-origin": "center center",
"-wmso-transform": "rotate(0)",
"opacity": "1"
}
}
}
break;
case "rotateInDownLeft":
r.keyframes = {
name: "rotateInDownLeft",
frames: {
"0%": {
"transform-origin": "left bottom",
"-wmso-transform": "rotate(-90deg)",
"opacity": "0"
},
"100%": {
"transform-origin": "left bottom",
"-wmso-transform": "rotate(0)",
"opacity": "1"
}
}
}
break;
case "rotateInDownRight":
r.keyframes = {
name: "rotateInDownRight",
frames: {
"0%": {
"transform-origin": "right bottom",
"-wmso-transform": "rotate(90deg)",
"opacity": "0"
},
"100%": {
"transform-origin": "right bottom",
"-wmso-transform": "rotate(0)",
"opacity": "1"
}
}
}
break;
case "rotateInUpLeft":
r.keyframes = {
name: "rotateInUpLeft",
frames: {
"0%": {
"transform-origin": "left bottom",
"-wmso-transform": "rotate(90deg)",
"opacity": "0"
},
"100%": {
"transform-origin": "left bottom",
"-wmso-transform": "rotate(0)",
"opacity": "1"
}
}
}
break;
case "rotateInUpRight":
r.keyframes = {
name: "rotateInUpRight",
frames: {
"0%": {
"transform-origin": "right bottom",
"-wmso-transform": "rotate(-90deg)",
"opacity": "0"
},
"100%": {
"transform-origin": "right bottom",
"-wmso-transform": "rotate(0)",
"opacity": "1"
}
}
}
break;
case "rotateOut":
r.keyframes = {
name: "rotateOut",
frames: {
"0%": {
"transform-origin": "center center",
"-wmso-transform": "rotate(0)",
"opacity": "1"
},
"100%": {
"transform-origin": "center center",
"-wmso-transform": "rotate(200deg)",
"opacity": "0"
}
}
}
break;
case "rotateOutDownLeft":
r.keyframes = {
name: "rotateOutDownLeft",
frames: {
"0%": {
"transform-origin": "left bottom",
"-wmso-transform": "rotate(0)",
"opacity": "1"
},
"100%": {
"transform-origin": "left bottom",
"-wmso-transform": "rotate(90deg)",
"opacity": "0"
}
}
}
break;
case "rotateOutDownRight":
r.keyframes = {
name: "rotateOutDownRight",
frames: {
"0%": {
"transform-origin": "right bottom",
"-wmso-transform": "rotate(0)",
"opacity": "1"
},
"100%": {
"transform-origin": "right bottom",
"-wmso-transform": "rotate(-90deg)",
"opacity": "0"
}
}
}
break;
case "rotateOutUpLeft":
r.keyframes = {
name: "rotateOutUpLeft",
frames: {
"0%": {
"transform-origin": "left bottom",
"-wmso-transform": "rotate(0)",
"opacity": "1"
},
"100%": {
"transform-origin": "left bottom",
"-wmso-transform": "rotate(-90deg)",
"opacity": "0"
}
}
}
break;
case "rotateOutUpRight":
r.keyframes = {
name: "rotateOutUpRight",
frames: {
"0%": {
"transform-origin": "right bottom",
"-wmso-transform": "rotate(0)",
"opacity": "1"
},
"100%": {
"transform-origin": "right bottom",
"-wmso-transform": "rotate(90deg)",
"opacity": "0"
}
}
}
break;
case "slideInDown":
r.keyframes = {
name: "slideInDown",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateY(-2000px)"
},
"100%": {
"-wmso-transform": "translateY(0)"
}
}
}
break;
case "slideInLeft":
r.keyframes = {
name: "slideInLeft",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateX(-2000px)"
},
"100%": {
"-wmso-transform": "translateX(0)"
}
}
}
break;
case "slideInRight":
r.keyframes = {
name: "slideInRight",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateX(2000px)"
},
"100%": {
"-wmso-transform": "translateX(0)"
}
}
}
break;
case "slideOutLeft":
r.keyframes = {
name: "slideOutLeft",
frames: {
"0%": {
"-wmso-transform": "translateX(0)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateX(-2000px)"
}
}
}
break;
case "slideOutRight":
r.keyframes = {
name: "slideOutRight",
frames: {
"0%": {
"-wmso-transform": "translateX(0)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateX(2000px)"
}
}
}
break;
case "slideOutUp":
r.keyframes = {
name: "slideOutUp",
frames: {
"0%": {
"-wmso-transform": "translateY(0)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateY(-2000px)"
}
}
}
break;
case "hinge":
r.keyframes = {
name: "hinge",
frames: {
"0%": {
"-wmso-transform": "rotate(0)",
"transform-origin": "top left",
"animation-timing-function": "ease-in-out"
},
"20%, 60%": {
"-wmso-transform": "rotate(80deg)",
"transform-origin": "top left",
"animation-timing-function": "ease-in-out"
},
"40%": {
"-wmso-transform": "rotate(60deg)",
"transform-origin": "top left",
"animation-timing-function": "ease-in-out"
},
"80%": {
"-wmso-transform": "rotate(60deg) translateY(0)",
"opacity": "1",
"transform-origin": "top left",
"animation-timing-function": "ease-in-out"
},
"100%": {
"-wmso-transform": "translateY(700px)",
"opacity": "0"
}
}
}
break;
case "rollIn":
r.keyframes = {
name: "rollIn",
frames: {
"0%": {
"opacity": "0",
"-wmso-transform": "translateX(-100%) rotate(-120deg)"
},
"100%": {
"opacity": "1",
"-wmso-transform": "translateX(0px) rotate(0deg)"
}
}
}
break;
case "rollOut":
r.keyframes = {
name: "rollOut",
frames: {
"0%": {
"opacity": "1",
"-wmso-transform": "translateX(0px) rotate(0deg)"
},
"100%": {
"opacity": "0",
"-wmso-transform": "translateX(100%) rotate(120deg)"
}
}
}
break;
}
return r;
}
o.lib.molecules.blur = function(value) {
return {
'-wms-filter': 'blur(' + value + 'px)'
}
}
o.lib.molecules.brightness = function(value) {
return {
'-wms-filter': 'brightness(' + value + ')'
}
}
o.lib.molecules.calc = function(value) {
var args = require('../../helpers/args')(value), r = {};
r['LhProperty'] = '0';
r['~~1~~' + args[0]] = '-webkit-calc(' + args[1] + ')';
r['~~2~~' + args[0]] = '-moz-calc(' + args[1] + ')';
r['~~3~~' + args[0]] = 'calc(' + args[1] + ')';
return r;
}
o.lib.molecules.cf = function(value) {
var r = {}, clearing = {
content: '" "',
display: 'table',
clear: 'both'
};
switch(value) {
case 'before':
r['&:before'] = clearing;
break;
case 'after':
r['&:after'] = clearing;
break;
default:
r['&:before'] = clearing;
r['&:after'] = clearing;
break;
}
return r;
}
o.lib.molecules.contrast = function(value) {
return {
'-wms-filter': 'contrast(' + value + '%)'
}
}
o.lib.molecules.dropshadow = function(value) {
return {
'-wms-filter': 'drop-shadow(' + value + ')'
}
}
var getMSColor = function(color) {
color = color.toString().replace('#', '');
if(color.length == 3) {
var tmp = '';
for(var i=0; i<color.length; i++) {
tmp += color[i] + color[i];
}
color = tmp;
}
return '#FF' + color.toUpperCase();
}
o.lib.molecules.gradient = function(value) {
var r = {},
args = require('../../helpers/args')(value);
switch(typeof value) {
case 'string':
var deg = args[args.length-1];
if(deg.indexOf('deg') > 0) {
deg = parseInt(args.pop().replace('deg', ''));
} else {
deg = 0;
}
var numOfStops = args.length,
stepsPercents = Math.floor(100 / (numOfStops-1)).toFixed(2),
gradientValue = [],
msGradientType = (deg >= 45 && deg <= 135) || (deg >= 225 && deg <= 315) ? 1 : 0,
msStartColor = msGradientType === 0 ? getMSColor(args[args.length-1]) : getMSColor(args[0]),
msEndColor = msGradientType === 0 ? getMSColor(args[0]) : getMSColor(args[args.length-1]);
for(var i=0; i<numOfStops; i++) {
if(args[i].indexOf('%') > 0) {
gradientValue.push(args[i]);
} else {
gradientValue.push(args[i] + ' ' + (i*stepsPercents) + '%');
}
}
gradientValue = deg + 'deg, ' + gradientValue.join(', ');
return [
{ 'background': '-webkit-linear-gradient(' + gradientValue + ')' },
{ '~~1~~background': '-moz-linear-gradient(' + gradientValue + ')' },
{ '~~2~~background': '-ms-linear-gradient(' + gradientValue + ')' },
{ '~~3~~background': '-o-linear-gradient(' + gradientValue + ')' },
{ '~~4~~background': 'linear-gradient(' + gradientValue + ')' },
{ 'filter': 'progid:DXImageTransform.Microsoft.gradient(startColorstr=\'' + msStartColor + '\', endColorstr=\'' + msEndColor + '\',GradientType=' + msGradientType + ')' },
{ 'MsFilter': 'progid:DXImageTransform.Microsoft.gradient(startColorstr=\'' + msStartColor + '\',endColorstr=\'' + msEndColor + '\',GradientType=' + msGradientType + ')' }
]
break;
}
return {};
}
o.lib.molecules.grid = function(value) {
var args = require('../../helpers/args')(value);
if(args.length == 2) {
var res = {
cf: 'both'
}
res[args[1]] = {
fl: 'l',
'-mw-bxz': 'bb',
wid: (100 / parseInt(args[0])).toFixed(2) + '%'
}
return res;
} else {
return {};
}
}
o.lib.molecules.invert = function(value) {
return {
'-wms-filter': 'invert(' + value + '%)'
}
}
o.lib.molecules.moveto = function(value) {
var units = require('../../helpers/units'),
args = require('../../helpers/args')(value),
x = units(!args[0] || args[0] == '' ? 0 : args[0], 'px'),
y = units(!args[1] || args[1] == '' ? 0 : args[1], 'px'),
z = units(!args[2] || args[2] == '' ? 0 : args[2], 'px');
if(args.length == 2) {
return {"-ws-trf": ">translate(" + x + "," + y + ")"};
} else if(args.length == 3) {
return {"-ws-trf": ">translate3d(" + x + "," + y + "," + z + ")"};
}
}
o.lib.molecules.rotateto = function(value) {
var units = require('../../helpers/units'),
args = require('../../helpers/args')(value);
if(args.length == 1) {
return {"-ws-trf": ">rotate(" + units(args[0], 'deg') + ")"};
}
}
o.lib.molecules.saturate = function(value) {
return {
'-wms-filter': 'saturate(' + value + 'deg)'
}
}
o.lib.molecules.scaleto = function(value) {
var args = require('../../helpers/args')(value),
x = !args[0] || args[0] == '' ? 0 : args[0],
y = !args[1] || args[1] == '' ? 0 : args[1];
if(args.length == 2) {
return {"-ws-trf": ">scale(" + x + "," + y + ")"};
}
}
o.lib.molecules.sepia = function(value) {
return {
'-wms-filter': 'sepia(' + value + '%)'
}
}
o.lib.molecules.size = function(value) {
var units = require('../../helpers/units'),
args = require('../../helpers/args')(value),
r = {};
if(args.length == 2) {
if(args[0] != '') {
r.width = units(args[0]);
}
if(args[1] != '') {
r.height = units(args[1]);
}
return r;
} else {
return {
width: units(args[0]),
height: units(args[0])
}
}
}
o.lib.molecules.transparent = function(value) {
var args = require('../../helpers/args')(value),
r = {};
value = parseFloat(value);
r['-s-filter'] = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + (value * 100) + ')';
r['filter'] = 'alpha(opacity=' + (value * 100) + ')';
r['-m-opacity'] = value;
r['opacity'] = value;
r['KhtmlOpacity'] = value;
return r;
}
o.lib.molecules.trsform = function(value) {
return {
'-wmso-transform': value
}
};
return o.index;
})(window); |
//// Copyright (c) Microsoft Corporation. All rights reserved
(function () {
"use strict";
var page = WinJS.UI.Pages.define("/html/scenario6.html", {
ready: function (element, options) {
document.getElementById("Delete").addEventListener("click", Delete_Click, false);
}
});
function Delete_Click() {
if (null === SdkSample.reader || undefined === SdkSample.reader) {
WinJS.log("Use Scenario One to create a TPM virtual smart card.", "sample", "error");
return;
}
var button = document.getElementById("Delete");
button.disabled = true;
WinJS.log("Deleting TPM virtual smart card...", "sample", "status");
SdkSample.getSmartCard().then(
function (smartCard) {
// The following two lines are not directly related to TPM virtual
// smart card deletion, but are used to demonstrate how to handle
// CardRemoved events by registering an event handler with a
// SmartCardReader object. Since we are using a TPM virtual smart
// card in this case, the card cannot actually be added to or
// removed from the reader, but a CardRemoved event will fire when
// the card and reader are deleted.
SdkSample.reader.oncardremoved = handleCardRemoved;
return Windows.Devices.SmartCards.SmartCardProvisioning.requestVirtualSmartCardDeletionAsync(smartCard);
}).done(
function (result) {
if (result) {
WinJS.log("TPM virtual smart card deletion completed.", "sample", "status");
SdkSample.reader = null;
}
else {
WinJS.log("TPM virtual smart card deletion was canceled by " + "the user.", "sample", "status");
}
button.disabled = false;
},
function (error) {
WinJS.log && WinJS.log("TPM virtual smart card deletion failed with exception: " + error.toString(), "sample", "error");
button.disabled = false;
});
}
function handleCardRemoved(eventArgs) {
WinJS.log("Card removed from reader " + eventArgs.target.name + ".", "sample", "status");
}
})();
|
;(function(define){define(function(require,exports,module){
function denodeify(nodeStyleFunction, filter) {
'use strict';
return function() {
var self = this;
var functionArguments = new Array(arguments.length + 1);
for (var i = 0; i < arguments.length; i += 1) {
functionArguments[i] = arguments[i];
}
function promiseHandler(resolve, reject) {
function callbackFunction() {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; i += 1) {
args[i] = arguments[i];
}
if (filter) {
args = filter.apply(self, args);
}
var error = args[0];
var result = args[1];
if (error) {
return reject(error);
}
return resolve(result);
}
functionArguments[functionArguments.length - 1] = callbackFunction;
nodeStyleFunction.apply(self, functionArguments);
}
return new Promise(promiseHandler);
};
}
module.exports = denodeify;
});})(typeof define=='function'&&define.amd?define
:(function(n,w){'use strict';return typeof module=='object'?function(c){
c(require,exports,module);}:function(c){var m={exports:{}};c(function(n){
return w[n];},m.exports,m);w[n]=m.exports;};})('denodeify',this));
|
module.exports = function(hljs) {
var Q_KEYWORDS = {
keyword:
'do while select delete by update from',
constant:
'0b 1b',
built_in:
'neg not null string reciprocal floor ceiling signum mod xbar xlog and or each scan over prior mmu lsq inv md5 ltime gtime count first var dev med cov cor all any rand sums prds mins maxs fills deltas ratios avgs differ prev next rank reverse iasc idesc asc desc msum mcount mavg mdev xrank mmin mmax xprev rotate distinct group where flip type key til get value attr cut set upsert raze union inter except cross sv vs sublist enlist read0 read1 hopen hclose hdel hsym hcount peach system ltrim rtrim trim lower upper ssr view tables views cols xcols keys xkey xcol xasc xdesc fkeys meta lj aj aj0 ij pj asof uj ww wj wj1 fby xgroup ungroup ej save load rsave rload show csv parse eval min max avg wavg wsum sin cos tan sum',
typename:
'`float `double int `timestamp `timespan `datetime `time `boolean `symbol `char `byte `short `long `real `month `date `minute `second `guid'
};
return {
aliases:['k', 'kdb'],
keywords: Q_KEYWORDS,
lexemes: /\b(`?)[A-Za-z0-9_]+\b/,
contains: [
hljs.C_LINE_COMMENT_MODE,
hljs.QUOTE_STRING_MODE,
hljs.C_NUMBER_MODE
]
};
}; |
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _seq = require('./seq');
var _seq2 = _interopRequireDefault(_seq);
var _baseRest = require('lodash/_baseRest');
var _baseRest2 = _interopRequireDefault(_baseRest);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Creates a function which is a composition of the passed asynchronous
* functions. Each function consumes the return value of the function that
* follows. Composing functions `f()`, `g()`, and `h()` would produce the result
* of `f(g(h()))`, only this version uses callbacks to obtain the return values.
*
* Each function is executed with the `this` binding of the composed function.
*
* @name compose
* @static
* @memberOf module:ControlFlow
* @method
* @category Control Flow
* @param {...Function} functions - the asynchronous functions to compose
* @returns {Function} an asynchronous function that is the composed
* asynchronous `functions`
* @example
*
* function add1(n, callback) {
* setTimeout(function () {
* callback(null, n + 1);
* }, 10);
* }
*
* function mul3(n, callback) {
* setTimeout(function () {
* callback(null, n * 3);
* }, 10);
* }
*
* var add1mul3 = async.compose(mul3, add1);
* add1mul3(4, function (err, result) {
* // result now equals 15
* });
*/
exports.default = (0, _baseRest2.default)(function (args) {
return _seq2.default.apply(null, args.reverse());
});
module.exports = exports['default']; |
/*!
* Qoopido.js library v3.2.7, 2014-5-19
* https://github.com/dlueth/qoopido.js
* (c) 2014 Dirk Lueth
* Dual licensed under MIT and GPL
*/
!function(t){window.qoopido.register("support/element/canvas/todataurl",t,["../../../support","../canvas"])}(function(t,e,n,o,a,s,r){"use strict";var c=t.support;return c.addTest("/element/canvas/todataurl",function(e){t["support/element/canvas"]().then(function(){var t=c.pool?c.pool.obtain("canvas"):s.createElement("canvas");t.toDataURL!==r?e.resolve():e.reject(),t.dispose&&t.dispose()},function(){e.reject()}).done()})}); |
Accounts.validateNewUser(function (user) {
if (user.profile && user.profile.invalidAndThrowException)
throw new Meteor.Error(403, "An exception thrown within Accounts.validateNewUser");
return !(user.profile && user.profile.invalid);
});
Accounts.onCreateUser(function (options, user) {
if (options.testOnCreateUserHook) {
user.profile = user.profile || {};
user.profile.touchedByOnCreateUser = true;
return user;
} else {
return 'TEST DEFAULT HOOK';
}
});
// connection id -> action
var invalidateLogins = {};
Meteor.methods({
testInvalidateLogins: function (action) {
if (action)
invalidateLogins[this.connection.id] = action;
else
delete invalidateLogins[this.connection.id];
}
});
Accounts.validateLoginAttempt(function (attempt) {
var action =
attempt &&
attempt.connection &&
invalidateLogins[attempt.connection.id];
if (! action)
return true;
else if (action === 'fail')
return false;
else if (action === 'hide')
throw new Meteor.Error(403, 'hide actual error');
else
throw new Error('unknown action: ' + action);
});
// connection id -> [{successful: boolean, attempt: object}]
var capturedLogins = {};
Meteor.methods({
testCaptureLogins: function () {
capturedLogins[this.connection.id] = [];
},
testFetchCapturedLogins: function () {
if (capturedLogins[this.connection.id]) {
var logins = capturedLogins[this.connection.id];
delete capturedLogins[this.connection.id];
return logins;
}
else
return [];
}
});
Accounts.onLogin(function (attempt) {
if (!attempt.connection) // if login method called from the server
return;
if (capturedLogins[attempt.connection.id])
capturedLogins[attempt.connection.id].push({
successful: true,
attempt: _.omit(attempt, 'connection')
});
});
Accounts.onLoginFailure(function (attempt) {
if (!attempt.connection) // if login method called from the server
return;
if (capturedLogins[attempt.connection.id]) {
capturedLogins[attempt.connection.id].push({
successful: false,
attempt: _.omit(attempt, 'connection')
});
}
});
// Because this is global state that affects every client, we can't turn
// it on and off during the tests. Doing so would mean two simultaneous
// test runs could collide with each other.
//
// We should probably have some sort of server-isolation between
// multiple test runs. Perhaps a separate server instance per run. This
// problem isn't unique to this test, there are other places in the code
// where we do various hacky things to work around the lack of
// server-side isolation.
//
// For now, we just test the one configuration state. You can comment
// out each configuration option and see that the tests fail.
Accounts.config({
sendVerificationEmail: true
});
Meteor.methods({
testMeteorUser: function () { return Meteor.user(); },
clearUsernameAndProfile: function () {
if (!this.userId)
throw new Error("Not logged in!");
Meteor.users.update(this.userId,
{$unset: {profile: 1, username: 1}});
},
expireTokens: function () {
Accounts._expireTokens(new Date(), this.userId);
},
removeUser: function (username) {
Meteor.users.remove({ "username": username });
}
});
// Create a user that had previously logged in with SRP.
Meteor.methods({
testCreateSRPUser: function () {
var username = Random.id();
Meteor.users.remove({username: username});
var userId = Accounts.createUser({username: username});
Meteor.users.update(
userId,
{ '$set': { 'services.password.srp': {
"identity" : "iPNrshUEcpOSO5fRDu7o4RRDc9OJBCGGljYpcXCuyg9",
"salt" : "Dk3lFggdEtcHU3aKm6Odx7sdcaIrMskQxBbqtBtFzt6",
"verifier" : "2e8bce266b1357edf6952cc56d979db19f699ced97edfb2854b95972f820b0c7006c1a18e98aad40edf3fe111b87c52ef7dd06b320ce452d01376df2d560fdc4d8e74f7a97bca1f67b3cfaef34dee34dd6c76571c247d762624dc166dab5499da06bc9358528efa75bf74e2e7f5a80d09e60acf8856069ae5cfb080f2239ee76"
} } }
);
return username;
},
testSRPUpgrade: function (username) {
var user = Meteor.users.findOne({username: username});
if (user.services && user.services.password && user.services.password.srp)
throw new Error("srp wasn't removed");
if (!(user.services && user.services.password && user.services.password.bcrypt))
throw new Error("bcrypt wasn't added");
},
testNoSRPUpgrade: function (username) {
var user = Meteor.users.findOne({username: username});
if (user.services && user.services.password && user.services.password.bcrypt)
throw new Error("bcrypt was added");
if (user.services && user.services.password && ! user.services.password.srp)
throw new Error("srp was removed");
}
});
|
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("echarts"));
else if(typeof define === 'function' && define.amd)
define(["echarts"], factory);
else if(typeof exports === 'object')
exports["bmap"] = factory(require("echarts"));
else
root["echarts"] = root["echarts"] || {}, root["echarts"]["bmap"] = factory(root["echarts"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
var __WEBPACK_AMD_DEFINE_RESULT__;/**
* BMap component extension
*/
!(__WEBPACK_AMD_DEFINE_RESULT__ = function (require) {
__webpack_require__(1).registerCoordinateSystem(
'bmap', __webpack_require__(2)
);
__webpack_require__(3);
__webpack_require__(4);
// Action
__webpack_require__(1).registerAction({
type: 'bmapRoam',
event: 'bmapRoam',
update: 'updateLayout'
}, function (payload, ecModel) {
ecModel.eachComponent('bmap', function (bMapModel) {
var bmap = bMapModel.getBMap();
var center = bmap.getCenter();
bMapModel.setCenterAndZoom([center.lng, center.lat], bmap.getZoom());
});
});
return {
version: '1.0.0'
};
}.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
/***/ },
/* 1 */
/***/ function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_1__;
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
var __WEBPACK_AMD_DEFINE_RESULT__;!(__WEBPACK_AMD_DEFINE_RESULT__ = function (require) {
var echarts = __webpack_require__(1);
function BMapCoordSys(bmap, api) {
this._bmap = bmap;
this.dimensions = ['lng', 'lat'];
this._mapOffset = [0, 0];
this._api = api;
}
BMapCoordSys.prototype.dimensions = ['lng', 'lat'];
BMapCoordSys.prototype.setMapOffset = function (mapOffset) {
this._mapOffset = mapOffset;
};
BMapCoordSys.prototype.getBMap = function () {
return this._bmap;
};
BMapCoordSys.prototype.dataToPoint = function (data) {
var point = new BMap.Point(data[0], data[1]);
var px = this._bmap.pointToOverlayPixel(point);
var mapOffset = this._mapOffset;
return [px.x - mapOffset[0], px.y - mapOffset[1]];
};
BMapCoordSys.prototype.pointToData = function (pt) {
var mapOffset = this._mapOffset;
var pt = this._bmap.overlayPixelToPoint({
x: pt[0] + mapOffset[0],
y: pt[1] + mapOffset[1]
});
return [pt.lng, pt.lat];
};
BMapCoordSys.prototype.getViewRect = function () {
var api = this._api;
return new echarts.graphic.BoundingRect(0, 0, api.getWidth(), api.getHeight());
};
BMapCoordSys.prototype.getRoamTransform = function () {
return echarts.matrix.create();
};
var Overlay;
// For deciding which dimensions to use when creating list data
BMapCoordSys.dimensions = BMapCoordSys.prototype.dimensions;
function createOverlayCtor() {
function Overlay(root) {
this._root = root;
}
Overlay.prototype = new BMap.Overlay();
/**
* 初始化
*
* @param {BMap.Map} map
* @override
*/
Overlay.prototype.initialize = function (map) {
map.getPanes().labelPane.appendChild(this._root);
return this._root;
};
/**
* @override
*/
Overlay.prototype.draw = function () {};
return Overlay;
}
BMapCoordSys.create = function (ecModel, api) {
var bmapCoordSys;
var root = api.getDom();
// TODO Dispose
ecModel.eachComponent('bmap', function (bmapModel) {
var viewportRoot = api.getZr().painter.getViewportRoot();
if (typeof BMap === 'undefined') {
throw new Error('BMap api is not loaded');
}
Overlay = Overlay || createOverlayCtor();
if (bmapCoordSys) {
throw new Error('Only one bmap component can exist');
}
if (!bmapModel.__bmap) {
// Not support IE8
var bmapRoot = root.querySelector('.ec-extension-bmap');
if (bmapRoot) {
// Reset viewport left and top, which will be changed
// in moving handler in BMapView
viewportRoot.style.left = '0px';
viewportRoot.style.top = '0px';
root.removeChild(bmapRoot);
}
bmapRoot = document.createElement('div');
bmapRoot.style.cssText = 'width:100%;height:100%';
// Not support IE8
bmapRoot.classList.add('ec-extension-bmap');
root.appendChild(bmapRoot);
var bmap = bmapModel.__bmap = new BMap.Map(bmapRoot);
var overlay = new Overlay(viewportRoot);
bmap.addOverlay(overlay);
}
var bmap = bmapModel.__bmap;
// Set bmap options
// centerAndZoom before layout and render
var center = bmapModel.get('center');
var zoom = bmapModel.get('zoom');
if (center && zoom) {
var pt = new BMap.Point(center[0], center[1]);
bmap.centerAndZoom(pt, zoom);
}
bmapCoordSys = new BMapCoordSys(bmap, api);
bmapCoordSys.setMapOffset(bmapModel.__mapOffset || [0, 0]);
bmapModel.coordinateSystem = bmapCoordSys;
});
ecModel.eachSeries(function (seriesModel) {
if (seriesModel.get('coordinateSystem') === 'bmap') {
seriesModel.coordinateSystem = bmapCoordSys;
}
});
};
return BMapCoordSys;
}.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
var __WEBPACK_AMD_DEFINE_RESULT__;!(__WEBPACK_AMD_DEFINE_RESULT__ = function (require) {
function v2Equal(a, b) {
return a && b && a[0] === b[0] && a[1] === b[1];
}
return __webpack_require__(1).extendComponentModel({
type: 'bmap',
getBMap: function () {
// __bmap is injected when creating BMapCoordSys
return this.__bmap;
},
setCenterAndZoom: function (center, zoom) {
this.option.center = center;
this.option.zoom = zoom;
},
centerOrZoomChanged: function (center, zoom) {
var option = this.option;
return !(v2Equal(center, option.center) && zoom === option.zoom);
},
defaultOption: {
center: null,
zoom: 1,
mapStyle: {},
roam: false
}
});
}.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
var __WEBPACK_AMD_DEFINE_RESULT__;!(__WEBPACK_AMD_DEFINE_RESULT__ = function (require) {
return __webpack_require__(1).extendComponentView({
type: 'bmap',
render: function (bMapModel, ecModel, api) {
var rendering = true;
var bmap = bMapModel.getBMap();
var viewportRoot = api.getZr().painter.getViewportRoot();
var coordSys = bMapModel.coordinateSystem;
var moveHandler = function (type, target) {
if (rendering) {
return;
}
var offsetEl = viewportRoot.parentNode.parentNode.parentNode;
var mapOffset = [
-parseInt(offsetEl.style.left, 10) || 0,
-parseInt(offsetEl.style.top, 10) || 0
];
viewportRoot.style.left = mapOffset[0] + 'px';
viewportRoot.style.top = mapOffset[1] + 'px';
coordSys.setMapOffset(mapOffset);
bMapModel.__mapOffset = mapOffset;
api.dispatchAction({
type: 'bmapRoam'
});
};
function zoomEndHandler() {
if (rendering) {
return;
}
api.dispatchAction({
type: 'bmapRoam'
});
}
bmap.removeEventListener('moving', this._oldMoveHandler);
// FIXME
// Moveend may be triggered by centerAndZoom method when creating coordSys next time
// bmap.removeEventListener('moveend', this._oldMoveHandler);
bmap.removeEventListener('zoomend', this._oldZoomEndHandler);
bmap.addEventListener('moving', moveHandler);
// bmap.addEventListener('moveend', moveHandler);
bmap.addEventListener('zoomend', zoomEndHandler);
this._oldMoveHandler = moveHandler;
this._oldZoomEndHandler = zoomEndHandler;
var roam = bMapModel.get('roam');
if (roam && roam !== 'scale') {
bmap.enableDragging();
}
else {
bmap.disableDragging();
}
if (roam && roam !== 'move') {
bmap.enableScrollWheelZoom();
bmap.enableDoubleClickZoom();
bmap.enablePinchToZoom();
}
else {
bmap.disableScrollWheelZoom();
bmap.disableDoubleClickZoom();
bmap.disablePinchToZoom();
}
var originalStyle = bMapModel.__mapStyle;
var newMapStyle = bMapModel.get('mapStyle') || {};
// FIXME, Not use JSON methods
var mapStyleStr = JSON.stringify(newMapStyle);
if (JSON.stringify(originalStyle) !== mapStyleStr) {
bmap.setMapStyle(newMapStyle);
bMapModel.__mapStyle = JSON.parse(mapStyleStr);
}
rendering = false;
}
});
}.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
/***/ }
/******/ ])
});
; |
import {createValidator, required, maxLength, integer, oneOf} from 'utils/validation';
export const colors = ['Blue', 'Fuchsia', 'Green', 'Orange', 'Red', 'Taupe'];
const widgetValidation = createValidator({
color: [required, oneOf(colors)],
sprocketCount: [required, integer],
owner: [required, maxLength(30)]
});
export default widgetValidation;
|
var OpenLayers={VERSION_NUMBER:"Release 2.12",singleFile:!0,_getScriptLocation:function(){for(var e,t,i=RegExp("(^|(.*?\\/))(OpenLayers[^\\/]*?\\.js)(\\?|$)"),n=document.getElementsByTagName("script"),r="",s=0,o=n.length;o>s;s++)if(e=n[s].getAttribute("src"),e&&(t=e.match(i))){r=t[1];break}return function(){return r}}(),ImgPath:""}; |
/* @flow */
'use strict';
const _ = require('lodash');
const parseAmount = require('./amount');
import type {Amount, RippledAmount} from '../../common/types.js';
import type {GetPaths, RippledPathsResponse} from '../pathfind-types.js';
function parsePaths(paths) {
return paths.map(steps => steps.map(step =>
_.omit(step, ['type', 'type_hex'])));
}
function removeAnyCounterpartyEncoding(address: string, amount: Amount) {
return amount.counterparty === address ?
_.omit(amount, 'counterparty') : amount;
}
function createAdjustment(address: string, adjustmentWithoutAddress: Object) {
const amountKey = _.keys(adjustmentWithoutAddress)[0];
const amount = adjustmentWithoutAddress[amountKey];
return _.set({address: address}, amountKey,
removeAnyCounterpartyEncoding(address, amount));
}
function parseAlternative(sourceAddress: string, destinationAddress: string,
destinationAmount: RippledAmount, alternative: Object
) {
// we use "maxAmount"/"minAmount" here so that the result can be passed
// directly to preparePayment
const amounts = (alternative.destination_amount !== undefined) ?
{source: {amount: parseAmount(alternative.source_amount)},
destination: {minAmount: parseAmount(alternative.destination_amount)}} :
{source: {maxAmount: parseAmount(alternative.source_amount)},
destination: {amount: parseAmount(destinationAmount)}};
return {
source: createAdjustment(sourceAddress, amounts.source),
destination: createAdjustment(destinationAddress, amounts.destination),
paths: JSON.stringify(parsePaths(alternative.paths_computed))
};
}
function parsePathfind(pathfindResult: RippledPathsResponse): GetPaths {
const sourceAddress = pathfindResult.source_account;
const destinationAddress = pathfindResult.destination_account;
const destinationAmount = pathfindResult.destination_amount;
return pathfindResult.alternatives.map(_.partial(parseAlternative,
sourceAddress, destinationAddress, destinationAmount));
}
module.exports = parsePathfind;
|
(function () {
var STEP_STOP = 15;
var root = typeof self == 'object' && self.self === self && self ||
typeof global == 'object' && global.global === global && global ||
this;
var previousNba = root.nba;
var nba = function(array) {
if (!array.hasOwnProperty('length')) throw new TypeError('Expecting an array...');
return new Nba(array);
};
nba.noConflict = function() {
root.nba = previousNba;
return this;
};
if (typeof exports != 'undefined' && !exports.nodeType) {
if (typeof module != 'undefined' && !module.nodeType && module.exports) {
exports = module.exports = nba;
}
exports.nba = nba;
} else {
root.nba = nba;
}
function Nba (array, previous) {
this.array = array;
this.previous = previous || null;
this.operation = null;
}
Nba.prototype.then = function (success, error) {
if (!this.previous) {
startExecution.call(this, success, error, this.array);
}
else {
this.previous.then(startExecution.bind(this, success, error), error);
}
};
function startExecution (success, error, array) {
if (!this.operation) {
success(array);
}
else {
this.operation(success, error, array);
}
}
Nba.prototype.sort = function (comparator) {
comparator = comparator || defaultComparator;
this.operation = function (success, error, array) {
try {
quickSort(array, 0, array.length - 1, comparator, success, error);
} catch (e) {
if (!error) throw e;
error(e);
}
};
return new Nba(this.array, this);
};
Nba.prototype.reverse = function (comparator) {
this.operation = function (success, error, array) {
var i = 0,
j = array.length - 1,
start;
(function step () {
try {
start = new Date();
while (i < j) {
if (mustStop(step, start)) return;
swap(array, i, j);
i++;
j--;
}
success(array);
} catch (e) {
if (!error) throw e;
error(e);
}
})();
};
return new Nba(this.array, this);
};
Nba.prototype.forEach = function (action, that) {
that = that || root;
this.operation = function (success, error, array) {
var i = 0,
start;
(function step () {
try {
start = new Date();
while (i < array.length) {
if (mustStop(step, start)) return;
action.call(that, array[i], i, array);
i++;
}
success(array);
} catch (e) {
if (!error) throw e;
error(e);
}
})();
};
return new Nba(this.array, this);
};
Nba.prototype.filter = function (action, that) {
that = that || root;
this.operation = function (success, error, array) {
var i = 0,
start,
result = [];
(function step () {
try {
start = new Date();
while (i < array.length) {
if (mustStop(step, start)) return;
if (action.call(that, array[i], i, array)) result.push(array[i]);
i++;
}
success(result);
} catch (e) {
if (!error) throw e;
error(e);
}
})();
};
return new Nba(this.array, this);
};
Nba.prototype.map = function (action, that) {
that = that || root;
this.operation = function (success, error, array) {
var i = 0,
start,
result = [];
(function step () {
try {
start = new Date();
while (i < array.length) {
if (mustStop(step, start)) return;
result.push(action.call(that, array[i], i, array));
i++;
}
success(result);
} catch (e) {
if (!error) throw e;
error(e);
}
})();
};
return new Nba(this.array, this);
};
Nba.prototype.some = function (action, that) {
that = that || root;
this.operation = function (success, error, array) {
var i = 0,
start,
result = false;
(function step () {
try {
start = new Date();
while (i < array.length) {
if (mustStop(step, start)) return;
result = !!action.call(that, array[i], i, array);
if (result) break;
i++;
}
success(result);
} catch (e) {
if (!error) throw e;
error(e);
}
})();
};
return new Nba(this.array, this);
};
Nba.prototype.every = function (action, that) {
that = that || root;
this.operation = function (success, error, array) {
var i = 0,
start,
result = true;
(function step () {
try {
start = new Date();
while (i < array.length) {
if (mustStop(step, start)) return;
result = !!action.call(that, array[i], i, array);
if (!result) break;
i++;
}
success(result);
} catch (e) {
if (!error) throw e;
error(e);
}
})();
};
return new Nba(this.array, this);
};
Nba.prototype.reduce = function (action, initial) {
this.operation = function (success, error, array) {
var i = 0,
start,
result = initial;
(function step () {
try {
start = new Date();
while (i < array.length) {
if (mustStop(step, start)) return;
result = action.call(root, result, array[i], i, array);
i++;
}
success(result);
} catch (e) {
if (!error) throw e;
error(e);
}
})();
};
return new Nba(this.array, this);
};
function mustStop(operation, start) {
if (new Date() - start < STEP_STOP) return false;
setTimeout(operation, 0);
return true;
}
function quickSort (array, low, high, comparator, success, error) {
if (low >= high) {
success(array);
return;
}
partition(array, low, high, comparator, function (pivot) {
function _success () {
if (_success.both) success(array);
else _success.both = true;
}
quickSort(array, low, pivot, comparator, _success, error);
quickSort(array, pivot+1, high, comparator, _success, error);
}, error);
}
function partition(array, low, high, comparator, success, error) {
var pivot = array[low],
i = low - 1,
j = high + 1,
start;
(function step () {
try {
start = new Date();
while (true) {
if (mustStop(step, start)) return;
do {
i++;
} while (comparator(array[i], pivot) < 0);
do {
j--;
} while (comparator(array[j], pivot) > 0);
if (i >= j) break;
swap(array, i, j);
}
success(j);
} catch (e) {
if (!error) throw e;
error(e);
}
})();
}
function swap (array, i, j) {
var tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
function defaultComparator (a, b) {
return a.toString() < b.toString() ? -1 :
b.toString() < a.toString() ? 1 : 0;
}
})();
|
class Model_NPC extends Model_Abstract_Entity {
}
ObjectHelper.extend(Model_NPC.prototype, {
url: '/npcs',
name: 'npc',
fields: ObjectHelper.extend({
id: {
type: Enum_FieldTypes.KEY
},
name: {
type: Enum_FieldTypes.STRING,
max_length: 255
},
subtype: {
type: Enum_FieldTypes.INT,
default: null,
optional: true
},
deaths: {
type: Enum_FieldTypes.INT,
default: 0
},
kills: {
type: Enum_FieldTypes.INT,
default: 0
},
loot_group_id: {
type: Enum_FieldTypes.ID,
default: null,
optional: true
},
min_level: {
type: Enum_FieldTypes.INT,
default: 1
},
max_level: {
type: Enum_FieldTypes.INT,
default: 1
},
min_gold: {
type: Enum_FieldTypes.INT,
default: 0
},
max_gold: {
type: Enum_FieldTypes.INT,
default: 0
},
entity_type: {
type: Enum_FieldTypes.STRING,
values: Game_Enum_EntityTypes,
default: Game_Enum_EntityTypes.NPC,
virtual: true
},
created: {
type: Enum_FieldTypes.DATE,
role: Enum_Roles.OWNER,
default: SQL_Enum_DatetimeDefaults.CURRENT_TIMESTAMP
},
changed: {
type: Enum_FieldTypes.DATE,
role: Enum_Roles.OWNER,
default: SQL_Enum_DatetimeDefaults.NULL_ON_UPDATE_CURRENT_TIMESTAMP
},
active: {
type: Enum_FieldTypes.BOOLEAN,
default: true,
context: Enum_Contexts.SERVER
}
}, Model_Abstract_Entity.prototype.fields)
});
module.exports = Model_NPC;
|
// http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html#mongoclient-connection-pooling
var db = exports.db = require('monk')(process.env.MONGO_URL);
var articles = exports.articles = db.get(process.env.ARTICLES_COLLECTION)
, comments = exports.comments = db.get(process.env.COMMENTS_COLLECTION)
, users = exports.users = db.get(process.env.USERS_COLLECTION)
;
// indexes
comments.index('article');
comments.index('user');
users.index('name');
|
import React from 'react'
import Helmet from 'react-helmet'
import { ThemeProvider } from 'styled-components'
import Online from 'shared/components/Online'
import Offline from 'shared/components/Offline'
import InjectGlobalCSS from 'shared/components/InjectGlobalCSS'
const App = ({online, theme, _text}) => {
return (
<div className='react-root'>
<Helmet>
<title>{_text.title()}</title>
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
</Helmet>
<ThemeProvider theme={theme}>
<InjectGlobalCSS theme={theme}>
{ online ? (<Online />) : (<Offline />) }
</InjectGlobalCSS>
</ThemeProvider>
</div>
)
}
export default App
|
import mod1889 from './mod1889';
var value=mod1889+1;
export default value;
|
module.exports = {
Container: require('./SVGContainer'),
Controller: require('./SVGController'),
Creep: require('./SVGCreep'),
Extension: require('./SVGExtension'),
Extractor: require('./SVGExtractor'),
KeeperLair: require('./SVGKeeperLair'),
Lab: require('./SVGLab'),
Link: require('./SVGLink'),
Mineral: require('./SVGMineral'),
Nuker: require('./SVGNuker'),
Observer: require('./SVGObserver'),
PowerBank: require('./SVGPowerBank'),
PowerSpawn: require('./SVGPowerSpawn'),
Resource: require('./SVGResource'),
Room: require('./SVGRoom'),
Source: require('./SVGSource'),
Spawn: require('./SVGSpawn'),
Storage: require('./SVGStorage'),
Terminal: require('./SVGTerminal'),
Tower: require('./SVGTower'),
};
|
var express = require('express');
var path = require('path');
var http = require('http');
var app = express();
var Twitch = require('twit');
var html_dir = 'views/';
/*var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
app.use(session({
secret: 'foo',
store: new MongoStore(options)
}));
/* TWITTER APP
var T = new Twit({
consumer_key: config.twitter.consumerKey,
consumer_secret: config.twitter.consumerSecret,
access_token: config.twitter.accessToken,
access_token_secret: config.twitter.accessTokenSecret
});*/
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use(express.static(path.join(__dirname, 'static')));
// Twilio Credentials
var accountSid = 'ACce388b1f32389fcaad02832364b9607d';
var authToken = '[AuthToken]';
//require the Twilio module and create a REST client
/*
var client = require('twilio')(accountSid, authToken);
client.messages.create({
to: "0658363974",
from: "+33987679373",
body: "hello world",
}, function(err, message) {
console.log(message.sid);
});
*/
app.get('/', function(req, res) {
res.render('accueil');
// res.sendFile('accueil.html', {root: html_dir});
});
app.get('/started', function(req, res){
res.render('started');
});
app.get('/contact', function(req, res){
res.render('contact');
});
app.get('/FAQ', function(req, res){
res.render('faq');
});
var server = app.listen(process.env.PORT || 3000);
/*
var io = require('socket.io').listen(server);
var stream = T.stream('statuses/sample')
io.sockets.on('connection', function (socket) {
stream.on('tweet', function(tweet) {
socket.emit('info', { tweet: tweet});
});
});
*/ |
'use strict'
/* global describe, it */
const expect = require('unexpected')
const proxyquire = require('proxyquire').noPreserveCache()
const EventEmitter = require('events')
const caches = require('../../lib/caches')
const forks = {}
class Child extends EventEmitter {
constructor () {
super()
this.wasDisconnected = false
}
send (cmd) {
this.emit('send', cmd)
}
disconnect () {
this.wasDisconnected = true
}
}
const workerManagement = proxyquire('../../lib/workerManagement', {
'child_process': {
fork (workerPath, args, opts) {
const name = args[0]
const child = new Child()
forks[name] = child
return child
}
}
})
describe('lib/workerManagement', () => {
it('should shut down least recently used workers', () => {
workerManagement.getWorker('first', '/')
workerManagement.getWorker('second', '/')
expect(forks, 'to satisfy', {
first: {
wasDisconnected: false
},
second: {
wasDisconnected: false
}
})
workerManagement.getWorker('third', '/')
expect(forks, 'to have key', 'third')
expect(forks, 'to satisfy', {
first: {
wasDisconnected: true
},
second: {
wasDisconnected: false
},
third: {
wasDisconnected: false
}
})
})
it('should forward errors to the lint() promise', () => {
const worker = workerManagement.getWorker('linter', '/')
const expected = 'error'
forks.linter.on('send', cmd => {
forks.linter.emit('message', { id: cmd.id, error: { message: expected } })
})
return expect(worker.lint('', {}), 'to be rejected').then(actual => {
expect(actual, 'to be a', Error)
expect(actual, 'to have message', expected)
})
})
it('should ignore unexpected messages from the worker', () => {
workerManagement.getWorker('linter', '/')
forks.linter.emit('message', { id: 10 })
})
it('should clean up linters that exit', () => {
const foo = workerManagement.getWorker('first', '/')
expect(workerManagement.getWorker('first', '/'), 'to be', foo)
forks.first.emit('exit')
return new Promise(resolve => setTimeout(resolve, 10))
.then(() => {
const bar = workerManagement.getWorker('first', '/')
expect(bar, 'not to be', foo)
// Ensure first is purged from the cache
workerManagement.getWorker('second', '/')
workerManagement.getWorker('third', '/')
const child = forks.first
expect(child, 'to have property', 'wasDisconnected', true)
const baz = workerManagement.getWorker('first', '/')
child.emit('exit')
return new Promise(resolve => setTimeout(resolve, 10))
.then(() => {
expect(workerManagement.getWorker('first', '/'), 'to be', baz)
})
})
})
it('should ignore disconnect errors', () => {
const worker = workerManagement.getWorker('first', '/')
forks.first.disconnect = () => { throw new Error('ignore me') }
expect(() => worker.dispose(), 'not to throw')
})
describe('clearing all caches', () => {
it('should shut down all workers', () => {
// Reset state
caches.clearAll()
workerManagement.getWorker('first', '/')
workerManagement.getWorker('second', '/')
expect(forks, 'to satisfy', {
first: {
wasDisconnected: false
},
second: {
wasDisconnected: false
}
})
caches.clearAll()
expect(forks, 'to satisfy', {
first: {
wasDisconnected: true
},
second: {
wasDisconnected: true
}
})
})
})
})
|
import 'mocha';
import {assert} from 'chai';
import Asset from '../src/asset';
import Percent from '../src/percent';
import USD from '../src/usd'; const $ = USD;
import Portfolio from '../src/portfolio';
import Immutable from '../src/immutable';
describe('Portfolio', () => {
describe('#constructor', () => {
it('throws if input is not an Array', () => {
assert.throws(Portfolio.bind(Portfolio, 'ZVZZT'), TypeError);
});
it('throws if input Array is empty', () => {
assert.throws(Portfolio.bind(Portfolio, []), Error);
});
it('throws if input Array contains an element without an asset', () => {
const assets = Immutable([{
quantity: 1,
ideal: Percent(100)
}]);
assert.throws(Portfolio.bind(Portfolio, assets), TypeError);
});
it('throws if input Array contains an element without a quantity', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
ideal: Percent(100)
}]);
assert.throws(Portfolio.bind(Portfolio, assets), TypeError);
});
it('throws if input Array contains an element without an ideal allocation', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: 1
}]);
assert.throws(Portfolio.bind(Portfolio, assets), TypeError);
});
it('throws if input Array contains an element with an asset of the wrong type', () => {
const assets = Immutable([{
asset: 'ZVZZT',
quantity: 1,
ideal: Percent(100)
}]);
assert.throws(Portfolio.bind(Portfolio, assets), TypeError);
});
it('throws if input Array contains an element with a quantity of the wrong type', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: '1',
ideal: Percent(100)
}]);
assert.throws(Portfolio.bind(Portfolio, assets), TypeError);
});
it('throws if input Array contains an element with an ideal allocation of the wrong type', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: 1,
ideal: '100%'
}]);
assert.throws(Portfolio.bind(Portfolio, assets), TypeError);
});
it('throws if input Array contains an element has a negative quantity', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: -1,
ideal: Percent(100)
}]);
assert.throws(Portfolio.bind(Portfolio, assets), TypeError);
});
it('throws if ideal allocations sum to less than 100%', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: 1,
ideal: Percent(50)
}]);
assert.throws(Portfolio.bind(Portfolio, assets), TypeError);
});
it('throws if ideal allocations sum to more than 100%', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: 1,
ideal: Percent(150)
}]);
assert.throws(Portfolio.bind(Portfolio, assets), TypeError);
});
it('creates a Portfolio when all inputs are valid', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: 1,
ideal: Percent(100)
}]);
const portfolio = Portfolio(assets);
// Check portfolio
assert.instanceOf(portfolio, Portfolio);
assert.deepEqual($(200), $(portfolio.net));
// Check ZVZZT
assert.deepEqual(assets[0].asset, portfolio.assets[0].asset);
assert.strictEqual(assets[0].quantity, portfolio.assets[0].quantity);
assert.deepEqual(assets[0].ideal, portfolio.assets[0].ideal);
});
it('does not mutate input', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: 1,
ideal: Percent(100)
}]);
const konst = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: 1,
ideal: Percent(100)
}]);
Portfolio(assets);
assert.deepEqual(assets, konst);
});
});
describe('#load', () => {
it('throws if input is not in USD', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: 1,
ideal: Percent(100)
}]);
let portfolio = Portfolio(assets);
assert.throws(portfolio.load.bind(portfolio, 'ZVZZT'), TypeError);
});
it('loads additional funds without cash surplus', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: 1,
ideal: Percent(100)
}]);
let portfolio = Portfolio(assets);
portfolio.load($(200));
// Check portfolio
assert.deepEqual(portfolio.net, $(400));
assert.strictEqual(portfolio.assets.length, 1);
// Check ZVZZT
const zvzzt = portfolio.assets.find((a) => a.asset.symbol === 'ZVZZT');
assert.strictEqual(zvzzt.quantity, 2);
});
it('loads additional funds with cash surplus', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: 1,
ideal: Percent(100)
}]);
let portfolio = Portfolio(assets);
portfolio.load($(250));
// Check portfolio
assert.deepEqual(portfolio.net, $(450));
assert.strictEqual(portfolio.assets.length, 2);
// Check ZVZZT
const zvzzt = portfolio.assets.find((a) => a.asset.symbol === 'ZVZZT');
assert.strictEqual(zvzzt.quantity, 2);
// Check CASH
const cash = portfolio.assets.find((a) => a.asset.symbol === '_CASH');
assert.deepEqual(cash.asset.price, $(1));
assert.strictEqual(cash.quantity, 50);
});
it('loads cash surplus', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: 1,
ideal: Percent(100)
}]);
let portfolio = Portfolio(assets);
portfolio.load($(50));
// Check portfolio
assert.deepEqual(portfolio.net, $(250));
assert.strictEqual(portfolio.assets.length, 2);
// Check ZVZZT
const zvzzt = portfolio.assets.find((a) => a.asset.symbol === 'ZVZZT');
assert.strictEqual(zvzzt.quantity, 1);
// Check CASH
const cash = portfolio.assets.find((a) => a.asset.symbol === '_CASH');
assert.deepEqual(cash.asset.price, $(1));
assert.strictEqual(cash.quantity, 50);
});
it('loads cash surplus if load amount is less than the price of the selected underallocated asset', () => {
const assets = Immutable([{
asset: Asset('ZVZZT', $(200)),
quantity: 1,
ideal: Percent(50)
}, {
asset: Asset('ZZZZQ', $(50)),
quantity: 8,
ideal: Percent(50)
}]);
let portfolio = Portfolio(assets);
portfolio.load($(100));
// Check portfolio
assert.deepEqual(portfolio.net, $(700));
assert.strictEqual(portfolio.assets.length, 3);
// Check ZVZZT
const zvzzt = portfolio.assets.find((a) => a.asset.symbol === 'ZVZZT');
assert.strictEqual(zvzzt.quantity, 1);
// Check ZZZZQ
const zzzzq = portfolio.assets.find((a) => a.asset.symbol === 'ZZZZQ');
assert.strictEqual(zzzzq.quantity, 8);
// Check CASH
const cash = portfolio.assets.find((a) => a.asset.symbol === '_CASH');
assert.deepEqual(cash.asset.price, $(1));
assert.strictEqual(cash.quantity, 100);
});
});
});
|
'use strict';
var gulp = require('gulp');
var watch = require('gulp-watch');
var config = require('../config');
var _ = require('lodash');
function build() {
gulp.start('build');
}
module.exports = function () {
build();
var watchable = _.chain(config.paths).omit('dest').values().value();
return watch(watchable, build);
};
|
var chai = require('chai');
var assert = chai.assert;
var Immy = require('../../src/immy');
describe('ListPatches.Add', function () {
describe('#apply()', function () {
it('should add the given value at the given index', function () {
var array = [0, 1, 2, 3];
var patch = new Immy.ListPatches.Add(2, 'foo');
patch.apply(array);
assert.deepEqual(array, [0, 1, 'foo', 2, 3]);
});
});
describe('#inverse()', function () {
it('should return an inverse of the patch', function () {
var array = [0, 1, 'foo', 2, 3];
var patch = new Immy.ListPatches.Add(2, 'foo');
var inverse = patch.inverse();
inverse.apply(array);
assert.deepEqual(array, [0, 1, 2, 3]);
});
});
describe('#toPrimitives()', function () {
it('should return an array containing the same add patch', function () {
var patch = new Immy.ListPatches.Add(2, 'foo');
var primitives = patch.toPrimitives();
assert.equal(primitives.length, 1);
assert(primitives[0] instanceof Immy.ListPatches.Add);
assert.equal(primitives[0].index, 2);
assert.equal(primitives[0].value, 'foo');
});
});
describe('#forEachPrimitive()', function () {
it('should call the callback for each primitive operation', function () {
var patch = new Immy.ListPatches.Add(2, 'foo');
var primitives = [];
patch.forEachPrimitive(function (primOp) {
primitives.push(primOp);
});
assert.equal(primitives.length, 1);
assert(primitives[0] instanceof Immy.ListPatches.Add);
assert.equal(primitives[0].index, 2);
assert.equal(primitives[0].value, 'foo');
});
});
});
|
// REDEEMING /////////////////////////////////////////////////////////
window.handleRedeemResponse = function(data, textStatus, jqXHR) {
if(data.error) {
alert('Couldn’t redeem code. It may be used up, invalid or you have redeemed it already. (Code: '+data.error+')');
return;
}
var text = 'Success! However, pretty display is not implemented.\nMaybe you can make sense of the following:\n';
alert(text + JSON.stringify(data));
}
window.setupRedeem = function() {
$("#redeem").keypress(function(e) {
if((e.keyCode ? e.keyCode : e.which) != 13) return;
var data = {passcode: $(this).val()};
window.postAjax('redeemReward', data, window.handleRedeemResponse,
function() { alert('HTTP request failed. Maybe try again?'); });
});
}
|
"use strict";
var ViewRendererPlugin;
/**
* The view renderer plug-in is used to render the main view and minimap. The
* plug-in sends the <code>viewReady</code> event with the current
* <code>View</code> instance once the view has been initialized.
*/
ViewRendererPlugin = function () {
var view, map, x, y, borderOffset, viewWidth, viewHeight, viewLayerSize,
minX, minY, maxX, maxY, sfx;
/**
* Constructor.
*
* @param {Object} pluginSettings Configuration of this plug-in.
*/
(function (pluginSettings) {
borderOffset = pluginSettings.borderOffset;
x = borderOffset;
y = borderOffset;
minX = borderOffset;
minY = borderOffset;
}(Settings.pluginConfiguration.ViewRendererPlugin));
// override
this.renderFrame = function () {
view.display(x, y);
};
/**
* Handler for the <code>leftMouseButtonBoxSelectProgress</code> event. The
* event is triggered when the user is in the progress of drawing a
* selection box around the units to select. The handler notifies the SFX
* renderer to display the selection box.
*
* @param {Object} data Event's details.
*/
this.onLeftMouseButtonBoxSelectProgress = function (data) {
var startX, startY, endX, endY;
startX = Math.min(data.startX, data.endX);
startY = Math.min(data.startY, data.endY);
endX = Math.max(data.startX, data.endX);
endY = Math.max(data.startY, data.endY);
sfx.setSelectionBox(data.startX, data.startY, data.endX, data.endY);
};
/**
* Handler for the <code>leftMouseButtonBoxSelect</code> event. The event
* is triggered when the user has drawn a selection box to select a group
* of units. The handler notifies the SFX renderer to hide the selection
* box as the selection process is completed now.
*
* @param {Object} data The event's details.
*/
this.onLeftMouseButtonBoxSelect = function (data) {
sfx.setSelectionBox(0, 0, 0, 0); // hides the selection box
};
/**
* Event listener for the <code>scrollMapView</code> event. The listener
* will move the camera according to the specified scroll vector.
*
* @param {Object} vector 2D vector specifying how the map view should be
* scrolled. The object has the following properties:
* <ul>
* <li><code>x</code> - the horizontal scrolling speed.</li>
* <li><code>y</code> - the vertical scrolling speed.</li>
* </ul>
*/
this.onScrollMapView = function (vector) {
x = Math.min(maxX, Math.max(x + vector.x, minX));
y = Math.min(maxY, Math.max(y + vector.y, minY));
this.sendEvent("viewOffsetUpdate", {
offsetLeft: x,
offsetTop: y
});
};
/**
* Event listener for the <code>viewSetByMinimap</code> event. The view
* will be shifter according to the data received from the minimap: a
* position of the user's click on the minimap scaled to the interval 0.0
* to 1.0.
*
* @param {Object} data Event data, an object with the <code>x</code> and
* <code>y</code> fields representing the position of the user's
* click on the minimap.
*/
this.onViewSetByMinimap = function (data) {
x = Math.floor(viewLayerSize.width * data.x - viewWidth / 2);
y = Math.floor(viewLayerSize.height * data.y - viewHeight / 2);
x = Math.min(maxX, Math.max(x, minX));
y = Math.min(maxY, Math.max(y, minY));
this.sendEvent("viewOffsetUpdate", {
offsetLeft: x,
offsetTop: y
});
};
/**
* Event listener for the <code>gameMapInitialization</code> event. The map
* is not set to the view until the <code>viewInitialization</code> is
* received.
*
* @param {Map} data The game map.
*/
this.onGameMapInitialization = function (data) {
map = data;
if (view) {
view.setMap(map);
sfx = view.getSfx();
}
};
/**
* Event listener for the <code>viewInitialization</code> event. The
* listener initializes the view renderers, but does not render anything.
*
* @param {Object} data Event data - an object containing the main view
* canvas and the minimap container.
*/
this.onViewInitialization = function (data) {
var minimapSize, viewWidthInTiles, viewHeightInTiles;
view = new View();
view.setCanvas(data.view);
view.setMinimapContainer(data.minimap);
minimapSize = Settings.pluginConfiguration.ViewRendererPlugin.minimap;
view.setMinimapSize(minimapSize.width, minimapSize.height);
if (map) {
view.setMap(map);
sfx = view.getSfx();
}
viewWidth = data.view.width;
viewHeight = data.view.height;
viewWidthInTiles = Math.round(viewWidth / Settings.tileWidth);
viewHeightInTiles = Math.round(viewHeight / Settings.tileHeight);
view.setMainViewSize(viewWidthInTiles, viewHeightInTiles);
viewLayerSize = view.getMainViewLayersDimensions();
maxX = viewLayerSize.width - viewWidth - borderOffset;
maxY = viewLayerSize.height - viewHeight - borderOffset;
this.sendEvent('viewReady', view);
this.sendEvent("viewOffsetUpdate", {
offsetLeft: x,
offsetTop: y
});
};
this.onStart = function () {
addEventListener("selectstart", textSelectionPreventer, false);
};
this.onStop = function () {
removeEventListener("selectstart", textSelectionPreventer);
};
function textSelectionPreventer(event) {
event.preventDefault();
}
};
ViewRendererPlugin.prototype = new AdvancedUIPlugin();
|
import mod155 from './mod155';
var value=mod155+1;
export default value;
|
import * as React from 'react';
import { expect } from 'chai';
import { createClientRender, createMount, describeConformanceV5 } from 'test/utils';
import Icon, { iconClasses as classes } from '@material-ui/core/Icon';
describe('<Icon />', () => {
const render = createClientRender();
const mount = createMount();
describeConformanceV5(<Icon>account_circle</Icon>, () => ({
classes,
inheritComponent: 'span',
render,
mount,
muiName: 'MuiIcon',
refInstanceof: window.HTMLSpanElement,
testComponentPropWith: 'div',
skip: ['themeVariants', 'componentsProp'],
}));
it('renders children by default', () => {
const { getByTestId } = render(<Icon data-testid="root">account_circle</Icon>);
expect(getByTestId('root')).to.have.text('account_circle');
});
describe('optional classes', () => {
it('should render with the secondary color', () => {
const { getByTestId } = render(
<Icon data-testid="root" color="secondary">
account_circle
</Icon>,
);
expect(getByTestId('root')).to.have.class(classes.colorSecondary);
});
it('should render with the action color', () => {
const { getByTestId } = render(
<Icon data-testid="root" color="action">
account_circle
</Icon>,
);
expect(getByTestId('root')).to.have.class(classes.colorAction);
});
it('should render with the error color', () => {
const { getByTestId } = render(
<Icon data-testid="root" color="error">
account_circle
</Icon>,
);
expect(getByTestId('root')).to.have.class(classes.colorError);
});
it('should render with the primary class', () => {
const { getByTestId } = render(
<Icon data-testid="root" color="primary">
account_circle
</Icon>,
);
expect(getByTestId('root')).to.have.class(classes.colorPrimary);
});
it('should render without the default class', () => {
const { getByTestId } = render(
<Icon data-testid="root" baseClassName="material-icons-round">
account_circle
</Icon>,
);
expect(getByTestId('root')).not.to.have.class('material-icons');
});
it('should render with the supplied base class', () => {
const { getByTestId } = render(
<Icon data-testid="root" baseClassName="material-icons-round">
account_circle
</Icon>,
);
expect(getByTestId('root')).to.have.class('material-icons-round');
});
});
describe('prop: fontSize', () => {
it('should be able to change the fontSize', () => {
const { getByTestId } = render(
<Icon data-testid="root" fontSize="inherit">
account_circle
</Icon>,
);
expect(getByTestId('root')).to.have.class(classes.fontSizeInherit);
});
});
});
|
'use strict';
module.exports = (function () {
return require('./env/' + process.env.NODE_ENV);
})();
|
version https://git-lfs.github.com/spec/v1
oid sha256:b30a7ec010b2352452ab09324c5ac80e8fc97de454b937cd62efb1a461c769cb
size 83171
|
version https://git-lfs.github.com/spec/v1
oid sha256:0ae681debd6acdfeb3fb1958cc9a5ce43236201b48ca063230d5057f5e2809e9
size 21510
|
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const {
isStackOverflowError,
codes: {
ERR_CONSOLE_WRITABLE_STREAM,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
},
} = require('internal/errors');
const { previewMapIterator, previewSetIterator } = require('internal/v8');
const { Buffer: { isBuffer } } = require('buffer');
const cliTable = require('internal/cli_table');
const util = require('util');
const {
isTypedArray, isSet, isMap, isSetIterator, isMapIterator,
} = util.types;
const kCounts = Symbol('counts');
const {
keys: ObjectKeys,
values: ObjectValues,
} = Object;
const hasOwnProperty = Function.call.bind(Object.prototype.hasOwnProperty);
const {
isArray: ArrayIsArray,
from: ArrayFrom,
} = Array;
// Track amount of indentation required via `console.group()`.
const kGroupIndent = Symbol('kGroupIndent');
const kFormatForStderr = Symbol('kFormatForStderr');
const kFormatForStdout = Symbol('kFormatForStdout');
const kGetInspectOptions = Symbol('kGetInspectOptions');
const kColorMode = Symbol('kColorMode');
function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
if (!(this instanceof Console)) {
return new Console(...arguments);
}
if (!options || typeof options.write === 'function') {
options = {
stdout: options,
stderr: arguments[1],
ignoreErrors: arguments[2]
};
}
const {
stdout,
stderr = stdout,
ignoreErrors = true,
colorMode = 'auto'
} = options;
if (!stdout || typeof stdout.write !== 'function') {
throw new ERR_CONSOLE_WRITABLE_STREAM('stdout');
}
if (!stderr || typeof stderr.write !== 'function') {
throw new ERR_CONSOLE_WRITABLE_STREAM('stderr');
}
var prop = {
writable: true,
enumerable: false,
configurable: true
};
prop.value = stdout;
Object.defineProperty(this, '_stdout', prop);
prop.value = stderr;
Object.defineProperty(this, '_stderr', prop);
prop.value = Boolean(ignoreErrors);
Object.defineProperty(this, '_ignoreErrors', prop);
prop.value = new Map();
Object.defineProperty(this, '_times', prop);
prop.value = createWriteErrorHandler(stdout);
Object.defineProperty(this, '_stdoutErrorHandler', prop);
prop.value = createWriteErrorHandler(stderr);
Object.defineProperty(this, '_stderrErrorHandler', prop);
if (typeof colorMode !== 'boolean' && colorMode !== 'auto')
throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode);
this[kCounts] = new Map();
this[kColorMode] = colorMode;
Object.defineProperty(this, kGroupIndent, { writable: true });
this[kGroupIndent] = '';
// bind the prototype functions to this Console instance
var keys = Object.keys(Console.prototype);
for (var v = 0; v < keys.length; v++) {
var k = keys[v];
this[k] = this[k].bind(this);
}
}
// Make a function that can serve as the callback passed to `stream.write()`.
function createWriteErrorHandler(stream) {
return (err) => {
// This conditional evaluates to true if and only if there was an error
// that was not already emitted (which happens when the _write callback
// is invoked asynchronously).
if (err !== null && !stream._writableState.errorEmitted) {
// If there was an error, it will be emitted on `stream` as
// an `error` event. Adding a `once` listener will keep that error
// from becoming an uncaught exception, but since the handler is
// removed after the event, non-console.* writes won't be affected.
// we are only adding noop if there is no one else listening for 'error'
if (stream.listenerCount('error') === 0) {
stream.on('error', noop);
}
}
};
}
function write(ignoreErrors, stream, string, errorhandler, groupIndent) {
if (groupIndent.length !== 0) {
if (string.indexOf('\n') !== -1) {
string = string.replace(/\n/g, `\n${groupIndent}`);
}
string = groupIndent + string;
}
string += '\n';
if (ignoreErrors === false) return stream.write(string);
// There may be an error occurring synchronously (e.g. for files or TTYs
// on POSIX systems) or asynchronously (e.g. pipes on POSIX systems), so
// handle both situations.
try {
// Add and later remove a noop error handler to catch synchronous errors.
stream.once('error', noop);
stream.write(string, errorhandler);
} catch (e) {
// console is a debugging utility, so it swallowing errors is not desirable
// even in edge cases such as low stack space.
if (isStackOverflowError(e))
throw e;
// Sorry, there's no proper way to pass along the error here.
} finally {
stream.removeListener('error', noop);
}
}
const kColorInspectOptions = { colors: true };
const kNoColorInspectOptions = {};
Console.prototype[kGetInspectOptions] = function(stream) {
let color = this[kColorMode];
if (color === 'auto') {
color = stream.isTTY && (
typeof stream.getColorDepth === 'function' ?
stream.getColorDepth() > 2 : true);
}
return color ? kColorInspectOptions : kNoColorInspectOptions;
};
Console.prototype[kFormatForStdout] = function(args) {
const opts = this[kGetInspectOptions](this._stdout);
return util.formatWithOptions(opts, ...args);
};
Console.prototype[kFormatForStderr] = function(args) {
const opts = this[kGetInspectOptions](this._stderr);
return util.formatWithOptions(opts, ...args);
};
Console.prototype.log = function log(...args) {
write(this._ignoreErrors,
this._stdout,
this[kFormatForStdout](args),
this._stdoutErrorHandler,
this[kGroupIndent]);
};
Console.prototype.debug = Console.prototype.log;
Console.prototype.info = Console.prototype.log;
Console.prototype.dirxml = Console.prototype.log;
Console.prototype.warn = function warn(...args) {
write(this._ignoreErrors,
this._stderr,
this[kFormatForStderr](args),
this._stderrErrorHandler,
this[kGroupIndent]);
};
Console.prototype.error = Console.prototype.warn;
Console.prototype.dir = function dir(object, options) {
options = Object.assign({
customInspect: false
}, this[kGetInspectOptions](this._stdout), options);
write(this._ignoreErrors,
this._stdout,
util.inspect(object, options),
this._stdoutErrorHandler,
this[kGroupIndent]);
};
Console.prototype.time = function time(label = 'default') {
// Coerces everything other than Symbol to a string
label = `${label}`;
this._times.set(label, process.hrtime());
};
Console.prototype.timeEnd = function timeEnd(label = 'default') {
// Coerces everything other than Symbol to a string
label = `${label}`;
const time = this._times.get(label);
if (!time) {
process.emitWarning(`No such label '${label}' for console.timeEnd()`);
return;
}
const duration = process.hrtime(time);
const ms = duration[0] * 1000 + duration[1] / 1e6;
this.log('%s: %sms', label, ms.toFixed(3));
this._times.delete(label);
};
Console.prototype.trace = function trace(...args) {
const err = {
name: 'Trace',
message: this[kFormatForStderr](args)
};
Error.captureStackTrace(err, trace);
this.error(err.stack);
};
Console.prototype.assert = function assert(expression, ...args) {
if (!expression) {
args[0] = `Assertion failed${args.length === 0 ? '' : `: ${args[0]}`}`;
this.warn(this[kFormatForStderr](args));
}
};
// Defined by: https://console.spec.whatwg.org/#clear
Console.prototype.clear = function clear() {
// It only makes sense to clear if _stdout is a TTY.
// Otherwise, do nothing.
if (this._stdout.isTTY) {
// The require is here intentionally to avoid readline being
// required too early when console is first loaded.
const { cursorTo, clearScreenDown } = require('readline');
cursorTo(this._stdout, 0, 0);
clearScreenDown(this._stdout);
}
};
// Defined by: https://console.spec.whatwg.org/#count
Console.prototype.count = function count(label = 'default') {
// Ensures that label is a string, and only things that can be
// coerced to strings. e.g. Symbol is not allowed
label = `${label}`;
const counts = this[kCounts];
let count = counts.get(label);
if (count === undefined)
count = 1;
else
count++;
counts.set(label, count);
this.log(`${label}: ${count}`);
};
// Not yet defined by the https://console.spec.whatwg.org, but
// proposed to be added and currently implemented by Edge. Having
// the ability to reset counters is important to help prevent
// the counter from being a memory leak.
Console.prototype.countReset = function countReset(label = 'default') {
const counts = this[kCounts];
counts.delete(`${label}`);
};
Console.prototype.group = function group(...data) {
if (data.length > 0) {
this.log(...data);
}
this[kGroupIndent] += ' ';
};
Console.prototype.groupCollapsed = Console.prototype.group;
Console.prototype.groupEnd = function groupEnd() {
this[kGroupIndent] =
this[kGroupIndent].slice(0, this[kGroupIndent].length - 2);
};
const keyKey = 'Key';
const valuesKey = 'Values';
const indexKey = '(index)';
const iterKey = '(iteration index)';
const isArray = (v) => ArrayIsArray(v) || isTypedArray(v) || isBuffer(v);
// https://console.spec.whatwg.org/#table
Console.prototype.table = function(tabularData, properties) {
if (properties !== undefined && !ArrayIsArray(properties))
throw new ERR_INVALID_ARG_TYPE('properties', 'Array', properties);
if (tabularData == null ||
(typeof tabularData !== 'object' && typeof tabularData !== 'function'))
return this.log(tabularData);
const final = (k, v) => this.log(cliTable(k, v));
const inspect = (v) => {
const opt = { depth: 0, maxArrayLength: 3 };
if (v !== null && typeof v === 'object' &&
!isArray(v) && ObjectKeys(v).length > 2)
opt.depth = -1;
Object.assign(opt, this[kGetInspectOptions](this._stdout));
return util.inspect(v, opt);
};
const getIndexArray = (length) => ArrayFrom({ length }, (_, i) => inspect(i));
const mapIter = isMapIterator(tabularData);
if (mapIter)
tabularData = previewMapIterator(tabularData);
if (mapIter || isMap(tabularData)) {
const keys = [];
const values = [];
let length = 0;
for (const [k, v] of tabularData) {
keys.push(inspect(k));
values.push(inspect(v));
length++;
}
return final([
iterKey, keyKey, valuesKey
], [
getIndexArray(length),
keys,
values,
]);
}
const setIter = isSetIterator(tabularData);
if (setIter)
tabularData = previewSetIterator(tabularData);
const setlike = setIter || isSet(tabularData);
if (setlike) {
const values = [];
let length = 0;
for (const v of tabularData) {
values.push(inspect(v));
length++;
}
return final([setlike ? iterKey : indexKey, valuesKey], [
getIndexArray(length),
values,
]);
}
const map = {};
let hasPrimitives = false;
const valuesKeyArray = [];
const indexKeyArray = ObjectKeys(tabularData);
for (var i = 0; i < indexKeyArray.length; i++) {
const item = tabularData[indexKeyArray[i]];
const primitive = item === null ||
(typeof item !== 'function' && typeof item !== 'object');
if (properties === undefined && primitive) {
hasPrimitives = true;
valuesKeyArray[i] = inspect(item);
} else {
const keys = properties || ObjectKeys(item);
for (const key of keys) {
if (map[key] === undefined)
map[key] = [];
if ((primitive && properties) || !hasOwnProperty(item, key))
map[key][i] = '';
else
map[key][i] = item == null ? item : inspect(item[key]);
}
}
}
const keys = ObjectKeys(map);
const values = ObjectValues(map);
if (hasPrimitives) {
keys.push(valuesKey);
values.push(valuesKeyArray);
}
keys.unshift(indexKey);
values.unshift(indexKeyArray);
return final(keys, values);
};
module.exports = new Console({
stdout: process.stdout,
stderr: process.stderr
});
module.exports.Console = Console;
function noop() {}
|
function get(n, m, s, prices) {
var store = {}
for (var c in prices) {
if (prices.hasOwnProperty(c)) {
prices[c] = Math.min(prices[c].add, prices[c].remove)
}
}
function f(i, j) {
var key = i + '-' + j
if (store[key]) return store[key];
var min = 0
if (i < j && !isPalindRome(s.substr(i, j-i+1))) {
min = Math.min(
prices[s[i]] + f(i+1, j),
prices[s[j]] + f(i, j-1)
)
}
store[key] = min
return min
}
function isPalindRome(s) {
return s === s.split('').reverse().join('')
}
return f(0, m-1)
}
console.log(
get(3, 4, 'abcb', {
a: {add: 1000, remove: 1100},
b: {add: 350, remove: 700},
c: {add: 200, remove: 800}
})
) |
/**
* A very basic Nightwatch custom command. The command name is the filename and the
* exported "command" function is the command.
*
* Example usage:
* browser.customExecute(function() {
* console.log('Hello from the browser window')
* });
*
* For more information on writing custom commands see:
* https://nightwatchjs.org/guide/extending-nightwatch/#writing-custom-commands
*
* @param {*} data
*/
exports.command = function command (data) {
// Other Nightwatch commands are available via "this"
// .execute() inject a snippet of JavaScript into the page for execution.
// the executed script is assumed to be synchronous.
//
// See https://nightwatchjs.org/api/execute.html for more info.
//
this.execute(
// The function argument is converted to a string and sent to the browser
function (argData) { return argData },
// The arguments for the function to be sent to the browser are specified in this array
[data],
function (result) {
// The "result" object contains the result of what we have sent back from the browser window
console.log('custom execute result:', result.value)
},
)
return this
}
|
import { observable, action } from 'mobx'
const <%= name %> = observable({
})
<%= name %>.replaceMeWithAnAction = action(() => {
})
export default <%= name %>
|
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { propType } from 'v2/util/inlinedGraphqlAnywhere'
import { graphql } from '@apollo/client/react/hoc'
import styled from 'styled-components'
import isEmail from 'lib/is_email.coffee'
import collaboratorSearchResultsQuery from 'v2/components/CollaboratorSearch/components/CollaboratorSearchResults/queries/collaboratorSearchResults'
import collaboratorSearchResultsFragment from 'v2/components/CollaboratorSearch/components/CollaboratorSearchResults/fragments/collaboratorSearchResults'
import SearchResult from 'v2/components/CollaboratorSearch/components/SearchResult'
import CollaboratorSearchResult from 'v2/components/CollaboratorSearch/components/CollaboratorSearchResult'
import CollaboratorInviteButton from 'v2/components/CollaboratorSearch/components/CollaboratorInviteButton'
const Status = styled(SearchResult)`
justify-content: center;
padding: 1.75em 1em;
`
class CollaboratorSearchResults extends Component {
static propTypes = {
query: PropTypes.string.isRequired,
onAdd: PropTypes.func.isRequired,
onInvite: PropTypes.func.isRequired,
data: PropTypes.shape({
loading: PropTypes.bool.isRequired,
results: propType(collaboratorSearchResultsFragment),
}).isRequired,
}
render() {
const {
data: { loading },
onAdd,
onInvite,
query,
} = this.props
if (isEmail(query)) {
return <CollaboratorInviteButton email={query} onInvite={onInvite} />
}
if (loading) {
return <Status>Searching...</Status>
}
const {
data: {
results: { collaborators },
},
} = this.props
if (collaborators.length === 0) {
return <Status>Nothing found.</Status>
}
return (
<div>
{collaborators.map(collaborator => (
<CollaboratorSearchResult
key={`${collaborator.__typename}-${collaborator.id}`}
result={collaborator}
onAdd={onAdd}
/>
))}
</div>
)
}
}
export default graphql(collaboratorSearchResultsQuery)(
CollaboratorSearchResults
)
|
'use strict';
const Fs = require('fs');
const Hoek = require('hoek');
const internals = {};
exports.Player = module.exports.Player = internals.Player = function (id) {
Hoek.assert(this instanceof internals.Player, 'Player must be instantiated using new');
this.id = id;
};
exports.Team = module.exports.Team = internals.Team = function (id) {
Hoek.assert(this instanceof internals.Team, 'Team must be instantiated using new');
this.id = id;
this.players = [];
this.spymaster = null;
};
exports.BoardElement = module.exports.BoardElement = internals.BoardElement = function (value, type) {
Hoek.assert(this instanceof internals.BoardElement, 'BoardElement must be instantiated using new');
this.value = value;
this.type = type;
};
//-- Game
exports.Game = module.exports.Game = internals.Game = function (id, creatorId) {
Hoek.assert(this instanceof internals.Game, 'Game must be instantiated using new');
this.id = id;
this.players = [];
this.players.push(creatorId);
this.teams = [new internals.Team('red'), new internals.Team('blue')]; //note hardcoded to 2 teams
this.NUM_RED_CARDS = 8;
this.NUM_BLUE_CARDS = 8;
this.NUM_GRAY_CARDS = 7;
this.NUM_BLACK_CARDS = 1;
this.BOARD_SIZE = 25;
// Define phases of the game
this.phases = {
SETUP: 'setup',
GIVE_CLUE: 'give_clue',
SELECT_WORD: 'select_word',
GAME_OVER: 'game_over'
};
// Define phases of the game
this.cardColors = {
RED: 'red',
BLUE: 'blue',
GRAY: 'gray',
BLACK: 'black',
NONE: 'none'
};
this._LoadWords();
this._LoadDictionary();
this.phase = this.phases.SETUP;
this.activeTeam = this.cardColors.RED;
this.remainingGuesses = 0;
this.remainingRedCards = 0;
this.remainingBlueCards = 0;
this.clue = null;
this.board = [];
this.winner = null;
};
internals.Game.prototype.Reset = function () {
this.players = [];
this.players.push(creatorId);
this.teams = [new internals.Team('red'), new internals.Team('blue')];
this.phase = this.phases.SETUP;
this.activeTeam = this.cardColors.RED;
this.remainingGuesses = 0;
this.remainingRedCards = 0;
this.remainingBlueCards = 0;
this.clue = null;
this.board = [];
this.winner = null;
};
//-- Game setup
internals.Game.prototype.AddPlayer = function (playerId) {
// Throw an error if game is not in setup
if (this.phase !== this.phases.SETUP) {
throw 'Cannot add player in current phase';
}
if (this.players.indexOf(playerId) !== -1) {
throw playerId + ' is already a player';
}
this.players.push(playerId);
};
internals.Game.prototype.RemovePlayer = function (playerId) {
const indexOfPlayer = this.players.indexOf(playerId);
// If the player is a current player:
if (indexOfPlayer !== -1) {
this.players.slice(index, 1);
}
// Loop for each team:
for (const team of this.teams) {
const indexOfPlayerInTeam = team.players.indexOf(playerId);
// If the player is on this team, remove him:
if (indexOfPlayerInTeam !== -1) {
team.players.slice(indexOfPlayerInTeam, 1);
}
// If the player is the spymaster on this team, remove him:
if (team.spymaster === playerId) {
team.spymaster = null;
}
}
};
internals.Game.prototype.getPlayers = function () {
return this.players;
};
internals.Game.prototype.AssignPlayerToTeam = function (playerId, teamId) {
// Throw an error if playerId is not a current player
if (this.players.indexOf(playerId) === -1) {
throw playerId + ' is not a current player';
}
// Throw an error if teamId is not a valid team
if ((teamId !== 'red') && (teamId !== 'blue')) {
throw teamId + ' is an invalid team';
}
// Throw an error if game is not in setup
if (this.phase !== this.phases.SETUP) {
throw 'Cannot assign player to team in current phase';
}
const otherTeamId = (teamId === 'red') ? 'blue' : 'red';
const index = this.GetTeam(otherTeamId).players.indexOf(playerId);
// If the player is already on the other team
if (index !== -1) {
// Remove player from other team
this.GetTeam(otherTeamId).players.splice(index, 1);
// If needed, remove player as other team's spymaster
if (this.GetTeam(otherTeamId).spymaster === playerId) {
this.GetTeam(otherTeamId).spymaster = null;
}
}
// Add player to specified team if he is not already on it
if (this.GetTeam(teamId).players.indexOf(playerId) === -1) {
this.GetTeam(teamId).players.push(playerId);
}
};
internals.Game.prototype.AssignTeamsRandomly = function () {
// Throw an error if game is not in setup
if (this.phase !== this.phases.SETUP) {
throw 'Cannot assign teams randomly in current phase';
}
this.teams[0] = new internals.Team('red');
this.teams[1] = new internals.Team('blue');
const firstTeamSize = Math.floor(this.players.length / 2);
while (this.GetTeam('red').players.length < firstTeamSize) {
const randomIndex = Math.floor(Math.random() * this.players.length);
this.AssignPlayerToTeam(this.players[randomIndex], 'red');
}
for (const playerId of this.players) {
// If this player is not on the first team, assign him to the second team:
if (this.GetTeam('red').players.indexOf(playerId) === -1) {
this.AssignPlayerToTeam(playerId, 'blue');
}
}
};
internals.Game.prototype.AssignSpymaster = function (playerId, teamId) {
// Throw an error if game is not in setup
if (this.phase !== this.phases.SETUP) {
throw 'Cannot assign spymasters in current phase';
}
// Throw an error if teamId is not a valid team
if ((teamId !== 'red') && (teamId !== 'blue')) {
throw teamId + ' is an invalid team';
}
this.AssignPlayerToTeam(playerId, teamId);
this.GetTeam(teamId).spymaster = playerId;
};
internals.Game.prototype.ChooseSpymasters = function () {
// Throw an error if game is not in setup
if (this.phase !== this.phases.SETUP) {
throw 'Cannot randomly assign spymasters in current phase';
}
if (this.GetTeam('red').players.length > 0) {
const randomIndex = Math.floor(Math.random() * this.GetTeam('red').players.length);
this.GetTeam('red').spymaster = this.GetTeam('red').players[randomIndex];
}
if (this.GetTeam('blue').players.length > 0) {
const randomIndex = Math.floor(Math.random() * this.GetTeam('blue').players.length);
this.GetTeam('blue').spymaster = this.GetTeam('blue').players[randomIndex];
}
};
internals.Game.prototype.GetTeams = function () {
return this.teams;
};
internals.Game.prototype.GetTeam = function (teamId) {
// Throw an error if teamId is not a valid team
if ((teamId !== 'red') && (teamId !== 'blue')) {
throw teamId + ' is an invalid team';
}
for (const team of this.teams) {
if (teamId === team.id) {
return team;
}
}
return null;
};
internals.Game.prototype.Start = function () {
if (this.IsReadyToStart() === false) {
throw 'Start game requirements has not been met.';
}
this._RandomizeBoardWords();
this._RandomizeBoardMap();
this.gameStarted = true;
this.phase = this.phases.GIVE_CLUE;
};
internals.Game.prototype.IsReadyToStart = function () {
const enoughPlayers = ((this.GetTeam('red').players.length >= 2) && (this.GetTeam('blue').players.length >= 2));
const spymastersAssigned = ((this.GetTeam('red').spymaster !== null) && (this.GetTeam('blue').spymaster !== null));
return (enoughPlayers && spymastersAssigned);
};
internals.Game.prototype.GiveClue = function (playerId, word, count) {
// Throw an error if playerId is not a current player
if (this.players.indexOf(playerId) === -1) {
throw playerId + ' is not a current player';
}
// Throw an error if the current phase is not GIVE_CLUE
if (this.phase !== this.phases.GIVE_CLUE) {
throw 'Current phase is not \'GIVE CLUE\'';
}
// Throw an error if playerId is not spymaster
if (!this._IsPlayerSpymaster(playerId)) {
throw playerId + ' is not spymaster';
}
// Throw an error if player's team is not active
if (this._GetTeamIdFromPlayer(playerId) !== this.activeTeam) {
throw playerId + ' is not on active team';
}
// Throw an error if count is invalid
if (isNaN(count) || count < 0) {
throw count + ' is invalid value';
}
const card = this._GetCardFromWord(word);
// Throw an error if the clue exists on the board and is not selected:
if ((card !== null) && (card.selected === false)) {
throw word + ' cannot be used as a clue';
}
// Throw an error if this word is not in the dictionary
if (!this.dictionary.has(word)) {
throw word + ' is not in the dictionary';
}
this.clue = { word: word, count: count };
this.remainingGuesses = count;
this.phase = this.phases.SELECT_WORD;
};
internals.Game.prototype.SelectWord = function (playerId, word) {
// Throw an error if playerId is not a current player
if (this.players.indexOf(playerId) === -1) {
throw playerId + ' is not a current player';
}
// Throw an error if the current phase is not SELECT_WORD
if (this.phase !== this.phases.SELECT_WORD) {
throw 'Current phase is not \'SELECT WORD\'';
}
// Throw an error if playerId is spymaster
if (this._IsPlayerSpymaster(playerId)) {
throw playerId + ' is a spymaster';
}
// Throw an error if player's team is not active
if (this._GetTeamIdFromPlayer(playerId) !== this.activeTeam) {
throw playerId + ' is not on active team';
}
const card = this._GetCardFromWord(word);
// Throw an error if word does not exist on board
if (card === null) {
throw word + ' does not exist on board';
}
// Throw an error if word is already selected
if (card.selected === true) {
throw word + ' does not exist on board';
}
card.selected = true;
--this.remainingGuesses;
if (card.color === this.cardColors.RED) {
--this.remainingRedCards;
}
else if (card.color === this.cardColors.BLUE) {
--this.remainingBlueCards;
}
// If either team has no more cards remaining:
if ((this.remainingRedCards === 0) || (this.remainingBlueCards === 0)) {
// Select the winner
if (this.numRedCardsRemaining === 0) {
this.winner = this.cardColors.RED;
}
else {
this.winner = this.cardColors.BLUE;
}
// End the game
this.phase = this.phases.GAME_OVER;
}
// Otherwise, if the active team chose the other team's card or a neutral card:
else if ((card.color === this._GetInactiveTeam()) || (card.color === this.cardColors.GRAY)) {
// End this team's turn
this._PassTurn();
}
// Otherwise, if the active team chose the assassin:
else if (card.color === this.cardColors.BLACK ) {
// The other team is automatically the winner
this.winner = this._GetInactiveTeam();
this.phase = this.phases.GAME_OVER;
}
else if (this.remainingGuesses === 0) {
// End this team's turn
this._PassTurn();
}
};
internals.Game.prototype.PassTurn = function (playerId) {
// Throw an error if playerId is not a current player
if (this.players.indexOf(playerId) === -1) {
throw playerId + ' is not a current player';
}
// Throw an error if the current phase is not SELECT_WORD
if (this.phase !== this.phases.SELECT_WORD) {
throw 'Current phase is not \'SELECT WORD\'';
}
// Throw an error if playerId is spymaster
if (this._IsPlayerSpymaster(playerId)) {
throw playerId + ' is a spymaster';
}
// Throw an error if player's team is not active
if (this._GetTeamIdFromPlayer(playerId) !== this.activeTeam) {
throw playerId + ' is not on active team';
}
this._PassTurn();
};
internals.Game.prototype._PassTurn = function () {
// End this team's turn
this.remainingGuesses = 0;
this.clue = null;
this.activeTeam = this._GetInactiveTeam();
this.phase = this.phases.GIVE_CLUE;
};
internals.Game.prototype.GetGameState = function () {
return {
phase: this.phase,
activeTeam: this.activeTeam,
clue: this.clue,
remainingGuesses: this.remainingGuesses,
remainingRedCards: this.remainingRedCards,
remainingBlueCards: this.remainingBlueCards,
board: this.board,
winner: this.winner
};
};
internals.Game.prototype.GetPhase = function () {
return this.phase;
};
internals.Game.prototype.GetActiveTeam = function () {
return this.activeTeam;
};
internals.Game.prototype.GetCurrentClue = function () {
return this.clue;
};
internals.Game.prototype.GetRemainingGuesses = function () {
return this.remainingGuesses;
};
internals.Game.prototype.GetRemainingCardsForTeam = function (teamId) {
// Throw an error if teamId is not a valid team
if ((teamId !== 'red') && (teamId !== 'blue')) {
throw teamId + ' is an invalid team';
}
if (teamId === 'red') {
return this.remainingRedCards;
}
return this.remainingBlueCards;
};
internals.Game.prototype.GetBoard = function () {
return this.board;
};
internals.Game.prototype.GetWinner = function () {
return this.winner;
};
internals.Game.prototype._GetTeamIdFromPlayer = function (playerId) {
for (const team of this.teams) {
if (team.players.indexOf(playerId) !== -1) {
return team.id;
}
}
return null;
};
internals.Game.prototype._IsPlayerSpymaster = function (playerId) {
for (const team of this.teams) {
if (team.spymaster === playerId) {
return true;
}
}
return false;
};
internals.Game.prototype._GetCardFromWord = function (word) {
for (const card of this.board) {
if (card.word === word) {
return card;
}
}
return null;
};
internals.Game.prototype._GetInactiveTeam = function () {
const inactiveTeam = (this.activeTeam === 'blue') ? 'red' : 'blue';
return inactiveTeam;
};
internals.Game.prototype._LoadWords = function () {
this.words = Fs.readFileSync(__dirname + '/resources/words.txt').toString().replace(/[\r]+/g,'').split('\n');
};
internals.Game.prototype._LoadDictionary = function () {
this.dictionary = new Set();
const dictionaryArray = Fs.readFileSync(__dirname + '/resources/dictionary.txt').toString().replace(/[\r]+/g,'').split('\n');
for (const word of dictionaryArray) {
this.dictionary.add(word);
}
};
internals.Game.prototype._RandomizeBoardWords = function () {
const wordSet = new Set();
while (this.board.length < this.BOARD_SIZE) {
const randomIndex = Math.floor(Math.random() * this.words.length);
const randomWord = this.words[randomIndex];
if (!wordSet.has(randomWord)) {
this.board.push({ word: randomWord, color: this.cardColors.NONE, selected: false });
wordSet.add(randomWord);
}
}
};
internals.Game.prototype._RandomizeBoardMap = function () {
let numRedCardsRemaining = this.NUM_RED_CARDS;
let numBlueCardsRemaining = this.NUM_BLUE_CARDS;
let numGrayCardsRemaining = this.NUM_GRAY_CARDS;
let numBlackCardsRemaining = this.NUM_BLACK_CARDS;
const randomCoin = Math.floor(Math.random() * 2);
if (randomCoin === 0) {
++numRedCardsRemaining;
this.activeTeam = this.cardColors.RED;
}
else {
++numBlueCardsRemaining;
this.activeTeam = this.cardColors.BLUE;
}
this.remainingRedCards = numRedCardsRemaining;
this.remainingBlueCards = numBlueCardsRemaining;
for (const card of this.board) {
if (numRedCardsRemaining > 0) {
card.color = this.cardColors.RED;
--numRedCardsRemaining;
}
else if (numBlueCardsRemaining > 0) {
card.color = this.cardColors.BLUE;
--numBlueCardsRemaining;
}
else if (numGrayCardsRemaining > 0) {
card.color = this.cardColors.GRAY;
--numGrayCardsRemaining;
}
else {
card.color = this.cardColors.BLACK;
--numBlackCardsRemaining;
}
}
// Shuffle the board again to shuffle the color assignments
this._ShuffleBoard();
};
internals.Game.prototype._ShuffleBoard = function () {
for (let i = this.board.length - 1; i > 0; --i) {
// Add 1 to allow all cards an equal chance of being selected
const randomIndex = Math.floor(Math.random() * (i + 1));
// Swap a card at random index with index i
const tempCard = this.board[i];
this.board[i] = this.board[randomIndex];
this.board[randomIndex] = tempCard;
}
};
|
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
cordova.define("org.apache.cordova.file.Entry", function(require, exports, module) {
var argscheck = require('cordova/argscheck');
var exec = require('cordova/exec');
var FileError = require('./FileError');
var Metadata = require('./Metadata');
/**
* Represents a file or directory on the local file system.
*
* @param isFile
* {boolean} true if Entry is a file (readonly)
* @param isDirectory
* {boolean} true if Entry is a directory (readonly)
* @param name
* {DOMString} name of the file or directory, excluding the path
* leading to it (readonly)
* @param fullPath
* {DOMString} the absolute full path to the file or directory
* (readonly)
* @param fileSystem
* {FileSystem} the filesystem on which this entry resides
* (readonly)
* @param nativeURL
* {DOMString} an alternate URL which can be used by native
* webview controls, for example media players.
* (optional, readonly)
*/
function Entry (isFile, isDirectory, name, fullPath, fileSystem, nativeURL) {
this.isFile = !!isFile;
this.isDirectory = !!isDirectory;
this.name = name || '';
this.fullPath = fullPath || '';
this.filesystem = fileSystem || null;
this.nativeURL = nativeURL || null;
}
/**
* Look up the metadata of the entry.
*
* @param successCallback
* {Function} is called with a Metadata object
* @param errorCallback
* {Function} is called with a FileError
*/
Entry.prototype.getMetadata = function (successCallback, errorCallback) {
argscheck.checkArgs('FF', 'Entry.getMetadata', arguments);
var success = successCallback && function (entryMetadata) {
var metadata = new Metadata({
size: entryMetadata.size,
modificationTime: entryMetadata.lastModifiedDate
});
successCallback(metadata);
};
var fail = errorCallback && function (code) {
errorCallback(new FileError(code));
};
exec(success, fail, 'File', 'getFileMetadata', [this.toInternalURL()]);
};
/**
* Set the metadata of the entry.
*
* @param successCallback
* {Function} is called with a Metadata object
* @param errorCallback
* {Function} is called with a FileError
* @param metadataObject
* {Object} keys and values to set
*/
Entry.prototype.setMetadata = function (successCallback, errorCallback, metadataObject) {
argscheck.checkArgs('FFO', 'Entry.setMetadata', arguments);
exec(successCallback, errorCallback, 'File', 'setMetadata', [this.toInternalURL(), metadataObject]);
};
/**
* Move a file or directory to a new location.
*
* @param parent
* {DirectoryEntry} the directory to which to move this entry
* @param newName
* {DOMString} new name of the entry, defaults to the current name
* @param successCallback
* {Function} called with the new DirectoryEntry object
* @param errorCallback
* {Function} called with a FileError
*/
Entry.prototype.moveTo = function (parent, newName, successCallback, errorCallback) {
argscheck.checkArgs('oSFF', 'Entry.moveTo', arguments);
var fail = errorCallback && function (code) {
errorCallback(new FileError(code));
};
var srcURL = this.toInternalURL();
// entry name
var name = newName || this.name;
var success = function (entry) {
if (entry) {
if (successCallback) {
// create appropriate Entry object
var newFSName = entry.filesystemName || (entry.filesystem && entry.filesystem.name);
var fs = newFSName ? new FileSystem(newFSName, { name: '', fullPath: '/' }) : new FileSystem(parent.filesystem.name, { name: '', fullPath: '/' }); // eslint-disable-line no-undef
var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath, fs, entry.nativeURL) : new (require('cordova-plugin-file.FileEntry'))(entry.name, entry.fullPath, fs, entry.nativeURL);
successCallback(result);
}
} else {
// no Entry object returned
if (fail) {
fail(FileError.NOT_FOUND_ERR);
}
}
};
// copy
exec(success, fail, 'File', 'moveTo', [srcURL, parent.toInternalURL(), name]);
};
/**
* Copy a directory to a different location.
*
* @param parent
* {DirectoryEntry} the directory to which to copy the entry
* @param newName
* {DOMString} new name of the entry, defaults to the current name
* @param successCallback
* {Function} called with the new Entry object
* @param errorCallback
* {Function} called with a FileError
*/
Entry.prototype.copyTo = function (parent, newName, successCallback, errorCallback) {
argscheck.checkArgs('oSFF', 'Entry.copyTo', arguments);
var fail = errorCallback && function (code) {
errorCallback(new FileError(code));
};
var srcURL = this.toInternalURL();
// entry name
var name = newName || this.name;
// success callback
var success = function (entry) {
if (entry) {
if (successCallback) {
// create appropriate Entry object
var newFSName = entry.filesystemName || (entry.filesystem && entry.filesystem.name);
var fs = newFSName ? new FileSystem(newFSName, { name: '', fullPath: '/' }) : new FileSystem(parent.filesystem.name, { name: '', fullPath: '/' }); // eslint-disable-line no-undef
var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath, fs, entry.nativeURL) : new (require('cordova-plugin-file.FileEntry'))(entry.name, entry.fullPath, fs, entry.nativeURL);
successCallback(result);
}
} else {
// no Entry object returned
if (fail) {
fail(FileError.NOT_FOUND_ERR);
}
}
};
// copy
exec(success, fail, 'File', 'copyTo', [srcURL, parent.toInternalURL(), name]);
};
/**
* Return a URL that can be passed across the bridge to identify this entry.
*/
Entry.prototype.toInternalURL = function () {
if (this.filesystem && this.filesystem.__format__) {
return this.filesystem.__format__(this.fullPath, this.nativeURL);
}
};
/**
* Return a URL that can be used to identify this entry.
* Use a URL that can be used to as the src attribute of a <video> or
* <audio> tag. If that is not possible, construct a cdvfile:// URL.
*/
Entry.prototype.toURL = function () {
if (this.nativeURL) {
return this.nativeURL;
}
// fullPath attribute may contain the full URL in the case that
// toInternalURL fails.
return this.toInternalURL() || 'file://localhost' + this.fullPath;
};
/**
* Backwards-compatibility: In v1.0.0 - 1.0.2, .toURL would only return a
* cdvfile:// URL, and this method was necessary to obtain URLs usable by the
* webview.
* See CB-6051, CB-6106, CB-6117, CB-6152, CB-6199, CB-6201, CB-6243, CB-6249,
* and CB-6300.
*/
Entry.prototype.toNativeURL = function () {
console.log("DEPRECATED: Update your code to use 'toURL'");
return this.toURL();
};
/**
* Returns a URI that can be used to identify this entry.
*
* @param {DOMString} mimeType for a FileEntry, the mime type to be used to interpret the file, when loaded through this URI.
* @return uri
*/
Entry.prototype.toURI = function (mimeType) {
console.log("DEPRECATED: Update your code to use 'toURL'");
return this.toURL();
};
/**
* Remove a file or directory. It is an error to attempt to delete a
* directory that is not empty. It is an error to attempt to delete a
* root directory of a file system.
*
* @param successCallback {Function} called with no parameters
* @param errorCallback {Function} called with a FileError
*/
Entry.prototype.remove = function (successCallback, errorCallback) {
argscheck.checkArgs('FF', 'Entry.remove', arguments);
var fail = errorCallback && function (code) {
errorCallback(new FileError(code));
};
exec(successCallback, fail, 'File', 'remove', [this.toInternalURL()]);
};
/**
* Look up the parent DirectoryEntry of this entry.
*
* @param successCallback {Function} called with the parent DirectoryEntry object
* @param errorCallback {Function} called with a FileError
*/
Entry.prototype.getParent = function (successCallback, errorCallback) {
argscheck.checkArgs('FF', 'Entry.getParent', arguments);
var fs = this.filesystem;
var win = successCallback && function (result) {
var DirectoryEntry = require('./DirectoryEntry');
var entry = new DirectoryEntry(result.name, result.fullPath, fs, result.nativeURL);
successCallback(entry);
};
var fail = errorCallback && function (code) {
errorCallback(new FileError(code));
};
exec(win, fail, 'File', 'getParent', [this.toInternalURL()]);
};
module.exports = Entry;
}); |
var searchData=
[
['ddbuilder',['DdBuilder',['../classtdzdd_1_1DdBuilder.html',1,'tdzdd']]],
['ddbuildermp',['DdBuilderMP',['../classtdzdd_1_1DdBuilderMP.html',1,'tdzdd']]],
['dddumper',['DdDumper',['../classtdzdd_1_1DdDumper.html',1,'tdzdd']]],
['ddeval',['DdEval',['../classtdzdd_1_1DdEval.html',1,'tdzdd']]],
['ddeval_3c_20bddcardinality_3c_20t_2c_20ar_20_3e_2c_20t_20_3e',['DdEval< BddCardinality< T, AR >, T >',['../classtdzdd_1_1DdEval.html',1,'tdzdd']]],
['ddeval_3c_20e_2c_20bignumber_2c_20std_3a_3astring_20_3e',['DdEval< E, BigNumber, std::string >',['../classtdzdd_1_1DdEval.html',1,'tdzdd']]],
['ddeval_3c_20e_2c_20t_20_3e',['DdEval< E, T >',['../classtdzdd_1_1DdEval.html',1,'tdzdd']]],
['ddeval_3c_20tozbdd_2c_20zbdd_20_3e',['DdEval< ToZBDD, ZBDD >',['../classtdzdd_1_1DdEval.html',1,'tdzdd']]],
['ddeval_3c_20zddcardinality_3c_20t_2c_20ar_20_3e_2c_20t_20_3e',['DdEval< ZddCardinality< T, AR >, T >',['../classtdzdd_1_1DdEval.html',1,'tdzdd']]],
['ddspec',['DdSpec',['../classtdzdd_1_1DdSpec.html',1,'tdzdd']]],
['ddspec_3c_20ddstructure_3c_20arity_20_3e_2c_20nodeid_2c_20arity_20_3e',['DdSpec< DdStructure< ARITY >, NodeId, ARITY >',['../classtdzdd_1_1DdSpec.html',1,'tdzdd']]],
['ddspec_3c_20graphillionzdd_2c_20uint64_5ft_2c_202_20_3e',['DdSpec< GraphillionZdd, uint64_t, 2 >',['../classtdzdd_1_1DdSpec.html',1,'tdzdd']]],
['ddspec_3c_20pathzddbystdmap_2c_20std_3a_3amap_3c_20int16_5ft_2c_20int16_5ft_20_3e_2c_202_20_3e',['DdSpec< PathZddByStdMap, std::map< int16_t, int16_t >, 2 >',['../classtdzdd_1_1DdSpec.html',1,'tdzdd']]],
['ddspec_3c_20sapporozdd_2c_20zbdd_2c_202_20_3e',['DdSpec< SapporoZdd, ZBDD, 2 >',['../classtdzdd_1_1DdSpec.html',1,'tdzdd']]],
['ddspec_3c_20sizeconstraint_2c_20int_2c_202_20_3e',['DdSpec< SizeConstraint, int, 2 >',['../classtdzdd_1_1DdSpec.html',1,'tdzdd']]],
['ddspecbase',['DdSpecBase',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20bddlookahead_3c_20s_20_3e_2c_20s_3a_3aarity_20_3e',['DdSpecBase< BddLookahead< S >, S::ARITY >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20bddunreduction_3c_20s_20_3e_2c_20ar_20_3e',['DdSpecBase< BddUnreduction< S >, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20cyclezdd_2c_20ar_20_3e',['DdSpecBase< CycleZdd, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20ddstructure_3c_20arity_20_3e_2c_20ar_20_3e',['DdSpecBase< DdStructure< ARITY >, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20degreeconstraint_2c_20ar_20_3e',['DdSpecBase< DegreeConstraint, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20frontierbasedsearch_2c_20ar_20_3e',['DdSpecBase< FrontierBasedSearch, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20graphillionzdd_2c_20ar_20_3e',['DdSpecBase< GraphillionZdd, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20hamiltoncyclezdd_2c_20ar_20_3e',['DdSpecBase< HamiltonCycleZdd, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20hamiltonpathzdd_2c_20ar_20_3e',['DdSpecBase< HamiltonPathZdd, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20linearconstraints_3c_20t_20_3e_2c_20ar_20_3e',['DdSpecBase< LinearConstraints< T >, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20pathzdd_2c_20ar_20_3e',['DdSpecBase< PathZdd, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20pathzddbystdmap_2c_20ar_20_3e',['DdSpecBase< PathZddByStdMap, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20sapporozdd_2c_20ar_20_3e',['DdSpecBase< SapporoZdd, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20sizeconstraint_2c_20ar_20_3e',['DdSpecBase< SizeConstraint, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20t_2c_20ar_20_3e',['DdSpecBase< T, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20zddlookahead_3c_20s_20_3e_2c_20s_3a_3aarity_20_3e',['DdSpecBase< ZddLookahead< S >, S::ARITY >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddspecbase_3c_20zddunreduction_3c_20s_20_3e_2c_20ar_20_3e',['DdSpecBase< ZddUnreduction< S >, AR >',['../classtdzdd_1_1DdSpecBase.html',1,'tdzdd']]],
['ddstructure',['DdStructure',['../classtdzdd_1_1DdStructure.html',1,'tdzdd']]],
['ddsweeper',['DdSweeper',['../classtdzdd_1_1DdSweeper.html',1,'tdzdd']]],
['ddsweeper_3c_20ar_20_3e',['DdSweeper< AR >',['../classtdzdd_1_1DdSweeper.html',1,'tdzdd']]],
['ddvalues',['DdValues',['../classtdzdd_1_1DdValues.html',1,'tdzdd']]]
];
|
import ApplicationRoutes from "./ApplicationRoutes.js";
export default ApplicationRoutes;
|
/**
* TIPOS DE ACCIONES
*
*/
// Muestra el loading
export const SHOW_LOADING = 'SHOW_LOADING';
// Oculta el loading
export const HIDDEN_LOADING = 'HIDDEN_LOADING';
// Deshabilita el loading para que no se pueda mostrar
export const DISABLE_LOADING = 'DISABLE_LOADING';
// Vuelve a habilitar el loading
export const ACTIVE_LOADING = 'ACTIVE_LOADING';
|
/**
* Created by chris on 1/9/17.
*/
import {Observable} from 'rxjs';
import {getCurrentUserId, getUser, getCurrentGameId, getGame, getGamePlayerFromUser} from 'utils/stateTraversal';
import {gameCanStart, checkForVictory} from 'utils/validators';
import {wordList, CardColor, CardState} from 'models/card';
import {Teams, Roles, GameStatus} from 'models/game';
export const INIT_GAMEPLAY = 'codenames/actions/gameplay/initGameplay';
export const CHECK_VICTORY = 'codenames/actions/gameplay/checkVictory';
export const GUESS = 'codenames/actions/gameplay/guess';
export const PASS = 'codenames/actions/gameplay/pass';
export const GIVE_CLUE = 'codenames/actions/gameplay/giveClue';
export const SUGGEST_TO_TEAM = 'codenames/actions/gameplay/suggestToTeam';
import horizonRedux from 'app/horizon/redux';
export const ACTION_TYPES = {
GUESS: 'Guess',
GIVE_CLUE: 'Give Clue',
PASS: 'Pass'
};
horizonRedux.takeLatest(
INIT_GAMEPLAY,
(horizon, action, getState) => {
const state = getState();
const currentGameId = getCurrentGameId(state);
if (currentGameId != action.gameId) {
return Observable.empty();
}
const game = getGame(state, action.gameId);
if (!gameCanStart(game.players)) {
return Observable.empty();
}
game.play = getInitialState();
game.status = GameStatus.IN_PROGRESS;
return horizon('games').replace(game);
},
(result, action, dispatch) => {},
err => console.err('error initializing gameplay', err)
);
horizonRedux.takeLatest(
PASS,
(horizon, action, getState) => {
const state = getState();
const gameId = getCurrentGameId(state);
const userId = getCurrentUserId(state);
const game = getGame(state, gameId);
const player = getGamePlayerFromUser(state, gameId, userId);
if (!gameId || !userId || !game || !player) {
return Observable.empty();
}
const gameplay = game.play;
if (gameplay.nextMoveType != ACTION_TYPES.GUESS || player.team != gameplay.nextMove || player.role != Roles.GUESSER) {
return Observable.empty();
}
const move = {userId, action: ACTION_TYPES.PASS};
const newGameplay = {
nextMove: getOtherTeam(player.team),
nextMoveType: ACTION_TYPES.GIVE_CLUE,
moves: [...gameplay.moves, move]
};
return horizon('games').update({id: gameId, play: newGameplay});
},
(result, action, dispatch) => {},
err => console.err('error attempting to pass', err)
);
horizonRedux.takeLatest(
GUESS,
(horizon, action, getState) => {
const state = getState();
const gameId = getCurrentGameId(state);
const userId = getCurrentUserId(state);
const game = getGame(state, gameId);
const player = getGamePlayerFromUser(state, gameId, userId);
if (!gameId || !userId || !game || !player) {
return Observable.empty();
}
const gameplay = game.play;
// Make sure it's a valid time to guess
if (gameplay.nextMoveType != ACTION_TYPES.GUESS || player.team != gameplay.nextMove || player.role != Roles.GUESSER) {
return Observable.empty();
}
// If it's already been guessed, ignore it.
if (action.card.status == CardState.GUESSED) {
return Observable.empty();
}
action.card.status = CardState.GUESSED;
const move = {userId, action: ACTION_TYPES.GUESS, card: action.card};
if (action.card.color == CardColor.BLACK) {
// Assassin card
const newGameplay = {
guessesRemaining: 0,
clue: '',
nextMove: null,
nextMoveType: null,
board: gameplay.board,
moves: [...gameplay.moves, move],
};
return horizon('games').update({id: gameId, status: GameStatus.COMPLETED, victor: getOtherTeam(player.team), play: newGameplay});
}
else if (action.card.color == player.team && gameplay.guessesRemaining != 1) {
const newGameplay = {
guessesRemaining: gameplay.guessesRemaining - 1,
board: gameplay.board,
moves: [...gameplay.moves, move]
};
return horizon('games').update({id: gameId, play: newGameplay});
}
// If they guessed incorrectly, or they're out of guesses, it passes to the other team.
else {
const newGameplay = {
guessesRemaining: 0,
nextMoveType: ACTION_TYPES.GIVE_CLUE,
nextMove: getOtherTeam(player.team),
clue: '',
board: gameplay.board,
moves: [...gameplay.moves, move]
};
return horizon('games').update({id: gameId, play: newGameplay});
}
},
(result, action, dispatch) => {
dispatch(checkVictory(result));
},
err => console.err('error making guess', err)
);
horizonRedux.takeLatest(
GIVE_CLUE,
(horizon, action, getState) => {
const state = getState();
const gameId = getCurrentGameId(state);
const userId = getCurrentUserId(state);
const game = getGame(state, gameId);
const player = getGamePlayerFromUser(state, gameId, userId);
if (!gameId || !userId || !game || !player) {
return Observable.empty();
}
const gameplay = game.play;
if (gameplay.nextMoveType != ACTION_TYPES.GIVE_CLUE || player.team != gameplay.nextMove || player.role != Roles.GIVER) {
return Observable.empty();
}
const move = {
userId,
action: ACTION_TYPES.GIVE_CLUE,
clue: action.clue,
count: action.count
};
const newGameplay = {
guessesRemaining: action.count != 0 ? action.count + 1 : -1,
clue: action.clue,
moves: [...gameplay.moves, move],
nextMoveType: ACTION_TYPES.GUESS
};
return horizon('games').update({id: gameId, play: newGameplay});
},
(result, action, dispatch) => {},
err => console.err('error giving clue', err)
);
horizonRedux.takeLatest(
CHECK_VICTORY,
(horizon, action, getState) => {
const state = getState();
const gameId = getCurrentGameId(state);
const userId = getCurrentUserId(state);
const game = getGame(state, gameId);
if (!gameId || !userId || !game) {
return Observable.empty();
}
const victor = checkForVictory(game.play.board);
if (!victor) {
return Observable.empty();
}
const play = {
nextMove: null,
nextMoveType: null,
clue: '',
guessesRemaining: 0
};
return horizon('games').update({id: gameId, status: GameStatus.COMPLETED, play, victor});
},
(result, action, dispatch) => {},
err => console.err('error checking victory')
);
const getOtherTeam = team => team == Teams.RED ? Teams.BLUE : Teams.RED;
const generateBoard = (firstTeam = Teams.RED) => {
const getNextWord = (alreadyUsed, wordList) => {
let nextIndex = Math.random() * wordList.length | 0;
while(alreadyUsed.indexOf(nextIndex) != -1) {
nextIndex++;
}
alreadyUsed.push(nextIndex);
return wordList[nextIndex];
};
const board = [];
const alreadyUsed = [];
let i;
for (i = 0; i < 25; i++) {
const nextWord = getNextWord(alreadyUsed, wordList);
const newCard = {word: nextWord, status: CardState.FRESH, key: alreadyUsed[alreadyUsed.length - 1]};
if (i < 8) {
newCard.color = CardColor.RED;
}
else if (i < 16) {
newCard.color = CardColor.BLUE;
}
else if (i < 23) {
newCard.color = CardColor.BROWN;
}
else if (i == 23) {
newCard.color = CardColor.BLACK;
}
else {
newCard.color = firstTeam;
}
board.push(newCard);
}
for (i = 0; i < board.length; i++) {
const swapIndex = Math.random() * board.length | 0;
const swapCard = board[swapIndex];
board[swapIndex] = board[i];
board[i] = swapCard;
}
return board;
};
const getInitialState = () => {
const board = generateBoard();
const redFirst = board.filter(card => card.color == CardColor.RED).length == 9;
return {
board: board,
nextMove: redFirst ? Teams.RED : Teams.BLUE,
nextMoveType: ACTION_TYPES.GIVE_CLUE,
guessesRemaining: 0,
clue: '',
moves: []
}
};
export const initGameplay = (gameId) => ({
type: INIT_GAMEPLAY,
gameId
});
export const checkVictory = (gameId) => ({
type: CHECK_VICTORY,
gameId
});
export const pass = (userId) => ({
type: PASS,
userId
});
export const guess = (userId, card) => ({
type: GUESS,
userId,
card
});
export const giveClue = (userId, clue, count) => ({
type: GIVE_CLUE,
userId,
clue,
count
});
export const gameplayActions = {
initGameplay,
pass,
guess,
giveClue
};
|
'use strict';
var factory = require('../octicon.js');
// This is an auto-generated ES2015 icon from the modularize script. Please do not modify this file.
var quote = factory('quote', 14, 16, {"fill-rule":"evenodd","d":"M6.16 3.5C3.73 5.06 2.55 6.67 2.55 9.36c.16-.05.3-.05.44-.05 1.27 0 2.5.86 2.5 2.41 0 1.61-1.03 2.61-2.5 2.61-1.9 0-2.99-1.52-2.99-4.25 0-3.8 1.75-6.53 5.02-8.42L6.16 3.5zm7 0c-2.43 1.56-3.61 3.17-3.61 5.86.16-.05.3-.05.44-.05 1.27 0 2.5.86 2.5 2.41 0 1.61-1.03 2.61-2.5 2.61-1.89 0-2.98-1.52-2.98-4.25 0-3.8 1.75-6.53 5.02-8.42l1.14 1.84h-.01z"}, ["quotation"]);
module.exports = quote;
|
describe('mobie.core.helpers', function () {
describe('Helpers', function () {
beforeEach(module('mobie.core.helpers'));
it('should generate an id', function () {
assert.equal('number', typeof nextId())
});
});
}); |
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isEmptyObjectOrArray = exports.makeFlattenedShallow = undefined;
var _typeOf = require('type-of');
var _typeOf2 = _interopRequireDefault(_typeOf);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
* Returns a shallow version of the shallow object to remove redundancy
* and simplify complex operations.
*
* @param {object} subject the flattened object to perform the operation on.
* @returns {object}
*/
var makeFlattenedShallow = exports.makeFlattenedShallow = function makeFlattenedShallow(subject) {
var resp = {};
for (var keyChain in subject) {
var shallow = false;
for (var keyChain2 in subject) {
if (keyChain !== keyChain2 && keyChain2.indexOf(keyChain) === 0) {
// also make sure that if the different still contains a period
// otherwise we could be dealing with similar keys like 'name' and 'names'
var remainder = keyChain2.replace(keyChain);
if (remainder.includes('.') && remainder.split('.').length === 2) {
shallow = true;
}
}
}
if (isEmptyObjectOrArray(subject[keyChain])) {
shallow = false;
}
if (!shallow) {
resp[keyChain] = subject[keyChain];
}
}
return resp;
};
/*
* Returns true if we're dealing with an empty array or object
*
* @param {object} subject the array or object to check
* @returns {boolean}
*/
var isEmptyObjectOrArray = exports.isEmptyObjectOrArray = function isEmptyObjectOrArray(subject) {
if ((0, _typeOf2.default)(subject) === 'object' || (0, _typeOf2.default)(subject) === 'array') {
if (Object.keys(subject).length === 0) {
return true;
}
}
return false;
}; |
var React = require('react');
var Reflux = require('reflux');
var Example3Store = require('../../stores/example3Store');
var InputAChangeActions = require('../../actions/InputAChangeAction');
var Child1 = React.createClass({
mixins: [Reflux.connect(Example3Store, 'store')],
handleChange: function(name, event) {
InputAChangeActions.inputChanged(name, event.target.value);
},
render: function() {
var inputA = null;
if (this.state.store) {
inputA = this.state.inputA;
}
return (
<div>
<br/>
<label>Input A</label>
<input id="inputA" name="inputA" onChange={this.handleChange.bind(this, 'inputA')}/>
</div>
);
}
});
module.exports = Child1; |
var pump__8py_8js =
[
[ "pump_8py", "pump__8py_8js.html#a9e35fb16373682bd01b46640ceb90119", null ]
]; |
const choo = require('choo');
const html = require('choo/html');
const http = require('xhr');
const find = require('lodash/find');
const queryString = require('query-string');
const store = require('./utils/localstorage.js');
const scrollIntoView = require('scroll-into-view');
const app = choo();
const appURL = 'https://5calls.org';
const debug = false;
// const appURL = 'http://localhost:8090';
// get the stored zip location
cachedAddress = '';
store.getAll('org.5calls.location', (location) => {
if (location.length > 0) {
cachedAddress = location[0]
}
});
// get the stored geo location
cachedGeo = '';
store.getAll('org.5calls.geolocation', (geo) => {
if (geo.length > 0) {
console.log("geo get",geo[0]);
cachedGeo = geo[0]
}
});
let cachedFetchingLocation = (cachedGeo === '') ? true : false;
// get the stored geo location
cachedAllowBrowserGeo = true;
store.getAll('org.5calls.allow_geolocation', (allowGeo) => {
if (allowGeo.length > 0) {
console.log("allowGeo get",allowGeo[0]);
cachedAllowBrowserGeo = allowGeo[0]
}
});
let cachedLocationFetchType = (cachedAllowBrowserGeo) ? 'browserGeolocation' : 'ipAddress';
// get the time the geo was last fetched
cachedGeoTime = '';
store.getAll('org.5calls.geolocation_time', (geo) => {
if (geo.length > 0) {
console.log("geo time get",geo[0]);
cachedGeoTime = geo[0]
}
});
cachedCity = '';
store.getAll('org.5calls.geolocation_city', (city) => {
if (city.length > 0) {
console.log("city get",city[0]);
cachedCity = city[0]
}
});
cachedFetchingLocation = (cachedCity !== '') ? true : cachedFetchingLocation;
cachedLocationFetchType = (cachedAddress !== '') ? 'address' : cachedLocationFetchType;
// get the stored completed issues
completedIssues = [];
store.getAll('org.5calls.completed', (completed) => {
completedIssues = completed == null ? [] : completed;
});
app.model({
state: {
// remote data
issues: [],
totalCalls: 0,
splitDistrict: false,
// manual input address
address: cachedAddress,
// automatically geolocating
geolocation: cachedGeo,
geoCacheTime: cachedGeoTime,
allowBrowserGeo: cachedAllowBrowserGeo,
cachedCity: cachedCity,
// view state
// getInfo: false,
// activeIssue: false,
// completeIssue: false,
askingLocation: false,
fetchingLocation: cachedFetchingLocation,
locationFetchType: cachedLocationFetchType,
contactIndex: 0,
completedIssues: completedIssues,
showFieldOfficeNumbers: false,
debug: debug,
},
reducers: {
receiveIssues: (state, data) => {
response = JSON.parse(data)
issues = response.issues //.filter((v) => { return v.contacts.length > 0 });
return { issues: issues, splitDistrict: response.splitDistrict, invalidAddress: response.invalidAddress }
},
receiveTotals: (state, data) => {
totals = JSON.parse(data);
return { totalCalls: totals.count }
},
receiveIPInfoLoc: (state, data) => {
geo = data.loc
city = data.city
time = new Date().valueOf()
store.replace("org.5calls.geolocation", 0, geo, () => {});
store.replace("org.5calls.geolocation_city", 0, city, () => {});
store.replace("org.5calls.geolocation_time", 0, time, () => {});
return { geolocation: geo, cachedCity: city, geoCacheTime: time, fetchingLocation: false, askingLocation: false }
},
changeActiveIssue: (state, issueId) => {
return { contactIndex: 0 }
},
setContactIndex: (state, data) => {
if (data.newIndex != 0) {
return { contactIndex: data.newIndex }
} else {
store.add("org.5calls.completed", data.issueid, () => {})
return { contactIndex: 0, completedIssues: state.completedIssues.concat(data.issueid) }
}
},
setAddress: (state, address) => {
Raven.setExtraContext({ address: address })
store.replace("org.5calls.location", 0, address, () => {});
return { address: address, askingLocation: false }
},
setGeolocation: (state, data) => {
store.replace("org.5calls.geolocation", 0, data, () => {});
return { geolocation: data, fetchingLocation: false }
},
setCachedCity: (state, data) => {
response = JSON.parse(data);
if (response.normalizedLocation && state.cachedCity == '') {
store.replace("org.5calls.geolocation_city", 0, response.normalizedLocation, () => {});
return { cachedCity: response.normalizedLocation }
} else {
return null
}
},
fetchingLocation: (state, data) => {
return { fetchingLocation: data }
},
allowBrowserGeolocation: (state, data) => {
store.replace("org.5calls.allow_geolocation", 0, data, () => {})
return { allowBrowserGeo: data }
},
enterLocation: (state, data) => {
scrollIntoView(document.querySelector('#address'));
return { askingLocation: true }
},
setLocationFetchType: (state, data) => {
let askingLocation = (data === 'address');
return { locationFetchType: data, askingLocation: askingLocation, fetchingLocation: !askingLocation }
},
resetLocation: (state, data) => {
store.remove("org.5calls.location", () => {});
store.remove("org.5calls.geolocation", () => {});
store.remove("org.5calls.geolocation_city", () => {});
store.remove("org.5calls.geolocation_time", () => {});
return { address: '', geolocation: '', cachedCity: '', geoCacheTime: '' }
},
resetCompletedIssues: (state, data) => {
store.remove("org.5calls.completed", () => {});
return { completedIssues: [] }
},
home: (state, data) => {
return { activeIssue: false, getInfo: false }
},
toggleFieldOfficeNumbers: (state, data) => ({ showFieldOfficeNumbers: !state.showFieldOfficeNumbers }),
hideFieldOfficeNumbers: (state, data) => ({ showFieldOfficeNumbers: false }),
},
effects: {
fetch: (state, data, send, done) => {
address = "?address="
if (state.address !== '') {
address += state.address
} else if (state.geolocation !== "") {
address += state.geolocation
}
const issueURL = appURL+'/issues/'+address
// console.log("fetching url",issueURL);
http(issueURL, (err, res, body) => {
send('setCachedCity', body, done)
send('receiveIssues', body, done)
})
},
getTotals: (state, data, send, done) => {
http(appURL+'/report/', (err, res, body) => {
send('receiveTotals', body, done)
})
},
changeActiveIssueEffect: (state, issueId, send, done) => {
send('location:set', "/#issue/"+issueId, done)
send('changeActiveIssue', issueId, done)
},
setLocation: (state, data, send, done) => {
send('setAddress', data, done);
send('fetch', {}, done);
},
setBrowserGeolocation: (state, data, send, done) => {
send('setGeolocation', data, done);
send('fetch', {}, done);
},
unsetLocation: (state, data, send, done) => {
send('resetLocation', data, done)
send('startup', data, done)
},
fetchLocationBy: (state, data, send, done) => {
send('setLocationFetchType', data, done)
send('startup', data, done)
},
fetchLocationByIP: (state, data, send, done) => {
http('https://ipinfo.io/json', (err, res, body) => {
if (res.statusCode == 200) {
try {
response = JSON.parse(body)
if (response.city != "") {
send('receiveIPInfoLoc', response, done);
send('fetch', {}, done);
} else {
send('fetchLocationBy', 'address', done);
Raven.captureMessage("Location with no city: "+response.loc, { level: 'warning' });
}
} catch(e) {
send('fetchLocationBy', 'address', done);
Raven.setExtraContext({ json: data })
Raven.captureMessage("Couldn’t parse ipinfo json", { level: 'error' });
}
} else {
send('fetchLocationBy', 'address', done);
Raven.captureMessage("Non-200 from ipinfo", { level: 'info' });
}
})
},
fetchLocationByBrowswer: (state, data, send, done) => {
let geoSuccess = function(position) {
if (typeof position.coords !== 'undefined') {
let lat = position.coords.latitude;
let long = position.coords.longitude;
if (lat && long) {
let geo = Math.floor(lat*10000)/10000 + ',' + Math.floor(long*10000)/10000;
send('allowBrowserGeolocation', true, done);
send('setBrowserGeolocation', geo, done);
} else {
console.log("Error: bad browser location results");
send('fetchLocationBy', 'ipAddress', done);
}
} else {
console.log("Error: bad browser location results");
send('fetchLocationBy', 'ipAddress', done);
}
}
let geoError = function(error) {
if (error.code === 1) {
send('allowBrowserGeolocation', false, done);
}
send('fetchLocationBy', 'ipAddress', done);
console.log("Error with browser location (code: " + error.code + ")");
}
navigator.geolocation.getCurrentPosition(geoSuccess, geoError);
},
startup: (state, data, send, done) => {
// sometimes we trigger this again when reloading mainView, check for issues
if (state.issues.length == 0 || state.geolocation == '') {
// Check for browser support of geolocation
if ((state.allowBrowserGeo !== false && navigator.geolocation) &&
state.locationFetchType === 'browserGeolocation' && state.geolocation == '') {
send('fetchLocationByBrowswer', {}, done);
}
else if (state.locationFetchType === 'ipAddress' && state.geolocation == '') {
send('fetchLocationByIP', {}, done);
}
else if (state.address !== '' || state.geolocation !== '') {
send('fetchingLocation', false, done);
send('fetch', {}, done);
}
}
},
incrementContact: (state, data, send, done) => {
const issue = find(state.issues, ['id', data.issueid]);
if (state.contactIndex < issue.contacts.length - 1) {
scrollIntoView(document.querySelector('#contact'));
send('setContactIndex', { newIndex: state.contactIndex + 1, issueid: issue.id }, done)
} else {
scrollIntoView(document.querySelector('#content'));
store.add("org.5calls.completed", issue.id, () => {})
send('location:set', "/#done/" + issue.id, done)
send('setContactIndex', { newIndex: 0, issueid: issue.id }, done)
}
},
callComplete: (state, data, send, done) => {
send('hideFieldOfficeNumbers', data, done);
if (data.result == 'unavailable') {
ga('send', 'event', 'call_result', 'unavailable', 'unavailable');
} else {
ga('send', 'event', 'call_result', 'success', data.result);
}
const body = queryString.stringify({ location: state.zip, result: data.result, contactid: data.contactid, issueid: data.issueid })
http.post(appURL+'/report', { body: body, headers: {"Content-Type": "application/x-www-form-urlencoded"} }, (err, res, body) => {
// don’t really care about the result
})
send('incrementContact', data, done);
},
skipCall: (state, data, send, done) => {
send('hideFieldOfficeNumbers', data, done);
ga('send', 'event', 'call_result', 'skip', 'skip');
send('incrementContact', data, done);
},
activateIssue: (state, data, send, done) => {
send('hideFieldOfficeNumbers', data, done);
ga('send', 'event', 'issue_flow', 'select', 'select');
scrollIntoView(document.querySelector('#content'));
location.hash = "issue/" + data.id;
}
},
});
app.router({ default: '/404' }, [
['/', require('./pages/mainView.js')],
['/issue', require('./pages/mainView.js'),
[':issueid', require('./pages/mainView.js')]
],
['/done', require('./pages/doneView.js'),
[':issueid', require('./pages/doneView.js')]
],
['/about', require('./pages/aboutView.js')],
]);
const tree = app.start();
const rootNode = document.getElementById('root');
document.body.replaceChild(tree, rootNode);
|
import {
CHECKFLOAT
} from './mutation-types.js'
export default {
checkFloat({ commit }, obj) {
// let regex = new RegExp('/^[+|-]?/d*/.?/d*$/')
// if (!regex.test(obj.value)) {
// return obj.cb(new Error('类型错误'))
// }
if (obj.value === '9090') {
obj.value = 9090
}
commit(CHECKFLOAT)
}
}
|
module.exports = function(grunt) {
grunt.initConfig({
jsbeautifier: {
files: [
"src/**/*.js",
"test/**/*.js",
"Gruntfile.js"
]
},
jshint: {
options: {
es3: true,
unused: true,
curly: true,
eqeqeq: true,
expr: true,
eqnull: true,
evil: true
},
files: [
"src/**/*.js",
"test/**/*.js",
"Gruntfile.js"
]
}
});
grunt.loadNpmTasks("grunt-jsbeautifier");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.registerTask("default", ["jsbeautifier", "jshint"]);
};
|
expect(getType(true)).toBe("boolean");
|
var repl = require("repl"),
count = 1;
// Ported from
// https://github.com/jgautier/firmata
function Repl(opts) {
if (!Repl.active) {
Repl.active = true;
if (!(this instanceof Repl)) {
return new Repl(opts);
}
// Store context values in instance property
// this will be used for managing scope when
// injecting new values into an existing Repl
// session.
this.context = {};
this.ready = false;
process.stdin.resume();
process.stdin.setEncoding("utf8");
// Create a one time data event handler for initializing
// the repl session via keyboard input
process.stdin.once("data", function() {
var cmd, replDefaults;
console.info("Repl", "Initialized");
replDefaults = {
prompt: ">> ",
useGlobal: false
};
// Initialize the REPL session with the default
// repl settings.
// Assign the returned repl instance to "cmd"
cmd = repl.start(replDefaults);
// Assign a reference to the REPL's "content" object
// This will be use later by the Repl.prototype.inject
// method for allowing user programs to inject their
// own explicit values and reference
this.context = cmd.context;
cmd.on("exit", function() {
console.warn("Board", "Closing.");
process.reallyExit();
});
// this.emit("ready");
// Inject the "opts" object into the repl context
this.inject(opts);
}.bind(this));
// Store an accessible copy of the Repl instance
// on a static property. This is later used by the
// Board constructor to automattically setup Repl
// sessions for all programs, which reduces the
// boilerplate requirement.
Repl.ref = this;
}
}
// Inherit event api
// util.inherits(Repl, events.EventEmitter);
Repl.active = false;
// See Repl.ref notes above.
Repl.ref = null;
Repl.prototype.inject = function(obj) {
Object.keys(obj).forEach(function(key) {
Object.defineProperty(
this.context, key, Object.getOwnPropertyDescriptor(obj, key)
);
}, this);
};
Repl.isActive = false;
Repl.isBlocked = false;
module.exports = Repl; |
version https://git-lfs.github.com/spec/v1
oid sha256:32e58cb27429f505009c74aaf7263f62d0b338cba2f925c5595c9f14a9467dfa
size 1533
|
var david = require('david');
var manifest = require('../../package.json');
var chalk = require('chalk');
david.getDependencies(manifest, function (er, deps) {
console.log(chalk.green.underline.bold('latest dependencies information for', manifest.name));
listDependencies(deps);
});
david.getDependencies(manifest, { dev: true }, function (er, deps) {
console.log(chalk.green('latest devDependencies information for', manifest.name));
listDependencies(deps);
});
david.getUpdatedDependencies(manifest, function (er, deps) {
console.log(chalk.yellow.underline.bold('dependencies with newer versions for', manifest.name));
listDependencies(deps);
});
david.getUpdatedDependencies(manifest, { dev: true }, function (er, deps) {
console.log(chalk.yellow('devDependencies with newer versions for', manifest.name));
listDependencies(deps);
});
david.getUpdatedDependencies(manifest, { stable: true }, function (er, deps) {
console.log(chalk.red.underline.bold('dependencies with newer STABLE versions for', manifest.name));
listDependencies(deps);
});
david.getUpdatedDependencies(manifest, { dev: true, stable: true }, function (er, deps) {
console.log(chalk.red('devDependencies with newer STABLE versions for', manifest.name));
listDependencies(deps);
});
function listDependencies(deps) {
Object.keys(deps).forEach(function(depName) {
var required = deps[depName].required || '*';
var stable = deps[depName].stable || 'None';
var latest = deps[depName].latest;
if(required != stable){
console.log('%s Required: %s Stable: %s Latest: %s', chalk.gray.italic(depName), chalk.red(required), chalk.green(stable), chalk.yellow(latest));
}
});
} |
import { expect } from "chai"
import Home from "../pages/home.page"
import MyCounterparts from "../pages/myCounterparts.page"
describe("Home page", () => {
describe("When checking the navigation items", () => {
it("Should see a link to my counterparts page when the user is an advisor", () => {
Home.open()
Home.linksMenuButton.click()
Home.myCounterpartsLink.waitForDisplayed()
// eslint-disable-next-line no-unused-expressions
expect(Home.myCounterpartsLink.isExisting()).to.be.true
Home.logout()
})
})
describe("When checking the navigation items", () => {
it("Should NOT see a link to my counterparts page when the user does not have a position", () => {
Home.openAsPositionlessUser()
// eslint-disable-next-line no-unused-expressions
expect(Home.myCounterpartsLink.isExisting()).to.be.false
// No Logout link, so just call logout directly
browser.url("/api/logout")
})
})
})
describe("My counterparts page", () => {
afterEach("On the my counterparts page...", () => {
MyCounterparts.logout()
})
describe("When Erin is checking the content of the page", () => {
it("Should see an empty table of the counterparts", () => {
MyCounterparts.open()
MyCounterparts.myCounterparts.waitForDisplayed()
const myCounterpartsItems = MyCounterparts.myCounterparts.$$("tr")
// table has a header and 1 counterpart rows
expect(myCounterpartsItems).to.have.length(2)
})
})
describe("When Rebecca is checking the content of the page", () => {
it("Should see a table of the counterparts", () => {
MyCounterparts.openAsSuperUser()
MyCounterparts.myCounterparts.waitForDisplayed()
const myCounterpartsItems = MyCounterparts.myCounterparts.$$("tr")
expect(myCounterpartsItems).to.have.length(0)
})
})
})
|
/* globals cc */
(function () {
'use strict';
var GameController = require('./gameController'); // cylic(!)
var ScreenDimensions = require('./screenDimensions');
// Menu (initial screen)
//
var MenuLayer = cc.Layer.extend({
ctor: function () {
this._super();
cc.associateWithNative(this, cc.Layer);
},
init: function () {
this._super();
this.createTouchListener();
this.addMainMenu();
GameController = require('./gameController'); // cyclic dep
return true;
},
_walkChildren: function (callback) {
var children = this.getChildren();
for (var i = children.length - 1; i >= 0; i--) {
var child = children[i];
if (callback(child) === true) {
break;
}
}
},
createTouchListener: function () {
this.touchListener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: function (touch, event) {
var target = event.getCurrentTarget();
var locationInNode =
target.convertToNodeSpace(touch.getLocation());
var s = target.getContentSize();
var rect = cc.rect(0, 0, s.width, s.height);
if (cc.rectContainsPoint(rect, locationInNode)) {
return true;
}
return false;
},
onTouchMoved: function () {},
onTouchEnded: function (touch, event) {
var target = event.getCurrentTarget();
target.touchEnded();
return true;
}
});
},
// Add some menu items
//
addMainMenu: function () {
var texts = [
{ t : 'Start',
callback : function () {
GameController.showChapter(0);
}},
{ t : 'Resume',
callback : function () {}
},
{ t : 'About',
callback : function () {}
}
];
var actualWidth = ScreenDimensions.viewportSize.width;
var actualHeight = ScreenDimensions.viewportSize.height;
var highlightActionFactory = function (label, callback) {
return function () {
label.runAction(
cc.Sequence.create(
cc.ScaleTo.create(0.10, 0.4, 0.4),
cc.CallFunc.create(callback)
)
);
};
};
for (var i = 0; i < texts.length; i++) {
var label = cc.LabelBMFont.create(
texts[i].t,
'res/images/headers-100.fnt',
ScreenDimensions.viewportSize.width,
cc.TEXT_ALIGNMENT_LEFT,
cc.p(0, 0)
);
label.setPosition(cc.p(actualWidth / 2,
(texts.length - i - 0.5) * actualHeight / texts.length));
this.addChild(label);
cc.eventManager.addListener(this.touchListener.clone(), label);
label.touchEnded = highlightActionFactory(label, texts[i].callback);
}
}
});
module.exports = MenuLayer;
})();
|
var Vector = require('vector2d')
, utils
, topSpeed
;
function Mover(cx, canvas) {
utils = require('utils')(cx, canvas);
this.cx = cx;
this.canvas = canvas;
this.topSpeed = 5;
this.location = new Vector.ObjectVector(utils.halfX(), utils.halfY());
this.velocity = new Vector.ObjectVector(utils.range(-2, 2), utils.range(-2, 2));
this.acceleration = new Vector.ObjectVector(0.001, 0.01);
}
Mover.prototype.update = function() {
this.velocity.add(this.acceleration);
this.velocity.limit(this.topSpeed);
this.location.add(this.velocity);
};
Mover.prototype.checkEdges = function () {
if (this.location.getX() > this.canvas.width) {
this.location.setX(0);
} else if (this.location.getX() < 0) {
this.location.setX(canvas.width);
}
if (this.location.getY() > this.canvas.height) {
this.location.setY(0);
} else if (this.location.getY() < 0){
this.location.setY(this.canvas.height);
}
};
Mover.prototype.display = function () {
this.cx.fillRect(this.location.getX(), this.location.getY(), 10, 10);
};
module.exports = Mover;
|
import ButtonContainer from './Button.container';
export const route = {
routeName: 'Button',
screen: ButtonContainer
};
export default from './Button.component';
|
exports['utilPicker'] = function(assert) {
var done = assert.done || assert.async();
assert.expect(1);
assert.ok(1);
console.warn('no utilPicker tests!');
done();
};
|
import React, {Component} from 'react'
import List from 'react-toolbox/lib/list/List'
import ListItem from 'react-toolbox/lib/list/ListItem'
import Icon from 'react-toolbox/lib/font_icon/FontIcon'
import Tooltip from 'react-toolbox/lib/tooltip'
import Button from 'react-toolbox/lib/button/Button'
import ConfirmDeleteButton from 'components/shared/ConfirmDeleteButton'
import { Link } from 'react-router'
import { connect } from 'react-redux'
import {
fetchUsersPage, deleteUser, getUsersPage
} from 'reducers/users.reducer'
import PaginationFooter from 'components/shared/PaginationFooter'
const TooltipButton = Tooltip(Button);
const TooltipIcon = Tooltip(Icon);
class Users extends Component {
pageSize = 5;
componentDidMount() {
this.fetchPage(0);
}
fetchPage(page) {
this.props.fetchUsersPage(page, this.pageSize);
}
renderListActions(user) {
return [
<Link
to={`/users/${user._id}`}
key={`edit_link_${user._id}`}
style={{color: '#757575'}} >
<TooltipIcon tooltipPosition="left" tooltip="Editar" value="edit" />
</Link>,
<ConfirmDeleteButton
tooltip="Borrar"
tooltipPosition="left"
title={`Borrar usuario ${user.name}`}
key={`delete_user_${user._id}`}
onDelete={() => this.props.deleteUser(user)}
/>
]
}
render() {
const {loading, children, pageParams} = this.props;
const users = this.props.users || [];
return (
<div className="users list-container">
<div className="list-title-container">
<h2 className="list-title">Usuarios</h2>
{loading && <p className="color-primary">Cargando ... </p>}
</div>
<Link to="/users/new">
<TooltipButton
icon="add"
floating accent
tooltip="Nuevo usuario"
tooltipPosition="left"
className="list-corner-fab"
/>
</Link>
<List className="list">
{users.map((user, i) => (
<ListItem
key={user._id || i}
caption={user.name}
className="list-item"
leftIcon="person"
rightActions={this.renderListActions(user)}
/>
))}
</List>
<PaginationFooter
params={pageParams}
onPageChange={page => this.fetchPage(page)}
/>
{children}
</div>
)
}
}
const mapStateToProps = state => {
const {items, loading, params} = getUsersPage(state)
return {
pageParams: params,
users: items,
loading
}
}
const actions = {fetchUsersPage, deleteUser}
export default connect(mapStateToProps, actions)(Users);
|
const WebpackOnBuildPlugin = require('on-build-webpack')
const replace = require('replace')
const path = require('path')
const fs = require('fs')
// Workaround for https://github.com/ethereum/web3.js/issues/555:
module.exports = new WebpackOnBuildPlugin(function () {
const bundle = path.resolve(__dirname, '..', 'dist', 'bundle.js')
if (fs.existsSync(bundle)) {
replace({
regex: '\u00A0',
replacement: ' ',
paths: [ bundle ]
})
}
})
|
/**
* API:
*
* ifNeeded([ignoreNativeAPNG bool]) → Promise()
* animateImage(img HTMLImageElement) → Promise()
*
* animateContext(url String, CanvasRenderingContext2D context) → Promise(Animation)
* parseBuffer(ArrayBuffer) → Promise(Animation)
* parseURL(url String) → Promise(Animation)
* checkNativeFeatures() → Promise(features)
*/
"use strict";
var support = require("./support-test");
var parseAPNG = require("./parser");
var loadUrl = require('./loader');
var APNG = global.APNG = {};
APNG.checkNativeFeatures = support.checkNativeFeatures;
APNG.ifNeeded = support.ifNeeded;
/**
* @param {ArrayBuffer} buffer
* @return {Promise}
*/
APNG.parseBuffer = function (buffer) { return parseAPNG(buffer); };
var url2promise = {};
/**
* @param {String} url
* @return {Promise}
*/
APNG.parseURL = function (url) {
if (!(url in url2promise)) url2promise[url] = loadUrl(url).then(parseAPNG);
return url2promise[url];
};
/**
* @param {String} url
* @param {CanvasRenderingContext2D} context
* @return {Promise}
*/
APNG.animateContext = function (url, context) {
return APNG.parseURL(url).then(function (a) {
a.addContext(context);
a.play();
return a;
});
};
/**
* @param {HTMLImageElement} img
* @return {Promise}
*/
APNG.animateImage = function (img) {
img.setAttribute("data-is-apng", "progress");
return APNG.parseURL(img.src).then(
function (anim) {
img.setAttribute("data-is-apng", "yes");
var canvas = document.createElement("canvas");
canvas.width = anim.width;
canvas.height = anim.height;
Array.prototype.slice.call(img.attributes).forEach(function (attr) {
if (["alt", "src", "usemap", "ismap", "data-is-apng", "width", "height"].indexOf(attr.nodeName) == -1) {
canvas.setAttributeNode(attr.cloneNode(false));
}
});
canvas.setAttribute("data-apng-src", img.src);
if (img.alt != "") canvas.appendChild(document.createTextNode(img.alt));
var imgWidth = "", imgHeight = "", val = 0, unit = "";
if (img.style.width != "" && img.style.width != "auto") {
imgWidth = img.style.width;
} else if (img.hasAttribute("width")) {
imgWidth = img.getAttribute("width") + "px";
}
if (img.style.height != "" && img.style.height != "auto") {
imgHeight = img.style.height;
} else if (img.hasAttribute("height")) {
imgHeight = img.getAttribute("height") + "px";
}
if (imgWidth != "" && imgHeight == "") {
val = parseFloat(imgWidth);
unit = imgWidth.match(/\D+$/)[0];
imgHeight = Math.round(canvas.height * val / canvas.width) + unit;
}
if (imgHeight != "" && imgWidth == "") {
val = parseFloat(imgHeight);
unit = imgHeight.match(/\D+$/)[0];
imgWidth = Math.round(canvas.width * val / canvas.height) + unit;
}
canvas.style.width = imgWidth;
canvas.style.height = imgHeight;
var p = img.parentNode;
p.insertBefore(canvas, img);
p.removeChild(img);
anim.addContext(canvas.getContext("2d"));
anim.play();
},
function () {
img.setAttribute("data-is-apng", "no");
});
};
/**
* @param {HTMLCanvasElement} canvas
* @return {void}
*/
APNG.releaseCanvas = function(canvas) {
var ctx = canvas.getContext("2d");
if ('_apng_animation' in ctx) {
ctx['_apng_animation'].removeContext(ctx);
}
}; |
const nunjucksRender = require('gulp-nunjucks-render');
const cache = require('gulp-cached');
const flatmap = require('gulp-flatmap');
const site_data = require('./../site_data.js');
const nunjucksMarkdown = require('nunjucks-markdown');
const marked = require('marked');
const data = site_data.site_data();
marked.setOptions({
baseUrl: data.base_path,
gfm: true,
tables: true,
breaks: true,
})
function manageEnv(environment) {
nunjucksMarkdown.register(environment, marked);
}
module.exports = ($, gulp, config) => () => {
return gulp.src(config.html.src)
.pipe(nunjucksRender({
path: 'app',
data: data,
manageEnv: manageEnv
})).pipe($.useref({
searchPath: ['.tmp', '.'],
}))
.pipe(cache('html_file_preprocessing'))
//.pipe($.if('*.js', $.uglify()))
.pipe($.if('*.css', $.cssnano({safe: true, autoprefixer: false})))
.pipe($.if('*.html', $.htmlmin({collapseWhitespace: true})))
.pipe(flatmap((stream, file) => { console.log(file.path); return stream; }))
.pipe(gulp.dest('.tmp'))
.pipe(gulp.dest('dist'));
}; |
const { circumferenceOfACircle } = require("../lib");
describe("circumferenceOfACircle", () => {
test("circle with a radius of 10 to equal a circumference of 62.83", () => {
expect(circumferenceOfACircle(10)).toBe(62.83);
});
test("circle with a radius of 10 (passed as string) to equal a circumference of 62.83", () => {
expect(circumferenceOfACircle("10")).toBe(62.83);
});
test("circle with a radius of 'asdf' to equal a circumference of 0", () => {
expect(circumferenceOfACircle("asdf")).toBe(0);
});
});
|
var mongoose = require('mongoose');
var randtoken = require('rand-token');
var bcrypt = require('bcrypt-nodejs');
var deviceSchema = mongoose.Schema({
name : {type : String, required : true},
token : {type : String, required : true},
description : {type : String},
owner : {type : mongoose.Schema.Types.ObjectId, ref : 'User'}
});
deviceSchema.statics.findDevice = function(req, res, next) {
mongoose.model('Device').findOne({name : req.body.name || req.params.name},
function(err, device){
if(err){
next(err)
}
else {
req.device = device
next();
}
})
}
deviceSchema.statics.createDevice = function(req, next){
if(req.user){
var name = req.body.name
var description = req.body.description || 'No description provided'
// Check if the Device had a duplicated name
var duplicated = false;
for(var i=0; i < req.user.devices.length; i++){
if(req.user.devices[i].name == name){
duplicated = true;
}
}
if(duplicated){
var err = new Error('Device name already taked')
err.status = 400
err.message = 'Device name already taked'
return next(err)
}
else {
var newDevice = mongoose.model('Device')()
newDevice['name'] = name
newDevice['token'] = randtoken.generate(10);
newDevice['description'] = description
newDevice['owner'] = req.user._id
return next(null, newDevice)
}
}
else{
var err = new Error('User not found')
err.status = 400
err.message = 'User not found'
return next(err)
}
}
module.exports = mongoose.model('Device', deviceSchema);
|
'use strict';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const limiter = require('limiter');
const config = require('../../../shared/config');
const gcloudTranslate = require('@google-cloud/translate');
const gcloudVision = require('@google-cloud/vision');
const KEY_FILE_PATH = path.join(config.customizationPath, 'google-key.json');
assert.ok(fs.existsSync(KEY_FILE_PATH),
'Missing the Google API key file: ' + KEY_FILE_PATH);
const vision = gcloudVision({
keyFilename: KEY_FILE_PATH
});
const visionLimiter = new limiter.RateLimiter(8, 'second');
// const visionLimiter = new limiter.RateLimiter(1, 'second');
// Override the vision.annotate method to apply throttling
const originalAnnotate = vision.annotate;
vision.annotate = function() {
return new Promise((resolve, reject) => {
visionLimiter.removeTokens(1, (err) => {
if(err) {
reject(err);
} else {
const result = originalAnnotate.apply(vision, arguments);
resolve(result);
}
});
});
};
const translate = gcloudTranslate({
keyFilename: KEY_FILE_PATH
});
module.exports = {
vision: vision,
translate: translate
};
|
/* =========================================================
* bootstrap-datepicker.js
* http://www.eyecon.ro/bootstrap-datepicker
* =========================================================
* Copyright 2012 Stefan Petre
* Improvements by Andrew Rowls
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ========================================================= */
!function( $ ) {
// Picker object
var Datepicker = function(element, options){
this.element = $(element);
this.language = options.language||this.element.data('date-language')||"en";
this.language = this.language in dates ? this.language : "en";
this.format = DPGlobal.parseFormat(options.format||this.element.data('date-format')||'mm/dd/yyyy');
this.picker = $(DPGlobal.template)
.appendTo('body')
.on({
click: $.proxy(this.click, this),
mousedown: $.proxy(this.mousedown, this)
});
this.isInput = this.element.is('input');
this.component = this.element.is('.date') ? this.element.find('.add-on') : false;
if(this.component && this.component.length === 0)
this.component = false;
if (this.isInput) {
this.element.on({
focus: $.proxy(this.show, this),
blur: $.proxy(this._hide, this),
keyup: $.proxy(this.update, this),
keydown: $.proxy(this.keydown, this)
});
} else {
if (this.component){
// For components that are not readonly, allow keyboard nav
this.element.find('input').on({
focus: $.proxy(this.show, this),
blur: $.proxy(this._hide, this),
keyup: $.proxy(this.update, this),
keydown: $.proxy(this.keydown, this)
});
this.component.on('click', $.proxy(this.show, this));
var element = this.element.find('input');
element.on({
blur: $.proxy(this._hide, this)
})
} else {
this.element.on('click', $.proxy(this.show, this));
}
}
this.autoclose = false;
if ('autoclose' in options) {
this.autoclose = options.autoclose;
} else if ('dateAutoclose' in this.element.data()) {
this.autoclose = this.element.data('date-autoclose');
}
switch(options.startView){
case 2:
case 'decade':
this.viewMode = this.startViewMode = 2;
break;
case 1:
case 'year':
this.viewMode = this.startViewMode = 1;
break;
case 0:
case 'month':
default:
this.viewMode = this.startViewMode = 0;
break;
}
this.weekStart = ((options.weekStart||this.element.data('date-weekstart')||dates[this.language].weekStart||0) % 7);
this.weekEnd = ((this.weekStart + 6) % 7);
this.startDate = -Infinity;
this.endDate = Infinity;
this.setStartDate(options.startDate||this.element.data('date-startdate'));
this.setEndDate(options.endDate||this.element.data('date-enddate'));
this.fillDow();
this.fillMonths();
this.update();
this.showMode();
};
Datepicker.prototype = {
constructor: Datepicker,
show: function(e) {
this.picker.show();
this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();
this.place();
$(window).on('resize', $.proxy(this.place, this));
if (e ) {
e.stopPropagation();
e.preventDefault();
}
if (!this.isInput) {
$(document).on('mousedown', $.proxy(this.hide, this));
}
this.element.trigger({
type: 'show',
date: this.date
});
},
_hide: function(e){
// When going from the input to the picker, IE handles the blur/click
// events differently than other browsers, in such a way that the blur
// event triggers a hide before the click event can stop propagation.
if ($.browser.msie) {
var t = this, args = arguments;
function cancel_hide(){
clearTimeout(hide_timeout);
e.target.focus();
t.picker.off('click', cancel_hide);
}
function do_hide(){
t.hide.apply(t, args);
t.picker.off('click', cancel_hide);
}
this.picker.on('click', cancel_hide);
var hide_timeout = setTimeout(do_hide, 100);
} else {
return this.hide.apply(this, arguments);
}
},
hide: function(e){
this.picker.hide();
$(window).off('resize', this.place);
this.viewMode = this.startViewMode;
this.showMode();
if (!this.isInput) {
$(document).off('mousedown', this.hide);
}
if (e && e.currentTarget.value)
this.setValue();
this.element.trigger({
type: 'hide',
date: this.date
});
},
setValue: function() {
var formated = DPGlobal.formatDate(this.date, this.format, this.language);
if (!this.isInput) {
if (this.component){
this.element.find('input').prop('value', formated);
}
this.element.data('date', formated);
} else {
this.element.prop('value', formated);
}
},
setStartDate: function(startDate){
this.startDate = startDate||-Infinity;
if (this.startDate !== -Infinity) {
this.startDate = DPGlobal.parseDate(this.startDate, this.format, this.language);
}
this.update();
this.updateNavArrows();
},
setEndDate: function(endDate){
this.endDate = endDate||Infinity;
if (this.endDate !== Infinity) {
this.endDate = DPGlobal.parseDate(this.endDate, this.format, this.language);
}
this.update();
this.updateNavArrows();
},
place: function(){
var offset = this.component ? this.component.offset() : this.element.offset();
this.picker.css({
top: offset.top + this.height,
left: offset.left
});
},
update: function(){
this.date = DPGlobal.parseDate(
this.isInput ? this.element.prop('value') : this.element.data('date') || this.element.find('input').prop('value'),
this.format, this.language
);
if (this.date < this.startDate) {
this.viewDate = new Date(this.startDate);
} else if (this.date > this.endDate) {
this.viewDate = new Date(this.endDate);
} else {
this.viewDate = new Date(this.date);
}
this.fill();
},
fillDow: function(){
var dowCnt = this.weekStart;
var html = '<tr>';
while (dowCnt < this.weekStart + 7) {
html += '<th class="dow">'+dates[this.language].daysMin[(dowCnt++)%7]+'</th>';
}
html += '</tr>';
this.picker.find('.datepicker-days thead').append(html);
},
fillMonths: function(){
var html = '';
var i = 0
while (i < 12) {
html += '<span class="month">'+dates[this.language].monthsShort[i++]+'</span>';
}
this.picker.find('.datepicker-months td').html(html);
},
fill: function() {
var d = new Date(this.viewDate),
year = d.getFullYear(),
month = d.getMonth(),
startYear = this.startDate !== -Infinity ? this.startDate.getFullYear() : -Infinity,
startMonth = this.startDate !== -Infinity ? this.startDate.getMonth() : -Infinity,
endYear = this.endDate !== Infinity ? this.endDate.getFullYear() : Infinity,
endMonth = this.endDate !== Infinity ? this.endDate.getMonth() : Infinity,
currentDate = this.date.valueOf();
this.picker.find('.datepicker-days th:eq(1)')
.text(dates[this.language].months[month]+' '+year);
this.updateNavArrows();
this.fillMonths();
var prevMonth = new Date(year, month-1, 28,0,0,0,0),
day = DPGlobal.getDaysInMonth(prevMonth.getFullYear(), prevMonth.getMonth());
prevMonth.setDate(day);
prevMonth.setDate(day - (prevMonth.getDay() - this.weekStart + 7)%7);
var nextMonth = new Date(prevMonth);
nextMonth.setDate(nextMonth.getDate() + 42);
nextMonth = nextMonth.valueOf();
html = [];
var clsName;
while(prevMonth.valueOf() < nextMonth) {
if (prevMonth.getDay() == this.weekStart) {
html.push('<tr>');
}
clsName = '';
if (prevMonth.getFullYear() < year || (prevMonth.getFullYear() == year && prevMonth.getMonth() < month)) {
clsName += ' old';
} else if (prevMonth.getFullYear() > year || (prevMonth.getFullYear() == year && prevMonth.getMonth() > month)) {
clsName += ' new';
}
if (prevMonth.valueOf() == currentDate) {
clsName += ' active';
}
if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate) {
clsName += ' disabled';
}
html.push('<td class="day'+clsName+'">'+prevMonth.getDate() + '</td>');
if (prevMonth.getDay() == this.weekEnd) {
html.push('</tr>');
}
prevMonth.setDate(prevMonth.getDate()+1);
}
this.picker.find('.datepicker-days tbody').empty().append(html.join(''));
var currentYear = this.date.getFullYear();
var months = this.picker.find('.datepicker-months')
.find('th:eq(1)')
.text(year)
.end()
.find('span').removeClass('active');
if (currentYear == year) {
months.eq(this.date.getMonth()).addClass('active');
}
if (year < startYear || year > endYear) {
months.addClass('disabled');
}
if (year == startYear) {
months.slice(0, startMonth).addClass('disabled');
}
if (year == endYear) {
months.slice(endMonth+1).addClass('disabled');
}
html = '';
year = parseInt(year/10, 10) * 10;
var yearCont = this.picker.find('.datepicker-years')
.find('th:eq(1)')
.text(year + '-' + (year + 9))
.end()
.find('td');
year -= 1;
for (var i = -1; i < 11; i++) {
html += '<span class="year'+(i == -1 || i == 10 ? ' old' : '')+(currentYear == year ? ' active' : '')+(year < startYear || year > endYear ? ' disabled' : '')+'">'+year+'</span>';
year += 1;
}
yearCont.html(html);
},
updateNavArrows: function() {
var d = new Date(this.viewDate),
year = d.getFullYear(),
month = d.getMonth();
switch (this.viewMode) {
case 0:
if (this.startDate !== -Infinity && year <= this.startDate.getFullYear() && month <= this.startDate.getMonth()) {
this.picker.find('.prev').css({visibility: 'hidden'});
} else {
this.picker.find('.prev').css({visibility: 'visible'});
}
if (this.endDate !== Infinity && year >= this.endDate.getFullYear() && month >= this.endDate.getMonth()) {
this.picker.find('.next').css({visibility: 'hidden'});
} else {
this.picker.find('.next').css({visibility: 'visible'});
}
break;
case 1:
case 2:
if (this.startDate !== -Infinity && year <= this.startDate.getFullYear()) {
this.picker.find('.prev').css({visibility: 'hidden'});
} else {
this.picker.find('.prev').css({visibility: 'visible'});
}
if (this.endDate !== Infinity && year >= this.endDate.getFullYear()) {
this.picker.find('.next').css({visibility: 'hidden'});
} else {
this.picker.find('.next').css({visibility: 'visible'});
}
break;
}
},
click: function(e) {
e.stopPropagation();
e.preventDefault();
var target = $(e.target).closest('span, td, th');
if (target.length == 1) {
switch(target[0].nodeName.toLowerCase()) {
case 'th':
switch(target[0].className) {
case 'switch':
this.showMode(1);
break;
case 'prev':
case 'next':
var dir = DPGlobal.modes[this.viewMode].navStep * (target[0].className == 'prev' ? -1 : 1);
switch(this.viewMode){
case 0:
this.viewDate = this.moveMonth(this.viewDate, dir);
break;
case 1:
case 2:
this.viewDate = this.moveYear(this.viewDate, dir);
break;
}
this.fill();
break;
}
break;
case 'span':
if (!target.is('.disabled')) {
this.viewDate.setDate(1);
if (target.is('.month')) {
var month = target.parent().find('span').index(target);
this.viewDate.setMonth(month);
} else {
var year = parseInt(target.text(), 10)||0;
this.viewDate.setFullYear(year);
}
this.showMode(-1);
this.fill();
}
break;
case 'td':
if (target.is('.day') && !target.is('.disabled')){
var day = parseInt(target.text(), 10)||1;
var year = this.viewDate.getFullYear(),
month = this.viewDate.getMonth();
if (target.is('.old')) {
if (month == 0) {
month = 11;
year -= 1;
} else {
month -= 1;
}
} else if (target.is('.new')) {
if (month == 11) {
month = 0;
year += 1;
} else {
month += 1;
}
}
this.date = new Date(year, month, day,0,0,0,0);
this.viewDate = new Date(year, month, day,0,0,0,0);
this.fill();
this.setValue();
this.element.trigger({
type: 'changeDate',
date: this.date
});
var element;
if (this.isInput) {
element = this.element;
} else if (this.component){
element = this.element.find('input');
}
if (element) {
element.change();
if (this.autoclose) {
element.blur();
}
}
}
break;
}
}
},
mousedown: function(e){
e.stopPropagation();
e.preventDefault();
},
moveMonth: function(date, dir){
if (!dir) return date;
var new_date = new Date(date.valueOf()),
day = new_date.getDate(),
month = new_date.getMonth(),
mag = Math.abs(dir),
new_month, test;
dir = dir > 0 ? 1 : -1;
if (mag == 1){
test = dir == -1
// If going back one month, make sure month is not current month
// (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
? function(){ return new_date.getMonth() == month; }
// If going forward one month, make sure month is as expected
// (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
: function(){ return new_date.getMonth() != new_month; };
new_month = month + dir;
new_date.setMonth(new_month);
// Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
if (new_month < 0 || new_month > 11)
new_month = (new_month + 12) % 12;
} else {
// For magnitudes >1, move one month at a time...
for (var i=0; i<mag; i++)
// ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
new_date = this.moveMonth(new_date, dir);
// ...then reset the day, keeping it in the new month
new_month = new_date.getMonth();
new_date.setDate(day);
test = function(){ return new_month != new_date.getMonth(); };
}
// Common date-resetting loop -- if date is beyond end of month, make it
// end of month
while (test()){
new_date.setDate(--day);
new_date.setMonth(new_month);
}
return new_date;
},
moveYear: function(date, dir){
return this.moveMonth(date, dir*12);
},
keydown: function(e){
if (this.picker.is(':not(:visible)')){
if (e.keyCode == 27) // allow escape to hide and re-show picker
this.show();
return;
}
var dateChanged = false,
dir, day, month;
switch(e.keyCode){
case 27: // escape
this.hide();
e.preventDefault();
break;
case 37: // left
case 39: // right
dir = e.keyCode == 37 ? -1 : 1;
if (e.ctrlKey){
this.date = this.moveYear(this.date, dir);
this.viewDate = this.moveYear(this.viewDate, dir);
} else if (e.shiftKey){
this.date = this.moveMonth(this.date, dir);
this.viewDate = this.moveMonth(this.viewDate, dir);
} else {
this.date.setDate(this.date.getDate() + dir);
this.viewDate.setDate(this.viewDate.getDate() + dir);
}
this.setValue();
this.update();
e.preventDefault();
dateChanged = true;
break;
case 38: // up
case 40: // down
dir = e.keyCode == 38 ? -1 : 1;
if (e.ctrlKey){
this.date = this.moveYear(this.date, dir);
this.viewDate = this.moveYear(this.viewDate, dir);
} else if (e.shiftKey){
this.date = this.moveMonth(this.date, dir);
this.viewDate = this.moveMonth(this.viewDate, dir);
} else {
this.date.setDate(this.date.getDate() + dir * 7);
this.viewDate.setDate(this.viewDate.getDate() + dir * 7);
}
this.setValue();
this.update();
e.preventDefault();
dateChanged = true;
break;
case 13: // enter
this.hide();
e.preventDefault();
break;
}
if (dateChanged){
this.element.trigger({
type: 'changeDate',
date: this.date
});
var element;
if (this.isInput) {
element = this.element;
} else if (this.component){
element = this.element.find('input');
}
if (element) {
element.change();
}
}
},
showMode: function(dir) {
if (dir) {
this.viewMode = Math.max(0, Math.min(2, this.viewMode + dir));
}
this.picker.find('>div').hide().filter('.datepicker-'+DPGlobal.modes[this.viewMode].clsName).show();
this.updateNavArrows();
}
};
$.fn.datepicker = function ( option ) {
var args = Array.apply(null, arguments);
args.shift();
return this.each(function () {
var $this = $(this),
data = $this.data('datepicker'),
options = typeof option == 'object' && option;
if (!data) {
$this.data('datepicker', (data = new Datepicker(this, $.extend({}, $.fn.datepicker.defaults,options))));
}
if (typeof option == 'string') data[option].apply(data, args);
});
};
$.fn.datepicker.defaults = {
};
$.fn.datepicker.Constructor = Datepicker;
var dates = $.fn.datepicker.dates = {
en: {
days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
}
}
var DPGlobal = {
modes: [
{
clsName: 'days',
navFnc: 'Month',
navStep: 1
},
{
clsName: 'months',
navFnc: 'FullYear',
navStep: 1
},
{
clsName: 'years',
navFnc: 'FullYear',
navStep: 10
}],
isLeapYear: function (year) {
return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0))
},
getDaysInMonth: function (year, month) {
return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
},
validParts: /dd?|mm?|MM?|yy(?:yy)?/g,
nonpunctuation: /[^ -\/:-@\[-`{-~\t\n\r]+/g,
parseFormat: function(format){
// IE treats \0 as a string end in inputs (truncating the value),
// so it's a bad format delimiter, anyway
var separators = format.replace(this.validParts, '\0').split('\0'),
parts = format.match(this.validParts);
if (!separators || !separators.length || !parts || parts.length == 0){
throw new Error("Invalid date format.");
}
return {separators: separators, parts: parts};
},
parseDate: function(date, format, language) {
if (date instanceof Date) return date;
if (/^[-+]\d+[dmwy]([\s,]+[-+]\d+[dmwy])*$/.test(date)) {
var part_re = /([-+]\d+)([dmwy])/,
parts = date.match(/([-+]\d+)([dmwy])/g),
part, dir;
date = new Date();
for (var i=0; i<parts.length; i++) {
part = part_re.exec(parts[i]);
dir = parseInt(part[1]);
switch(part[2]){
case 'd':
date.setDate(date.getDate() + dir);
break;
case 'm':
date = Datepicker.prototype.moveMonth.call(Datepicker.prototype, date, dir);
break;
case 'w':
date.setDate(date.getDate() + dir * 7);
break;
case 'y':
date = Datepicker.prototype.moveYear.call(Datepicker.prototype, date, dir);
break;
}
}
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
}
var parts = date ? date.match(this.nonpunctuation) : [],
date = new Date(),
parsed = {},
setters_order = ['yyyy', 'yy', 'M', 'MM', 'm', 'mm', 'd', 'dd'],
setters_map = {
yyyy: function(d,v){ return d.setFullYear(v); },
yy: function(d,v){ return d.setFullYear(2000+v); },
m: function(d,v){
v -= 1;
d.setMonth(v);
while (d.getMonth() != v)
d.setDate(d.getDate()-1);
return d;
},
d: function(d,v){ return d.setDate(v); }
},
val, filtered, part;
setters_map['M'] = setters_map['MM'] = setters_map['mm'] = setters_map['m'];
setters_map['dd'] = setters_map['d'];
date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
if (parts.length == format.parts.length) {
for (var i=0, cnt = format.parts.length; i < cnt; i++) {
val = parseInt(parts[i], 10)||1;
part = format.parts[i];
switch(part) {
case 'MM':
filtered = $(dates[language].months).filter(function(){
var m = this.slice(0, parts[i].length),
p = parts[i].slice(0, m.length);
return m == p;
});
val = $.inArray(filtered[0], dates[language].months) + 1;
break;
case 'M':
filtered = $(dates[language].monthsShort).filter(function(){
var m = this.slice(0, parts[i].length),
p = parts[i].slice(0, m.length);
return m == p;
});
val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
break;
}
parsed[part] = val;
}
for (var i=0, s; i<setters_order.length; i++){
s = setters_order[i];
if (s in parsed)
setters_map[s](date, parsed[s])
}
}
return date;
},
formatDate: function(date, format, language){
var val = {
d: date.getDate(),
m: date.getMonth() + 1,
M: dates[language].monthsShort[date.getMonth()],
MM: dates[language].months[date.getMonth()],
yy: date.getFullYear().toString().substring(2),
yyyy: date.getFullYear()
};
val.dd = (val.d < 10 ? '0' : '') + val.d;
val.mm = (val.m < 10 ? '0' : '') + val.m;
var date = [],
seps = $.extend([], format.separators);
for (var i=0, cnt = format.parts.length; i < cnt; i++) {
if (seps.length)
date.push(seps.shift())
date.push(val[format.parts[i]]);
}
return date.join('');
},
headTemplate: '<thead>'+
'<tr>'+
'<th class="prev"><i class="icon-arrow-left"/></th>'+
'<th colspan="5" class="switch"></th>'+
'<th class="next"><i class="icon-arrow-right"/></th>'+
'</tr>'+
'</thead>',
contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>'
};
DPGlobal.template = '<div class="datepicker dropdown-menu">'+
'<div class="datepicker-days">'+
'<table class=" table-condensed">'+
DPGlobal.headTemplate+
'<tbody></tbody>'+
'</table>'+
'</div>'+
'<div class="datepicker-months">'+
'<table class="table-condensed">'+
DPGlobal.headTemplate+
DPGlobal.contTemplate+
'</table>'+
'</div>'+
'<div class="datepicker-years">'+
'<table class="table-condensed">'+
DPGlobal.headTemplate+
DPGlobal.contTemplate+
'</table>'+
'</div>'+
'</div>';
}( window.jQuery ) |
/*!
* Jinja Templating for JavaScript v0.1.7
* https://github.com/sstur/jinja-js
*
* This is a slimmed-down Jinja2 implementation [http://jinja.pocoo.org/]
*
* In the interest of simplicity, it deviates from Jinja2 as follows:
* - Line statements, cycle, super, macros and block nesting are not implemented
* - auto escapes html by default (the filter is "html" not "e")
* - Only "html" and "safe" filters are built in
* - Filters are not valid in expressions; `foo|length > 1` is not valid
* - Expression Tests (`if num is odd`) not implemented (`is` translates to `==` and `isnot` to `!=`)
*
* Notes:
* - if property is not found, but method '_get' exists, it will be called with the property name (and cached)
* - `{% for n in obj %}` iterates the object's keys; get the value with `{% for n in obj %}{{ obj[n] }}{% endfor %}`
* - subscript notation `a[0]` takes literals or simple variables but not `a[item.key]`
* - `.2` is not a valid number literal; use `0.2`
*
*/
/*global require, exports, module, define */
var jinja;
(function(definition) {
if (typeof exports == 'object' && typeof module == 'object') {
// CommonJS/Node
return definition(require, exports, module);
}
if (typeof define == 'function') {
//AMD or Other
return define.amd ? define(['exports'], definition) : define('jinja', definition);
}
definition(function() {}, jinja = {});
})(function(require, jinja) {
"use strict";
var STRINGS = /'(\\.|[^'])*'|"(\\.|[^"'"])*"/g;
var IDENTS_AND_NUMS = /([$_a-z][$\w]*)|([+-]?\d+(\.\d+)?)/g;
var NUMBER = /^[+-]?\d+(\.\d+)?$/;
//non-primitive literals (array and object literals)
var NON_PRIMITIVES = /\[[@#~](,[@#~])*\]|\[\]|\{([@i]:[@#~])(,[@i]:[@#~])*\}|\{\}/g;
//bare identifiers such as variables and in object literals: {foo: 'value'}
var IDENTIFIERS = /[$_a-z][$\w]*/ig;
var VARIABLES = /i(\.i|\[[@#i]\])*/g;
var ACCESSOR = /(\.i|\[[@#i]\])/g;
var OPERATORS = /(===?|!==?|>=?|<=?|&&|\|\||[+\-\*\/%])/g;
//extended (english) operators
var EOPS = /(^|[^$\w])(and|or|not|is|isnot)([^$\w]|$)/g;
var LEADING_SPACE = /^\s+/;
var TRAILING_SPACE = /\s+$/;
var START_TOKEN = /\{\{\{|\{\{|\{%|\{#/;
var TAGS = {
'{{{': /^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?\}\}\}/,
'{{': /^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?\}\}/,
'{%': /^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?%\}/,
'{#': /^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?#\}/
};
var delimeters = {
'{%': 'directive',
'{{': 'output',
'{#': 'comment'
};
var operators = {
and: '&&',
or: '||',
not: '!',
is: '==',
isnot: '!='
};
var constants = {
'true': true,
'false': false,
'null': null
};
function Parser() {
this.nest = [];
this.compiled = [];
this.childBlocks = 0;
this.parentBlocks = 0;
this.isSilent = false;
}
Parser.prototype.push = function(line) {
if (!this.isSilent) {
this.compiled.push(line);
}
};
Parser.prototype.parse = function(src) {
this.tokenize(src);
return this.compiled;
};
Parser.prototype.tokenize = function(src) {
var lastEnd = 0, parser = this, trimLeading = false;
matchAll(src, START_TOKEN, function(open, index, src) {
//here we match the rest of the src against a regex for this tag
var match = src.slice(index + open.length).match(TAGS[open]);
match = (match ? match[0] : '');
//here we sub out strings so we don't get false matches
var simplified = match.replace(STRINGS, '@');
//if we don't have a close tag or there is a nested open tag
if (!match || ~simplified.indexOf(open)) {
return index + 1;
}
var inner = match.slice(0, 0 - open.length);
//check for white-space collapse syntax
if (inner.charAt(0) == '-') var wsCollapseLeft = true;
if (inner.slice(-1) == '-') var wsCollapseRight = true;
inner = inner.replace(/^-|-$/g, '').trim();
//if we're in raw mode and we are not looking at an "endraw" tag, move along
if (parser.rawMode && (open + inner) != '{%endraw') {
return index + 1;
}
var text = src.slice(lastEnd, index);
lastEnd = index + open.length + match.length;
if (trimLeading) text = trimLeft(text);
if (wsCollapseLeft) text = trimRight(text);
if (wsCollapseRight) trimLeading = true;
if (open == '{{{') {
//liquid-style: make {{{x}}} => {{x|safe}}
open = '{{';
inner += '|safe';
}
parser.textHandler(text);
parser.tokenHandler(open, inner);
});
var text = src.slice(lastEnd);
if (trimLeading) text = trimLeft(text);
this.textHandler(text);
};
Parser.prototype.textHandler = function(text) {
this.push('write(' + JSON.stringify(text) + ');');
};
Parser.prototype.tokenHandler = function(open, inner) {
var type = delimeters[open];
if (type == 'directive') {
this.compileTag(inner);
} else
if (type == 'output') {
var extracted = this.extractEnt(inner, STRINGS, '@');
//replace || operators with ~
extracted.src = extracted.src.replace(/\|\|/g, '~').split('|');
//put back || operators
extracted.src = extracted.src.map(function(part) {
return part.split('~').join('||');
});
var parts = this.injectEnt(extracted, '@');
if (parts.length > 1) {
var filters = parts.slice(1).map(this.parseFilter.bind(this));
this.push('filter(' + this.parseExpr(parts[0]) + ',' + filters.join(',') + ');');
} else {
this.push('filter(' + this.parseExpr(parts[0]) + ');');
}
}
};
Parser.prototype.compileTag = function(str) {
var directive = str.split(' ')[0];
var handler = tagHandlers[directive];
if (!handler) {
throw new Error('Invalid tag: ' + str);
}
handler.call(this, str.slice(directive.length).trim());
};
Parser.prototype.parseFilter = function(src) {
src = src.trim();
var match = src.match(/[:(]/);
var i = match ? match.index : -1;
if (i < 0) return JSON.stringify([src]);
var name = src.slice(0, i);
var args = src.charAt(i) == ':' ? src.slice(i + 1) : src.slice(i + 1, -1);
args = this.parseExpr(args, {terms: true});
return '[' + JSON.stringify(name) + ',' + args + ']';
};
Parser.prototype.extractEnt = function(src, regex, placeholder) {
var subs = [], isFunc = typeof placeholder == 'function';
src = src.replace(regex, function(str) {
var replacement = isFunc ? placeholder(str) : placeholder;
if (replacement) {
subs.push(str);
return replacement;
}
return str;
});
return {src: src, subs: subs};
};
Parser.prototype.injectEnt = function(extracted, placeholder) {
var src = extracted.src, subs = extracted.subs, isArr = Array.isArray(src);
var arr = (isArr) ? src : [src];
var re = new RegExp('[' + placeholder + ']', 'g'), i = 0;
arr.forEach(function(src, index) {
arr[index] = src.replace(re, function() {
return subs[i++];
});
});
return isArr ? arr : arr[0];
};
//replace complex literals without mistaking subscript notation with array literals
Parser.prototype.replaceComplex = function(s) {
var parsed = this.extractEnt(s, /i(\.i|\[[@#i]\])+/g, 'v');
parsed.src = parsed.src.replace(NON_PRIMITIVES, '~');
return this.injectEnt(parsed, 'v');
};
//parse expression containing literals (including objects/arrays) and variables (including dot and subscript notation)
//valid expressions: `a + 1 > b.c or c == null`, `a and b[1] != c`, `(a < b) or (c < d and e)`, 'a || [1]`
Parser.prototype.parseExpr = function(src, opts) {
opts = opts || {};
//extract string literals -> @
var parsed1 = this.extractEnt(src, STRINGS, '@');
//note: this will catch {not: 1} and a.is; could we replace temporarily and then check adjacent chars?
parsed1.src = parsed1.src.replace(EOPS, function(s, before, op, after) {
return (op in operators) ? before + operators[op] + after : s;
});
//sub out non-string literals (numbers/true/false/null) -> #
// the distinction is necessary because @ can be object identifiers, # cannot
var parsed2 = this.extractEnt(parsed1.src, IDENTS_AND_NUMS, function(s) {
return (s in constants || NUMBER.test(s)) ? '#' : null;
});
//sub out object/variable identifiers -> i
var parsed3 = this.extractEnt(parsed2.src, IDENTIFIERS, 'i');
//remove white-space
parsed3.src = parsed3.src.replace(/\s+/g, '');
//the rest of this is simply to boil the expression down and check validity
var simplified = parsed3.src;
//sub out complex literals (objects/arrays) -> ~
// the distinction is necessary because @ and # can be subscripts but ~ cannot
while (simplified != (simplified = this.replaceComplex(simplified)));
//now @ represents strings, # represents other primitives and ~ represents non-primitives
//replace complex variables (those with dot/subscript accessors) -> v
while (simplified != (simplified = simplified.replace(/i(\.i|\[[@#i]\])+/, 'v')));
//empty subscript or complex variables in subscript, are not permitted
simplified = simplified.replace(/[iv]\[v?\]/g, 'x');
//sub in "i" for @ and # and ~ and v (now "i" represents all literals, variables and identifiers)
simplified = simplified.replace(/[@#~v]/g, 'i');
//sub out operators
simplified = simplified.replace(OPERATORS, '%');
//allow 'not' unary operator
simplified = simplified.replace(/!+[i]/g, 'i');
var terms = opts.terms ? simplified.split(',') : [simplified];
terms.forEach(function(term) {
//simplify logical grouping
while (term != (term = term.replace(/\(i(%i)*\)/g, 'i')));
if (!term.match(/^i(%i)*$/)) {
throw new Error('Invalid expression: ' + src);
}
});
parsed3.src = parsed3.src.replace(VARIABLES, this.parseVar.bind(this));
parsed2.src = this.injectEnt(parsed3, 'i');
parsed1.src = this.injectEnt(parsed2, '#');
return this.injectEnt(parsed1, '@');
};
Parser.prototype.parseVar = function(src) {
var args = Array.prototype.slice.call(arguments);
var str = args.pop(), index = args.pop();
//quote bare object identifiers (might be a reserved word like {while: 1})
if (src == 'i' && str.charAt(index + 1) == ':') {
return '"i"';
}
var parts = ['"i"'];
src.replace(ACCESSOR, function(part) {
if (part == '.i') {
parts.push('"i"');
} else
if (part == '[i]') {
parts.push('get("i")');
} else {
parts.push(part.slice(1, -1));
}
});
return 'get(' + parts.join(',') + ')';
};
//escapes a name to be used as a javascript identifier
Parser.prototype.escName = function(str) {
return str.replace(/\W/g, function(s) {
return '$' + s.charCodeAt(0).toString(16);
});
};
Parser.prototype.parseQuoted = function(str) {
if (str.charAt(0) == "'") {
str = str.slice(1, -1).replace(/\\.|"/, function(s) {
if (s == "\\'") return "'";
return s.charAt(0) == '\\' ? s : ('\\' + s);
});
str = '"' + str + '"';
}
//todo: try/catch or deal with invalid characters (linebreaks, control characters)
return JSON.parse(str);
};
//the context 'this' inside tagHandlers is the parser instance
var tagHandlers = {
'if': function(expr) {
this.push('if (' + this.parseExpr(expr) + ') {');
this.nest.unshift('if');
},
'else': function() {
if (this.nest[0] == 'for') {
this.push('}, function() {');
} else {
this.push('} else {');
}
},
'elseif': function(expr) {
this.push('} else if (' + this.parseExpr(expr) + ') {');
},
'endif': function() {
this.nest.shift();
this.push('}');
},
'for': function(str) {
var i = str.indexOf(' in ');
var name = str.slice(0, i).trim();
var expr = str.slice(i + 4).trim();
this.push('each(' + this.parseExpr(expr) + ',' + JSON.stringify(name) + ',function() {');
this.nest.unshift('for');
},
'endfor': function() {
this.nest.shift();
this.push('});');
},
'raw': function() {
this.rawMode = true;
},
'endraw': function() {
this.rawMode = false;
},
'set': function(stmt) {
var i = stmt.indexOf('=');
var name = stmt.slice(0, i).trim();
var expr = stmt.slice(i + 1).trim();
this.push('set(' + JSON.stringify(name) + ',' + this.parseExpr(expr) + ');');
},
'block': function(name) {
if (this.isParent) {
++this.parentBlocks;
var blockName = 'block_' + (this.escName(name) || this.parentBlocks);
this.push('block(typeof ' + blockName + ' == "function" ? ' + blockName + ' : function() {');
} else
if (this.hasParent) {
this.isSilent = false;
++this.childBlocks;
blockName = 'block_' + (this.escName(name) || this.childBlocks);
this.push('function ' + blockName + '() {');
}
this.nest.unshift('block');
},
'endblock': function() {
this.nest.shift();
if (this.isParent) {
this.push('});');
} else
if (this.hasParent) {
this.push('}');
this.isSilent = true;
}
},
'extends': function(name) {
name = this.parseQuoted(name);
var parentSrc = this.readTemplateFile(name);
this.isParent = true;
this.tokenize(parentSrc);
this.isParent = false;
this.hasParent = true;
//silence output until we enter a child block
this.isSilent = true;
},
'include': function(name) {
name = this.parseQuoted(name);
var incSrc = this.readTemplateFile(name);
this.isInclude = true;
this.tokenize(incSrc);
this.isInclude = false;
}
};
//liquid style
tagHandlers.assign = tagHandlers.set;
//python/django style
tagHandlers.elif = tagHandlers.elseif;
var getRuntime = function runtime(data, opts) {
var defaults = {autoEscape: 'html'};
var _toString = Object.prototype.toString;
var _hasOwnProperty = Object.prototype.hasOwnProperty;
var toString = function(val) {
if (val == null) return '';
return (typeof val.toString == 'function') ? val.toString() : _toString.call(val);
};
var extend = function(dest, src) {
Object.keys(src).forEach(function(key) {
dest[key] = src[key];
});
return dest;
};
//get a value, lexically, starting in current context; a.b -> get("a","b")
var get = function() {
var val, n = arguments[0], c = stack.length;
while (c--) {
val = stack[c][n];
if (typeof val != 'undefined') break;
}
for (var i = 1, len = arguments.length; i < len; i++) {
if (val == null) continue;
n = arguments[i];
val = (_hasOwnProperty.call(val, n)) ? val[n] : (typeof val._get == 'function' ? (val[n] = val._get(n)) : null);
}
return (val == null) ? null : val;
};
var set = function(n, val) {
stack[stack.length - 1][n] = val;
};
var push = function(ctx) {
stack.push(ctx || {});
};
var pop = function() {
stack.pop();
};
var write = function(str) {
output.push(str);
};
var filter = function(val) {
for (var i = 1, len = arguments.length; i < len; i++) {
var arr = arguments[i], name = arr[0], filter = filters[name];
if (filter) {
arr[0] = val;
//now arr looks like [val, arg1, arg2]
val = filter.apply(data, arr);
} else {
throw new Error('Invalid filter: ' + name);
}
}
if (opts.autoEscape && name != opts.autoEscape && name != 'safe') {
//auto escape if not explicitly safe or already escaped
val = filters[opts.autoEscape].call(data, val);
}
output.push(val);
};
var each = function(obj, loopvar, fn1, fn2) {
if (obj == null) return;
var arr = Array.isArray(obj) ? obj : Object.keys(obj), len = arr.length;
var ctx = {loop: {length: len, first: arr[0], last: arr[len - 1]}};
push(ctx);
for (var i = 0; i < len; i++) {
extend(ctx.loop, {index: i + 1, index0: i});
fn1(ctx[loopvar] = arr[i]);
}
if (len == 0 && fn2) fn2();
pop();
};
var block = function(fn) {
push();
fn();
pop();
};
var render = function() {
return output.join('');
};
data = data || {};
opts = extend(defaults, opts || {});
var filters = extend({
html: function(val) {
return toString(val)
.split('&').join('&')
.split('<').join('<')
.split('>').join('>')
.split('"').join('"');
},
safe: function(val) {
return val;
}
}, opts.filters || {});
var stack = [Object.create(data || {})], output = [];
return {get: get, set: set, push: push, pop: pop, write: write, filter: filter, each: each, block: block, render: render};
};
var runtime;
jinja.compile = function(markup, opts) {
opts = opts || {};
var parser = new Parser();
parser.readTemplateFile = this.readTemplateFile;
var code = [];
code.push('function render($) {');
code.push('var get = $.get, set = $.set, push = $.push, pop = $.pop, write = $.write, filter = $.filter, each = $.each, block = $.block;');
code.push.apply(code, parser.parse(markup));
code.push('return $.render();');
code.push('}');
code = code.join('\n');
if (opts.runtime === false) {
var fn = new Function('data', 'options', 'return (' + code + ')(runtime(data, options))');
} else {
runtime = runtime || (runtime = getRuntime.toString());
fn = new Function('data', 'options', 'return (' + code + ')((' + runtime + ')(data, options))');
}
return {render: fn};
};
jinja.render = function(markup, data, opts) {
var tmpl = jinja.compile(markup);
return tmpl.render(data, opts);
};
jinja.templateFiles = [];
jinja.readTemplateFile = function(name) {
var templateFiles = this.templateFiles || [];
var templateFile = templateFiles[name];
if (templateFile == null) {
throw new Error('Template file not found: ' + name);
}
return templateFile;
};
/*!
* Helpers
*/
function trimLeft(str) {
return str.replace(LEADING_SPACE, '');
}
function trimRight(str) {
return str.replace(TRAILING_SPACE, '');
}
function matchAll(str, reg, fn) {
//copy as global
reg = new RegExp(reg.source, 'g' + (reg.ignoreCase ? 'i' : '') + (reg.multiline ? 'm' : ''));
var match;
while ((match = reg.exec(str))) {
var result = fn(match[0], match.index, str);
if (typeof result == 'number') {
reg.lastIndex = result;
}
}
}
}); |
import path from 'path'
import fs from 'fs'
import images from 'images'
import _ from 'lodash'
import layout from 'layout'
import message from './message'
import { plainBgCSS, plainItemCss, keyframeCSS } from './template'
/**
* 合并图片、生成css
*/
export default class SpriteImage {
// 默认配置
config = {
prefix: 'sprite',
imgdir: '../../img',
cssdir: '../../css',
cssimg: '../img',
limit: {
width: 64000,
height: 64000
}
}
sort = {
x: 'left-right',
y: 'top-down',
tree: 'binary-tree'
}
constructor(config = {}) {
// 合并传入配置、默认配置
this.config = {...this.config, ...config }
// 去掉cssimg最后的斜杠
let cssimg = this.config.cssimg
if (cssimg.endsWith('/')) {
this.config.cssimg = cssimg.substring(0, cssimg.length - 1)
}
// 设置处理图片的宽高限制
images.setLimit(this.config.limit.width, this.config.limit.height)
}
generate(dir, options = {}) {
if (!dir) {
throw Error(message.PATH_NULL)
}
if (!_.isString(dir)) {
throw Error(message.PATH_NOT_STRING)
}
if (dir.indexOf(path.sep) === -1) {
throw Error(message.PATH_ERROR)
}
if (!fs.existsSync(dir)) {
throw Error(message.PATH_NOT_EXISTS)
}
if (!_.isPlainObject(options)) {
throw Error(message.CONFIG_ERROR)
}
options = this._handleOptions({ dir, ...options })
this._createImage(options, this._wrapImage(options))
}
_handleOptions(options) {
// 从路径中获取目录名称
let name = options.dir.split(path.sep).pop()
// 默认选项
let defaults = {
name,
enable: true,
outext: 'png',
padding: 0,
sort: this.sort.tree
}
// 如果为序列帧图,合并传入配置和默认配置
if (options.keyframe) {
options.keyframe = {
width: 640,
height: 1136,
duration: 80,
vertical: '100%',
infinite: false,
...options.keyframe
}
options.padding = 0
options.sort = this.sort.x
} else {
options.sort = this.sort.hasOwnProperty(options.sort) ? this.sort[options.sort] : this.sort.tree
}
return {
...defaults,
...options
}
}
_wrapImage(options) {
// 创建layout对象
let layer = layout(options.sort, {
sort: !options.keyframe
})
// 读取目录文件
let imgs = fs.readdirSync(options.dir)
// 过滤非png、jpg图片
imgs = imgs.filter(file => /\.(png|jpe?g)$/.test(file))
// 序列图按照文件名称从小到大排序
if (options.keyframe) {
imgs.sort((a, b) => parseInt(a) - parseInt(b))
}
// 忽略没有图片文件的文件夹
if (!imgs.length) {
return
}
imgs.forEach(file => {
let wrap = images(path.join(options.dir, file))
let keyframe = options.keyframe
// 如果为序列帧图并且图片实际宽高大于设置宽高时缩放图片
if (keyframe && wrap.width() > keyframe.width && wrap.height() > keyframe.height) {
wrap.resize(keyframe.width, keyframe.height)
}
// 添加图片到layout中
layer.addItem({
width: wrap.width() + options.padding,
height: wrap.height() + options.padding,
meta: file.replace(path.extname(file), ''),
img: wrap
})
})
// 图片排列信息
let result = layer.export()
if (result.items.length) {
result.width -= options.padding
result.height -= options.padding
}
return result
}
_createImage(options, result) {
// 创建合并后的图片
let newImg = images(result.width, result.height)
let prefix = this.config.prefix
let cssname = `${prefix}-${options.name}.css`
let imgname = `${prefix}-${options.name}.${options.outext}`
let cssurl = `${this.config.cssimg}/${imgname}`
let css = ''
if (options.keyframe) {
// 生成动画以及动画调用
if (Number.isInteger(options.keyframe.vertical)) {
options.keyframe.vertical += 'px'
}
css += _.template(keyframeCSS)({
prefix,
cssurl,
...result,
...options
})
} else {
// 生成背景设置
css += _.template(plainBgCSS)({
prefix,
cssurl,
...options
})
}
result.items.forEach(item => {
// 把每张图片画到新创建的大图上
newImg.draw(item.img, item.x, item.y)
// 生成图片宽高位置信息
if (!options.keyframe) {
css += _.template(plainItemCss)({
prefix,
item,
...result,
...options
})
}
})
// 保存css
let csspath = path.resolve(options.dir, this.config.cssdir)
fs.existsSync(csspath) || fs.mkdirSync(csspath)
fs.writeFileSync(path.join(csspath, cssname), css)
// 保存图片
let imgpath = path.resolve(options.dir, this.config.imgdir)
fs.existsSync(imgpath) || fs.mkdirSync(imgpath)
newImg.save(path.join(imgpath, imgname))
// 垃圾回收
images.gc()
}
}
|
import React, { PropTypes } from 'react'
import { Link } from 'react-router'
// Imported Link from react-router which is an easy way to make a link that causes a route change
export default function QuizListItem (props) {
let handleClick = (event) => {
props.onClick(props.id)
}
return (
<div style={{cursor: 'pointer'}}
className='row' >
{ /* Moved this content into a span so the onClick wouldn't interfere with the questions */ }
<span onClick={handleClick}> {props.name} - {props.isActive ? 'Yes' : 'No'} - </span>
{ /* Added a link pointing to /Quizzes/5/Questions This does nothing now but will function later */ }
<Link to={`/Quizzes/${props.id}/Questions`}>{props.questionCount} Questions</Link>
</div>
)
}
// Added questionCount to our proptypes
QuizListItem.propTypes = {
id: PropTypes.number.isRequired,
onClick: PropTypes.func.isRequired,
name: PropTypes.string.isRequired,
isActive: PropTypes.bool.isRequired,
questionCount: PropTypes.number.isRequired
}
|
var assert = require('assert');
var Template = require('../lib/template');
var Config = require('../lib/config');
var Permalink = require('../lib/permalink');
describe('Template', function() {
var config = new Config({
layoutsDirectory: 'test/fixtures'
});
describe('#constructor', function() {
it('creates a new template instance from a filename '
+ 'and config object', function() {
var filename = 'test/fixtures/template-child.html';
var template = new Template(filename, config);
assert.equal(template.filename, filename);
assert.equal(template.content.trim(), 'Template contents.');
assert.equal(template.config, config);
});
it('creates a new template instance from a data object, content '
+ 'and config object', function() {
var data = { foo: 'bar', fizz: 'buzz' };
var content = 'This is the content.';
var template = new Template(data, content, config);
assert.equal(template.data, data);
assert.equal(template.content.trim(), content);
assert.equal(template.config, config);
});
});
describe('#clone', function() {
it('clones all properties of a template created from an object.',
function() {
var data = {
foo: 'bar',
fizz: 'buzz'
};
var template = new Template(data, 'This is the content', config);
var clone = template.clone();
assert.equal(clone.content, template.content);
assert.deepEqual(clone.data, template.data);
assert.notEqual(clone, template);
});
it('clones all properties of a template created from a file.', function() {
var template = new Template('test/fixtures/post.md', config);
var clone = template.clone();
assert.equal(clone.content, template.content);
assert.equal(clone.format, template.format);
assert.equal(clone.layout, template.layout);
assert.deepEqual(clone.data, template.data);
assert.notEqual(clone, template);
});
});
describe('#getData', function() {
it('returns its merged data with the data '
+ 'all the way up its layout chain.', function() {
var data = {
layout: 'template-parent',
foo: 'foo from data',
query: {
type: 'post'
}
};
var template1 = new Template('test/fixtures/template-child.html', config);
var template2 = new Template(data, '', config);
assert.deepEqual(template1.data, {
layout: 'template-parent',
foo: 'foo from child',
bar: 'foo from parent',
query: {
type: 'article',
limit: 6
}
});
assert.deepEqual(template2.data, {
layout: 'template-parent',
foo: 'foo from data',
bar: 'foo from parent',
query: {
type: 'post',
limit: 6
}
});
});
});
describe('#renderContent', function() {
it('renders the template content by itself');
});
describe('#renderLayout', function() {
it('renders the template up the layout chain');
});
describe('.getOrCreateFromFile', function() {
it('returns a new instance '
+ 'unless one already exists at that location', function() {
var f1 = Template.getOrCreateFromFile('test/fixtures/template.html', config);
var f2 = Template.getOrCreateFromFile('test/fixtures/template.html', config);
assert.equal(f2, f1);
});
});
});
|
// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See https://js.arcgis.com/4.16/esri/copyright.txt for details.
//>>built
define("require exports ../../core/tsSupport/awaiterHelper ../../core/tsSupport/generatorHelper ../../core/tsSupport/assignHelper ../../request ../../core/Logger ../../core/promiseUtils ../cim/cimAnalyzer".split(" "),function(z,f,g,h,q,r,t,u,v){function l(a,c){return g(this,void 0,void 0,function(){var b,d;return h(this,function(e){switch(e.label){case 0:if(a.data||!a.styleUrl||!a.styleName)return[2,a];b=a.styleName;d=a;return[4,w(b,a.styleUrl,c)];case 1:return d.data=e.sent(),[2,a]}})})}function w(a,
c,b){return g(this,void 0,void 0,function(){var d,e;return h(this,function(g){if(k.has(a))return[2,k.get(a).then(function(a){return f.makeSymbolRef(a.data)})];try{return d=c+"/resources/styles/cim/"+a+".json",e=r(d,q({responseType:"json",query:{f:"json"}},b)),k.set(a,e),[2,e.then(function(a){return f.makeSymbolRef(a.data)})]}catch(x){return y.error("error requesting "+a+", reason is "+x.message),k.has(a)&&k.delete(a),[2,null]}})})}var m=this;Object.defineProperty(f,"__esModule",{value:!0});var y=
t.getLogger("esri/symbols/support/cimSymbolUtils"),n=function(){function a(a,b){this.layers=a;this.data=b}Object.defineProperty(a.prototype,"type",{get:function(){return"expanded-cim"},enumerable:!0,configurable:!0});a.prototype.hash=function(){for(var a="",b=0,d=this.layers;b<d.length;b++)a+=d[b].templateHash;return a};return a}();f.expandSymbols=function(a,c,b,d){return g(m,void 0,void 0,function(){return h(this,function(e){return[2,u.all(a.map(function(a){return f.expandSymbol(a,c,b,d)}))]})})};
var p=function(a,c,b){return g(m,void 0,void 0,function(){var d;return h(this,function(e){switch(e.label){case 0:return d=n.bind,[4,v.analyzeCIMSymbol(a.data,c&&"dictionary"===c.type?c.fieldMap:null,b)];case 1:return[2,new (d.apply(n,[void 0,e.sent(),a.data]))]}})})};f.expandSymbol=function(a,c,b,d){return g(m,void 0,void 0,function(){var e,f;return h(this,function(g){switch(g.label){case 0:if(!a)return[2,null];if("cim"!==a.type)return[3,2];e=p;return[4,l(a,d)];case 1:return[2,e.apply(void 0,[g.sent(),
c,b])];case 2:if("web-style"!==a.type)return[3,4];f=p;return[4,a.fetchCIMSymbol(d)];case 3:return[2,f.apply(void 0,[g.sent(),c,b])];case 4:return[2,a]}})})};f.fetchSymbol=function(a,c){return g(this,void 0,void 0,function(){return h(this,function(b){return a?"cim"===a.type?[2,l(a,c)]:"web-style"===a.type?[2,a.fetchCIMSymbol(c)]:[2,a]:[2,null]})})};f.fetchCIMData=l;f.makeSymbolRef=function(a,c){return null===a?null:"CIMSymbolReference"===a.type?a:{type:"CIMSymbolReference",symbol:a,primitiveOverrides:c}};
var k=new Map}); |
import styled from 'styled-components';
const Input = styled.input`
width: 50%;
height: 40px;
font-size: 16px;
color: #727272;
background-color: #403F44;
border: none;
border-radius: 2px;
padding-left: 18px;
padding-right: 18px;
font-family: 'Open Sans', sans-serif;
:focus {
background-color: #F1F1F1;
outline: none;
width: 70%;
}
-webkit-transition: background-color, width 0.4s;
-moz-transition: background-color, width 0.4s;
-o-transition: background-color, width 0.4s;
transition: background-color, width 0.4s;
@media screen and (max-width: 768px) {
width: 70%;
:focus {
width: 90%;
}
}
`;
export default Input; |
import { apiProxyAxios } from '../../../../../../client/components/Task/utils'
export const updateInvestorDetails = (values) => {
const { id, investor_company_id } = values
return apiProxyAxios.patch(
`v4/large-investor-profile/${id}?investor_company_id=${investor_company_id}`,
values
)
}
|
'use strict';
angular.module('mean').controller('NotesController', ['$scope','$rootScope','$sce','$upload', '$stateParams','$http', '$location', 'Global', 'Notes', 'Comments', 'Classes','Socket',
function($scope,$rootScope, $sce, $upload, $stateParams, $http, $location, Global, Notes, Comments,Classes,Socket) {
$scope.global = Global;
$scope.isEditModel = false;
$scope.fileNames = [];
// Incoming
Socket.on('onCommentCreated', function(data) {
if (data.onNote == $stateParams.noteId ) {
$scope.findComment();
};
});
$scope.editorOptions = {
language: 'en',
'extraPlugins': "imagebrowser,mediaembed",
imageBrowser_listUrl: '/api/gallery',
filebrowserBrowseUrl: '/api/ckeditor/files',
filebrowserImageUploadUrl: '/api/ckeditor/images',
filebrowserUploadUrl: '/api/ckeditor/files',
toolbar: 'full',
toolbar_full: [
{ name: 'basicstyles',
items: [ 'Bold', 'Italic', 'Strike', 'Underline' ] },
{ name: 'paragraph', items: [ 'BulletedList', 'NumberedList', 'Blockquote' ] },
{ name: 'editing', items: ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock' ] },
{ name: 'links', items: [ 'Link', 'Unlink', 'Anchor' ] },
{ name: 'tools', items: [ 'SpellChecker', 'Maximize' ] },
{ name: 'clipboard', items: [ 'Undo', 'Redo' ] },
{ name: 'styles', items: [ 'Format', 'FontSize', 'TextColor', 'PasteText', 'PasteFromWord', 'RemoveFormat' ] },
{ name: 'insert', items: [ 'Image', 'Table', 'SpecialChar', 'MediaEmbed' ] },'/',
]
};
$scope.onFileSelect = function($files) {
$scope.fileNames = [];
for (var i = 0; i < $files.length; i++) {
var file = $files[i];
$scope.upload = $upload.upload({
url: '/upload/note',
method: 'POST',
withCredentials: true,
file: file
}).success(function(data, status, headers, config) {
// file is uploaded successfully
$scope.fileNames.push(data.files.file.name);
})
.progress(function(evt) {
var percent =parseInt(100.0 * evt.loaded / evt.total);
});
}
};
// utility function
$scope.$on('LoadJs', function() {
// load menu
$('#nav-accordion').dcAccordion({
eventType: 'click',
autoClose: true,
saveState: true,
disableLink: true,
speed: 'slow',
showCount: false,
autoExpand: false,
classExpand: 'dcjq-current-parent'
});
$("#selectClasses").select2({
placeholder: "Select a class",
multiple: true,
ajax: {
url: "/classes/suggest",
dataType: 'jsonp',
data: function (term, page) {
return {
q: term, // search term
page_limit: 10,
};
},
results: function (data, page) {
var classData = [];
for (var i = 0; i < data.length; i++) {
classData[i] = { id : data[i]._id , text : data[i].name , members : data[i].members , owner : data[i].createBy._id, file : data[i].file };
};
return {results: classData};
}
},
initSelection: function(element, callback) {
var id=$(element).val();
},
formatResult: classFormatResult,
formatSelection: classFormatSelection,
escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
});
});
$scope.$on('LoadCreateNoteJs', function() {
$('#tags_1').tagsInput({width:'auto'});
// $('.wysihtml5').wysihtml5();
$http.get('/api/users?q=')
.success(function(users){
$('#commentOnNote').mention({
users : users,
delimiter: '@', // Username Delimiter
sensitive : true,
queryBy: ['name', 'username']
});
});
});
$scope.$on('LoadEditNoteJs', function() {
$scope.isEditModel = true;
});
$scope.hasAuthorization = function(note) {
if (!note || !note.createBy) return false;
return $scope.global.isAdmin || note.createBy._id === $scope.global.user._id;
};
// list all
$scope.find = function() {
Notes.query({share : 0},function(notes) {
$scope.notes = notes;
});
$("#draggable_portlets").sortable({
connectWith: ".col-md-4",
items: ".col-md-4",
opacity: 0.8,
coneHelperSize: true,
placeholder: 'sortable-box-placeholder round-all',
forcePlaceholderSize: true,
tolerance: "pointer",
stop : function(event,ui) {
var sortedIDs = $( this ).sortable( "toArray" );
$http.post('/notes/order', { uid : $scope.global.user._id , notes : sortedIDs }).success(function(resp){
});
}
});
$( "#draggable_portlets" ).disableSelection();
};
$scope.findComment = function() {
Comments.query({ noteId : $stateParams.noteId },function(comments) {
$scope.comments = comments;
});
};
$scope.share = function() {
Notes.query({share : 1},function(notes) {
$scope.notes = notes;
});
};
// find by id
$scope.findOne = function() {
$('#myModalDetail').modal('hide');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
$http.get('/api/users?q=')
.success(function(users){
$('#comment').mention({
users : users,
delimiter: '@', // Username Delimiter
sensitive : true,
queryBy: ['name', 'username']
});
});
Notes.get({
noteId: $stateParams.noteId
}, function(note) {
note.tags = note.tags.join(',');
note.classes = note.sendToClass.join(',');
$scope.note = note;
$scope.htmlContent = $sce.trustAsHtml($scope.note.content);
if ($scope.isEditModel) {
$('#tags_1').val(note.tags);
$('#tags_1').tagsInput({
width:'auto',
'height':'30px',
'interactive':true,
'defaultText':'add a tag'
}
);
// $('.wysihtml5').wysihtml5();
var tmpData = [];
for (var i = 0; i < note.sendToClass.length; i++) {
tmpData[i] = { 'id' : note.sendToClassIds[i] , 'text' : note.sendToClass[i] } ;
};
$("#selectClasses").select2('data', tmpData);
$scope.isEditModel = false;
};
});
};
// send comment
$scope.comment = function(){
var comment = new Comments({
content : this.content,
onNote : $stateParams.noteId
});
comment.$save(function(response) {
// reload comment
$scope.findComment();
Socket.emit('createComment', comment);
});
this.content = '';
}
// create note
$scope.create = function() {
var classArr = [];
var classIdArr = [];
var membersArr = [];
var classSelection = $('#selectClasses').select2('data');
for (var i = 0; i < classSelection.length; i++) {
// insert class
classArr.push(classSelection[i].text.trim());
classIdArr.push(classSelection[i].id.trim());
// push member
for (var j = 0; j < classSelection[i].members.length; j++) {
if (membersArr.indexOf(classSelection[i].members[j]) === -1) {
membersArr.push(classSelection[i].members[j]);
};
};
};
// this.content = $('.wysihtml5').val();
this.tags = $('.tags').val();
var note = new Notes({
title : this.title,
content : this.content,
tags : this.tags,
classes : classArr,
classesIds : classIdArr,
members : membersArr,
quickComment : this.quickComment,
fileNames : $scope.fileNames
});
note.$save(function(response) {
$location.path('notes/' + response._id);
});
this.title = '';
$('#tags_1').importTags('');
this.content = '';
this.quickComment = '';
$('#myModalDetail').modal('hide');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
};
$scope.remove = function(note) {
if (note) {
note.$remove();
for (var i in $scope.notes) {
if ($scope.notes[i] === note) {
$scope.notes.splice(i, 1);
}
}
} else {
$scope.note.$remove(function(response) {
$location.path('notes');
});
}
};
$scope.removeFile = function(note,file){
for (var i in note.fileNames) {
if (note.fileNames[i] === file) {
note.fileNames.splice(i, 1);
}
}
note.$update(function() {
//todo: remove file
});
}
$scope.update = function() {
var note = $scope.note;
// note.content = $('.wysihtml5').val();
note.tags = $('.tags').val();
if (!note.updated) {
note.updated = [];
}
note.updated.push(new Date().getTime());
var classArr = [];
var classIdArr = [];
var membersArr = [];
var classSelection = $('#selectClasses').select2('data');
for (var i = 0; i < classSelection.length; i++) {
// insert class
classArr.push(classSelection[i].text.trim());
classIdArr.push(classSelection[i].id.trim());
// todo : process member or not
// push member later
};
note.sendToClass = classArr;
note.sendToClassIds = classIdArr;
if ($scope.fileNames.length) {
for (var i = 0; i < $scope.fileNames.length; i++) {
var filename = $scope.fileNames[i];
note.fileNames.push(filename);
};
};
note.$update(function() {
$location.path('notes/' + note._id);
});
};
}
]);
|
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, hashHistory, Link, Redirect } from 'react-router';
import Containment from './pages/containment';
import Dynamic from './pages/dynamic';
import HOC from './pages/hoc';
import Image from './pages/image';
import Handle from './pages/handle';
import Fixed from './pages/fixed';
import Vertical from './pages/vertical';
const App = ({ children }) => (
<div className="wrapper">
<ul>
<li><Link to="/normal" activeClassName="active">Normal</Link></li>
<li><Link to="/image" activeClassName="active">Image</Link></li>
<li><Link to="/dynamic" activeClassName="active">Dynamic</Link></li>
<li><Link to="/containment" activeClassName="active">Containment</Link></li>
<li><Link to="/handle" activeClassName="active">Handle</Link></li>
<li><Link to="/fixed" activeClassName="active">Mixed with un-sortable items</Link></li>
<li><Link to="/vertical" activeClassName="active">Vertically sorting</Link></li>
</ul>
{children}
</div>
);
const routes = (
<Router history={hashHistory}>
<Route path="/" component={App}>
<Route path="/normal" component={HOC} />
<Route path="/image" component={Image} />
<Route path="/dynamic" component={Dynamic} />
<Route path="/containment" component={Containment} />
<Route path="/handle" component={Handle} />
<Route path="/fixed" component={Fixed} />
<Route path="/vertical" component={Vertical} />
<Redirect to="/normal" />
</Route>
</Router>
);
ReactDOM.render(routes, document.getElementById('app'));
|
/* eslint-env mocha */
'use strict'
const hat = require('hat')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const delay = require('delay')
module.exports = (common, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.files.touch', function () {
this.timeout(10 * 1000)
let ipfs
async function testMtime (mtime, expectedMtime) {
const testPath = `/test-${hat()}`
await ipfs.files.write(testPath, Buffer.from('Hello, world!'), {
create: true
})
const stat = await ipfs.files.stat(testPath)
expect(stat).to.not.have.deep.property('mtime', expectedMtime)
await ipfs.files.touch(testPath, {
mtime
})
const stat2 = await ipfs.files.stat(testPath)
expect(stat2).to.have.nested.deep.property('mtime', expectedMtime)
}
before(async () => { ipfs = (await common.spawn()).api })
after(() => common.clean())
it('should have default mtime', async function () {
this.slow(5 * 1000)
const testPath = `/test-${hat()}`
await ipfs.files.write(testPath, Buffer.from('Hello, world!'), {
create: true
})
const stat = await ipfs.files.stat(testPath)
expect(stat).to.not.have.property('mtime')
await ipfs.files.touch(testPath)
const stat2 = await ipfs.files.stat(testPath)
expect(stat2).to.have.property('mtime').that.does.not.deep.equal({
secs: 0,
nsecs: 0
})
})
it('should update file mtime', async function () {
this.slow(5 * 1000)
const testPath = `/test-${hat()}`
const mtime = new Date()
const seconds = Math.floor(mtime.getTime() / 1000)
await ipfs.files.write(testPath, Buffer.from('Hello, world!'), {
create: true,
mtime
})
await delay(2000)
await ipfs.files.touch(testPath)
const stat = await ipfs.files.stat(testPath)
expect(stat).to.have.nested.property('mtime.secs').that.is.greaterThan(seconds)
})
it('should update directory mtime', async function () {
this.slow(5 * 1000)
const testPath = `/test-${hat()}`
const mtime = new Date()
const seconds = Math.floor(mtime.getTime() / 1000)
await ipfs.files.mkdir(testPath, {
create: true,
mtime
})
await delay(2000)
await ipfs.files.touch(testPath)
const stat2 = await ipfs.files.stat(testPath)
expect(stat2).to.have.nested.property('mtime.secs').that.is.greaterThan(seconds)
})
it('should set mtime as Date', async function () {
await testMtime(new Date(5000), {
secs: 5,
nsecs: 0
})
})
it('should set mtime as { nsecs, secs }', async function () {
const mtime = {
secs: 5,
nsecs: 0
}
await testMtime(mtime, mtime)
})
it('should set mtime as timespec', async function () {
await testMtime({
Seconds: 5,
FractionalNanoseconds: 0
}, {
secs: 5,
nsecs: 0
})
})
it('should set mtime as hrtime', async function () {
const mtime = process.hrtime()
await testMtime(mtime, {
secs: mtime[0],
nsecs: mtime[1]
})
})
})
}
|
var React = require('react-native');
var Dimensions = require('Dimensions');
var {
PixelRatio
} = React;
var Util = {
//单位像素
pixel: 1 / PixelRatio.get(),
//屏幕尺寸
size: {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height
},
//post
post: function (url, data, callback) {
var fetchOptions = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
};
fetch(url, fetchOptions)
.then((response) => response.text())
.then((responseText) => {
callback(JSON.parse(responseText));
});
},
//get
getJSON: function(url, callback){
fetch(url)
.then((response) => response.text())
.then((responseText) => {
callback(JSON.parse(responseText));
});
},
//高德地图key,测试key,请勿商用,后期会清理。申请key请到:http://lbs.amap.com/console/
amapKey: '98cd4d3c1c2865132e73d851654c9c1b',
//周边搜索服务
searchURL: 'http://restapi.amap.com/v3/place/around?',
detailURL: 'http://restapi.amap.com/v3/place/detail?'
};
module.exports = Util; |
var babel = require('../lib/gulp-blueprint-babel');
babel({
dependencies: [],
input: 'app.js',
browserify: {
debug: true,
basedir: 'app/client'
}
}); |
/*! Loop protect | (c) 2014 JS Bin | jsbin.mit-license.org */
/**
* Protect against infinite loops.
* Look for for, while and do loops, and insert a check function at the start of
* the loop. If the check function is called many many times then it returns
* true, preventing the loop from running again.
*/
if (typeof DEBUG === 'undefined') { DEBUG = true; } //jshint ignore:line
(function (root, factory) {
'use strict';
/*global define*/
if (typeof define === 'function' && define.amd) {
define(factory(root));
} else if (typeof exports === 'object') {
module.exports = factory(root);
} else {
root.loopProtect = factory(root);
}
})(this, function loopProtectModule(root) {
/*global DEBUG*/
'use strict';
let debug = null;
// the standard loops - note that recursive is not supported
let re = /\b(for|while|do)\b/g;
let reSingle = /\b(for|while|do)\b/;
let labelRe = /\b(?!default:)([a-z_]{1}\w+:)/i;
let comments = /(?:\/\*(?:[\s\S]*?)\*\/)|(?:([\s;])+\/\/(?:.*)$)/gm;
let loopProtect = rewriteLoops;
// used in the loop detection
loopProtect.counters = {};
// expose debug info
loopProtect.debug = function debugSwitch(state) {
debug = state ? function () {
console.log.apply(console, [].slice.apply(arguments));
} : function () { };
};
loopProtect.debug(false); // off by default
// the method - as this could be aliased to something else
loopProtect.alias = 'loopProtect.protect';
loopProtect.timeout = 500;
function inMultilineComment(lineNum, lines) {
if (lineNum === 0) {
return false;
}
let j = lineNum;
let closeCommentTags = 1; // let's assume we're inside a comment
let closePos = -1;
let openPos = -1;
do {
DEBUG && debug('looking backwards ' + lines[j]); // jshint ignore:line
closePos = lines[j].indexOf('*/');
openPos = lines[j].indexOf('/*');
if (closePos !== -1) {
closeCommentTags++;
}
//check for single line /* comment */ formatted comments.
if (closePos === lines[j].length - 2 && openPos !== -1) {
closeCommentTags--;
}
if (openPos !== -1) {
closeCommentTags--;
if (closeCommentTags === 0) {
DEBUG && debug('- exit: part of a multiline comment'); // jshint ignore:line
return true;
}
}
j -= 1;
} while (j >= 0);
return false;
}
function inCommentOrString(index, line) {
let character;
while (--index > -1) {
character = line.substr(index, 1);
if (character === '"' || character === '\'' || character === '.') {
// our loop keyword was actually either in a string or a property, so let's exit and ignore this line
DEBUG && debug('- exit: matched inside a string or property key'); // jshint ignore:line
return true;
}
if (character === '/' || character === '*') {
// looks like a comment, go back one to confirm or not
let prevCharacter = line.substr(index - 1, 1);
if (prevCharacter === '/') {
// we've found a comment, so let's exit and ignore this line
DEBUG && debug('- exit: part of a comment'); // jshint ignore:line
return true;
}
}
}
return false;
}
function directlyBeforeLoop(index, lineNum, lines) {
reSingle.lastIndex = 0;
labelRe.lastIndex = 0;
let beforeLoop = false;
let theRest = lines.slice(lineNum).join('\n').substr(index).replace(labelRe, '');
theRest.replace(reSingle, function commentStripper(match, capture, i) {
let target = theRest.substr(0, i).replace(comments, '').trim();
DEBUG && debug('- directlyBeforeLoop: ' + target); // jshint ignore:line
if (target.length === 0) {
beforeLoop = true;
}
// strip comments out of the target, and if there's nothing else
// it's a valid label...I hope!
});
return beforeLoop;
}
/**
* Look for for, while and do loops, and inserts *just* at the start of the
* loop, a check function.
*/
function rewriteLoops(code, offset) {
let recompiled = [];
let lines = code.split('\n');
let disableLoopProtection = false;
let method = loopProtect.alias;
let ignore = {};
let pushonly = {};
let labelPostion = null;
function insertReset(lineNum, line, matchPosition) {
// recompile the line with the reset **just** before the actual loop
// so that we insert in to the correct location (instead of possibly
// outside the logic
// wrap reset and loop in a block to avoid one line loop behind
// `if (false)`, insert the open brace in this function, and the close
// brace after loop close brace.
return line.slice(0, matchPosition) + '{;' + method + '(' + lineNum + ', true); ' + line.slice(matchPosition);
}
if (!offset) {
offset = 0;
}
lines.forEach(function eachLine(line, lineNum) {
// reset our regexp each time.
re.lastIndex = 0;
labelRe.lastIndex = 0;
if (disableLoopProtection) {
return;
}
if (line.toLowerCase().indexOf('noprotect') !== -1) {
disableLoopProtection = true;
}
let index = -1;
let matchPosition = -1;
let originalLineNum = lineNum;
// +1 since we're humans and don't read lines numbers from zero
let printLineNumber = lineNum - offset + 1;
let character = '';
// special case for `do` loops, as they're end with `while`
let dofound = false;
let findwhile = false;
let terminator = false;
let matches = line.match(re) || [];
let match = matches.length ? matches[0] : '';
let labelMatch = line.match(labelRe) || [];
let openBrackets = 0;
let openBraces = 0;
let foundLoopEnd = false;
if (labelMatch.length) {
DEBUG && debug('- label match'); // jshint ignore:line
index = line.indexOf(labelMatch[1]);
if (!inCommentOrString(index, line)) {
if (!inMultilineComment(lineNum, lines)) {
if (directlyBeforeLoop(index, lineNum, lines)) {
DEBUG && debug('- found a label: "' + labelMatch[0] + '"'); // jshint ignore:line
labelPostion = lineNum;
} else {
DEBUG && debug('- ignored "label", false positive'); // jshint ignore:line
}
} else {
DEBUG && debug('- ignored label in multline comment'); // jshint ignore:line
}
} else {
DEBUG && debug('- ignored label in string or comment'); // jshint ignore:line
}
}
if (ignore[lineNum]) {
DEBUG && debug(' -exit: ignoring line ' + lineNum + ': ' + line); // jshint ignore:line
return;
}
if (pushonly[lineNum]) {
DEBUG && debug('- exit: ignoring, but adding line ' + lineNum + ': ' + line); // jshint ignore:line
recompiled.push(line);
return;
}
// if there's more than one match, we just ignore this kind of loop
// otherwise I'm going to be writing a full JavaScript lexer...and god
// knows I've got better things to be doing.
if (match && matches.length === 1 && line.indexOf('jsbin') === -1) {
DEBUG && debug('match on ' + match + '\n'); // jshint ignore:line
// there's a special case for protecting `do` loops, we need to first
// prtect the `do`, but then ignore the closing `while` statement, so
// we reset the search state for this special case.
dofound = match === 'do';
// make sure this is an actual loop command by searching backwards
// to ensure it's not a string, comment or object property
matchPosition = index = line.indexOf(match);
// first we need to walk backwards to ensure that our match isn't part
// of a string or part of a comment
if (inCommentOrString(index, line)) {
recompiled.push(line);
return;
}
// it's quite possible we're in the middle of a multiline
// comment, so we'll cycle up looking for an opening comment,
// and if there's one (and not a closing `*/`), then we'll
// ignore this line as a comment
if (inMultilineComment(lineNum, lines)) {
recompiled.push(line);
return;
}
// now work our way forward to look for '{'
index = line.indexOf(match) + match.length;
if (index === line.length) {
if (index === line.length && lineNum < (lines.length - 1)) {
// move to the next line
DEBUG && debug('- moving to next line'); // jshint ignore:line
recompiled.push(line);
lineNum++;
line = lines[lineNum];
ignore[lineNum] = true;
index = 0;
}
}
while (index < line.length) {
character = line.substr(index, 1);
// DEBUG && debug(character, index); // jshint ignore:line
if (character === '(') {
openBrackets++;
}
if (character === ')') {
openBrackets--;
if (openBrackets === 0 && terminator === false) {
terminator = index;
}
}
if (character === '{') {
openBraces++;
}
if (character === '}') {
openBraces--;
}
if (openBrackets === 0 && (character === ';' || character === '{')) {
// if we're a non-curlies loop, then convert to curlies to get our code inserted
// add a close brace after loop to match the open brace before reset
if (character === ';') {
if (lineNum !== originalLineNum) {
DEBUG && debug('- multiline inline loop'); // jshint ignore:line
// affect the compiled line
recompiled[originalLineNum] = recompiled[originalLineNum].substring(0, terminator + 1) + '{\nif (' + method + '(' + printLineNumber + ')) break;\n' + recompiled[originalLineNum].substring(terminator + 1);
line += '\n}}\n';
} else {
// simpler
DEBUG && debug('- single line inline loop'); // jshint ignore:line
line = line.substring(0, terminator + 1) + '{\nif (' + method + '(' + printLineNumber + ')) break;\n' + line.substring(terminator + 1) + '\n}}\n';
}
foundLoopEnd = true;
} else if (character === '{') {
DEBUG && debug('- multiline with braces'); // jshint ignore:line
let insert = ';\nif (' + method + '(' + printLineNumber + ')) break;\n';
line = line.substring(0, index + 1) + insert + line.substring(index + 1);
index += insert.length;
}
// work out where to put the reset
if (lineNum === originalLineNum && labelPostion === null) {
DEBUG && debug('- simple reset insert'); // jshint ignore:line
line = insertReset(printLineNumber, line, matchPosition);
index += (';' + method + '(' + lineNum + ', true); ').length;
} else {
// insert the reset above the originalLineNum OR if this loop used
// a label, we have to insert the reset *above* the label
if (labelPostion === null) {
DEBUG && debug('- reset inserted above original line'); // jshint ignore:line
recompiled[originalLineNum] = insertReset(printLineNumber, recompiled[originalLineNum], matchPosition);
} else {
DEBUG && debug('- reset inserted above matched label on line ' + labelPostion); // jshint ignore:line
if (recompiled[labelPostion] === undefined) {
labelPostion--;
matchPosition = 0;
}
recompiled[labelPostion] = insertReset(printLineNumber, recompiled[labelPostion], matchPosition);
labelPostion = null;
}
}
if (!dofound) {
if (foundLoopEnd) {
recompiled.push(line);
return;
}
DEBUG && debug('searching for closing brace of loop for: ' + line); // jshint ignore:line
while (line !== null) {
character = line.substr(index, 1);
DEBUG && debug(index, character, openBraces); // jshint ignore:line
if (character === '{') {
openBraces++;
}
if (character === '}') {
openBraces--;
if (openBraces === 0) {
DEBUG && debug('outside of loop, add a close brace to: ' + line); // jshint ignore:line
line = line.substring(0, index + 1) + '}' + line.substring(index + 1);
recompiled.push(line);
ignore[lineNum] = true;
return;
}
}
index++;
if (index >= line.length) {
recompiled.push(line);
ignore[lineNum] = true;
lineNum++;
line = lines[lineNum];
DEBUG && debug(line); // jshint ignore:line
index = 0;
}
}
return;
} else {
DEBUG && debug('searching for closing `while` statement for: ' + line); // jshint ignore:line
// cycle forward until we find the close brace, after which should
// be our while statement to ignore
findwhile = false;
while (index < line.length) {
character = line.substr(index, 1);
if (character === '{') {
openBraces++;
}
if (character === '}') {
openBraces--;
}
if (openBraces === 0) {
findwhile = true;
} else {
findwhile = false;
}
if (openBraces === 0) {
DEBUG && debug('outside of closure, looking for `while` statement: ' + line); // jshint ignore:line
}
if (findwhile && line.indexOf('while') !== -1) {
DEBUG && debug('- exit as we found `while`: ' + line); // jshint ignore:line
// TODO: handle while statement in multiple lines
line += '}';
recompiled.push(line);
ignore[lineNum] = true;
return;
}
index++;
if (index === line.length && lineNum < (lines.length - 1)) {
recompiled.push(line);
ignore[lineNum] = true;
lineNum++;
line = lines[lineNum];
DEBUG && debug(line); // jshint ignore:line
index = 0;
}
}
return;
}
}
index++;
if (index === line.length && lineNum < (lines.length - 1)) {
// move to the next line
DEBUG && debug('- moving to next line'); // jshint ignore:line
recompiled.push(line);
lineNum++;
line = lines[lineNum];
ignore[lineNum] = true;
index = 0;
}
}
} else {
// else we're a regular line, and we shouldn't be touched
DEBUG && debug('regular line ' + line); // jshint ignore:line
recompiled.push(line);
}
});
DEBUG && debug('---- source ----'); // jshint ignore:line
DEBUG && debug(code); // jshint ignore:line
DEBUG && debug('---- rewrite ---'); // jshint ignore:line
DEBUG && debug(recompiled.join('\n')); // jshint ignore:line
DEBUG && debug(''); // jshint ignore:line
return disableLoopProtection ? code : recompiled.join('\n');
}
/**
* Injected code in to user's code to **try** to protect against infinite
* loops cropping up in the code, and killing the browser. Returns true
* when the loops has been running for more than 100ms.
*/
loopProtect.protect = function protect(lineNumber, reset) {
let line = loopProtect.counters[lineNumber];
if (!line)
line = loopProtect.counters[lineNumber] = {};
let now = Date.now();
if (reset) {
line.time = now;
line.hit = 0;
line.last = 0;
}
line.hit++;
if ((now - line.time) > loopProtect.timeout) {//} && line.hit !== line.last+1) {
// We've spent over 100ms on this loop... smells infinite.
loopProtect.hit(lineNumber);
// Returning true prevents the loop running again
return true;
}
line.last++;
return false;
};
loopProtect.hit = function hit(line) {
let msg = 'Exiting potential infinite loop at line ' + line + '. To disable loop protection: add "// noprotect" to your code';
if (root.proxyConsole) {
root.proxyConsole.error(msg);
} else {
console.error(msg);
}
};
loopProtect.reset = function reset() {
// reset the counters
loopProtect.counters = {};
};
return loopProtect;
}); |
import log from "../log"
import execute from "./execute"
// End the current session and release the acquired connection
// to the connection pool.
export default function end() {
let d = process.domain
if (d == null ||
d.context == null ||
d.context.bardo == null) {
// TODO: Report an error that we weren't in an active session
return null
}
return new Promise(function(resolve, reject) {
function next() {
// Release our client from the pool
d.context.bardo.done()
// DEBUG: Report total SQL execution time for this session
let {id, elapsed, count} = d.context.bardo
elapsed = elapsed.toFixed(2)
if (count > 0) {
log.debug({id, elapsed: `${elapsed}ms`, count},
`${count} statement${count > 1 ? "s" : ""} executed in ${elapsed}ms`)
}
// Remove us from the domain
delete d.context.bardo
// Leave our domain
d.exit()
resolve()
}
// If we are currently in a transaction; rollback the transaction
if (d.context.bardo.inTransaction) {
execute("ROLLBACK").then(next).catch(reject)
} else {
next()
}
})
}
|
/*global define*/
define([
'../Core/Cartesian2',
'../Core/Cartesian3',
'../Core/Cartographic',
'../Core/defaultValue',
'../Core/defined',
'../Core/DeveloperError',
'../Core/EasingFunction',
'../Core/Math',
'./PerspectiveFrustum',
'./PerspectiveOffCenterFrustum',
'./SceneMode'
], function(
Cartesian2,
Cartesian3,
Cartographic,
defaultValue,
defined,
DeveloperError,
EasingFunction,
CesiumMath,
PerspectiveFrustum,
PerspectiveOffCenterFrustum,
SceneMode) {
'use strict';
/**
* Creates tweens for camera flights.
* <br /><br />
* Mouse interaction is disabled during flights.
*
* @private
*/
var CameraFlightPath = {
};
function getAltitude(frustum, dx, dy) {
var near;
var top;
var right;
if (frustum instanceof PerspectiveFrustum) {
var tanTheta = Math.tan(0.5 * frustum.fovy);
near = frustum.near;
top = frustum.near * tanTheta;
right = frustum.aspectRatio * top;
return Math.max(dx * near / right, dy * near / top);
} else if (frustum instanceof PerspectiveOffCenterFrustum) {
near = frustum.near;
top = frustum.top;
right = frustum.right;
return Math.max(dx * near / right, dy * near / top);
}
return Math.max(dx, dy);
}
var scratchCart = new Cartesian3();
var scratchCart2 = new Cartesian3();
function createHeightFunction(camera, destination, startHeight, endHeight, optionAltitude) {
var altitude = optionAltitude;
var maxHeight;
if (!defined(optionAltitude)) {
var start = camera.position;
var end = destination;
var up = camera.up;
var right = camera.right;
var frustum = camera.frustum;
var diff = Cartesian3.subtract(start, end, scratchCart);
var verticalDistance = Cartesian3.magnitude(Cartesian3.multiplyByScalar(up, Cartesian3.dot(diff, up), scratchCart2));
var horizontalDistance = Cartesian3.magnitude(Cartesian3.multiplyByScalar(right, Cartesian3.dot(diff, right), scratchCart2));
maxHeight = Math.max(startHeight, endHeight);
altitude = Math.min(getAltitude(frustum, verticalDistance, horizontalDistance) * 0.20, 1000000000.0);
}
if ((defined(optionAltitude) && optionAltitude < altitude) || maxHeight < altitude) {
var power = 8.0;
var factor = 1000000.0;
var s = -Math.pow((altitude - startHeight) * factor, 1.0 / power);
var e = Math.pow((altitude - endHeight) * factor, 1.0 / power);
return function(t) {
var x = t * (e - s) + s;
return -Math.pow(x, power) / factor + altitude;
};
}
return function(t) {
return CesiumMath.lerp(startHeight, endHeight, t);
};
}
function adjustAngleForLERP(startAngle, endAngle) {
if (CesiumMath.equalsEpsilon(startAngle, CesiumMath.TWO_PI, CesiumMath.EPSILON11)) {
startAngle = 0.0;
}
if (endAngle > startAngle + Math.PI) {
startAngle += CesiumMath.TWO_PI;
} else if (endAngle < startAngle - Math.PI) {
startAngle -= CesiumMath.TWO_PI;
}
return startAngle;
}
var scratchStart = new Cartesian3();
function createUpdateCV(scene, duration, destination, heading, pitch, roll, optionAltitude) {
var camera = scene.camera;
var start = Cartesian3.clone(camera.position, scratchStart);
var startPitch = camera.pitch;
var startHeading = adjustAngleForLERP(camera.heading, heading);
var startRoll = adjustAngleForLERP(camera.roll, roll);
var heightFunction = createHeightFunction(camera, destination, start.z, destination.z, optionAltitude);
function update(value) {
var time = value.time / duration;
camera.setView({
orientation: {
heading : CesiumMath.lerp(startHeading, heading, time),
pitch : CesiumMath.lerp(startPitch, pitch, time),
roll : CesiumMath.lerp(startRoll, roll, time)
}
});
Cartesian2.lerp(start, destination, time, camera.position);
camera.position.z = heightFunction(time);
}
return update;
}
var scratchStartCart = new Cartographic();
var scratchEndCart = new Cartographic();
function createUpdate3D(scene, duration, destination, heading, pitch, roll, optionAltitude) {
var camera = scene.camera;
var projection = scene.mapProjection;
var ellipsoid = projection.ellipsoid;
var startCart = Cartographic.clone(camera.positionCartographic, scratchStartCart);
var startPitch = camera.pitch;
var startHeading = adjustAngleForLERP(camera.heading, heading);
var startRoll = adjustAngleForLERP(camera.roll, roll);
var destCart = ellipsoid.cartesianToCartographic(destination, scratchEndCart);
if (destCart.height <= 0.0) {
destCart.height = startCart.height;
}
startCart.longitude = CesiumMath.zeroToTwoPi(startCart.longitude);
destCart.longitude = CesiumMath.zeroToTwoPi(destCart.longitude);
var diff = startCart.longitude - destCart.longitude;
if (diff < -CesiumMath.PI) {
startCart.longitude += CesiumMath.TWO_PI;
} else if (diff > CesiumMath.PI) {
destCart.longitude += CesiumMath.TWO_PI;
}
var heightFunction = createHeightFunction(camera, destination, startCart.height, destCart.height, optionAltitude);
function update(value) {
var time = value.time / duration;
var position = Cartesian3.fromRadians(
CesiumMath.lerp(startCart.longitude, destCart.longitude, time),
CesiumMath.lerp(startCart.latitude, destCart.latitude, time),
heightFunction(time)
);
camera.setView({
destination : position,
orientation: {
heading : CesiumMath.lerp(startHeading, heading, time),
pitch : CesiumMath.lerp(startPitch, pitch, time),
roll : CesiumMath.lerp(startRoll, roll, time)
}
});
}
return update;
}
function createUpdate2D(scene, duration, destination, heading, pitch, roll, optionAltitude) {
var camera = scene.camera;
var start = Cartesian3.clone(camera.position, scratchStart);
var startHeading = adjustAngleForLERP(camera.heading, heading);
var startHeight = camera.frustum.right - camera.frustum.left;
var heightFunction = createHeightFunction(camera, destination, startHeight, destination.z, optionAltitude);
function update(value) {
var time = value.time / duration;
camera.setView({
orientation: {
heading : CesiumMath.lerp(startHeading, heading, time)
}
});
Cartesian2.lerp(start, destination, time, camera.position);
var zoom = heightFunction(time);
var frustum = camera.frustum;
var ratio = frustum.top / frustum.right;
var incrementAmount = (zoom - (frustum.right - frustum.left)) * 0.5;
frustum.right += incrementAmount;
frustum.left -= incrementAmount;
frustum.top = ratio * frustum.right;
frustum.bottom = -frustum.top;
}
return update;
}
var scratchCartographic = new Cartographic();
var scratchDestination = new Cartesian3();
function emptyFlight(complete, cancel) {
return {
startObject : {},
stopObject : {},
duration : 0.0,
complete : complete,
cancel : cancel
};
}
function wrapCallback(controller, cb) {
function wrapped() {
if (typeof cb === 'function') {
cb();
}
controller.enableInputs = true;
}
return wrapped;
}
CameraFlightPath.createTween = function(scene, options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
var destination = options.destination;
//>>includeStart('debug', pragmas.debug);
if (!defined(scene)) {
throw new DeveloperError('scene is required.');
}
if (!defined(destination)) {
throw new DeveloperError('destination is required.');
}
//>>includeEnd('debug');
var mode = scene.mode;
if (mode === SceneMode.MORPHING) {
return emptyFlight();
}
var convert = defaultValue(options.convert, true);
var projection = scene.mapProjection;
var ellipsoid = projection.ellipsoid;
var maximumHeight = options.maximumHeight;
var easingFunction = options.easingFunction;
if (convert && mode !== SceneMode.SCENE3D) {
ellipsoid.cartesianToCartographic(destination, scratchCartographic);
destination = projection.project(scratchCartographic, scratchDestination);
}
var camera = scene.camera;
var transform = options.endTransform;
if (defined(transform)) {
camera._setTransform(transform);
}
var duration = options.duration;
if (!defined(duration)) {
duration = Math.ceil(Cartesian3.distance(camera.position, destination) / 1000000.0) + 2.0;
duration = Math.min(duration, 3.0);
}
var heading = defaultValue(options.heading, 0.0);
var pitch = defaultValue(options.pitch, -CesiumMath.PI_OVER_TWO);
var roll = defaultValue(options.roll, 0.0);
var controller = scene.screenSpaceCameraController;
controller.enableInputs = false;
var complete = wrapCallback(controller, options.complete);
var cancel = wrapCallback(controller, options.cancel);
var frustum = camera.frustum;
var empty = scene.mode === SceneMode.SCENE2D;
empty = empty && Cartesian2.equalsEpsilon(camera.position, destination, CesiumMath.EPSILON6);
empty = empty && CesiumMath.equalsEpsilon(Math.max(frustum.right - frustum.left, frustum.top - frustum.bottom), destination.z, CesiumMath.EPSILON6);
empty = empty || (scene.mode !== SceneMode.SCENE2D &&
Cartesian3.equalsEpsilon(destination, camera.position, CesiumMath.EPSILON10));
empty = empty &&
CesiumMath.equalsEpsilon(CesiumMath.negativePiToPi(heading), CesiumMath.negativePiToPi(camera.heading), CesiumMath.EPSILON10) &&
CesiumMath.equalsEpsilon(CesiumMath.negativePiToPi(pitch), CesiumMath.negativePiToPi(camera.pitch), CesiumMath.EPSILON10) &&
CesiumMath.equalsEpsilon(CesiumMath.negativePiToPi(roll), CesiumMath.negativePiToPi(camera.roll), CesiumMath.EPSILON10);
if (empty) {
return emptyFlight(complete, cancel);
}
var updateFunctions = new Array(4);
updateFunctions[SceneMode.SCENE2D] = createUpdate2D;
updateFunctions[SceneMode.SCENE3D] = createUpdate3D;
updateFunctions[SceneMode.COLUMBUS_VIEW] = createUpdateCV;
if (duration <= 0.0) {
var newOnComplete = function() {
var update = updateFunctions[mode](scene, 1.0, destination, heading, pitch, roll, maximumHeight);
update({ time: 1.0 });
if (typeof complete === 'function') {
complete();
}
};
return emptyFlight(newOnComplete, cancel);
}
var update = updateFunctions[mode](scene, duration, destination, heading, pitch, roll, maximumHeight);
if (!defined(easingFunction)) {
var startHeight = camera.positionCartographic.height;
var endHeight = mode === SceneMode.SCENE3D ? ellipsoid.cartesianToCartographic(destination).height : destination.z;
if (startHeight > endHeight && startHeight > 11500.0) {
easingFunction = EasingFunction.CUBIC_OUT;
} else {
easingFunction = EasingFunction.QUINTIC_IN_OUT;
}
}
return {
duration : duration,
easingFunction : easingFunction,
startObject : {
time : 0.0
},
stopObject : {
time : duration
},
update : update,
complete : complete,
cancel: cancel
};
};
return CameraFlightPath;
});
|
import React from 'react'
import PropTypes from 'prop-types'
import {Platform} from 'react-native'
import Svg, {LinearGradient, Text, Defs, Stop} from 'react-native-svg'
Slogan.propTypes = {
fontSize: PropTypes.number,
width: PropTypes.number,
height: PropTypes.number
}
export default function Slogan ({fontSize = 32, width = 250, height = 50}) {
return (
<Svg width={width} height={height} viewbox='0 0 250 50'>
<Defs>
<LinearGradient id='grad' x1='0' y1='0' x2='250' y2='0'>
<Stop offset='0' stopColor='#fbda61' stopOpacity='1' />
<Stop offset='1' stopColor='#f75b1c' stopOpacity='1' />
</LinearGradient>
</Defs>
<Text
fontSize={fontSize}
fontWeight='bold'
fontFamily={Platform.OS === 'ios' ? 'Courier New' : 'monospace'}
fontStyle='italic'
fill='url(#grad)'
>
def future():
</Text>
</Svg>
)
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.