parking.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. goog.provide('Blockly.Python.parking');
  3. goog.require('Blockly.Python');
  4. PythonToBlocks.KNOWN_MODULES['parking'] = {
  5. "today": ["datetime_day", ["DAY", "TODAY"]],
  6. "day_compare": ["datetime_check_day", "OP",
  7. {"type": "variable", "mode": "value", "name": "LEFT"},
  8. {"type": "mapper", "name": "VALUE", "method": convertDate}],
  9. "Day": ["datetime_day", {"type": "mapper", "name": "DAY", "method": convertDate}],
  10. "now": ["datetime_time", ["HOUR", "NOW"]],
  11. "Time": ["datetime_time", {"type": "integer", "name": "HOUR", "add_mutation": {"name": "@isnow", "value": "false"}},
  12. {"type": "integer_mapper", "name": "MINUTE", "method": convertMinute},
  13. "MERIDIAN"],
  14. "time_compare": ["datetime_check_time", "OP",
  15. {"type": "variable", "mode": "value", "name": "LEFT"},
  16. {"type": "integer", "name": "HOURS"},
  17. {"type": "integer_mapper", "name": "MINUTES", "method": convertMinute},
  18. "MERIDIANS"]
  19. };
  20. Blockly.Python['datetime_day'] = function(block) {
  21. Blockly.Python.definitions_['import_parking'] = 'import parking';
  22. var operator = DAYS_MAP[block.getFieldValue('DAY').slice(0, 3).toLowerCase()];
  23. return [operator, Blockly.Python.ORDER_ATOMIC];
  24. };
  25. Blockly.Python['datetime_check_time'] = function(block) {
  26. Blockly.Python.definitions_['import_parking'] = 'import parking';
  27. var hour = parseInt(block.getFieldValue('HOURS'));
  28. var minute = parseInt(block.getFieldValue('MINUTES'));
  29. var meridian = Blockly.Python.quote_(block.getFieldValue('MERIDIANS'));
  30. var operator = Blockly.Python.quote_(block.getFieldValue('OP'));
  31. var left = Blockly.Python.valueToCode(block, 'LEFT', Blockly.Python.ORDER_ATOMIC)
  32. var code = "parking.time_compare(" + operator+", "+left + ',' + hour + ',' + minute + ',' +meridian + ")";
  33. return [code, Blockly.Python.ORDER_ATOMIC];
  34. };
  35. Blockly.Python['datetime_check_day'] = function(block) {
  36. Blockly.Python.definitions_['import_parking'] = 'import parking';
  37. var value = Blockly.Python.quote_(block.getFieldValue('VALUE'));
  38. var operator = Blockly.Python.quote_(block.getFieldValue('OP'));
  39. var left = Blockly.Python.valueToCode(block, 'LEFT', Blockly.Python.ORDER_ATOMIC) || "___";
  40. var code = "parking.day_compare(" + operator + ", " + left + ', ' + value + ")";
  41. return [code, Blockly.Python.ORDER_ATOMIC];
  42. };
  43. Blockly.Python['datetime_time'] = function(block) {
  44. Blockly.Python.definitions_['import_parking'] = 'import parking';
  45. var hour = block.getFieldValue('HOUR');
  46. var code;
  47. if (hour == "NOW") {
  48. code = "parking.now()";
  49. } else {
  50. var minute = parseInt(block.getFieldValue('MINUTE'));
  51. var meridian = Blockly.Python.quote_(block.getFieldValue('MERIDIAN'));
  52. code = 'parking.Time('+hour+','+minute+','+meridian+')';
  53. }
  54. return [code, Blockly.Python.ORDER_ATOMIC];
  55. };