Merge branch 'MDL-71834-master-v02' of git://github.com/ferranrecio/moodle

This commit is contained in:
Andrew Nicols 2021-10-11 14:06:23 +08:00
commit 551371c710
33 changed files with 274 additions and 25 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -431,4 +431,39 @@ export default class {
component.reactive = this.reactive;
this.reactive.registerComponent(component);
}
/**
* Set the lock value and locks or unlocks the element.
*
* @param {boolean} locked the new locked value
*/
set locked(locked) {
this.element.dataset.locked = locked ?? false;
if (locked) {
// Disable interactions.
this.element.style.pointerEvents = 'none';
this.element.style.userSelect = 'none';
// Check if it is draggable.
if (this.element.hasAttribute('draggable')) {
this.element.setAttribute('draggable', false);
}
} else {
// Reanable interactions.
this.element.style.pointerEvents = null;
this.element.style.userSelect = null;
// Check if it was draggable.
if (this.element.hasAttribute('draggable')) {
this.element.setAttribute('draggable', true);
}
}
}
/**
* Get the current locket value from the element.
*
* @return {boolean}
*/
get locked() {
return this.element.dataset.locked ?? false;
}
}

View file

@ -151,6 +151,11 @@ export default class {
* @param {Object} newFunctions an object with new mutation functions.
*/
addMutations(newFunctions) {
// Mutations can provide an init method to do some setup in the statemanager.
if (newFunctions.init !== undefined) {
newFunctions.init(this.stateManager);
}
// Save all mutations.
for (const [mutation, mutationFunction] of Object.entries(newFunctions)) {
this.mutations[mutation] = mutationFunction.bind(newFunctions);
}
@ -166,6 +171,10 @@ export default class {
*/
setMutations(manager) {
this.mutations = manager;
// Mutations can provide an init method to do some setup in the statemanager.
if (manager.init !== undefined) {
manager.init(this.stateManager);
}
}
/**

View file

@ -107,6 +107,7 @@ export default class StateManager {
"delete": this.defaultDelete.bind(this),
"put": this.defaultPut.bind(this),
"override": this.defaultOverride.bind(this),
"prepareFields": this.defaultPrepareFields.bind(this),
};
// The state_loaded event is special because it only happens one but all components
@ -258,9 +259,29 @@ export default class StateManager {
throw Error(`Unkown update action ${action}`);
}
method(this, updateName, fields);
// Some state data may require some cooking before sending to the
// state. Reactive instances can overrdide the default fieldDefaults
// method to add extra logic to all updates.
const prepareFields = updateTypes.prepareFields ?? this.updateTypes.prepareFields;
method(this, updateName, prepareFields(this, updateName, fields));
}
/**
* Prepare fields for processing.
*
* This method is used to add default values or calculations from the frontend side.
*
* @param {Object} stateManager the state manager
* @param {String} updateName the state element to update
* @param {Object} fields the new data
* @returns {Object} final fields data
*/
defaultPrepareFields(stateManager, updateName, fields) {
return fields;
}
/**
* Process a create state message.
*