Initial commit
This commit is contained in:
+17
@@ -0,0 +1,17 @@
|
||||
Utilizes https://github.com/substack/browserify-handbook#how-node_modules-works,
|
||||
to avoid annoying `require('../../../../../')` for both source and test environment
|
||||
|
||||
## Relevant changes
|
||||
|
||||
### 2018-02-01
|
||||
|
||||
- We don't need this under a node_modules anymore, using webpack alias
|
||||
|
||||
### 2016-06-09 - Installing jquery-ui (v0.11.4)
|
||||
- npm package is unofficial and outdated (v0.10.5)
|
||||
- jquery-ui team won't update it until releasing v0.12 (see https://forum.jquery.com/topic/publishing-jquery-ui-version-1-11-2-to-npm)
|
||||
- until available, installing it the recommended/official way
|
||||
|
||||
### 2016-06-03 - Porting cartodb.js/core/view
|
||||
- Removed unused parts (e.g. Profiler)
|
||||
- compare `./backbone/core-view` vs. https://github.com/CartoDB/cartodb.js/blob/470399abb12b40d5476ab6bbfd792d95aa819f50/src/core/
|
||||
@@ -0,0 +1,81 @@
|
||||
var $ = require('jquery');
|
||||
var _ = require('underscore');
|
||||
var Backbone = require('backbone');
|
||||
|
||||
/**
|
||||
* Base Model for all CartoDB model.
|
||||
* DO NOT USE Backbone.Model directly
|
||||
*/
|
||||
var Model = Backbone.Model.extend({
|
||||
|
||||
initialize: function (options) {
|
||||
_.bindAll(this, 'fetch', 'save', 'retrigger');
|
||||
return Backbone.Model.prototype.initialize.call(this, options);
|
||||
},
|
||||
/**
|
||||
* We are redefining fetch to be able to trigger an event when the ajax call ends, no matter if there's
|
||||
* a change in the data or not. Why don't backbone does this by default? ahh, my friend, who knows.
|
||||
* @method fetch
|
||||
* @param args {Object}
|
||||
*/
|
||||
fetch: function (args) {
|
||||
var self = this;
|
||||
// var date = new Date();
|
||||
this.trigger('loadModelStarted');
|
||||
$.when(Backbone.Model.prototype.fetch.call(this, args)).done(function (ev) {
|
||||
self.trigger('loadModelCompleted', ev, self);
|
||||
// var dateComplete = new Date()
|
||||
// console.log('completed in '+(dateComplete - date));
|
||||
}).fail(function (ev) {
|
||||
self.trigger('loadModelFailed', ev, self);
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Changes the attribute used as Id
|
||||
* @method setIdAttribute
|
||||
* @param attr {String}
|
||||
*/
|
||||
setIdAttribute: function (attr) {
|
||||
this.idAttribute = attr;
|
||||
},
|
||||
/**
|
||||
* Listen for an event on another object and triggers on itself, with the same name or a new one
|
||||
* @method retrigger
|
||||
* @param ev {String} event who triggers the action
|
||||
* @param obj {Object} object where the event happens
|
||||
* @param obj {Object} [optional] name of the retriggered event
|
||||
* @todo [xabel]: This method is repeated here and in the base view definition. There's should be a way to make it unique
|
||||
*/
|
||||
retrigger: function (ev, obj, retrigEvent) {
|
||||
if (!retrigEvent) {
|
||||
retrigEvent = ev;
|
||||
}
|
||||
var self = this;
|
||||
obj.bind && obj.bind(ev, function () {
|
||||
self.trigger(retrigEvent);
|
||||
}, self);
|
||||
},
|
||||
|
||||
/**
|
||||
* We need to override backbone save method to be able to introduce new kind of triggers that
|
||||
* for some reason are not present in the original library. Because you know, it would be nice
|
||||
* to be able to differenciate "a model has been updated" of "a model is being saved".
|
||||
* TODO: remove jquery from here
|
||||
* @param {object} opt1
|
||||
* @param {object} opt2
|
||||
* @return {$.Deferred}
|
||||
*/
|
||||
save: function (opt1, opt2) {
|
||||
var self = this;
|
||||
if (!opt2 || !opt2.silent) this.trigger('saving');
|
||||
var promise = Backbone.Model.prototype.save.apply(this, arguments);
|
||||
$.when(promise).done(function () {
|
||||
if (!opt2 || !opt2.silent) self.trigger('saved');
|
||||
}).fail(function () {
|
||||
if (!opt2 || !opt2.silent) self.trigger('errorSaving');
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = Model;
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
var _ = require('underscore');
|
||||
var Backbone = require('backbone');
|
||||
|
||||
/**
|
||||
* NOTE! Migrated as-is from https://github.com/CartoDB/cartodb.js/blob/470399abb12b40d5476ab6bbfd792d95aa819f50/src/core/view.js 2016-06-03
|
||||
* Base View for all CartoDB views.
|
||||
* DO NOT USE Backbone.View directly
|
||||
*/
|
||||
var View = Backbone.View.extend({
|
||||
classLabel: 'cdb.core.View',
|
||||
|
||||
constructor: function (options) {
|
||||
this.options = _.defaults(options, this.options);
|
||||
this._models = [];
|
||||
this._subviews = {};
|
||||
Backbone.View.call(this, options);
|
||||
View.viewCount++;
|
||||
View.views[this.cid] = this;
|
||||
this._created_at = new Date();
|
||||
if (typeof __IN_DEV__ !== 'undefined' && __IN_DEV__ && this.module) { // eslint-disable-line
|
||||
this.$el.attr('data-module', this.module);
|
||||
}
|
||||
},
|
||||
|
||||
add_related_model: function (m) {
|
||||
if (!m) throw new Error('added non valid model');
|
||||
this._models.push(m);
|
||||
},
|
||||
|
||||
addView: function (v) {
|
||||
this._subviews[v.cid] = v;
|
||||
v._parent = this;
|
||||
},
|
||||
|
||||
removeView: function (v) {
|
||||
delete this._subviews[v.cid];
|
||||
},
|
||||
|
||||
clearSubViews: function () {
|
||||
_(this._subviews).each(function (v) {
|
||||
v.clean();
|
||||
});
|
||||
this._subviews = {};
|
||||
},
|
||||
|
||||
/**
|
||||
* this methid clean removes the view
|
||||
* and clean and events associated. call it when
|
||||
* the view is not going to be used anymore
|
||||
*/
|
||||
clean: function () {
|
||||
this.trigger('clean');
|
||||
this.clearSubViews();
|
||||
// remove from parent
|
||||
if (this._parent) {
|
||||
this._parent.removeView(this);
|
||||
this._parent = null;
|
||||
}
|
||||
this.remove();
|
||||
this.allOff();
|
||||
View.viewCount--;
|
||||
delete View.views[this.cid];
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove all event listeners on related models
|
||||
*/
|
||||
allOff: function () {
|
||||
this.off();
|
||||
|
||||
if (this.model && this.model.off) this.model.off(null, null, this);
|
||||
|
||||
var self = this;
|
||||
_(this._models).each(function (m) {
|
||||
m.off(null, null, self);
|
||||
});
|
||||
this._models = [];
|
||||
},
|
||||
|
||||
show: function () {
|
||||
this.$el.show();
|
||||
},
|
||||
|
||||
hide: function () {
|
||||
this.$el.hide();
|
||||
},
|
||||
|
||||
/**
|
||||
* Listen for an event on another object and triggers on itself, with the same name or a new one
|
||||
* @method retrigger
|
||||
* @param ev {String} event who triggers the action
|
||||
* @param obj {Object} object where the event happens
|
||||
* @param obj {Object} [optional] name of the retriggered event
|
||||
*/
|
||||
retrigger: function (ev, obj, retrigEvent) {
|
||||
if (!retrigEvent) {
|
||||
retrigEvent = ev;
|
||||
}
|
||||
var self = this;
|
||||
obj.bind && obj.bind(ev, function () {
|
||||
self.trigger(retrigEvent);
|
||||
}, self);
|
||||
// add it as related model//object
|
||||
this.add_related_model(obj);
|
||||
},
|
||||
/**
|
||||
* Captures an event and prevents the default behaviour and stops it from bubbling
|
||||
* @method killEvent
|
||||
* @param event {Event}
|
||||
*/
|
||||
killEvent: function (ev) {
|
||||
if (ev && ev.preventDefault) {
|
||||
ev.preventDefault();
|
||||
}
|
||||
if (ev && ev.stopPropagation) {
|
||||
ev.stopPropagation();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove all the tipsy tooltips from the document
|
||||
* @method cleanTooltips
|
||||
*/
|
||||
cleanTooltips: function () {
|
||||
this.$('.tipsy').remove();
|
||||
}
|
||||
|
||||
}, {
|
||||
viewCount: 0,
|
||||
views: {},
|
||||
|
||||
/**
|
||||
* when a view with events is inherit and you want to add more events
|
||||
* this helper can be used:
|
||||
* var MyView = new core.View({
|
||||
* events: View.extendEvents({
|
||||
* 'click': 'fn'
|
||||
* })
|
||||
* })
|
||||
*/
|
||||
extendEvents: function (newEvents) {
|
||||
return function () {
|
||||
return _.extend(newEvents, this.constructor.__super__.events);
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* search for views in a view and check if they are added as subviews
|
||||
*/
|
||||
runChecker: function () {
|
||||
_.each(View.views, function (view) {
|
||||
_.each(view, function (prop, k) {
|
||||
if (k !== '_parent' &&
|
||||
view.hasOwnProperty(k) &&
|
||||
prop instanceof View &&
|
||||
view._subviews[prop.cid] === undefined) {
|
||||
console.log('=========');
|
||||
console.log('untracked view: ');
|
||||
console.log(prop.el);
|
||||
console.log('parent');
|
||||
console.log(view.el);
|
||||
console.log(' ');
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = View;
|
||||
+16617
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user