Initial commit

This commit is contained in:
Juho Teperi
2013-05-13 11:43:34 +03:00
commit fbf8fb087a
44 changed files with 2425 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
define([
'backbone',
'select2'
], function (Backbone) {
var Dropdown = Backbone.View.extend({
events: {
'change': 'change'
},
initialize: function (opts) {
this.$el.select2(opts.select2);
},
open: function () {
this.$el.select2('open');
},
change: function () {
this.trigger('new-leg', this.$el.select2('data'));
this.$el.select2('data', {});
}
});
return Dropdown;
});

29
app/scripts/views/leg.js Normal file
View File

@@ -0,0 +1,29 @@
'use strict';
define([
'backbone',
'Template'
], function (Backbone, Template) {
var LegView = Backbone.View.extend({
tagName: 'div',
className: 'leg',
events: {
'click a.delete': 'destroy'
},
initialize: function () {
this.model.on('change', this.render, this);
this.model.on('destroy', this.remove, this);
},
destroy: function () {
this.model.destroy();
},
render: function () {
this.$el.data('id', this.model.get('id'));
this.$el.attr('id', 'leg-' + this.model.get('id')); // fuu
this.$el.html(Template.leg(this.model.toJSON()));
return this;
}
});
return LegView;
});

View File

@@ -0,0 +1,39 @@
'use strict';
define([
'backbone',
'models/leg',
'views/dropdown',
'libs/airports',
'Template'
], function (Backbone, Leg, Dropdown, airports, Template) {
var LegInput = Backbone.View.extend({
el: '.legInputWidget',
events: {
'click button': 'openDropdown'
},
initialize: function () {
this.dropdown = new Dropdown({
el: this.$el.find('input'),
select2: {
initSelection: airports.airportById,
formatResult: Template.choice,
formatSelection: Template.choice,
minimumInputLength: 1,
ajax: airports.ajax
}
});
this.dropdown.bind('new-leg', this.newLeg, this);
},
openDropdown: function () {
this.dropdown.open();
},
newLeg: function (data) {
this.collection.push(new Leg(data));
}
});
return LegInput;
});

41
app/scripts/views/map.js Normal file
View File

@@ -0,0 +1,41 @@
'use strict';
define([
'backbone',
'libs/maps'
], function (Backbone, Maps) {
var MapView = Backbone.View.extend({
el: '#gmap',
initialize: function () {
this.collection.on('sort', this.render, this);
this.collection.on('remove', this.render, this);
this.collection.on('add', this.add, this);
this.map = new Maps.Map(this.el);
this.route = new Maps.Line({
geodesic: true,
map: this.map,
strokeColor: '#000'
});
this.bounds = new Maps.Bounds(this.map);
},
render: function () {
this.route.clear();
this.bounds.clear();
this.collection.forEach(this.route.add);
this.collection.forEach(this.bounds.add);
this.bounds.use();
},
add: function (model, collection, options) {
this.route.add(model);
this.bounds.add(model);
this.bounds.use();
}
});
return MapView;
});

View File

@@ -0,0 +1,22 @@
'use strict';
define([
'backbone',
'Template',
'config',
'bootstrap'
], function (Backbone, Template, config) {
var Operation = Backbone.View.extend({
el: '#operation',
initialize: function () {
this.render.apply(this);
},
render: function () {
this.$el.html(Template.operation(config));
this.$el.find('abbr').tooltip();
return this;
}
});
return Operation;
});

View File

@@ -0,0 +1,28 @@
define([
'backbone',
'backbone-mediator'
], function (Backbone) {
var PassengersInput = Backbone.View.extend({
el: '.passengersWidget',
events: {
'change input': 'change',
'keyup input': 'change'
},
initialize: function () {
this.value = 1;
},
change: function (event) {
var val = this.$el.find('input').val();
if (!val) {
val = 1;
}
val = Number(val);
if (val !== this.value) {
this.value = val;
Backbone.Mediator.publish('passengers:change', this.value);
}
}
});
return PassengersInput;
});

View File

@@ -0,0 +1,22 @@
'use strict';
define([
'backbone',
'backbone-mediator'
], function (Backbone) {
var RoundtripInput = Backbone.View.extend({
el: '.roundtripWidget',
events: {
'click button': 'click'
},
click: function (event, el) {
this.$el.find('button').removeClass('active');
var current = this.$el.find(event.currentTarget);
current.addClass('active');
Backbone.Mediator.publish('roundtrip:change', current.hasClass('roundtrip'));
}
});
return RoundtripInput;
});

View File

@@ -0,0 +1,49 @@
'use strict';
define([
'backbone',
'views/leg',
'jquery-ui-sortable'
], function (Backbone, LegView) {
var RouteView = Backbone.View.extend({
el: '.route',
events: {
'sortstart': 'sortStart',
'sortstop': 'sortStop'
},
initialize: function () {
this.collection.bind('add', this.add, this);
this.collection.bind('sort', this.render, this);
this.$el.sortable({
handle: 'div > div',
scroll: false,
});
},
sortStart: function () {
// fadeOut results
},
sortStop: function (event, ui) {
this.updateSort();
// fadeIn results
},
updateSort: function () {
// jQuery UI already updates DOM order,
// so we only have to update Backbone collection to match that
var self = this;
this.$el.find('.leg').each(function (index) {
// Get id of model from element
var id = $(this).data('id');
self.collection.get(id).set('order', index);
});
this.collection.sort();
},
add: function (model, collection, options) {
var item = new LegView({model: model});
this.$el.append(item.render().el);
}
});
return RouteView;
});

View File

@@ -0,0 +1,19 @@
'use strict';
define([
'backbone',
'Template'
], function (Backbone, Template) {
var TotalView = Backbone.View.extend({
el: '.totalWidget',
initialize: function () {
this.render.apply(this);
this.model.bind('change', this.render, this);
},
render: function () {
this.$el.html(Template.total(this.model.toJSON()));
}
});
return TotalView;
});