BaseHtmlAttr.js
2 years ago
FileExtensionHtmlAttr.js
2 years ago
MaxFileSizeHtmlAttr.js
2 years ago
MaxFilesHtmlAttr.js
2 years ago
RemainingCalcAttr.js
2 years ago
BaseHtmlAttr.js
77 lines
| 1 | import ReactiveVar from '../reactive/ReactiveVar'; |
| 2 | |
| 3 | function BaseHtmlAttr() { |
| 4 | this.attrName = ''; |
| 5 | this.initial = null; |
| 6 | this.isFromData = false; |
| 7 | this.value = null; |
| 8 | } |
| 9 | |
| 10 | BaseHtmlAttr.prototype = { |
| 11 | /** |
| 12 | * Name of data attribute |
| 13 | */ |
| 14 | attrName: '', |
| 15 | /** |
| 16 | * @type {InputData} |
| 17 | */ |
| 18 | input: null, |
| 19 | initial: null, |
| 20 | /** |
| 21 | * @type {ReactiveVar} |
| 22 | */ |
| 23 | value: null, |
| 24 | observe() { |
| 25 | this.value = new ReactiveVar( this.initial ); |
| 26 | this.value.make(); |
| 27 | |
| 28 | this.addWatcherAttr(); |
| 29 | }, |
| 30 | nodeSignal() { |
| 31 | const [ node ] = this.input.nodes; |
| 32 | |
| 33 | node[ this.attrName ] = this.value.current; |
| 34 | }, |
| 35 | addWatcherAttr() { |
| 36 | this.value.watch( () => this.nodeSignal() ); |
| 37 | }, |
| 38 | /** |
| 39 | * If you need specific check, |
| 40 | * you can rewrite this function |
| 41 | * |
| 42 | * @param input {InputData} |
| 43 | * @return {boolean} |
| 44 | */ |
| 45 | isSupported( input ) { |
| 46 | const [ node ] = input.nodes; |
| 47 | |
| 48 | const hasInRoot = -1 !== node[ this.attrName ] ?? -1; |
| 49 | const hasInDataSet = node.dataset.hasOwnProperty( this.attrName ); |
| 50 | |
| 51 | if ( !hasInDataSet && !hasInRoot ) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | this.initial = this.getInitial( input ); |
| 56 | |
| 57 | return Boolean( this.initial ); |
| 58 | }, |
| 59 | /** |
| 60 | * @param input {InputData} |
| 61 | * @return {*|boolean} |
| 62 | */ |
| 63 | getInitial( input ) { |
| 64 | const [ node ] = input.nodes; |
| 65 | |
| 66 | return node.dataset[ this.attrName ] || node[ this.attrName ] || false; |
| 67 | }, |
| 68 | |
| 69 | /** |
| 70 | * @param input {InputData} |
| 71 | */ |
| 72 | setInput( input ) { |
| 73 | this.input = input; |
| 74 | }, |
| 75 | }; |
| 76 | |
| 77 | export default BaseHtmlAttr; |