Permalink
Browse files

use helpers method on Templates

  • Loading branch information...
pkaushik committed Mar 30, 2015
1 parent 6b25643 commit 42295a2896237d953a5d5ff2a846ab474103aec2
Showing with 141 additions and 143 deletions.
  1. +25 −28 client/views/attendance.js
  2. +5 −3 client/views/create_dialog.js
  3. +23 −25 client/views/details.js
  4. +12 −11 client/views/invite_dialog.js
  5. +68 −69 client/views/map.js
  6. +8 −7 client/views/page.js
View
@@ -1,31 +1,28 @@
///////////////////////////////////////////////////////////////////////////////
// Party attendance widget
Template.attendance.rsvpName = function () {
var user = Meteor.users.findOne(this.user);
return displayName(user);
};
Template.attendance.outstandingInvitations = function () {
var party = Parties.findOne(this._id);
return Meteor.users.find({$and: [
{_id: {$in: party.invited}}, // they're invited
{_id: {$nin: _.pluck(party.rsvps, 'user')}} // but haven't RSVP'd
]});
};
Template.attendance.invitationName = function () {
return displayName(this);
};
Template.attendance.rsvpIs = function (what) {
return this.rsvp === what;
};
Template.attendance.nobody = function () {
return ! this.public && (this.rsvps.length + this.invited.length === 0);
};
Template.attendance.canInvite = function () {
return ! this.public && this.owner === Meteor.userId();
};
Template.attendance.helpers({
rsvpName: function () {
var user = Meteor.users.findOne(this.user);
return displayName(user);
},
outstandingInvitations: function () {
var party = Parties.findOne(this._id);
return Meteor.users.find({$and: [
{_id: {$in: party.invited}}, // they're invited
{_id: {$nin: _.pluck(party.rsvps, 'user')}} // but haven't RSVP'd
]});
},
invitationName: function () {
return displayName(this);
},
rsvpIs: function (what) {
return this.rsvp === what;
},
nobody: function () {
return ! this.public && (this.rsvps.length + this.invited.length === 0);
},
canInvite: function () {
return ! this.public && this.owner === Meteor.userId();
}
});
@@ -33,7 +33,9 @@ Template.createDialog.events({
}
});
Template.createDialog.error = function () {
return Session.get("createError");
};
Template.createDialog.helpers({
error: function () {
return Session.get("createError");
}
});
View
@@ -1,32 +1,30 @@
///////////////////////////////////////////////////////////////////////////////
// Party details sidebar
Template.details.party = function () {
return Parties.findOne(Session.get("selected"));
};
Template.details.anyParties = function () {
return Parties.find().count() > 0;
};
Template.details.creatorName = function () {
var owner = Meteor.users.findOne(this.owner);
if (owner._id === Meteor.userId())
return "me";
return displayName(owner);
};
Template.details.canRemove = function () {
return this.owner === Meteor.userId() && attending(this) === 0;
};
Template.details.maybeChosen = function (what) {
var myRsvp = _.find(this.rsvps, function (r) {
return r.user === Meteor.userId();
}) || {};
Template.details.helpers({
party: function () {
return Parties.findOne(Session.get("selected"));
},
anyParties: function () {
return Parties.find().count() > 0;
},
creatorName: function () {
var owner = Meteor.users.findOne(this.owner);
if (owner._id === Meteor.userId())
return "me";
return displayName(owner);
},
canRemove: function () {
return this.owner === Meteor.userId() && attending(this) === 0;
},
maybeChosen: function (what) {
var myRsvp = _.find(this.rsvps, function (r) {
return r.user === Meteor.userId();
}) || {};
return what == myRsvp.rsvp ? "chosen btn-inverse" : "";
};
return what == myRsvp.rsvp ? "chosen btn-inverse" : "";
}
});
Template.details.events({
'click .rsvp_yes': function () {
@@ -11,14 +11,15 @@ Template.inviteDialog.events({
}
});
Template.inviteDialog.uninvited = function () {
var party = Parties.findOne(Session.get("selected"));
if (! party)
return []; // party hasn't loaded yet
return Meteor.users.find({$nor: [{_id: {$in: party.invited}},
{_id: party.owner}]});
};
Template.inviteDialog.displayName = function () {
return displayName(this);
};
Template.inviteDialog.helpers({
uninvited: function () {
var party = Parties.findOne(Session.get("selected"));
if (! party)
return []; // party hasn't loaded yet
return Meteor.users.find({$nor: [{_id: {$in: party.invited}},
{_id: party.owner}]});
},
displayName: function () {
return displayName(this);
}
});
View
@@ -7,6 +7,74 @@ $(window).resize(function () {
$mc.css('height', (h - offsetTop));
}).resize();
Template.map.helpers({
created: function() {
Parties.find({}).observe({
added: function(party) {
var marker = new L.Marker(party.latlng, {
_id: party._id,
icon: createIcon(party)
}).on('click', function(e) {
Session.set("selected", e.target.options._id);
});
addMarker(marker);
},
changed: function(party) {
var marker = markers[party._id];
if (marker) marker.setIcon(createIcon(party));
},
removed: function(party) {
removeMarker(party._id);
}
});
},
rendered: function () {
// basic housekeeping
$(window).resize(function () {
var h = $(window).height(), offsetTop = 90; // Calculate the top offset
$('#map_canvas').css('height', (h - offsetTop));
}).resize();
// initialize map events
if (!map) {
initialize($("#map_canvas")[0], [ 41.8781136, -87.66677956445312 ], 13);
map.on("dblclick", function(e) {
if (! Meteor.userId()) // must be logged in to create parties
return;
openCreateDialog(e.latlng);
});
var self = this;
Meteor.autorun(function() {
var selectedParty = Parties.findOne(Session.get("selected"));
if (selectedParty) {
if (!self.animatedMarker) {
var line = L.polyline([[selectedParty.latlng.lat, selectedParty.latlng.lng]]);
self.animatedMarker = L.animatedMarker(line.getLatLngs(), {
autoStart: false,
distance: 3000, // meters
interval: 200, // milliseconds
icon: L.divIcon({
iconSize: [50, 50],
className: 'leaflet-animated-div-icon'
})
});
map.addLayer(self.animatedMarker);
} else {
// animate to here
var line = L.polyline([[self.animatedMarker.getLatLng().lat, self.animatedMarker.getLatLng().lng],
[selectedParty.latlng.lat, selectedParty.latlng.lng]]);
self.animatedMarker.setLine(line.getLatLngs());
self.animatedMarker.start();
}
}
})
}
}
});
var map, markers = [ ];
var initialize = function(element, centroid, zoom, features) {
@@ -55,72 +123,3 @@ var openCreateDialog = function (latlng) {
Session.set("showCreateDialog", true);
};
Template.map.created = function() {
Parties.find({}).observe({
added: function(party) {
var marker = new L.Marker(party.latlng, {
_id: party._id,
icon: createIcon(party)
}).on('click', function(e) {
Session.set("selected", e.target.options._id);
});
addMarker(marker);
},
changed: function(party) {
var marker = markers[party._id];
if (marker) marker.setIcon(createIcon(party));
},
removed: function(party) {
removeMarker(party._id);
}
});
}
Template.map.rendered = function () {
// basic housekeeping
$(window).resize(function () {
var h = $(window).height(), offsetTop = 90; // Calculate the top offset
$('#map_canvas').css('height', (h - offsetTop));
}).resize();
// initialize map events
if (!map) {
initialize($("#map_canvas")[0], [ 41.8781136, -87.66677956445312 ], 13);
map.on("dblclick", function(e) {
if (! Meteor.userId()) // must be logged in to create parties
return;
openCreateDialog(e.latlng);
});
var self = this;
Meteor.autorun(function() {
var selectedParty = Parties.findOne(Session.get("selected"));
if (selectedParty) {
if (!self.animatedMarker) {
var line = L.polyline([[selectedParty.latlng.lat, selectedParty.latlng.lng]]);
self.animatedMarker = L.animatedMarker(line.getLatLngs(), {
autoStart: false,
distance: 3000, // meters
interval: 200, // milliseconds
icon: L.divIcon({
iconSize: [50, 50],
className: 'leaflet-animated-div-icon'
})
});
map.addLayer(self.animatedMarker);
} else {
// animate to here
var line = L.polyline([[self.animatedMarker.getLatLng().lat, self.animatedMarker.getLatLng().lng],
[selectedParty.latlng.lat, selectedParty.latlng.lng]]);
self.animatedMarker.setLine(line.getLatLngs());
self.animatedMarker.start();
}
}
})
}
};
View
@@ -1,7 +1,8 @@
Template.page.showInviteDialog = function () {
return Session.get("showInviteDialog");
};
Template.page.showCreateDialog = function () {
return Session.get("showCreateDialog");
};
Template.page.helpers({
showInviteDialog: function () {
return Session.get("showInviteDialog");
},
showCreateDialog: function () {
return Session.get("showCreateDialog");
}
});

0 comments on commit 42295a2

Please sign in to comment.