()
| 36 | } |
| 37 | |
| 38 | create_element(): JQuery { |
| 39 | let spec = deep_copy(this.spec); |
| 40 | const id_name = spec.name + '-' + Math.floor(Math.random() * Math.floor(9999)); |
| 41 | spec['id_name'] = id_name; |
| 42 | if (spec['type'] == 'float') spec['type'] = 'text'; |
| 43 | |
| 44 | let html = Mustache.render(common_input_tpl, spec); |
| 45 | |
| 46 | this.element = $(html); |
| 47 | |
| 48 | this.element.find(`#${id_name}_action_btn`).on('click', function (e) { |
| 49 | let btn = $(this); |
| 50 | state.CurrentSession.send_message({ |
| 51 | event: "callback", |
| 52 | task_id: btn.data('callbackid') as string, |
| 53 | data: null |
| 54 | }); |
| 55 | }); |
| 56 | |
| 57 | let input_elem = this.element.find('input'); |
| 58 | if (spec.onblur) { |
| 59 | // blur事件时,发送当前值到服务器 |
| 60 | input_elem.on("blur", (e) => { |
| 61 | if (this.get_value()) |
| 62 | this.on_input_event("blur", this); |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | input_elem.on("input", (e) => { |
| 67 | this.rectify_input() |
| 68 | if (spec.onchange) { |
| 69 | this.on_input_event("change", this); |
| 70 | } |
| 71 | }); |
| 72 | |
| 73 | // 将额外的html参数加到input标签上 |
| 74 | const ignore_keys = make_set(['action', 'type', 'label', 'invalid_feedback', 'valid_feedback', 'help_text', |
| 75 | 'options', 'datalist', 'multiple', 'onchange', 'onblur']); |
| 76 | for (let key in this.spec) { |
| 77 | if (key in ignore_keys) continue; |
| 78 | input_elem.attr(key, this.spec[key]); |
| 79 | } |
| 80 | |
| 81 | return this.element; |
| 82 | } |
| 83 | |
| 84 | rectify_input() { |
| 85 | let val = '' + this.element.find('input').val() as string; |
no test coverage detected