Skip to content

Commit

Permalink
first try for multispecies
Browse files Browse the repository at this point in the history
  • Loading branch information
xavijam committed May 8, 2014
1 parent eacea19 commit 9e36008
Show file tree
Hide file tree
Showing 19 changed files with 3,832 additions and 203 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

/**
*
*
*/

var DatasetItem = View.extend({

className: 'dataset',
tagName: 'li',

initialize: function() {
this.template = this.getTemplate('datasets/dataset_list_view');
this._initBinds();
},

render: function() {
this.clearSubViews();

this.$el.html(this.template());

// sources view
var v = this.sources = new SourcesView({
el: this.$('ul.sources_list'),
collection: this.model.getSources(),
map: this.model.getMap()
});
v.render();
this.addView(v);

return this;
},

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

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

});
96 changes: 96 additions & 0 deletions app/assets/javascripts/editor/Layout/CurrentSources/datasets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

/**
* Dataset model
*
*/

var DatasetModel = Backbone.Model.extend({

defaults: {
name: 'Dataset',
active: true
},

initialize: function(obj, opts) {
this.map = opts.map;
this.sources = new SourcesCollection(null, { map: opts.map });
},

getMap: function() {
return this.map;
},

getSources: function() {
return this.sources;
}

});



/**
* Dataset collection
*
*/


var DatasetsCollection = Backbone.Collection.extend({

model: DatasetModel,

getActive: function() {
return this.find(function(m) {
return m.get('active') === true
});
},

sum: function(data, type, query) {
// DATASETS!
var dataset;

// Get dataset or active one
if (!data.dcid) {
dataset = this.getActive();
data.dcid = dataset.cid;
} else {
dataset = this.find(function(m) {
return m.cid === data.dcid
});
if (!dataset) {
console.log("Dataset not found!");
return false;
}
}

// SOURCES!
var sources = dataset.getSources();
sources.sum(data, type, query);

return data;
},

deduct: function(data, type, query) {
// DATASETS!
var dataset;

// Get dataset or active one
if (!data.dcid) {
dataset = this.getActive();
} else {
dataset = this.find(function(m) {
return m.cid === data.dcid
});
if (!dataset) {
console.log("Dataset not found!");
return false;
}
}

// SOURCES!
var sources = dataset.getSources();
sources.deduct(data, type, query);

return data;
}

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

/**
*
*
*/

var DatasetsView = View.extend({

events: {
'change select': '_setActiveDataset',
'click a.create_dataset': '_createDataset'
},

initialize: function() {
this._initBinds();
},

render: function() {
this.clearSubViews();
this.collection.each(this._addDataset);
return 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() {
_.bindAll(this, '_createDataset');
// Set select
this.collection.bind('add remove', this._changeSelect, this);
this.collection.bind('add', this._addDataset, this);
this.collection.bind('remove', this._removeDataset, this);
},

_changeSelect: function() {
var $select = this.$('select');
$select.html('');
this.collection.each(function(m) {
$select.append('<option data-cid="' + m.cid + '">' + m.get('name') + '</option>')
});
},

_setActiveDataset: function() {
var value = this.$('select').val();
var cid = this.$('select').find(":selected").data('cid');

this.collection.each(function(m) {
if (m.get('name') === value && m.cid == cid) {
// Set active dataset
m.set('active', true);
// Change sources_collection global variable :S
sources_collection = m.getSources();
} else {
m.set('active', false);
}
});
},

_onSelectDataset: function(e) {
if (e) this.killEvent(e);
console.log("other dataset selected, change panes!");
},

_createDataset: function(e) {
if (e) this.killEvent(e);
var n = this.collection.size();
this.collection.add(new DatasetModel({ name: 'Dataset ' + n }, { map: this.options.map }));
}


})
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@

this.$el
.css({
top: pos.top - 4 + 'px',
marginLeft: '+=10px',
top: pos.top - 22 + 'px',
left: '+=10px',
opacity: 0,
display: 'block'
})
.animate({
marginLeft: '-=10px',
left: '-=10px',
opacity: 1
}, 150);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
_.bindAll(this, '_toggleVisibility', '_deleteSpecie', '_finishEdit', '_startEdit', '_onSubmit');
this.model.bind('change', this.render, this);
this.model.bind('delete', this._removeSource, this);
this.model.bind('destroy', this.clean, this);
// this.model.bind('destroy', this.clean, this);
this.template = JST['editor/views/source_item'];
},

Expand All @@ -40,6 +40,9 @@
// Set cid as data
this.$el.attr('data-cid', this.cid);

// Show or hide depending the number of occs
this.$el[ d.total == 0 ? 'hide' : 'show' ]();

return this;
},

Expand Down Expand Up @@ -76,7 +79,7 @@

_removeSource: function(m) {
deleteAll(m.get('query'), m.get('type'));
this.model.destroy();
// this.model.destroy();
},

_deleteSpecie: function(e) {
Expand Down
109 changes: 86 additions & 23 deletions app/assets/javascripts/editor/Layout/CurrentSources/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,106 @@
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
});
// 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;

// If occ has source id (scid)
if (data.scid) {
// Get source
mdl = this.find(function(m) {
return m.cid === data.scid
});
} else {
// Search source
mdl = 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);
if (!mdl) {
mdl = new SourceModel({ type: type, query: query, alias: query }, { map: this.options.map });
this.add(mdl);
} else {
// If it does exist, add new one to the total
var t = m.get('total');
m.set('total', t+1);
var t = mdl.get('total');
mdl.set('total', t+1);
}

if (!data.scid) data.scid = mdl.cid;
return data;
},

deduct: function(query, type, alias) {
// Search model
var m = this.find(function(m) {
return m.get('query') == query && m.get('type') == type
});
// 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;

// If occ has source id (scid)
if (data.scid) {
// Get source
mdl = this.find(function(m) {
return m.cid === data.scid
});
} else {
// Search source
mdl = 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' );
if (!mdl) {
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);
var t = mdl.get('total') - 1;
mdl.set('total', t);
}

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

return data;
}

});
Loading

0 comments on commit 9e36008

Please sign in to comment.