|
| 1 | +/* Copyright 2020 Mozilla Foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +class Event { |
| 17 | + constructor(data) { |
| 18 | + this.change = data.change || ""; |
| 19 | + this.changeEx = data.changeEx || null; |
| 20 | + this.commitKey = data.commitKey || 0; |
| 21 | + this.fieldFull = data.fieldFull || false; |
| 22 | + this.keyDown = data.keyDown || false; |
| 23 | + this.modifier = data.modifier || false; |
| 24 | + this.name = data.name; |
| 25 | + this.rc = true; |
| 26 | + this.richChange = data.richChange || []; |
| 27 | + this.richChangeEx = data.richChangeEx || []; |
| 28 | + this.richValue = data.richValue || []; |
| 29 | + this.selEnd = data.selEnd || 0; |
| 30 | + this.selStart = data.selStart || 0; |
| 31 | + this.shift = data.shift || false; |
| 32 | + this.source = data.source || null; |
| 33 | + this.target = data.target || null; |
| 34 | + this.targetName = data.targetName || ""; |
| 35 | + this.type = "Field"; |
| 36 | + this.value = data.value || null; |
| 37 | + this.willCommit = data.willCommit || false; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +class EventDispatcher { |
| 42 | + constructor(document, calculationOrder, objects) { |
| 43 | + this._document = document; |
| 44 | + this._calculationOrder = calculationOrder; |
| 45 | + this._objects = objects; |
| 46 | + |
| 47 | + this._document.obj._eventDispatcher = this; |
| 48 | + } |
| 49 | + |
| 50 | + dispatch(baseEvent) { |
| 51 | + const id = baseEvent.id; |
| 52 | + if (!(id in this._objects)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const name = baseEvent.name.replace(" ", ""); |
| 57 | + const source = this._objects[id]; |
| 58 | + const event = (this._document.obj._event = new Event(baseEvent)); |
| 59 | + const oldValue = source.obj.value; |
| 60 | + |
| 61 | + this.runActions(source, source, event, name); |
| 62 | + if (event.rc && oldValue !== event.value) { |
| 63 | + source.wrapped.value = event.value; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + runActions(source, target, event, eventName) { |
| 68 | + event.source = source.wrapped; |
| 69 | + event.target = target.wrapped; |
| 70 | + event.name = eventName; |
| 71 | + event.rc = true; |
| 72 | + if (!target.obj._runActions(event)) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + return event.rc; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +export { Event, EventDispatcher }; |
0 commit comments