doctor-schedule.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /// <reference path="../../node_modules/moment/moment.d.ts"/>
  2. /// <reference path="../../node_modules/@types/jquery/index.d.ts"/>
  3. // import moment = require("moment");
  4. var DoctorSchedule;
  5. (function (DoctorSchedule_1) {
  6. var recalculateDate = function (dateSpec, action) {
  7. dateSpec = parseDate(dateSpec);
  8. dateSpec.push((new Date).getFullYear());
  9. dateSpec = dateSpec.reverse();
  10. dateSpec[1]--;
  11. var momentDate = moment(dateSpec);
  12. momentDate[action](7, 'days');
  13. return momentDate.format('DD.MM');
  14. };
  15. var parseDate = function (string) {
  16. return string.split('.').map(function (data) {
  17. return parseInt(data);
  18. });
  19. };
  20. var getDateColumn = function (row) {
  21. return row.find('td:first-child');
  22. };
  23. var nextDate = function (prev, next) {
  24. getRows(next).each(function (index) {
  25. var date = getDateColumn($(this)).html();
  26. getDateColumn($(this)).html(recalculateDate(date, 'add'));
  27. });
  28. setButtonsText(prev, next, 'add');
  29. };
  30. var prevDate = function (prev, next) {
  31. getRows(next).each(function (index) {
  32. var date = getDateColumn($(this)).html();
  33. getDateColumn($(this)).html(recalculateDate(date, 'subtract'));
  34. });
  35. setButtonsText(prev, next, 'subtract');
  36. };
  37. var setButtonsText = function (prev, next, action) {
  38. setButtonDate(prev, action);
  39. setButtonDate(next, action);
  40. };
  41. var setButtonDate = function (button, action) {
  42. var dates = button.html().trim().split(' – ');
  43. console.log(dates);
  44. button.html(recalculateDate(dates[0], action) + ' &ndash; ' + recalculateDate(dates[1], action));
  45. };
  46. var getRows = function (element) {
  47. return element.parent().find('tr');
  48. };
  49. var DoctorSchedule = (function () {
  50. function DoctorSchedule(prevBtn, nextBtn) {
  51. var next = nextDate.bind(this);
  52. var prev = prevDate.bind(this);
  53. prevBtn.on('click', function (e) {
  54. e.preventDefault();
  55. prev(prevBtn, nextBtn);
  56. });
  57. nextBtn.on('click', function (e) {
  58. e.preventDefault();
  59. next(prevBtn, nextBtn);
  60. });
  61. }
  62. return DoctorSchedule;
  63. }());
  64. function init($prevBtn, $nextBtn) {
  65. return new DoctorSchedule($prevBtn, $nextBtn);
  66. }
  67. DoctorSchedule_1.init = init;
  68. })(DoctorSchedule || (DoctorSchedule = {}));