Skip to content

Commit

Permalink
added remove dataset behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
xavijam committed May 22, 2014
1 parent 4be96af commit f610f3d
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 73 deletions.
37 changes: 0 additions & 37 deletions app/assets/javascripts/editor/Layout/CurrentSources/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,6 @@
return m.get('position')
},

// sumUp: function(query, type, alias) {
// // Search model
// var m = this.find(function(m) {
// return m.get('query') == query && m.get('type') == type
// });

// // If it doesn't exist, let's create it!
// if (!m) {
// m = new SourceModel({ type: type, query: query, alias: alias || query }, { map: this.options.map });
// this.add(m);
// } else {
// // If it does exist, add new one to the total
// var t = m.get('total');
// m.set('total', t+1);
// }
// },

sum: function(data, type, query) {
var mdl;
Expand Down Expand Up @@ -94,27 +78,6 @@
return data;
},

// deduct: function(query, type, alias) {
// // Search model
// var m = this.find(function(m) {
// return m.get('query') == query && m.get('type') == type
// });

// // If you can't find it, please console!
// if (!m) {
// // console.log('Ouch, source with query ' + query + ' and type ' + type + ' doesn\'t exist' );
// return false;
// } else {
// // Deduct one from total, setting model
// var t = m.get('total') - 1;
// m.set('total', t);
// }

// // If total is 0, remove the model + remove item + añsdlfjñlajsdflñajsd
// if (t == 0) {
// m.destroy();
// }
// },

deduct: function(data, type, query) {
var mdl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@

_initBinds: function() {
this.model.bind('change:active', this._onActiveChange, this);
this.model.bind('change:removed', this._onRemovedChange, this);
},

_onActiveChange: function() {
this.$el[ this.model.get('active') ? 'show' : 'hide' ]();
if (!this.model.get('removed')) {
this.$el[ this.model.get('active') ? 'show' : 'hide' ]();
}
},

_onRemovedChange: function() {
this.$el[ !this.model.get('removed') ? 'show' : 'hide' ]();
}

});
60 changes: 54 additions & 6 deletions app/assets/javascripts/editor/Layout/Datasets/dataset_selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
},

initialize: function() {
_.bindAll(this, 'search', 'format', '_onCreateNew', '_onSelect', '_onEdit');
_.bindAll(this, 'search', 'format', '_onCreateNew', '_onSelect', '_onEdit',
'_enableSelector', '_disableSelector');
this._initBinds()
},

Expand All @@ -29,13 +30,22 @@
this.$el.html('');

this.collection.each(function(m) {
this.$el.append('<option data-cid="' + m.cid + '" ' + ( m.get('active') ? 'selected' : '') + ' >' + m.get('name') + '</option>')
if (!m.get('removed')) {
this.$el.append('<option data-cid="' + m.cid + '" ' + ( m.get('active') ? 'selected' : '') + ' >' + m.get('name') + '</option>')
}
}, this);

// Check if there is any dataset selected
var actives = this.collection.filter(function(m) { return m.get('active') }).length;
if (this.collection.size() > 0 && actives === 0) {
this.collection.at(0).set('active', true);
// If not, select first not removed :)

var available_models = this.collection.where({ removed: false });
if (available_models.length > 0) {
available_models[0].set('active', true);
} else {
console.log("Not available datasets! :(");
}
}

var select2 = this.$el.select2({
Expand All @@ -54,19 +64,24 @@
},

_initBinds: function() {
this.collection.bind('add remove', this.render, this);
this.collection.bind('add remove', this.render, this);
this.collection.bind('change:removed', this.render, this);
},

search: function(val) {
return '<a class="create_new" href="#/create-' + val + '" data-name="' + val + '">Create dataset ' + val + '</a>';
},

format: function(state) {
// Count number of datasets to disable "remove" option
var size = this.collection.size();

if (!state.id) return state.text;

return "<i class='fa fa-dot-circle-o visible disabled'></i>\
<form><input class='text' type='text' value='" + state.text + "' readonly /></form>\
<i class='fa fa-pencil edit'></i>\
<i class='fa fa-times delete disabled'></i>";
<i class='fa fa-times delete " + ( size > 1 ? '' : 'disabled' ) + "'></i>";
},

// Oh god!
Expand Down Expand Up @@ -106,7 +121,40 @@
}
},

_onDelete: function(data, e) {},
_onDelete: function(data, e) {
// Get number of datasets.
// If there is only one, don't let remove it
var size = this.collection.size();
if (size < 2) return;

// Get dataset info
var cid = $(data.element).data('cid');
var d = this.collection.get({ cid: cid });

// Active the first one if the deleted is active
if (d.get('active') === true) {
var mdl = this.collection.find(function(m) { return m.cid !== cid });
this.trigger('onSelect', mdl.get('name'), mdl.cid, this);

this.$el.select2('val', mdl.get('name'));
}

// Disable selector
this._disableSelector();

// Start deleting all dataset markers
deleteDataset(d);
},

_disableSelector: function() {
$(document).bind('occs_removed', this._enableSelector);
this.$el.select2('disable');
},

_enableSelector: function() {
$(document).unbind('occs_removed', this._enableSelector);
this.$el.select2('enable');
},

_onVisible: function(data, e) {},

Expand Down
7 changes: 6 additions & 1 deletion app/assets/javascripts/editor/Layout/Datasets/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

defaults: {
name: 'Dataset',
active: false
active: false,
removed: false,
hidden: false
},

initialize: function(obj, opts) {
Expand Down Expand Up @@ -62,6 +64,9 @@
}
}

// If dataset was removed, let's make it visible
dataset.set('removed', false);

// SOURCES!
var sources = dataset.getSources();
sources.sum(data, type, query);
Expand Down
17 changes: 7 additions & 10 deletions app/assets/javascripts/editor/Layout/Datasets/datasets_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,22 @@
this.addView(selector);
},

_initBinds: function() {
this.collection.bind('add', this._addDataset, this);
this.collection.bind('remove', this._removeDataset, this);
},

_addDataset: function(m) {
var v = new DatasetItem({ model: m });
this.$('ul.datasets').append(v.render().el);
this.addView(v);
},

_removeDataset: function(m) {
console.log("dataset should be removed!");
},

_initBinds: function() {
// Set select
this.collection.bind('add', this._addDataset, this);
this.collection.bind('remove', this._removeDataset, this);
},
_removeDataset: function(m) {},

_setActiveDataset: function(value, cid) {
this.collection.each(function(m) {
if (m.get('name') === value && m.cid == cid) {
if (m.get('name') === value && m.cid == cid && !m.get('removed')) {
// Set active dataset
m.set('active', true);
// Change sources_collection global variable :S
Expand Down
92 changes: 82 additions & 10 deletions app/assets/javascripts/editor/Models/MapOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,9 @@

var geocat_query = info_data.geocat_query ? info_data.geocat_query.toLowerCase() : 'user';
var geocat_kind = info_data.geocat_kind ? info_data.geocat_kind.toLowerCase() : 'user';
// var geocat_alias = info_data.geocat_alias;
var latlng = new google.maps.LatLng(parseFloat(info_data.latitude),parseFloat(info_data.longitude));

if (!info_data.geocat_removed) {
// sources_collection.sumUp(geocat_query, geocat_kind, geocat_alias);
datasets.sum(info_data, geocat_kind, geocat_query);
}

Expand Down Expand Up @@ -483,6 +481,82 @@
}


/*========================================*/
/* Delete all the markers from a dataset. */
/*========================================*/
function deleteDataset(dataset) {
closeMapWindows();
showMamufasMap();

if (convex_hull.isVisible()) {
analysis_map.stop();
}

// Remove spiderfy!
oms.unspiderfy();

// Vars
var removed_markers = [];
var occsCopy = $.extend(true,{},occurrences);
var s; // Actual source


function asyncRemoveMarker(query,type) {
for (var i in occsCopy) {
var d = occsCopy[i].data;

if ((d.geocat_kind === type) && (!d.geocat_removed) && (d.scid === s.cid)) {
datasets.deduct(occurrences[i].data, type, query);

removed_markers.push(occurrences[i].data);
occurrences[i].data.geocat_removed = true;
occurrences[i].setMap(null);
}
delete occsCopy[i];
break;
}

if (i==undefined) {
asyncRemoveSource();
} else {
setTimeout(function(){
asyncRemoveMarker(query,type)
},10);
}
}

var j;
function asyncRemoveSource() {
if (j === undefined) {
j = 0;
} else {
++j;
}

if (dataset.getSources().size() > 0 && dataset.getSources().at(j)) {
s = dataset.getSources().at(j);
asyncRemoveMarker(s.get('query'),s.get('type'));
} else {
// Make dataset removed
dataset.set('removed', true);

actions.Do('remove', null, removed_markers);
hideMamufasMap(false);
delete occsCopy;

$(document).trigger('occs_removed');

if (convex_hull.isVisible()) {
$(document).trigger('occs_updated');
}
}
}

// Let's start it!
asyncRemoveSource();
}



/*============================================================================*/
/* Delete all the markers of a query and type. */
Expand All @@ -492,7 +566,6 @@
showMamufasMap();

if (convex_hull.isVisible()) {
// mamufasPolygon();
analysis_map.stop();
}

Expand All @@ -501,17 +574,16 @@

var remove_markers = [];
var occsCopy = $.extend(true,{},occurrences);

var s = datasets.getActive().getSources().where({ type: type, query: query })[0];

if (!s) {
console.log("Delete all function can't find this source { query:" + query + ", type:" + type + " }" );
}

function asynRemoveMarker(query,type) {
for (var i in occsCopy) {
var d = occsCopy[i].data;

if ((d.geocat_kind == type) && (!d.geocat_removed) && (d.geocat_query == query)) {

// points.deduct(query,type);
// sources_collection.deduct(query, type);

if ((d.geocat_kind == type) && (!d.geocat_removed) && (d.scid === s.cid)) {
datasets.deduct(occurrences[i].data, type, query);

remove_markers.push(occurrences[i].data);
Expand Down
5 changes: 0 additions & 5 deletions app/assets/javascripts/editor/Models/UnredoOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,11 @@
occurrences[observations_data[count].catalogue_id].data.geocat_removed = false;
occurrences[observations_data[count].catalogue_id].setMap(map);

// points.add(occurrences[observations_data[count].catalogue_id].data.geocat_query,occurrences[observations_data[count].catalogue_id].data.geocat_kind);
var query = occurrences[observations_data[count].catalogue_id].data.geocat_query;
var type = occurrences[observations_data[count].catalogue_id].data.geocat_kind;
// var alias = occurrences[observations_data[count].catalogue_id].data.geocat_alias;

// sources_collection.sumUp(query, type, alias);
datasets.sum(occurrences[observations_data[count].catalogue_id].data, type, query);

count++;
// count = count+1;
setTimeout(function(){AsynRestoreMarkers(count, observations_data);},0);
} else {
if (convex_hull.isVisible()) {
Expand Down
5 changes: 2 additions & 3 deletions app/assets/stylesheets/editor/sources.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,12 @@

i {
color:#787878;
&:hover {color:#333}

// Types
&.visible { margin-right:4px; }
&.edit { margin:0 4px }
&.delete {}

&:hover {color:#333}
&.delete { &:hover { color:red } }

// States
&.disabled {
Expand Down

0 comments on commit f610f3d

Please sign in to comment.