Restriction.js
68 lines
| 1 | function Restriction() { |
| 2 | /** |
| 3 | * @type {ReportingInterface} |
| 4 | */ |
| 5 | this.reporting = null; |
| 6 | /** |
| 7 | * You can override this property |
| 8 | * if you want to make this Restriction overridable |
| 9 | * |
| 10 | * @since 3.1.0 |
| 11 | * |
| 12 | * @type {string} |
| 13 | */ |
| 14 | this.type = ''; |
| 15 | } |
| 16 | |
| 17 | Restriction.prototype = { |
| 18 | /** |
| 19 | * @param node {HTMLElement|HTMLInputElement} |
| 20 | * @param reporting {ReportingInterface} |
| 21 | */ |
| 22 | isSupported: function ( node, reporting ) { |
| 23 | return true; |
| 24 | }, |
| 25 | /** |
| 26 | * @since 3.1.0 |
| 27 | * @returns {string} |
| 28 | */ |
| 29 | getType: function () { |
| 30 | return this.type; |
| 31 | }, |
| 32 | /** |
| 33 | * @param reporting {ReportingInterface} |
| 34 | */ |
| 35 | setReporting: function ( reporting ) { |
| 36 | this.reporting = reporting; |
| 37 | }, |
| 38 | getValue: function () { |
| 39 | return this.reporting.input.value.current; |
| 40 | }, |
| 41 | /** |
| 42 | * @returns {boolean} |
| 43 | */ |
| 44 | validate: function () { |
| 45 | throw new Error( 'validate is wrong' ); |
| 46 | }, |
| 47 | /** |
| 48 | * @return {Promise<*>} |
| 49 | */ |
| 50 | validatePromise: async function () { |
| 51 | let validationResult; |
| 52 | |
| 53 | try { |
| 54 | validationResult = await this.validate(); |
| 55 | } |
| 56 | catch ( error ) { |
| 57 | return Promise.reject( error?.message ?? error ); |
| 58 | } |
| 59 | |
| 60 | return validationResult |
| 61 | ? Promise.resolve() |
| 62 | : Promise.reject( 'validate is wrong' ); |
| 63 | }, |
| 64 | onReady() { |
| 65 | }, |
| 66 | }; |
| 67 | |
| 68 | export default Restriction; |