checkbox_radio.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {Session} from "../../session";
  2. import {InputItem} from "./base";
  3. import {deep_copy} from "../../utils"
  4. const options_tpl = `
  5. {{#options}}
  6. <div class="form-check {{#inline}}form-check-inline{{/inline}}">
  7. <input type="{{type}}" id="{{id_name_prefix}}-{{idx}}" name="{{name}}" {{#selected}}checked{{/selected}} {{#disabled}}disabled{{/disabled}} class="form-check-input">
  8. <label class="form-check-label" for="{{id_name_prefix}}-{{idx}}">
  9. {{label}}
  10. </label>
  11. </div>
  12. {{/options}}
  13. `;
  14. const checkbox_radio_tpl = `
  15. <div class="form-group">
  16. {{#label}}<label>{{label}}</label>{{/label}}
  17. {{#inline}}<br>{{/inline}}
  18. ${options_tpl}
  19. <div class="invalid-feedback">{{invalid_feedback}}</div>
  20. <div class="valid-feedback">{{valid_feedback}}</div>
  21. <small id="{{id_name}}_help" class="form-text text-muted">{{help_text}}</small>
  22. </div>`;
  23. export class CheckboxRadio extends InputItem {
  24. static accept_input_types: string[] = ["checkbox", "radio"];
  25. constructor(session: Session, task_id: string, spec: any) {
  26. super(session, task_id, spec);
  27. }
  28. create_element(): JQuery {
  29. let spec = this.setup_spec();
  30. const html = Mustache.render(checkbox_radio_tpl, spec);
  31. let elem = $(html);
  32. this.setup_input_options(elem, spec.options);
  33. this.element = elem;
  34. return this.element;
  35. }
  36. setup_spec() {
  37. let spec = deep_copy(this.spec);
  38. const id_name_prefix = spec.name + '-' + Math.floor(Math.random() * Math.floor(9999));
  39. spec['id_name_prefix'] = id_name_prefix;
  40. for (let idx in spec.options) {
  41. spec.options[idx]['idx'] = idx;
  42. }
  43. return spec;
  44. }
  45. setup_input_options(elem: JQuery, options: any) {
  46. let inputs = elem.find('input');
  47. for (let idx = 0; idx < options.length; idx++) {
  48. let input_elem = inputs.eq(idx);
  49. if(this.spec.onblur) {
  50. input_elem.on("blur", (e) => {
  51. this.send_value_listener(this, e);
  52. });
  53. }
  54. if(this.spec.onchange){
  55. input_elem.on("change", (e) => {
  56. this.send_value_listener(this, e);
  57. });
  58. }
  59. input_elem.val(JSON.stringify(options[idx].value));
  60. // 将额外的html参数加到input标签上
  61. for (let key in options[idx]) {
  62. if (key in {'value': '', 'label': '', 'selected': ''}) continue;
  63. input_elem.attr(key, options[idx][key]);
  64. }
  65. }
  66. }
  67. update_input(spec: any): any {
  68. let attributes = spec.attributes;
  69. let idx = -1;
  70. if ('target_value' in spec) {
  71. this.element.find('input').each(function (index) {
  72. if (JSON.parse($(this).val() as string) === spec.target_value) {
  73. idx = index;
  74. return false;
  75. }
  76. });
  77. }
  78. if ('options' in attributes) {
  79. this.spec.options = attributes.options;
  80. let spec = this.setup_spec();
  81. const html = Mustache.render(options_tpl, spec);
  82. this.element.find('.form-check').remove();
  83. this.element.find('.invalid-feedback').before(html);
  84. this.setup_input_options(this.element, spec.options);
  85. delete attributes['options'];
  86. }
  87. if ('valid_status' in attributes) {
  88. this.element.find('.invalid-feedback,.valid-feedback').hide();
  89. if (attributes.valid_status === true)
  90. this.element.find('.valid-feedback').show();
  91. else if (attributes.valid_status === false)
  92. this.element.find('.invalid-feedback').show();
  93. }
  94. this.update_input_helper(idx, attributes);
  95. }
  96. get_value(): any {
  97. if (this.spec.type === 'radio') {
  98. let raw_val = this.element.find('input:checked').val() || 'null';
  99. return JSON.parse(raw_val as string);
  100. } else {
  101. let value_arr = this.element.find('input').serializeArray();
  102. let res: any[] = [];
  103. let that = this;
  104. $.each(value_arr, function (idx, val) {
  105. if (val.name === that.spec.name)
  106. res.push(JSON.parse(val.value as string));
  107. console.log(JSON.parse(val.value as string), typeof JSON.parse(val.value as string));
  108. });
  109. return res;
  110. }
  111. }
  112. }