Advertisement
NikolayPaskulov

Untitled

Oct 29th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.12 KB | None | 0 0
  1.  
  2. var itemsList = {},
  3. clientsList = {},
  4. prevRow,
  5. prevCol = 0,
  6. tableHeader = document.getElementById('table-header'),
  7. docsWrapper = document.getElementById('docsWrapper'),
  8. mainWrapper = document.getElementById('main-wrapper'),
  9. firstDateInput = document.getElementById('firstDate'),
  10. clientSelect = document.getElementById('clientSelect'),
  11. storesSelect = document.getElementById('storesSelect'),
  12. secDateInput = document.getElementById('secDate'),
  13. checkBox = document.getElementById('checkBox'),
  14. clientsWithDocsBtn = document.getElementById('all-clients-with-docs'),
  15. table = document.getElementById('table'),
  16. storesList,
  17. motherShip = [],
  18. store = {};
  19.  
  20. var r1 = ['objects', 'ЛЕЙДИ СОФИЯ АД'],
  21. r2 = ['items', 'ЛЕЙДИ СОФИЯ АД', '{"warehouse":"ОБЕКТИ:10 НЕСТАНДАРТНА ПРОДУКЦИЯ"}'];
  22.  
  23. var storeIndex = 0;
  24. sendRequestToServer(JSON.stringify(r1), storesList);
  25. //sendRequestToServer('ГОТОВА ПРОДУКЦИЯ', 'store');
  26. function fillDocuments(store) {
  27. for (var i = 0; i < json.length; i++) {
  28. var len = Math.floor(Math.random() * 50) + 1;
  29. json[i].Items = [];
  30. for (var z = 0; z < len; z++) {
  31. var itemFromStore = Math.floor(Math.random() * store.length),
  32. itemCopy = {};
  33. for (var prop in store[itemFromStore]) {
  34. itemCopy[prop] = store[itemFromStore][prop];
  35. }
  36. itemCopy.quantity = Math.floor(Math.random() * 2) + 1;
  37. json[i].Items.push(itemCopy);
  38. }
  39. }
  40. }
  41.  
  42. function sendRequestToServer(arr, v, check) {
  43. var request = new XMLHttpRequest();
  44. request.onreadystatechange = function () {
  45. if (request.readyState == 4 && request.status == 200) {
  46. if (v == storesList) {
  47. storesList = request.responseText;
  48. storesList = JSON.parse(storesList);
  49. fillStoresSelect(storesList);
  50. }
  51. if (v == 'store') {
  52. v = request.responseText;
  53. v = v.replace(/""/g, '"');
  54. v = reformatString(v, 0, 0);
  55. v = JSON.parse(v);
  56. fillDocuments(v);
  57. changeStore(v);
  58. if (check) {
  59. roundAllStores(motherShip);
  60. }
  61. }
  62.  
  63. }
  64. }
  65. request.open("POST", "http://192.168.1.184:8000/pm", true);
  66. //request.open("POST", "http://127.0.0.1:8888/", true);
  67. request.send(arr);
  68. }
  69.  
  70. function reformatString(str, indexA, indexB) {
  71. var right = str.match(/{/g),
  72. left = str.match(/}/g),
  73. newStr,
  74. isEdited = false;
  75. if (right.length == left.length) {
  76. return str;
  77. }
  78.  
  79. if (str.lastIndexOf('{') > str.lastIndexOf('}')) {
  80. isEdited = true;
  81. newStr = str.slice(0, str.lastIndexOf(']')) + '}]';
  82. }
  83. if (str.lastIndexOf('}') == indexB) {
  84. return str
  85. }
  86.  
  87. var firstIndexOfRight = str.indexOf('{', indexA),
  88. firstIndexOfLeft = str.indexOf('}', firstIndexOfRight + 1),
  89. nextIndexOfRight = str.indexOf('{', firstIndexOfRight + 1);
  90.  
  91. if (nextIndexOfRight < firstIndexOfLeft) {
  92. if (str.lastIndexOf(':', nextIndexOfRight) < str.lastIndexOf(',', nextIndexOfRight)) {
  93. var lastIndexOfQ = str.lastIndexOf('"', nextIndexOfRight);
  94. newStr = str.slice(0, lastIndexOfQ + 1) + '}' + str.slice(lastIndexOfQ + 1);
  95. }
  96. } else if (!isEdited) {
  97. newStr = str;
  98. }
  99.  
  100. isEdited = false;
  101. return reformatString(newStr, firstIndexOfRight + 1, firstIndexOfLeft);
  102. }
  103.  
  104.  
  105. function fillStoresSelect(arr) {
  106. strArray = ['<option style="display:none;">Избери склад</option><option>Всички</option>'];
  107. for (var i = 0; i < arr.length; i++) {
  108. strArray.push("<option value='" + '{"warehouse":' + '"' + arr[i] + '"}' + "'>" + arr[i].slice(arr[i].lastIndexOf(':') + 1) + "</option>");
  109. }
  110. storesSelect.innerHTML += strArray.join('');
  111. }
  112.  
  113. storesSelect.onchange = function () {
  114. var selected = this.value;
  115. if (selected == 'Всички') {
  116. motherShip = [];
  117. var intervalCounter = 2;
  118. var selectOptions = this.getElementsByTagName('option');
  119. var interval = setInterval(intervalStart, 100);
  120. } else {
  121. sendRequestToServer(JSON.stringify(['items', 'ЛЕЙДИ СОФИЯ АД', selected]), 'store');
  122. }
  123.  
  124. function intervalStart() {
  125. console.log(selectOptions[intervalCounter].value);
  126. sendRequestToServer(JSON.stringify(['items', 'ЛЕЙДИ СОФИЯ АД', selectOptions[intervalCounter].value]), 'store');
  127. if (selectOptions.length - 2 == intervalCounter) {
  128. intervalEnd();
  129. }
  130.  
  131. intervalCounter++;
  132. }
  133. function intervalEnd() {
  134. sendRequestToServer(JSON.stringify(['items', 'ЛЕЙДИ СОФИЯ АД', selectOptions[intervalCounter].value]), 'store', true);
  135. clearInterval(interval);
  136. }
  137. }
  138.  
  139. function roundAllStores(arr) {
  140. store = arr[0];
  141. for (var i = 1; i < arr.length; i++) {
  142. for (var key in arr[i]) {
  143. if (store[key]) {
  144. if (arr[i][key].quantity) {
  145. store[key].quantity = Number(store[key].quantity) + Number(arr[i][key].quantity);
  146. } else {
  147. store[key].quantity = 0;
  148. }
  149.  
  150. } else {
  151. store[key] = {};
  152. if (arr[i][key].quantity) {
  153. store[key].quantity = Number(arr[i][key].quantity);
  154. } else {
  155. store[key].quantity = 0;
  156. }
  157. }
  158. }
  159. }
  160. }
  161.  
  162. //Extract all items and clients from the main JSON and put them into two associative arrays
  163. function generateRef(obj) {
  164. for (var a = 0; a < obj.length; a++) {
  165. var currentDate = obj[a].date;
  166. if (checkIfDateIsValid(currentDate.slice(6, currentDate.length), currentDate.slice(4, 6), currentDate.slice(0, 4))) {
  167. fillclientsList(obj[a].partner, obj[a]);
  168. for (var b = 0; b < obj[a].Items.length; b++) {
  169. checkItems(obj[a].partner, obj[a].Items[b]);
  170. }
  171. }
  172. }
  173. }
  174.  
  175. //Convert store JSON into an object
  176. function changeStore(currentStore) {
  177. store = {};
  178. for (var i = 0; i < currentStore.length; i++) {
  179. store[currentStore[i].name] = currentStore[i];
  180. }
  181. motherShip.push(store);
  182. }
  183.  
  184. //on every merchandise if array fill clients that have it with whole quantity
  185. function checkItems(client, march) {
  186.  
  187. var client = client.slice(client.lastIndexOf(':') + 1);
  188. if (itemsList[march.name]) {
  189. if (itemsList[march.name][client]) {
  190. itemsList[march.name][client] = Number(itemsList[march.name][client]) + Number(march.quantity);
  191. } else {
  192. itemsList[march.name][client] = Number(march.quantity);
  193. }
  194. } else if (march.name) {
  195. itemsList[march.name] = {};
  196. itemsList[march.name][client] = Number(march.quantity);
  197. }
  198. }
  199.  
  200. //all clients with theys documents and full info on document
  201. function fillclientsList(name, obj) {
  202. var Items = {},
  203. name = name.slice(name.lastIndexOf(':') + 1);
  204.  
  205. if (clientsList[name]) {
  206. if (clientsList[name][obj.docNumber]) {
  207. return;
  208. } else {
  209. clientsList[name][obj.docNumber] = {
  210. date: obj.date,
  211. };
  212. }
  213. } else {
  214. clientsList[name] = {};
  215. clientsList[name][obj.docNumber] = {
  216. date: obj.date,
  217. };
  218. }
  219.  
  220. for (var i = 0; i < obj.Items.length; i++) {
  221. if (Items[obj.Items[i].name]) {
  222. Items[obj.Items[i].name].quantity += Number(obj.Items[i].quantity);
  223. } else {
  224. Items[obj.Items[i].name] = {};
  225. Items[obj.Items[i].name].quantity = Number(obj.Items[i].quantity);
  226. }
  227. }
  228. clientsList[name][obj.docNumber]['Items'] = Items;
  229.  
  230. }
  231.  
  232. function buildTable(reUse) {
  233.  
  234. (function fillHeader() {
  235. var strArray = ['<div class="table-row"><div class="plain-cell">&nbsp;</div><div class="plain-cell">No</div>'],
  236. docArr = ['<div class="table-row"><div class="plain-cell">Док. номер</div><div class="plain-cell">&nbsp;</div>'],
  237. docDate = ['<div class="table-row"><div class="plain-cell">Док. дата</div><div class="plain-cell">&nbsp;</div>'];
  238. for (var key in clientsList) {
  239. if (reUse) {
  240. for (var prop in clientsList[key]) {
  241. var date = clientsList[key][prop].date;
  242. strArray.push('<div class="plain-cell no-print">' + key + '</div>')
  243. docArr.push('<div class="plain-cell">' + prop + '</div>');
  244. docDate.push('<div class="plain-cell">' + date.substr(6, 2) + '.' + date.substr(4, 2) + '.' + date.substr(0, 4) + '</div>');
  245. }
  246. }
  247. else {
  248. strArray.push('<div class="plain-cell no-print">' + key + '</div>');
  249. }
  250. }
  251. docArr.push('<div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div></div></div>')
  252. docDate.push('<div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div></div></div>')
  253. strArray.push('<div class="plain-cell">Общо планирани</div><div class="plain-cell">Количество от склад</div><div class="plain-cell">Блокирани</div><div class="plain-cell">Разлика</div></div>');
  254. if (reUse) {
  255. docsWrapper.innerHTML = docArr.join('') + docDate.join('');
  256. }
  257.  
  258. tableHeader.innerHTML = strArray.join('');
  259. resizeConteiners(tableHeader.children[0].children.length * 100 + 300);
  260. }());
  261.  
  262. (function fillRows() {
  263. var strArray = [],
  264. counter = 1,
  265. totalRowItems = 0;
  266.  
  267. for (var key in itemsList) {
  268. strArray.push('<div class="table-row"><div class="plain-cell">' + key + '</div><div class="plain-cell">' + counter + '</div>')
  269. for (var name in clientsList) {
  270. if (reUse) {
  271. var result = expandedRows(clientsList[name], key);
  272. strArray.push(result.html);
  273. totalRowItems += result.quantity;
  274. continue;
  275. }
  276. if (itemsList[key][name]) {
  277. strArray.push('<div class="plain-cell no-print">' + itemsList[key][name] + '</div>')
  278. totalRowItems += Number(itemsList[key][name]);
  279. } else {
  280. strArray.push('<div class="plain-cell no-print">&nbsp;</div>')
  281. }
  282. }
  283.  
  284. var storeQuantity = (typeof (store[key].quantity) == 'undefined') ? 0 : store[key].quantity,
  285. storeBlocked = (typeof (store[key].blocked) != 'undefined') ? store[key].blocked : 0;
  286.  
  287. quantitydifference = Number(storeQuantity) - storeBlocked - totalRowItems;
  288. strArray.push('<div class="plain-cell">' + totalRowItems + '</div><div class="plain-cell">' + storeQuantity + '</div>' +
  289. '<div class="plain-cell">' + storeBlocked + '</div>' +
  290. '<div class="plain-cell">' + quantitydifference + '</div></div>');
  291. counter++;
  292. totalRowItems = 0;
  293. }
  294.  
  295. if (reUse) {
  296. table.innerHTML = setSerialNumberRow(clientsList, true);
  297. }
  298. else {
  299. table.innerHTML = setSerialNumberRow(clientsList, false);
  300. }
  301. table.innerHTML += strArray.join('');
  302. printTotalQuantity();
  303. }());
  304. }
  305.  
  306. //Build row with serial number for each client or document
  307. function setSerialNumberRow(obj, isExpanded) {
  308. var strArr = [],
  309. prevProp,
  310. counter = 1;
  311.  
  312. strArr.push('<div class="table-row no-print"><div class="plain-cell">&nbsp;</div><div class="plain-cell">No</div>');
  313. for (var prop in obj) {
  314. strArr.push('<div class="plain-cell">' + counter + '</div>');
  315. counter++;
  316. if (isExpanded) {
  317. var countKeys = 0;
  318. for (var key in obj[prop]) {
  319. countKeys++;
  320. }
  321. for (var i = 0; i < countKeys - 1; i++) {
  322. strArr.push('<div class="plain-cell">&nbsp;</div>');
  323. }
  324. }
  325. prevProp = prop;
  326. }
  327. strArr.push('<div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div></div>');
  328. return strArr.join('');
  329. }
  330.  
  331. //expands basic table after click on chechbox
  332. function expandedRows(obj, itemName) {
  333. var strArray = [];
  334. totalRowItems = 0;
  335. for (var key in obj) {
  336. if (obj[key].Items[itemName]) {
  337. strArray.push('<div class="plain-cell no-print">' + obj[key].Items[itemName].quantity + '</div>');
  338. totalRowItems += Number(obj[key].Items[itemName].quantity);
  339. } else {
  340. strArray.push('<div class="plain-cell no-print">&nbsp;</div>');
  341. }
  342. }
  343. return {
  344. html: strArray.join(''),
  345. quantity: totalRowItems
  346. }
  347. }
  348.  
  349. //Visualize all documents from client chosen by click on the header
  350. tableHeader.onclick = function (e) {
  351.  
  352. var target = (typeof (event) != 'undefined') ? event.srcElement.outerText : e.target.innerHTML,
  353. checkBiggerWidth = 0;
  354. if (target == 'No' || target == 'Общо планирани' || target == 'Количество от склад' ||
  355. target == 'Блокирани' || target == 'Разлика' || target == '') {
  356. return;
  357. }
  358.  
  359. if (target == 'Всички') {
  360. clientsWithDocsBtn.disabled = false;
  361. clientsWithDocsBtn.checked = false;
  362. if (!checkBox.checked) {
  363. checkBox.click();
  364. }
  365. docsWrapper.style.display = 'none';
  366. buildTable();
  367. return;
  368. }
  369. docsWrapper.style.display = '';
  370. changeHeaderAfterClick(target);
  371. clientsWithDocsBtn.disabled = true;
  372. resizeConteiners(docsWrapper.childNodes[0].children.length * 100 + 300)
  373. };
  374.  
  375. //after click on some cell change color of a cell row and col
  376. table.onclick = function (ev) {
  377. var target = (typeof (event) != 'undefined') ? event.srcElement : ev.target;
  378. if (target.className == 'plain-cell no-print') {
  379. var rows = document.querySelectorAll('#table .table-row');
  380.  
  381. if (prevCol <= rows[0].children.length) {
  382. for (var z = 0; z < rows.length; z++) {
  383. rows[z].children[prevCol].style.backgroundColor = '';
  384. }
  385. }
  386. if (prevCol == Array.prototype.indexOf.call(target.parentNode.childNodes, target) && prevRow == target.parentNode) {
  387. prevRow.style.backgroundColor = '';
  388. prevCol = 0;
  389. return;
  390. }
  391. prevCol = Array.prototype.indexOf.call(target.parentNode.childNodes, target);
  392. for (var z = 0; z < rows.length; z++) {
  393. rows[z].children[prevCol].style.backgroundColor = 'lightBlue';
  394. }
  395.  
  396. if (prevRow) {
  397. prevRow.style.backgroundColor = '';
  398. }
  399. prevRow = target.parentNode;
  400. prevRow.style.backgroundColor = 'lightblue';
  401. target.style.backgroundColor = '#bde0eb';
  402. }
  403. };
  404.  
  405. //fill table with data for clicked client
  406. function fillTableAfterClick(targetName) {
  407. var strArray = [],
  408. invoiceNumber = [],
  409. invoiceDate = [],
  410. rowCounter = 1,
  411. iterationCounter = 0,
  412. countRowItems = 0;
  413.  
  414. invoiceNumber.push('<div class="table-row no-print"><div class="plain-cell">Док.ном</div><div class="plain-cell">&nbsp;</div>')
  415. invoiceDate.push('<div class="table-row no-print"><div class="plain-cell">Дата</div><div class="plain-cell">&nbsp;</div>')
  416.  
  417. var itemsArr = extractItemsFromClient(clientsList[targetName]);
  418. for (var name in itemsArr) {
  419. strArray.push('<div class="table-row"><div class="plain-cell">' + name + '</div><div class="plain-cell">' + rowCounter + '</div>');
  420. var clientKeys = 0;
  421. for (var keys in clientsList[targetName]) {
  422. clientKeys++;
  423. }
  424. for (var key in clientsList[targetName]) {
  425. if (iterationCounter < clientKeys) {
  426. var currentDate = clientsList[targetName][key].date;
  427. invoiceNumber.push('<div class="plain-cell">' + key + '</div>')
  428. invoiceDate.push('<div class="plain-cell">' + currentDate.slice(6, currentDate.length) + '.' + currentDate.slice(4, 6) + '.' + currentDate.slice(0, 4) + '</div>')
  429. }
  430. if (clientsList[targetName][key].Items[name]) {
  431. strArray.push('<div class="plain-cell no-print">' + clientsList[targetName][key].Items[name].quantity + '</div>');
  432. countRowItems += Number(clientsList[targetName][key].Items[name].quantity);
  433. } else {
  434. strArray.push('<div class="plain-cell no-print">&nbsp;</div>');
  435. }
  436. iterationCounter++;
  437. }
  438. var storeQuantity = (store[name].quantity != undefined) ? store[name].quantity : 0,
  439. quantitydifference = Number(storeQuantity) - 0 - countRowItems;
  440.  
  441. strArray.push('<div class="plain-cell">' + countRowItems + '</div><div class="plain-cell">' + storeQuantity + '</div>' +
  442. '<div class="plain-cell">' + 0 + '</div><div class="plain-cell">' + quantitydifference + '</div>');
  443. strArray.push('</div>');
  444. countRowItems = 0;
  445. rowCounter++;
  446. }
  447. invoiceNumber.push('<div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div></div>')
  448. invoiceDate.push('<div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div></div>')
  449. docsWrapper.innerHTML = invoiceNumber.join('') + invoiceDate.join('');
  450. table.innerHTML = setSerialNumberRow(clientsList[targetName], false) + strArray.join('');
  451. printTotalQuantity();
  452. }
  453.  
  454. //fill footer with calculated data from colums
  455. function printTotalQuantity() {
  456. var strArray = ['<div class="table-row"><div class="plain-cell">Общо</div><div class="plain-cell">&nbsp;</div>'],
  457. sum = 0;
  458. for (var i = 2; i < table.children[0].children.length; i++) {
  459. for (var z = 1; z < table.children.length - 1; z++) {
  460. sum += Number(table.children[z].children[i].innerHTML == '&nbsp;' ? '' : table.children[z].children[i].innerHTML);
  461. }
  462. if (i < table.children[0].children.length - 4) {
  463. strArray.push('<div class="plain-cell no-print">' + sum + '</div>')
  464. }
  465. else {
  466. strArray.push('<div class="plain-cell">' + sum + '</div>')
  467. }
  468. sum = 0;
  469. }
  470. strArray.push('</div>');
  471. table.innerHTML += strArray.join('');
  472. }
  473.  
  474. //extract only items that are in all clients
  475. function extractItemsFromClient(client) {
  476. var result = {};
  477. for (var doc in client) {
  478. for (var item in client[doc].Items) {
  479. if (!result[item]) result[item] = {};
  480. }
  481. }
  482. return result;
  483. }
  484.  
  485. document.getElementById('show-reference').onclick = checkIfDatesInputAreActive;
  486.  
  487.  
  488.  
  489. function fillSelectWithClients() {
  490. var strArray = [];
  491. if (firstDateInput.value && secDateInput.value) {
  492. strArray.push('<option>Всички</option>');
  493. itemsList = {};
  494. clientsList = {};
  495. generateRef(json);
  496.  
  497. for (var name in clientsList) {
  498. strArray.push('<option>' + name + '</option>');
  499. }
  500. }
  501. clientSelect.innerHTML = strArray.join('');
  502. }
  503.  
  504. //change header with only clicked client name and empty cells; call fillTableAfterClick with client name
  505. function changeHeaderAfterClick(name) {
  506. tableHeader.innerHTML = '<div class="table-row"><div class="plain-cell">&nbsp;</div><div class="plain-cell">No</div><div class="plain-cell no-print">' + name + '</div><div class="plain-cell no-print" id="empty-cell"></div><div class="plain-cell">Общо планирани</div>' +
  507. '<div class="plain-cell">Количество от склад</div><div class="plain-cell">Блокирани</div><div class="plain-cell">Разлика</div></div>';
  508. fillTableAfterClick(name);
  509. clientsWithDocsBtn.checked = true;
  510. clientsWithDocsBtn.disabled = true;
  511. tableHeader.childNodes[0].firstChild.innerHTML = 'Всички';
  512. }
  513.  
  514. function checkIfDatesInputAreActive() {
  515. var clientSelectValue = clientSelect.options[clientSelect.selectedIndex].value;
  516. if (firstDateInput.value && secDateInput.value) {
  517. checkBox.disabled = false;
  518. if (clientSelectValue != 'Всички') {
  519. docsWrapper.style.display = '';
  520. changeHeaderAfterClick(clientSelectValue);
  521. if (!checkBox.checked) {
  522. checkBox.click();
  523. }
  524. resizeConteiners(docsWrapper.children[0].children.length * 100 + 300)
  525. return;
  526. }
  527. clientsList = {};
  528. itemsList = {};
  529. generateRef(json);
  530.  
  531. if (!checkBox.checked) {
  532. checkBox.click();
  533. }
  534. if (clientsWithDocsBtn.disabled) {
  535. clientsWithDocsBtn.disabled = false;
  536. }
  537. if (clientsWithDocsBtn.checked) {
  538. clientsWithDocsBtn.checked = false;
  539. }
  540. docsWrapper.style.display = 'none';
  541. buildTable();
  542. }
  543. }
  544.  
  545. //find only dates that are in range
  546. function checkIfDateIsValid(day, month, year) {
  547. var firstDateInput = document.getElementById('firstDate'),
  548. secDateInput = document.getElementById('secDate'),
  549. regEx = /[0-9]+/g,
  550. firstDateArray,
  551. secDateArray;
  552.  
  553. firstDateArray = firstDateInput.value.match(regEx);
  554. secDateArray = secDateInput.value.match(regEx);
  555. if (firstDateArray && secDateArray) {
  556. var prevDate = new Date(firstDateArray[2], (firstDateArray[1] - 1), firstDateArray[0], 0, 0, 0, 0);
  557. var currentDate = new Date(secDateArray[2], (secDateArray[1] - 1), secDateArray[0], 0, 0, 0, 0);
  558.  
  559. if (prevDate > currentDate) {
  560. return;
  561. }
  562.  
  563. var productDate = new Date(year, (Number(month) - 1), Number(day), 0, 0, 0, 0);
  564. if (productDate.valueOf() >= prevDate.valueOf() && productDate.valueOf() <= currentDate.valueOf()) {
  565. return true;
  566. } else {
  567. return false;
  568. }
  569. }
  570. }
  571.  
  572. checkBox.onclick = function () {
  573. if (!this.checked) {
  574. document.head.innerHTML += '<style>.no-print{display:none}</style>';
  575. resizeConteiners(table.offsetWidth);
  576. docsWrapper.style.display = 'none';
  577. }
  578. else {
  579. document.head.removeChild(document.querySelectorAll('style')[0]);
  580. resizeConteiners(table.children[0].children.length * 100 + 300);
  581. if (clientsWithDocsBtn.checked) {
  582. docsWrapper.style.display = '';
  583. } else {
  584. docsWrapper.style.display = 'none';
  585. }
  586. }
  587. if (prevRow) {
  588. prevRow.style.backgroundColor = '';
  589. }
  590. };
  591.  
  592. clientsWithDocsBtn.onclick = function () {
  593. if (this.checked) {
  594. buildTable(true)
  595. docsWrapper.style.display = '';
  596. if (!checkBox.checked) checkBox.click();
  597. }
  598. else {
  599. buildTable();
  600. resizeConteiners(table.offsetWidth);
  601. docsWrapper.style.display = 'none';
  602. }
  603. };
  604.  
  605. function resizeConteiners(size) {
  606. docsWrapper.style.width = size + 'px';
  607. mainWrapper.style.width = size + 2 + 'px';
  608. tableHeader.childNodes[0].style.width = size + 'px';
  609.  
  610. if (docsWrapper.children.length == 0) {
  611. return;
  612. }
  613. if (!document.getElementById('empty-cell')) {
  614. return;
  615. }
  616. document.getElementById('empty-cell').style.width = (docsWrapper.offsetWidth - 1000) + 'px';
  617. if (docsWrapper.children[0].children.length <= 7) {
  618. document.getElementById('empty-cell').style.display = 'none'
  619. }
  620. }
  621.  
  622.  
  623. var datePickerBtns = document.querySelectorAll('.open-date-picker');
  624. for (var i = 0; i < datePickerBtns.length; i++) {
  625. datePickerBtns[i].onclick = generateDateForm;
  626. }
  627.  
  628. var prevDate;
  629. function generateDateForm(ev) {
  630. var datePicker = document.getElementById('date-picker');
  631.  
  632. //Set the main structure of the date-picker element;
  633. datePicker.innerHTML = '<div><button>&#9668;</button><span></span><button>&#9658;</button></div><span class="hor-line"></span><div id="date-picker-week"><span>Sun</span>' +
  634. '<span>Mon</span><span>Tue</span><span>Wed</span><span>Thu</span><span>Fri</span><span>Sat</span></div><span class="hor-line"></span><div id="date-picker-days"></div>';
  635.  
  636. var monthNames = ["January", "February", "March", "April", "May", "June",
  637. "July", "August", "September", "October", "November", "December"],
  638. month = datePicker.querySelectorAll('span')[0],
  639. currentMonth = (prevDate) ? prevDate.getMonth() : new Date().getMonth(),
  640. currentYear = (prevDate) ? prevDate.getFullYear() : new Date().getFullYear(),
  641. startOrEndDate = this.previousSibling,
  642. opacityVal = 0,
  643. hideCalendar;
  644.  
  645. function hideDatePicker() {
  646. if (opacityVal == 9) {
  647. datePicker.style.opacity = 0;
  648. clearInterval(hideCalendar);
  649. datePicker.style.display = 'none';
  650. opacityVal = 0;
  651. }
  652. else {
  653. datePicker.style.opacity = '0.' + (9 - opacityVal);
  654. opacityVal++;
  655. }
  656. }
  657.  
  658. if (datePicker.style.display == 'inline-block' && datePicker.style.left == Math.floor(this.offsetLeft - datePicker.offsetWidth / 2) + 'px') {
  659. hideCalendar = setInterval(hideDatePicker, 10);
  660. return;
  661. }
  662. datePicker.style.display = 'inline-block';
  663. datePicker.style.opacity = '0';
  664. var showCalendar = setInterval(function () {
  665. opacityVal++;
  666. if (opacityVal == 10) {
  667. datePicker.style.opacity = 1;
  668. clearInterval(showCalendar);
  669. opacityVal = 0;
  670. }
  671. else {
  672. datePicker.style.opacity = '0.' + opacityVal;
  673. }
  674. }, 10);
  675.  
  676. datePicker.style.left = Math.floor(this.offsetLeft - (datePicker.offsetWidth / 2)) + 'px';
  677. datePicker.style.top = (this.offsetTop + 23) + 'px';
  678. month.innerHTML = monthNames[currentMonth] + '&nbsp;-&nbsp;' + currentYear;
  679.  
  680. //Change months
  681. document.querySelectorAll('#date-picker button')[0].onclick = function () {
  682. if (currentMonth > 0) {
  683. currentMonth -= 1;
  684. }
  685. else {
  686. currentMonth = 11;
  687. currentYear -= 1;
  688. }
  689. month.innerHTML = monthNames[currentMonth] + '&nbsp;-&nbsp;' + currentYear;
  690. generateDays(currentMonth, currentYear);
  691. }
  692.  
  693. document.querySelectorAll('#date-picker button')[1].onclick = function () {
  694. if (currentMonth < 11) {
  695. currentMonth += 1;
  696. }
  697. else {
  698. currentMonth = 0;
  699. currentYear += 1;
  700. }
  701. month.innerHTML = monthNames[currentMonth] + '&nbsp;-&nbsp;' + currentYear;
  702. generateDays(currentMonth, currentYear);
  703. }
  704.  
  705. //Click on day to pick date
  706. datePicker.onclick = function (ev) {
  707.  
  708. var target = (typeof (event) != 'undefined') ? event.srcElement : ev.target,
  709. inputMonth = (Number(currentMonth) + 1 > 9) ? Number(currentMonth) + 1 : 0 + '' + (Number(currentMonth) + 1),
  710. inputDay = (target.innerHTML > 9) ? target.innerHTML : 0 + '' + target.innerHTML;
  711.  
  712. if (target.parentNode.id == 'date-picker-days' && target.className != 'inactive-day') {
  713. startOrEndDate.value = inputDay + '-' + inputMonth + '-' + currentYear;
  714. datePicker.style.display = 'none';
  715. prevDate = new Date(currentYear, currentMonth);
  716. }
  717. }
  718.  
  719. //Add days from current month, previous month (if current month starts from week day different from Sunday)
  720. //and next month (if there is empty space at the end of the last row with days)
  721. function generateDays(chosenMonth, chosenYear) {
  722. var activeMonth = new Date(chosenYear, chosenMonth),
  723. currentMonthDays = getDaysInMonth(chosenMonth, chosenYear).length,
  724. prevMonthDays = getDaysInMonth(chosenMonth - 1, chosenYear).length,
  725. wrapper = [],
  726. nextMonthDays = 0;
  727.  
  728. for (var z = 0; z < activeMonth.getDay() ; z++) {
  729. wrapper.push('<span class="inactive-day">' + ((prevMonthDays + 1) - (activeMonth.getDay() - z)) + '</span>');
  730. nextMonthDays++;
  731. }
  732. for (var i = 1; i <= currentMonthDays; i++) {
  733. wrapper.push('<span class="active-day">' + i + '</span>');
  734. nextMonthDays++;
  735. }
  736. lastDaysLength = (nextMonthDays <= 35) ? 35 : 42;
  737. lastDaysLength = ((nextMonthDays + activeMonth.getDay()) == 28) ? 28 : lastDaysLength;
  738. for (var h = 1; h <= lastDaysLength - nextMonthDays; h++) {
  739. wrapper.push('<span class="inactive-day">' + h + '</span>');
  740. }
  741. document.getElementById('date-picker-days').innerHTML = wrapper.join('');
  742. }
  743. generateDays(currentMonth, currentYear);
  744.  
  745. //Get the number of days in month
  746. function getDaysInMonth(month, year) {
  747. var month = month;
  748. if (month < 0) {
  749. month = 11;
  750. year -= year;
  751. }
  752. var date = new Date(year, month, 1),
  753. days = [];
  754. while (date.getMonth() === month) {
  755. days.push(new Date(date));
  756. date.setDate(date.getDate() + 1);
  757. }
  758. return days;
  759. }
  760.  
  761. document.onclick = function (ev) {
  762. var target = (typeof (event) != 'undefined') ? event.srcElement : ev.target;
  763. while (target) {
  764. if (target.id == 'date-picker' || target.className == 'open-date-picker') {
  765. return;
  766. };
  767. target = target.parentNode
  768. }
  769. hideCalendar = setInterval(hideDatePicker, 10);
  770. }
  771. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement