-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from getkirby/fiber/steps/notification
Notification module
- Loading branch information
Showing
24 changed files
with
654 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { isObject } from "@/helpers/object"; | ||
|
||
/** | ||
* Panel modules represent a particular part of state | ||
* for the panel. I.e. $system, $translation. | ||
* Features are built upon such state modules | ||
* | ||
* The inheritance cascade is: | ||
* Module -> Feature -> Island | ||
* | ||
* @param {Object} panel The panel singleton | ||
* @param {String} key Sets the $key for the module. Backend responses use this key. | ||
* @param {Object} defaults Sets the default state of the module | ||
*/ | ||
export default (key, defaults = {}) => { | ||
return { | ||
/** | ||
* Module defaults will be reactive and | ||
* must be present immediately in the object | ||
* to get reactivity out of the box. | ||
*/ | ||
...defaults, | ||
|
||
/** | ||
* The key is used to place the module | ||
* state in the right place within the global | ||
* panel state | ||
* | ||
* @returns {String} | ||
*/ | ||
key() { | ||
return key; | ||
}, | ||
|
||
/** | ||
* Returns all default values. | ||
* This will be used to restore the state | ||
* and fetch the existing state. | ||
* | ||
* @returns {Object} | ||
*/ | ||
defaults() { | ||
return defaults; | ||
}, | ||
|
||
/** | ||
* Restores the default state | ||
*/ | ||
reset() { | ||
return this.set(this.defaults()); | ||
}, | ||
|
||
/** | ||
* Sets a new state for the module | ||
* | ||
* @param {Object} state | ||
*/ | ||
set(state) { | ||
this.validateState(state); | ||
|
||
// merge the new state with the defaults | ||
// to always get a full object with all props | ||
for (const prop in this.defaults()) { | ||
this[prop] = state[prop] ?? this.defaults()[prop]; | ||
} | ||
|
||
return this.state(); | ||
}, | ||
|
||
/** | ||
* Returns the current state. The defaults | ||
* object is used to fetch all keys from the object | ||
* Keys which are not defined in the defaults | ||
* object will also not be in the final state | ||
* | ||
* @returns {Object} | ||
*/ | ||
state() { | ||
const state = {}; | ||
|
||
for (const prop in this.defaults()) { | ||
state[prop] = this[prop] ?? this.defaults()[prop]; | ||
} | ||
|
||
return state; | ||
}, | ||
|
||
/** | ||
* Validates the state object | ||
* | ||
* @param {Object} state | ||
* @returns {Boolean} | ||
*/ | ||
validateState(state) { | ||
if (isObject(state) === false) { | ||
throw new Error(`Invalid ${this.key()} state`); | ||
} | ||
|
||
return true; | ||
} | ||
}; | ||
}; |
Oops, something went wrong.