2014-07-30 03:18:43 +00:00
|
|
|
|
Metamaps.Backbone = {};
|
|
|
|
|
Metamaps.Backbone.Map = Backbone.Model.extend({
|
|
|
|
|
urlRoot: '/maps',
|
2015-01-31 17:39:48 +00:00
|
|
|
|
blacklist: ['created_at', 'updated_at', 'created_at_clean', 'updated_at_clean', 'user_name', 'contributor_count', 'topic_count', 'synapse_count', 'topics', 'synapses', 'mappings', 'mappers'],
|
2014-07-30 03:18:43 +00:00
|
|
|
|
toJSON: function (options) {
|
|
|
|
|
return _.omit(this.attributes, this.blacklist);
|
|
|
|
|
},
|
2014-11-04 17:37:17 +00:00
|
|
|
|
save: function (key, val, options) {
|
|
|
|
|
|
|
|
|
|
var attrs;
|
|
|
|
|
|
|
|
|
|
// Handle both `"key", value` and `{key: value}` -style arguments.
|
|
|
|
|
if (key == null || typeof key === 'object') {
|
|
|
|
|
attrs = key;
|
|
|
|
|
options = val;
|
|
|
|
|
} else {
|
|
|
|
|
(attrs = {})[key] = val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newOptions = options || {};
|
|
|
|
|
var s = newOptions.success;
|
|
|
|
|
|
|
|
|
|
newOptions.success = function (model, response, opt) {
|
|
|
|
|
if (s) s(model, response, opt);
|
|
|
|
|
model.trigger('saved');
|
|
|
|
|
};
|
|
|
|
|
return Backbone.Model.prototype.save.call(this, attrs, newOptions);
|
|
|
|
|
},
|
|
|
|
|
initialize: function () {
|
|
|
|
|
this.on('changeByOther', this.updateView);
|
|
|
|
|
this.on('saved', this.savedEvent);
|
|
|
|
|
},
|
|
|
|
|
savedEvent: function() {
|
|
|
|
|
Metamaps.Realtime.sendMapChange(this);
|
|
|
|
|
},
|
2014-07-30 03:18:43 +00:00
|
|
|
|
authorizeToEdit: function (mapper) {
|
|
|
|
|
if (mapper && (this.get('permission') === "commons" || this.get('user_id') === mapper.get('id'))) return true;
|
|
|
|
|
else return false;
|
2014-08-10 17:06:58 +00:00
|
|
|
|
},
|
2014-09-03 23:05:25 +00:00
|
|
|
|
authorizePermissionChange: function (mapper) {
|
|
|
|
|
if (mapper && this.get('user_id') === mapper.get('id')) return true;
|
|
|
|
|
else return false;
|
|
|
|
|
},
|
2014-08-10 17:06:58 +00:00
|
|
|
|
getUser: function () {
|
|
|
|
|
return Metamaps.Mapper.get(this.get('user_id'));
|
|
|
|
|
},
|
2014-08-11 22:57:34 +00:00
|
|
|
|
fetchContained: function () {
|
|
|
|
|
var bb = Metamaps.Backbone;
|
2014-08-12 02:31:28 +00:00
|
|
|
|
var that = this;
|
2014-08-11 22:57:34 +00:00
|
|
|
|
var start = function (data) {
|
2014-08-12 02:31:28 +00:00
|
|
|
|
that.set('mappers', new bb.MapperCollection(data.mappers));
|
|
|
|
|
that.set('topics', new bb.TopicCollection(data.topics));
|
|
|
|
|
that.set('synapses', new bb.SynapseCollection(data.synapses));
|
|
|
|
|
that.set('mappings', new bb.MappingCollection(data.mappings));
|
2014-11-04 17:37:17 +00:00
|
|
|
|
};
|
2014-08-11 22:57:34 +00:00
|
|
|
|
|
2014-11-04 17:37:17 +00:00
|
|
|
|
var e = $.ajax({
|
2014-08-12 02:31:28 +00:00
|
|
|
|
url: "/maps/" + this.id + "/contains.json",
|
2014-08-11 22:57:34 +00:00
|
|
|
|
success: start,
|
2014-11-04 17:37:17 +00:00
|
|
|
|
error: errorFunc,
|
2014-08-11 22:57:34 +00:00
|
|
|
|
async: false
|
|
|
|
|
});
|
|
|
|
|
},
|
2014-08-10 17:06:58 +00:00
|
|
|
|
getTopics: function () {
|
|
|
|
|
if (!this.get('topics')) {
|
2014-08-11 22:57:34 +00:00
|
|
|
|
this.fetchContained();
|
2014-08-10 17:06:58 +00:00
|
|
|
|
}
|
|
|
|
|
return this.get('topics');
|
|
|
|
|
},
|
|
|
|
|
getSynapses: function () {
|
|
|
|
|
if (!this.get('synapses')) {
|
2014-08-11 22:57:34 +00:00
|
|
|
|
this.fetchContained();
|
2014-08-10 17:06:58 +00:00
|
|
|
|
}
|
|
|
|
|
return this.get('synapses');
|
|
|
|
|
},
|
2014-08-11 22:57:34 +00:00
|
|
|
|
getMappings: function () {
|
|
|
|
|
if (!this.get('mappings')) {
|
|
|
|
|
this.fetchContained();
|
|
|
|
|
}
|
|
|
|
|
return this.get('mappings');
|
|
|
|
|
},
|
|
|
|
|
getMappers: function () {
|
|
|
|
|
if (!this.get('mappers')) {
|
|
|
|
|
this.fetchContained();
|
|
|
|
|
}
|
|
|
|
|
return this.get('mappers');
|
|
|
|
|
},
|
2014-08-10 17:06:58 +00:00
|
|
|
|
attrForCards: function () {
|
2014-08-15 22:04:22 +00:00
|
|
|
|
function capitalize(string) {
|
|
|
|
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
|
|
|
}
|
2014-11-26 19:49:01 +00:00
|
|
|
|
|
|
|
|
|
var n = this.get('name');
|
|
|
|
|
var d = this.get('desc');
|
|
|
|
|
|
|
|
|
|
var maxNameLength = 32;
|
|
|
|
|
var maxDescLength = 118;
|
|
|
|
|
var truncatedName = n ? (n.length > maxNameLength ? n.substring(0, maxNameLength) + "..." : n) : "";
|
|
|
|
|
var truncatedDesc = d ? (d.length > maxDescLength ? d.substring(0, maxDescLength) + "..." : d) : "";
|
|
|
|
|
|
2014-08-10 17:06:58 +00:00
|
|
|
|
var obj = {
|
|
|
|
|
id: this.id,
|
2014-11-26 19:49:01 +00:00
|
|
|
|
name: truncatedName,
|
|
|
|
|
fullName: n,
|
|
|
|
|
desc: truncatedDesc,
|
2014-08-15 22:04:22 +00:00
|
|
|
|
permission: this.get("permission") ? capitalize(this.get("permission")) : "Commons",
|
2014-08-10 17:06:58 +00:00
|
|
|
|
editPermission: this.authorizeToEdit(Metamaps.Active.Mapper) ? 'canEdit' : 'cannotEdit',
|
2014-08-15 22:04:22 +00:00
|
|
|
|
contributor_count_number: '<span class="cCountColor">' + this.get('contributor_count') + '</span>',
|
|
|
|
|
contributor_count_string: this.get('contributor_count') == 1 ? ' contributor' : ' contributors',
|
|
|
|
|
topic_count_number: '<span class="tCountColor">' + this.get('topic_count') + '</span>',
|
|
|
|
|
topic_count_string: this.get('topic_count') == 1 ? ' topic' : ' topics',
|
|
|
|
|
synapse_count_number: '<span class="sCountColor">' + this.get('synapse_count') + '</span>',
|
|
|
|
|
synapse_count_string: this.get('synapse_count') == 1 ? ' synapse' : ' synapses',
|
2014-10-08 04:07:54 +00:00
|
|
|
|
screenshot: '<img src="' + this.get('screenshot_url') + '" />'
|
2014-08-10 17:06:58 +00:00
|
|
|
|
};
|
|
|
|
|
return obj;
|
2014-11-04 17:37:17 +00:00
|
|
|
|
},
|
|
|
|
|
updateView: function() {
|
|
|
|
|
var map = Metamaps.Active.Map;
|
|
|
|
|
var isActiveMap = this.id === map.id;
|
|
|
|
|
var authorized = map && map.authorizeToEdit(Metamaps.Active.Mapper) ? 'canEditMap' : '';
|
|
|
|
|
var commonsMap = map && map.get('permission') === 'commons' ? 'commonsMap' : '';
|
|
|
|
|
if (isActiveMap) {
|
|
|
|
|
Metamaps.Map.InfoBox.updateNameDescPerm(this.get('name'), this.get('desc'), this.get('permission'));
|
|
|
|
|
this.updateMapWrapper();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
updateMapWrapper: function() {
|
|
|
|
|
var map = Metamaps.Active.Map;
|
|
|
|
|
var isActiveMap = this.id === map.id;
|
|
|
|
|
var authorized = map && map.authorizeToEdit(Metamaps.Active.Mapper) ? 'canEditMap' : '';
|
|
|
|
|
var commonsMap = map && map.get('permission') === 'commons' ? 'commonsMap' : '';
|
|
|
|
|
if (isActiveMap) {
|
|
|
|
|
$('.wrapper').removeClass('canEditMap commonsMap').addClass(authorized + ' ' + commonsMap);
|
|
|
|
|
}
|
2014-07-30 03:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Metamaps.Backbone.MapsCollection = Backbone.Collection.extend({
|
|
|
|
|
model: Metamaps.Backbone.Map,
|
2014-08-10 17:06:58 +00:00
|
|
|
|
initialize: function(models, options) {
|
|
|
|
|
this.id = options.id;
|
|
|
|
|
this.sortBy = options.sortBy;
|
2014-08-20 19:37:18 +00:00
|
|
|
|
|
2014-11-25 20:06:30 +00:00
|
|
|
|
if (options.mapperId) {
|
|
|
|
|
this.mapperId = options.mapperId;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-20 19:37:18 +00:00
|
|
|
|
// this.page represents the NEXT page to fetch
|
|
|
|
|
this.page = models.length > 0 ? (models.length < 20 ? "loadedAll" : 2) : 1;
|
2014-08-10 17:06:58 +00:00
|
|
|
|
},
|
|
|
|
|
url: function() {
|
2014-11-25 20:06:30 +00:00
|
|
|
|
if (!this.mapperId) {
|
|
|
|
|
return '/explore/' + this.id + '.json';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return '/explore/mapper/' + this.mapperId + '.json';
|
|
|
|
|
}
|
2014-08-10 17:06:58 +00:00
|
|
|
|
},
|
|
|
|
|
comparator: function (a, b) {
|
|
|
|
|
a = a.get(this.sortBy);
|
|
|
|
|
b = b.get(this.sortBy);
|
2014-08-12 02:31:28 +00:00
|
|
|
|
var temp;
|
2014-08-10 17:06:58 +00:00
|
|
|
|
if (this.sortBy === 'name') {
|
2014-08-10 20:20:21 +00:00
|
|
|
|
a = a ? a.toLowerCase() : "";
|
|
|
|
|
b = b ? b.toLowerCase() : "";
|
2014-08-10 17:06:58 +00:00
|
|
|
|
}
|
2014-08-12 02:31:28 +00:00
|
|
|
|
else {
|
|
|
|
|
// this is for updated_at and created_at
|
|
|
|
|
temp = a;
|
|
|
|
|
a = b;
|
|
|
|
|
b = temp;
|
2015-01-31 17:39:48 +00:00
|
|
|
|
a = (new Date(a)).getTime();
|
|
|
|
|
b = (new Date(b)).getTime();
|
2014-08-12 02:31:28 +00:00
|
|
|
|
}
|
2014-08-10 17:06:58 +00:00
|
|
|
|
return a > b ? 1 : a < b ? -1 : 0;
|
|
|
|
|
},
|
2015-01-31 17:39:48 +00:00
|
|
|
|
getMaps: function (cb) {
|
2014-08-10 17:06:58 +00:00
|
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
2015-01-31 01:49:30 +00:00
|
|
|
|
Metamaps.Loading.show();
|
|
|
|
|
|
2014-08-20 19:37:18 +00:00
|
|
|
|
if (this.page != "loadedAll") {
|
|
|
|
|
var numBefore = this.length;
|
|
|
|
|
this.fetch({
|
|
|
|
|
remove: false,
|
2015-01-31 01:49:30 +00:00
|
|
|
|
silent: true,
|
2014-08-20 19:37:18 +00:00
|
|
|
|
data: { page: this.page },
|
|
|
|
|
success: function (collection, response, options) {
|
|
|
|
|
// you can pass additional options to the event you trigger here as well
|
2015-01-31 01:49:30 +00:00
|
|
|
|
if (collection.length - numBefore < 20) {
|
|
|
|
|
self.page = "loadedAll";
|
|
|
|
|
}
|
2014-08-20 19:37:18 +00:00
|
|
|
|
else self.page += 1;
|
2015-01-31 17:39:48 +00:00
|
|
|
|
self.trigger('successOnFetch', cb);
|
2014-08-20 19:37:18 +00:00
|
|
|
|
},
|
|
|
|
|
error: function (collection, response, options) {
|
|
|
|
|
// you can pass additional options to the event you trigger here as well
|
|
|
|
|
self.trigger('errorOnFetch');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-10-15 03:20:40 +00:00
|
|
|
|
else {
|
2015-01-31 17:39:48 +00:00
|
|
|
|
self.trigger('successOnFetch', cb);
|
2014-10-15 03:20:40 +00:00
|
|
|
|
}
|
2014-08-10 17:06:58 +00:00
|
|
|
|
}
|
2014-07-30 03:18:43 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Metamaps.Backbone.Mapper = Backbone.Model.extend({
|
|
|
|
|
urlRoot: '/users',
|
|
|
|
|
blacklist: ['created_at', 'updated_at'],
|
|
|
|
|
toJSON: function (options) {
|
|
|
|
|
return _.omit(this.attributes, this.blacklist);
|
|
|
|
|
},
|
2014-08-04 16:20:16 +00:00
|
|
|
|
prepareLiForFilter: function () {
|
|
|
|
|
var li = '';
|
|
|
|
|
li += '<li data-id="' + this.id.toString() + '">';
|
2014-09-03 23:05:25 +00:00
|
|
|
|
li += '<img src="' + this.get("image") + '" data-id="' + this.id.toString() + '"';
|
2014-08-04 16:20:16 +00:00
|
|
|
|
li += ' alt="' + this.get('name') + '" />';
|
|
|
|
|
li += '<p>' + this.get('name') + '</p></li>';
|
|
|
|
|
return li;
|
|
|
|
|
}
|
2014-07-30 03:18:43 +00:00
|
|
|
|
});
|
|
|
|
|
Metamaps.Backbone.MapperCollection = Backbone.Collection.extend({
|
|
|
|
|
model: Metamaps.Backbone.Mapper,
|
|
|
|
|
url: '/users'
|
2014-07-31 01:10:10 +00:00
|
|
|
|
});
|