|
1 | | -import Component from "@ember/component" |
2 | | -import { get, set, computed } from "@ember/object" |
3 | | -import { htmlSafe } from "@ember/string" |
4 | | -import { run } from "@ember/runloop" |
| 1 | +import Component from '@ember/component' |
| 2 | +import { set, computed } from '@ember/object' |
| 3 | +import { htmlSafe } from '@ember/string' |
| 4 | +import { run } from '@ember/runloop' |
| 5 | +import { tryInvoke } from '@ember/utils' |
5 | 6 |
|
6 | | -import layout from "../templates/components/ember-inline-edit" |
| 7 | +import layout from '../templates/components/ember-inline-edit' |
7 | 8 |
|
8 | 9 | export default Component.extend({ |
9 | 10 | layout, |
10 | | - classNames: ["ember-inline-edit"], |
11 | | - classNameBindings: ["isEditing:is-editing", "enabled::disabled"], |
| 11 | + classNames: ['ember-inline-edit'], |
| 12 | + classNameBindings: ['isEditing:is-editing', 'enabled::disabled'], |
12 | 13 |
|
13 | 14 | isEditing: false, |
14 | | - isNotEditing: computed.not("isEditing"), |
| 15 | + isNotEditing: computed.not('isEditing'), |
15 | 16 |
|
16 | 17 | enabled: true, |
17 | | - field: "text", |
| 18 | + field: 'text', |
18 | 19 |
|
19 | 20 | value: null, |
20 | 21 | previousValue: null, |
21 | 22 |
|
22 | | - placeholder: "Not Provided", |
23 | | - saveLabel: "Save", |
24 | | - cancelLabel: "Cancel", |
25 | | - editLabel: "Edit", |
| 23 | + placeholder: 'Not Provided', |
| 24 | + saveLabel: 'Save', |
| 25 | + cancelLabel: 'Cancel', |
| 26 | + editLabel: 'Edit', |
26 | 27 |
|
27 | 28 | fieldWidth: null, |
28 | 29 |
|
29 | 30 | showSaveButton: true, |
30 | 31 | showCancelButton: true, |
31 | 32 |
|
32 | | - editorClass: "", |
33 | | - buttonContainerClass: "", |
34 | | - editButtonClass: "", |
35 | | - saveButtonClass: "", |
36 | | - cancelButtonClass: "", |
| 33 | + editorClass: '', |
| 34 | + buttonContainerClass: '', |
| 35 | + editButtonClass: '', |
| 36 | + saveButtonClass: '', |
| 37 | + cancelButtonClass: '', |
37 | 38 |
|
38 | 39 | didInsertElement() { |
| 40 | + this._super(...arguments) |
| 41 | + |
39 | 42 | this._handleClicks = this._handleClicks.bind(this) |
40 | | - document.addEventListener("click", this._handleClicks) |
| 43 | + document.addEventListener('click', this._handleClicks) |
41 | 44 | }, |
42 | 45 |
|
43 | 46 | willDestroyElement() { |
44 | | - document.removeEventListener("click", this._handleClicks) |
| 47 | + document.removeEventListener('click', this._handleClicks) |
45 | 48 | }, |
46 | 49 |
|
47 | 50 | _handleClicks(ev) { |
48 | | - let enabled = get(this, "enabled") |
49 | | - if (!enabled) return |
| 51 | + if (!this.enabled) return |
50 | 52 |
|
51 | | - let isEditing = get(this, "isEditing") |
| 53 | + let { isEditing } = this |
52 | 54 | let clickedInside = this.element.contains(ev.target) |
53 | 55 |
|
54 | 56 | if (clickedInside && !isEditing) { |
55 | | - if (get(this, "showEditButton")) { |
| 57 | + if (this.showEditButton) { |
56 | 58 | return |
57 | 59 | } |
58 | 60 |
|
59 | 61 | this._setFieldWidth() |
60 | | - this.send("startEditing", ev) |
| 62 | + this.send('startEditing', ev) |
61 | 63 | } else if (!clickedInside && isEditing) { |
62 | | - this.send("cancel") |
| 64 | + this.send('cancel') |
63 | 65 | } |
64 | 66 | }, |
65 | 67 |
|
66 | 68 | _setFieldWidth() { |
67 | 69 | const { width } = this.element.getBoundingClientRect() |
68 | 70 |
|
69 | 71 | run(this, () => { |
70 | | - set(this, "fieldWidth", htmlSafe(`width: ${width + 2}px`)) |
| 72 | + set(this, 'fieldWidth', htmlSafe(`width: ${width + 2}px`)) |
71 | 73 | }) |
72 | 74 | }, |
73 | 75 |
|
74 | 76 | didReceiveAttrs() { |
75 | | - if (get(this, "enabled") === false) { |
76 | | - this.send("cancel") |
| 77 | + if (this.enabled === false) { |
| 78 | + this.send('cancel') |
77 | 79 | } |
78 | 80 | }, |
79 | 81 |
|
80 | 82 | actions: { |
81 | 83 | save() { |
82 | | - this.sendAction("onSave", this.get("value")) |
| 84 | + tryInvoke(this, 'onSave', [this.value]) |
83 | 85 |
|
84 | 86 | run(this, () => { |
85 | | - set(this, "isEditing", false) |
| 87 | + set(this, 'isEditing', false) |
86 | 88 | }) |
87 | 89 | }, |
88 | 90 |
|
89 | 91 | startEditing(e) { |
90 | 92 | e.stopPropagation() |
91 | | - this.sendAction("onEdit") |
| 93 | + tryInvoke(this, 'onEdit') |
92 | 94 |
|
93 | 95 | run(this, () => { |
94 | | - set(this, "previousValue", this.get("value")) |
95 | | - set(this, "isEditing", true) |
| 96 | + set(this, 'previousValue', this.value) |
| 97 | + set(this, 'isEditing', true) |
96 | 98 | }) |
97 | 99 | }, |
98 | 100 |
|
99 | 101 | cancel() { |
100 | | - this.sendAction("onCancel") |
| 102 | + tryInvoke(this, 'onCancel') |
101 | 103 |
|
102 | 104 | run(this, () => { |
103 | | - set(this, "value", this.get("previousValue")) |
104 | | - set(this, "isEditing", false) |
| 105 | + set(this, 'value', this.previousValue) |
| 106 | + set(this, 'isEditing', false) |
105 | 107 | }) |
106 | 108 | } |
107 | 109 | } |
|
0 commit comments