-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
126 lines (103 loc) · 3.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var _ = require('lodash');
var insertCSS = require('insert-css');
var inherits = require('inherits');
var qwery = require('qwery');
var isarray = require('isarray');
var bonzo = require('bonzo');
function $(selector) {
return bonzo(qwery(selector))
}
var LightningVisualization = function(selector, data, images, options) {
if (!options && !isarray(images) && typeof images === 'object') {
options = images
images = null
}
this.options = _.defaults(options || {}, this.getDefaultOptions());
this.styles = this.getDefaultStyles();
this.qwery = qwery;
this.$el = $(selector)
this.el = this.$el[0];
this.width = (this.options.width || this.el.offsetWidth);
this.height = (this.options.height || (this.getHeight ? this.getHeight() : this.width * 0.6));
this.data = this.formatData(data || {});
this.images = images || [];
this.selector = selector;
this.init();
};
inherits(LightningVisualization, require('events').EventEmitter);
LightningVisualization.prototype.getDefaultOptions = function() {
return {};
}
LightningVisualization.prototype.getDefaultStyles = function() {
return {};
}
LightningVisualization.prototype.init = function() {
console.warn('init not implemented');
}
/*
* Take the provided data and return it in whatever data format is needed
*/
LightningVisualization.prototype.formatData = function(data) {
console.warn('formatData not implemented');
return data;
}
/*
* Optional function, use this if you want to users to send updated data to this plot
*/
LightningVisualization.prototype.updateData = function(data) {
console.warn('updateData not implemented');
}
/*
* Optional function, use this if you want to enable streaming updates to this plot
*/
LightningVisualization.prototype.appendData = function(data) {
console.warn('appendData not implemented');
}
// Modified from backbone.js
LightningVisualization.extend = function(protoProps, staticProps) {
var parent = this;
var child;
// Wrap these functions so that the user can assume
// the data has already been formatted by the time
// it gets here.
var wrapFuncs = ['appendData', 'updateData'];
_.each(wrapFuncs, function(d) {
if(protoProps[d]) {
var fn = protoProps[d];
protoProps[d] = function(data) {
var d = this.formatData(data);
return fn.call(this, d);
};
}
});
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent constructor.
if (protoProps && _.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = function(){
if(this.css && !child._stylesInitialized) {
insertCSS(this.css);
child._stylesInitialized = true;
}
return parent.apply(this, arguments);
};
child._initializedStyles = false;
}
// Add static properties to the constructor function, if supplied.
_.extend(child, parent, staticProps);
// Set the prototype chain to inherit from `parent`, without calling
// `parent` constructor function.
var Surrogate = function(){ this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) _.extend(child.prototype, protoProps);
// Set a convenience property in case the parent's prototype is needed
// later.
child.__super__ = parent.prototype;
return child;
};
module.exports = LightningVisualization;