routes.js 7.2 KB

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