checkbox_radio.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import {Session} from "../../session";
  2. import {InputItem} from "./base";
  3. import {deep_copy} from "../../utils"
  4. const checkbox_radio_tpl = `
  5. <div class="form-group">
  6. {{#label}}<label>{{label}}</label>{{/label}}
  7. {{#inline}}<br>{{/inline}}
  8. {{#options}}
  9. <div class="form-check {{#inline}}form-check-inline{{/inline}}">
  10. <input type="{{type}}" id="{{id_name_prefix}}-{{idx}}" name="{{name}}" {{#selected}}checked{{/selected}} {{#disabled}}disabled{{/disabled}} class="form-check-input">
  11. <label class="form-check-label" for="{{id_name_prefix}}-{{idx}}">
  12. {{label}}
  13. </label>
  14. <div class="invalid-feedback">{{invalid_feedback}}</div> <!-- input 添加 is-invalid 类 -->
  15. <div class="valid-feedback">{{valid_feedback}}</div> <!-- input 添加 is-valid 类 -->
  16. </div>
  17. {{/options}}
  18. <small id="{{id_name}}_help" class="form-text text-muted">{{help_text}}</small>
  19. </div>`;
  20. export class CheckboxRadio extends InputItem {
  21. static accept_input_types: string[] = ["checkbox", "radio"];
  22. constructor(session: Session, task_id: string, spec: any) {
  23. super(session, task_id, spec);
  24. }
  25. create_element(): JQuery {
  26. let spec = deep_copy(this.spec);
  27. const id_name_prefix = spec.name + '-' + Math.floor(Math.random() * Math.floor(9999));
  28. spec['id_name_prefix'] = id_name_prefix;
  29. for (let idx in spec.options) {
  30. spec.options[idx]['idx'] = idx;
  31. }
  32. const html = Mustache.render(checkbox_radio_tpl, spec);
  33. let elem = $(html);
  34. this.element = elem;
  35. let inputs = elem.find('input');
  36. for (let idx = 0; idx < spec.options.length; idx++)
  37. inputs.eq(idx).val(JSON.stringify(spec.options[idx].value));
  38. const ignore_keys = {'value': '', 'label': '', 'selected': ''};
  39. for (let idx = 0; idx < this.spec.options.length; idx++) {
  40. let input_elem = elem.find('#' + id_name_prefix + '-' + idx);
  41. // blur事件时,发送当前值到服务器
  42. // checkbox_radio 不产生blur事件
  43. // input_elem.on('blur', this.send_value_listener);
  44. // 将额外的html参数加到input标签上
  45. for (let key in this.spec.options[idx]) {
  46. if (key in ignore_keys) continue;
  47. input_elem.attr(key, this.spec.options[idx][key]);
  48. }
  49. }
  50. return this.element;
  51. }
  52. update_input(spec: any): any {
  53. let attributes = spec.attributes;
  54. let idx = -1;
  55. if ('target_value' in spec) {
  56. this.element.find('input').each(function (index) {
  57. if ($(this).val() === spec.target_value) {
  58. idx = index;
  59. return false;
  60. }
  61. });
  62. }
  63. this.update_input_helper(idx, attributes);
  64. }
  65. get_value(): any {
  66. if (this.spec.type === 'radio') {
  67. let raw_val = this.element.find('input:checked').val() || 'null';
  68. return JSON.parse(raw_val as string);
  69. } else {
  70. let value_arr = this.element.find('input').serializeArray();
  71. let res: any[] = [];
  72. let that = this;
  73. $.each(value_arr, function (idx, val) {
  74. if (val.name === that.spec.name)
  75. res.push(JSON.parse(val.value as string));
  76. console.log(JSON.parse(val.value as string), typeof JSON.parse(val.value as string));
  77. });
  78. return res;
  79. }
  80. }
  81. }