routes.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. var tables = [];
  2. var editors = [];
  3. var rows = {};
  4. function initializeRoutes()
  5. {
  6. router('/login', routes['login']);
  7. router('/logout', routes['logout']);
  8. router('/sd', routes['sd']);
  9. router('*', routes['login']);
  10. }
  11. //add a 'firstLoad' setup function into the route's middleware chain
  12. var routes =
  13. {
  14. login: function()
  15. {
  16. showPage('login');
  17. },
  18. logout: function()
  19. {
  20. $.post('api/logout.php');
  21. showPage('login');
  22. router('/');
  23. },
  24. sd: function()
  25. {
  26. showPage('sd');
  27. loadAllData();
  28. }
  29. };
  30. function showPage(page)
  31. {
  32. console.log('show page', page);
  33. var $page_old = $('.screen.active');
  34. var $page_new = $('#screen-' + page);
  35. //do nothing if we are already on the page
  36. if($page_old.is($page_new))
  37. return;
  38. $('body').attr('class', 'screen-' + page);
  39. //put new page on top of old
  40. $page_new.insertAfter('.screen.active');
  41. //slide it to the right while crossfading
  42. $page_old
  43. .one('animationend', function(event) //jshint ignore:line, unused
  44. {
  45. $(this).hide();
  46. }) //TODO: add detect webkitanimationend or animationend
  47. .removeClass('active');
  48. //slide from left while crossfading
  49. $page_new
  50. .show()
  51. .off('animationend')
  52. .addClass('active')
  53. .focus(); //this needs to be used in conjunction with a "tabindex" field on the html object to maintain keyboard focus
  54. }
  55. function loadAllData()
  56. {
  57. $.post('api/getAllData.php', 'json')
  58. .done(function(companies)
  59. {
  60. //go through each company and add a tab if one isn't present
  61. if(!companies.length)
  62. return;
  63. for(var c = 0; c < companies.length; c++)
  64. {
  65. var company = companies[c];
  66. var id_company = company['id_company'];
  67. if(company['file_rows'])
  68. for(var r = 0; r < company['file_rows'].length; r++)
  69. {
  70. var id_file_row = company['file_rows'][r]['id_file_row'];
  71. rows[id_file_row] = company['file_rows'][r];
  72. }
  73. //if the company's tab doesn't exist, create it
  74. if(!tables[id_company])
  75. {
  76. //add tab
  77. $('<li><a data-toggle="tab" href="#table-' + id_company + '-container">' + company['name'] + '</a></li>')
  78. .data('id_company', id_company)
  79. .appendTo('#screen-sd ul.nav-tabs');
  80. //add table
  81. $('<div id="table-' + id_company + '-container" class="tab-pane fade in">'
  82. + '<table id="table-' + id_company + '" class="table table-striped editable" width="100%"></table>'
  83. + '</div>')
  84. .appendTo('#screen-sd .tab-content');
  85. //create the datatable
  86. tables[id_company] = createTable(id_company, '#screen-sd #table-' + id_company);
  87. }
  88. else
  89. {
  90. tables[id_company].clear();
  91. }
  92. //load the data into the data
  93. tables[id_company].rows.add(company['file_rows']).draw();
  94. }
  95. if($('#screen-sd ul.nav-tabs > li.active').length === 0)
  96. $('#screen-sd ul.nav-tabs > li:eq(0) a').click();
  97. })
  98. .fail(function(err, xhr, text)
  99. {
  100. console.error(err['responseText']);
  101. });
  102. }
  103. function loadCompanies()
  104. {
  105. $.post('api/getCompanies.php')
  106. .done(function(response)
  107. {
  108. })
  109. .fail(function(err, xhr, text)
  110. {
  111. console.error(err.responseText);
  112. });
  113. }
  114. function createTable(id_company, table_dom)
  115. {
  116. var table = $(table_dom).DataTable(
  117. {
  118. 'data': [],
  119. 'order': [[0, 'row_number']],
  120. 'columns':
  121. [
  122. { 'title': 'Division', 'data': 'division', 'defaultContent': '', 'className': 'division' },
  123. { 'title': 'Consignee', 'data': 'consignee', 'defaultContent': '', 'className': 'consignee' },
  124. { 'title': 'Called&nbsp;In', 'data': 'date_called_in', 'defaultContent': '', 'className': 'date_called_in' },
  125. { 'title': 'Dispatch&nbsp;#', 'data': 'dispatch_number', 'defaultContent': '', 'className': 'dispatch_number' },
  126. { 'title': 'Shipper', 'data': 'shipper', 'defaultContent': '', 'className': 'shipper' },
  127. { 'title': 'Ready', 'data': 'date_ready', 'defaultContent': '', 'className': 'date_ready' },
  128. { 'title': 'Address&nbsp;1', 'data': 'address_1', 'address_1': '', 'className': 'address_1' },
  129. { 'title': 'Address&nbsp;2', 'data': 'address_2', 'address_2': '', 'className': 'address_2' },
  130. { 'title': 'City', 'data': 'city', 'defaultContent': '', 'className': 'city' },
  131. { 'title': 'State', 'data': 'state', 'defaultContent': '', 'className': 'state' },
  132. { 'title': 'Zip', 'data': 'zip', 'defaultContent': '', 'className': 'zip' },
  133. { 'title': 'Phone', 'data': 'phone', 'defaultContent': '', 'className': 'phone' },
  134. { 'title': 'Ready&nbsp;Time', 'data': 'time_ready', 'defaultContent': '', 'className': 'time_ready' },
  135. { 'title': 'Close&nbsp;Time', 'data': 'time_close', 'defaultContent': '', 'className': 'time_close' },
  136. { 'title': 'Dispatch&nbsp;Reference', 'data': 'dispatch_reference', 'defaultContent': '', 'className': 'dispatch_reference' },
  137. { 'title': 'Ctns', 'data': 'ctns', 'defaultContent': '', 'className': 'ctns' },
  138. { 'title': 'Weight', 'data': 'weight', 'defaultContent': '', 'className': 'weight' },
  139. { 'title': 'Cube', 'data': 'cube', 'defaultContent': '', 'className': 'cube' },
  140. { 'title': 'Pickup&nbsp;Time', 'data': 'time_pickup', 'defaultContent': '&nbsp;', 'className': 'time_pickup' },
  141. { 'title': 'Depart&nbsp;Time', 'data': 'time_depart', 'defaultContent': '&nbsp;', 'className': 'time_depart' },
  142. { 'title': 'BOL&nbsp;Delivered', 'data': 'bol_delivered', 'defaultContent': '&nbsp;', 'className': 'bol_delivered' }
  143. ],
  144. 'language':
  145. {
  146. 'search': '',
  147. 'searchPlaceholder': "Search"
  148. },
  149. 'deferRender': true,
  150. 'lengthChange': false,
  151. 'scrollX': true,
  152. 'fixedColumns':
  153. {
  154. leftColumns: 0,
  155. rightColumns: 3
  156. }
  157. });
  158. var editor = new $.fn.dataTable.Editor(
  159. {
  160. idSrc: 'id_file_row',
  161. table: table_dom,
  162. fields: [
  163. {
  164. label: "Pickup Time:",
  165. name: "time_pickup",
  166. type: "datetime",
  167. format: 'MM/DD/YYYY hh:mm A'
  168. },
  169. {
  170. label: "Depart Time:",
  171. name: "time_depart",
  172. type: "datetime",
  173. format: 'MM/DD/YYYY hh:mm A'
  174. },
  175. {
  176. label: "BOL Delivered:",
  177. name: "bol_delivered",
  178. type: "datetime",
  179. format: 'MM/DD/YYYY hh:mm A'
  180. }],
  181. ajax: function(method, url, d, success, error)
  182. {
  183. if(d['action'] === 'edit')
  184. {
  185. //just get the first row
  186. var id_file_row;
  187. for(id_file_row in d['data']);
  188. var row_partial = d['data'][id_file_row];
  189. //go thru all the properties on the object
  190. for(var property in row_partial)
  191. {
  192. var value = row_partial[property];
  193. //update our master record
  194. rows[id_file_row][property] = value;
  195. }
  196. //send the update up to the server thru api
  197. $.post('api/updateCell.php?id_file_row=' + id_file_row, row_partial)
  198. .done(function(response)
  199. {
  200. console.log(response);
  201. //report to the datatable a full updated row
  202. success({ "data": [ rows[id_file_row] ] });
  203. })
  204. .fail(function(err, xhr, text)
  205. {
  206. console.log(err['responseText'])
  207. //TODO: handle error
  208. });
  209. }
  210. }
  211. });
  212. //attach the editor
  213. $(table_dom).on('click', 'td.time_pickup', function(e) {
  214. editor.bubble(this, 'time_pickup');
  215. });
  216. $(table_dom).on('click', 'td.time_depart', function(e) {
  217. editor.bubble(this, 'time_depart');
  218. });
  219. $(table_dom).on('click', 'td.bol_delivered', function(e) {
  220. editor.bubble(this, 'bol_delivered');
  221. });
  222. /*
  223. $(table_dom).on('click', 'td.time_pickup, td.time_depart, td.bol_delivered', function(e) {
  224. editor.bubble(this, [ 'time_pickup', 'time_depart', 'bol_delivered' ]);
  225. });
  226. */
  227. return table;
  228. }