PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/public/JS/admin.js

https://bitbucket.org/sampepose/stl-food-rescue
JavaScript | 294 lines | 279 code | 15 blank | 0 comment | 23 complexity | 2a73e68278314e35a02129ed461f03cb MD5 | raw file
Possible License(s): Apache-2.0
  1. var getFirstDayOfWeek = function (d) {
  2. d = new Date(d);
  3. var day = d.getDay(),
  4. diff = d.getDate() - day + (day == 0 ? -6 : 1); // adjust when day is sunday
  5. return new Date(d.setDate(diff));
  6. };
  7. var validateEmail = function (x) {
  8. var re = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum|us)\b/;
  9. return re.test(x);
  10. };
  11. var FoodRunModel = Backbone.Model.extend({
  12. initialize:function () {
  13. if (this.get("total_volunteer_amount") == null) {
  14. this.set("total_volunteer_amount", 0);
  15. }
  16. if (this.get("total_car_amount") == null) {
  17. this.set("total_car_amount", 0);
  18. }
  19. },
  20. defaults:{
  21. food_run_id:"",
  22. pick_up_location:"",
  23. drop_off_location:"",
  24. time:"",
  25. total_volunteer_amount:"",
  26. total_car_amount:""
  27. },
  28. idAttribute:"food_run_id",
  29. urlRoot:'/data/foodrun/'
  30. });
  31. var FoodRunCollection = Backbone.Collection.extend({
  32. model:FoodRunModel,
  33. url:"/data/foodrun/"
  34. });
  35. var VolunteerModel = Backbone.Model.extend({
  36. defaults:{
  37. volunteer_id:"",
  38. first_name:"",
  39. last_name:"",
  40. phone_number:"",
  41. email:"",
  42. success:"0",
  43. failure:"0",
  44. last_delivery_date:"",
  45. sent_first_survey:"0"
  46. },
  47. idAttribute:"volunteer_id",
  48. urlRoot:'/data/volunteer/'
  49. });
  50. var VolunteerCollection = Backbone.Collection.extend({
  51. model:VolunteerModel,
  52. url:"/data/volunteer/"
  53. });
  54. var FoodRunColl = new FoodRunCollection();
  55. var VolunteerColl = new VolunteerCollection();
  56. var VolunteerView = Backbone.View.extend({
  57. template:_.template($("#volunteer").html()),
  58. addNewVolunteer:function () {
  59. var firstName = $("#firstName").val();
  60. var lastName = $("#lastName").val();
  61. var phone = $("#phoneNumber").val();
  62. var email = $("#email").val();
  63. if (validateEmail(email)) {
  64. var n = new VolunteerModel({first_name: firstName, last_name:lastName, phone_number:phone,email:email});
  65. delete n.id;
  66. delete n.attributes.volunteer_id;
  67. delete n.attributes.last_delivery_date;
  68. n.save({}, {async:false});
  69. VolunteerColl.fetch();
  70. alert("New volunteer added!");
  71. $("#newVolunteer").val('');
  72. } else {
  73. alert("Not a valid email.");
  74. }
  75. },
  76. initialize:function () {
  77. this.collection.bind("reset", this.render, this);
  78. this.collection.bind("add", this.render, this);
  79. this.collection.bind("remove", this.render, this);
  80. },
  81. render:function () {
  82. $('#inject').html(this.template());//this.model.toJSON()
  83. for (var i = 0; i < this.collection.length; i++) {
  84. new VolunteerRowView({model:this.collection.at(i)}).render();
  85. }
  86. $("#newSubmit").click(this.addNewVolunteer);
  87. }
  88. });
  89. var VolunteerRowView = Backbone.View.extend({
  90. template:_.template($("#volunteer-row").html()),
  91. events:{
  92. "click":"remove"
  93. },
  94. render:function () {
  95. $('#volunteerList').append(this.template(this.model.toJSON()));
  96. this.setElement($("#" + this.model.id));
  97. },
  98. remove:function () {
  99. var answer = confirm("Delete " + this.model.get("email") + "?");
  100. if (answer) {
  101. var res = prompt("Confirmation: Type \"confirm\" to delete the volunteer.");
  102. if (res === "confirm") {
  103. this.model.destroy();
  104. this.unbind("click");
  105. this.$el.remove();
  106. alert("Volunteer removed!");
  107. } else {
  108. alert("Volunteer removal aborted!");
  109. }
  110. }
  111. }
  112. });
  113. var FoodRunView = Backbone.View.extend({
  114. template:_.template($("#foodrun").html()),
  115. addNewFoodRun:function () {
  116. var time = new XDate($("#date").val() + " " + $("#time").val()).toString("yyyy-MM-dd'T'HH:mm:ss'.000Z'");
  117. if (!$("#date").val()) {
  118. alert("Select a date!");
  119. return;
  120. }
  121. var dropoff = $("#drop-off").val();
  122. var pickup = $("#pick-up").val();
  123. var n = new FoodRunModel({time:time, pick_up_location:pickup, drop_off_location:dropoff});
  124. delete n.id;
  125. delete n.attributes.food_run_id;
  126. delete n.attributes.total_car_amount;
  127. delete n.attributes.total_volunteer_amount;
  128. n.save({}, {async:false});
  129. FoodRunColl.fetch();
  130. alert("New delivery added!");
  131. },
  132. initialize:function () {
  133. this.collection.bind("reset", this.render, this);
  134. this.collection.bind("add", this.render, this);
  135. this.collection.bind("remove", this.render, this);
  136. },
  137. render:function () {
  138. $('#inject').html(this.template());//this.model.toJSON()
  139. for (var i = 0; i < this.collection.length; i++) {
  140. new FoodRunRowView({model:this.collection.at(i)}).render();
  141. }
  142. $("#date").datepicker({ minDate:0, showWeek:true, firstDay:1});
  143. $("#newSubmit").click(this.addNewFoodRun);
  144. }
  145. });
  146. var FoodRunRowView = Backbone.View.extend({
  147. template:_.template($("#foodRun-row").html()),
  148. events:{
  149. "click":"remove"
  150. },
  151. render:function () {
  152. $('#foodRunList').append(this.template(this.model.toJSON()));
  153. this.setElement($("#" + this.model.id));
  154. },
  155. remove:function () {
  156. var answer = confirm("Delete this delivery (highly unrecommended...)?");
  157. if (answer) {
  158. var res = prompt("Confirmation: Type \"confirm\" to delete the delivery.");
  159. if (res === "confirm") {
  160. this.model.destroy();
  161. this.unbind("click");
  162. this.$el.remove();
  163. alert("Delivery removed!");
  164. } else {
  165. alert("Delivery removal aborted!");
  166. }
  167. }
  168. }
  169. });
  170. var InvoiceView = Backbone.View.extend({
  171. week1:[],
  172. week2:[],
  173. template:_.template($("#invoice").html()),
  174. initialize:function () {
  175. var self = this;
  176. $.ajax({
  177. type:'GET',
  178. url:'/data/foodrun/filter/week1',
  179. dataType:'json',
  180. timeout:10000,
  181. async:false,
  182. success:function (data) {
  183. self.week1 = data;
  184. },
  185. error:function (xhr, type) {
  186. alert('Ajax error!');
  187. }
  188. });
  189. $.ajax({
  190. type:'GET',
  191. url:'/data/foodrun/filter/week2',
  192. dataType:'json',
  193. timeout:10000,
  194. async:false,
  195. success:function (data) {
  196. self.week2 = data;
  197. },
  198. error:function (xhr, type) {
  199. alert('Ajax error!');
  200. }
  201. });
  202. },
  203. render:function () {
  204. $('#inject').html(this.template());
  205. var day1 = new XDate(getFirstDayOfWeek(new Date().setHours(0, 0, 0, 0)), true);
  206. $("#week1Header").html(day1.toString("MMM. d") + "-" + day1.clone().addDays(6).toString("MMM. d"));
  207. day1.addWeeks(1);
  208. $("#week2Header").html(day1.toString("MMM. d") + "-" + day1.addDays(6).toString("MMM. d"));
  209. if (this.week1)
  210. for (var i = 0; i < this.week1.length; i++) {
  211. this.addOne(i, this.week1, "week1");
  212. }
  213. if (this.week2)
  214. for (var i = 0; i < this.week2.length; i++) {
  215. this.addOne(i, this.week2, "week2");
  216. }
  217. },
  218. addOne:function (i, arr, el) {
  219. var id = arr[i]['food_run_id'];
  220. var containerTemp = _.template($("#invoice-foodrun-container").html());
  221. $("#" + el).append(containerTemp({time:arr[i]['time'], drop_off_location:arr[i]['drop_off_location']}));
  222. $.ajax({
  223. type:'GET',
  224. url:'/data/foodrun/' + id + '/invoice',
  225. dataType:'json',
  226. timeout:10000,
  227. async:false,
  228. success:function (data) {
  229. if (data.length >= 1)
  230. for (var j = 0; j < data.length; j++) {
  231. new InvoiceFoodRunView({el:el, food_run_id:id, first_name:data[j]['first_name'],last_name:data[j]['last_name'], car_amount:data[j]['car_amount'], volunteer_amount:data[j]['volunteer_amount']}).render();
  232. }
  233. else
  234. $('#' + el).append("No volunteers signed up.");
  235. },
  236. error:function (xhr, type) {
  237. alert('Ajax error!');
  238. }
  239. });
  240. }
  241. });
  242. var InvoiceFoodRunView = Backbone.View.extend({
  243. template:_.template($("#invoice-foodrun").html()),
  244. render:function () {
  245. $('#' + this.options.el).append(this.template(this.options));
  246. }
  247. });
  248. var AppRouter = Backbone.Router.extend({
  249. routes:{
  250. "":"changeContent",
  251. "volunteer":"volunteerPage",
  252. "foodrun":"foodRunPage",
  253. "invoice":"invoicePage",
  254. "*path":"error"
  255. },
  256. changeContent:function () {
  257. var id = "#" + (Backbone.history.fragment == "" ? "homepage" : Backbone.history.fragment);
  258. $("#inject").html(_.template($(id).html()));
  259. },
  260. volunteerPage:function () {
  261. VolunteerColl.fetch({async:false});
  262. new VolunteerView({collection:VolunteerColl}).render();
  263. },
  264. foodRunPage:function () {
  265. FoodRunColl.fetch({async:false});
  266. new FoodRunView({collection:FoodRunColl}).render();
  267. },
  268. invoicePage:function () {
  269. new InvoiceView().render();
  270. },
  271. error:function () {
  272. this.navigate("#");
  273. this.changeContent();
  274. }
  275. });
  276. $(document).ready(function () {
  277. new AppRouter;
  278. Backbone.history.start();
  279. });