parking.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. var DAYS = [
  2. ["Monday", "MON"],
  3. ["Tuesday", "TUE"],
  4. ["Wednesday", "WED"],
  5. ["Thursday", "THU"],
  6. ["Friday", "FRI"],
  7. ["Saturday", "SAT"],
  8. ["Sunday", "SUN"]
  9. ]
  10. var DAYS_MAP = {
  11. 'mon': 'parking.Day("MON")',
  12. 'tue': 'parking.Day("TUE")',
  13. 'wed': 'parking.Day("WED")',
  14. 'thu': 'parking.Day("THU")',
  15. 'fri': 'parking.Day("FRI")',
  16. 'sat': 'parking.Day("SAT")',
  17. 'sun': 'parking.Day("SUN")',
  18. 'tod': 'parking.today()'
  19. };
  20. Blockly.Blocks['datetime_day'] = {
  21. /**
  22. * Block for datetime day.
  23. * @this Blockly.Block
  24. */
  25. init: function() {
  26. this.jsonInit({
  27. "message0": "%1",
  28. "args0": [
  29. {
  30. "type": "field_dropdown",
  31. "name": "DAY",
  32. "options": [["Today", "TODAY"]].concat(DAYS)
  33. }
  34. ],
  35. "output": "DatetimeDay",
  36. "colour": DATA_HUE,
  37. "tooltip": "Returns a day of the week",
  38. "helpUrl": ""
  39. });
  40. }
  41. };
  42. var HOURS = [["1", "1"], ["2", "2"], ["3", "3"],
  43. ["4", "4"], ["5", "5"], ["6", "6"], ["7", "7"],
  44. ["8", "8"], ["9", "9"], ["10", "10"], ["11", "11"],
  45. ["12", "12"]];
  46. var MINUTES = [["00", "00"], ["30", "30"]];
  47. var MERIDIANS = [["am", "AM"], ["pm", "PM"]];
  48. Blockly.Blocks['datetime_time'] = {
  49. /**
  50. * Block for datetime day.
  51. * @this Blockly.Block
  52. */
  53. init: function() {
  54. this.setColour(DATA_HUE);
  55. var dropdown = new Blockly.FieldDropdown([["Now", "NOW"]].concat(HOURS), function(opt) {
  56. var isNow = (opt == 'NOW');
  57. this.sourceBlock_.updateShape_(isNow);
  58. });
  59. this.appendDummyInput()
  60. .appendField(dropdown, 'HOUR');
  61. this.setInputsInline(true);
  62. this.setOutput(true, 'DatetimeTime');
  63. this.setTooltip("Returns a time of day");
  64. },
  65. mutationToDom: function() {
  66. var container = document.createElement('mutation');
  67. var isNow = (this.getFieldValue('HOUR').toUpperCase() == 'NOW');
  68. container.setAttribute('isnow', isNow);
  69. return container;
  70. },
  71. domToMutation: function(xmlElement) {
  72. var isNow = (xmlElement.getAttribute('isnow') == 'true');
  73. this.updateShape_(isNow);
  74. },
  75. updateShape_: function(isNow) {
  76. // Add or remove a Value Input.
  77. var inputExists = this.getInput('EXTENDED');
  78. if (!isNow) {
  79. if (!inputExists) {
  80. var minuteMenu = new Blockly.FieldDropdown(MINUTES);
  81. var meridianMenu = new Blockly.FieldDropdown(MERIDIANS);
  82. this.appendDummyInput('EXTENDED')
  83. .appendField(':')
  84. .appendField(minuteMenu, 'MINUTE')
  85. .appendField(meridianMenu, 'MERIDIAN');
  86. }
  87. } else if (inputExists) {
  88. this.removeInput('EXTENDED');
  89. }
  90. }
  91. };
  92. var HOURS_MAP = {
  93. '1': 'parking.one()',
  94. '2': 'parking.two()',
  95. '3': 'parking.three()',
  96. '4': 'parking.four()',
  97. '5': 'parking.five()',
  98. '6': 'parking.six()',
  99. '7': 'parking.seven()',
  100. '8': 'parking.eight()',
  101. '9': 'parking.nine()',
  102. '10': 'parking.ten()',
  103. '11': 'parking.eleven()',
  104. '12': 'parking.twelve()',
  105. 'NOW': 'parking.now()'
  106. };
  107. var MINUTES_MAP = {
  108. '00': 'parking.exactly()',
  109. '30': 'parking.half()'
  110. }
  111. var MERIDIANS_MAP = {
  112. 'AM': 'parking.am()',
  113. 'PM': 'parking.pm()'
  114. }
  115. var convertDate = function(date) {
  116. date = date.slice(0, 3).toLowerCase();
  117. switch (date) {
  118. case "mon": return "Monday";
  119. case "tue": return "Tuesday";
  120. case "wed": return "Wednesday";
  121. case "thu": return "Thursday";
  122. case "fri": return "Friday";
  123. case "sat": return "Saturday";
  124. case "sun": return "Sunday";
  125. default: return date;
  126. }
  127. }
  128. var convertMinute = function(minute) {
  129. if (minute < 10) {
  130. return "0"+minute;
  131. } else {
  132. return ""+minute;
  133. }
  134. }
  135. var equalityOperators = [
  136. ["==", "IS"],
  137. ["<", "BEFORE"],
  138. [">", "AFTER"],
  139. ["<=", "BEFORE_EQUAL"],
  140. ["=>", "AFTER_EQUAL"],
  141. ["!=", "IS_NOT"]
  142. ];
  143. var equalityOperatorsConversions = {
  144. "IS": "parking.equal",
  145. "BEFORE": "parking.before",
  146. "AFTER": "parking.after",
  147. "BEFORE_EQUAL": "parking.before_equal",
  148. "AFTER_EQUAL": "parking.after_equal",
  149. "IS_NOT": "parking.not_equal",
  150. }
  151. // FINISH _time and _day
  152. // Add in numbers and days to KNOWN_MODULES
  153. Blockly.Blocks['datetime_check_day'] = {
  154. /**
  155. * Block for testing if something contains something.
  156. * @this Blockly.Block
  157. */
  158. init: function() {
  159. this.setColour(Blockly.Blocks.logic.HUE);
  160. this.setOutput(true, 'Boolean');
  161. this.appendValueInput('LEFT')
  162. .setCheck('DatetimeDay')
  163. .appendField(new Blockly.FieldDropdown(DAYS), 'VALUE')
  164. .appendField(new Blockly.FieldDropdown(equalityOperators), 'OP');
  165. this.setInputsInline(false);
  166. }
  167. };
  168. Blockly.Blocks['datetime_check_time'] = {
  169. /**
  170. * Block for testing if something contains something.
  171. * @this Blockly.Block
  172. */
  173. init: function() {
  174. this.setColour(Blockly.Blocks.logic.HUE);
  175. this.setOutput(true, 'Boolean');
  176. this.appendValueInput('LEFT')
  177. .setCheck('DatetimeTime')
  178. .appendField(new Blockly.FieldDropdown(HOURS), 'HOURS')
  179. .appendField(':')
  180. .appendField(new Blockly.FieldDropdown(MINUTES), 'MINUTES')
  181. .appendField(new Blockly.FieldDropdown(MERIDIANS), 'MERIDIANS')
  182. .appendField(new Blockly.FieldDropdown(equalityOperators), 'OP');
  183. //this.setInputsInline(true);
  184. }
  185. };