input_ctrl.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import json
  2. import logging
  3. from .framework import WebIOFuture, Global
  4. from tornado.log import gen_log
  5. logger = logging.getLogger(__name__)
  6. def run_async(coro):
  7. Global.active_ws.inactive_coro_instances.append(coro)
  8. def send_msg(cmd, spec=None):
  9. msg = dict(command=cmd, spec=spec, coro_id=Global.active_coro_id)
  10. Global.active_ws.write_message(json.dumps(msg))
  11. async def next_event():
  12. res = await WebIOFuture()
  13. return res
  14. async def single_input(item_spec, valid_func, preprocess_func):
  15. """
  16. Note: 鲁棒性在上层完成
  17. 将单个input构造成input_group,并获取返回值
  18. :param item_spec: 单个输入项的参数 'name' must in item_spec, 参数一定已经验证通过
  19. :param valid_func: Not None
  20. :param preprocess_func: Not None
  21. """
  22. label = item_spec['label']
  23. name = item_spec['name']
  24. # todo 是否可以原地修改spec
  25. item_spec['label'] = ''
  26. item_spec.setdefault('autofocus', True) # 如果没有设置autofocus参数,则开启参数 todo CHECKBOX, RADIO 特殊处理
  27. spec = dict(label=label, inputs=[item_spec])
  28. data = await input_control(spec, {name: preprocess_func}, {name: valid_func})
  29. return data[name]
  30. async def input_control(spec, preprocess_funcs, item_valid_funcs, form_valid_funcs=None):
  31. """
  32. 发送input命令,监听事件,验证输入项,返回结果
  33. :param spec:
  34. :param preprocess_funcs: keys 严格等于 spec中的name集合
  35. :param item_valid_funcs: keys 严格等于 spec中的name集合
  36. :param form_valid_funcs:
  37. :return:
  38. """
  39. send_msg('input_group', spec)
  40. data = await input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_funcs)
  41. send_msg('destroy_form')
  42. return data
  43. def check_item(name, data, valid_func, preprocess_func):
  44. data = preprocess_func(data)
  45. error_msg = valid_func(data)
  46. if error_msg is not None:
  47. send_msg('update_input', dict(target_name=name, attributes={
  48. 'valid_status': False,
  49. 'invalid_feedback': error_msg
  50. }))
  51. return False
  52. return True
  53. async def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_funcs):
  54. """
  55. 根据提供的校验函数处理表单事件
  56. :param valid_funcs: map(name -> valid_func) valid_func 为 None 时,不进行验证
  57. valid_func: callback(data) -> error_msg
  58. :param whole_valid_func: callback(data) -> (name, error_msg)
  59. :param inputs_args:
  60. :return:
  61. """
  62. while True:
  63. event = await next_event()
  64. event_name, event_data = event['event'], event['data']
  65. if event_name == 'input_event':
  66. input_event = event_data['event_name']
  67. if input_event == 'blur':
  68. onblur_name = event_data['name']
  69. check_item(onblur_name, event_data['value'], item_valid_funcs[onblur_name],
  70. preprocess_funcs[onblur_name])
  71. elif event_name == 'from_submit':
  72. all_valid = True
  73. # 调用输入项验证函数进行校验
  74. for name, valid_func in item_valid_funcs.items():
  75. if not check_item(name, event_data[name], valid_func, preprocess_funcs[name]):
  76. all_valid = False
  77. # 调用表单验证函数进行校验
  78. if form_valid_funcs:
  79. data = {name: preprocess_funcs[name](val) for name, val in event_data.items()}
  80. v_res = form_valid_funcs(data)
  81. if v_res is not None:
  82. all_valid = False
  83. onblur_name, error_msg = v_res
  84. send_msg('update_input', dict(target_name=onblur_name, attributes={
  85. 'valid_status': False,
  86. 'invalid_feedback': error_msg
  87. }))
  88. if all_valid:
  89. break
  90. else:
  91. gen_log.warning("Unhandled Event: %s", event)
  92. return event['data']