mirror of
https://github.com/flarum/core.git
synced 2025-07-28 04:00:40 +02:00
Massive JavaScript cleanup
- Use JSX for templates - Docblock/comment everything - Mostly passes ESLint (still some work to do) - Lots of renaming, refactoring, etc. CSS hasn't been updated yet.
This commit is contained in:
75
js/lib/extend.js
Normal file
75
js/lib/extend.js
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Extend an object's method by running its output through a mutating callback
|
||||
* every time it is called.
|
||||
*
|
||||
* The callback accepts the method's return value and should perform any
|
||||
* mutations directly on this value. For this reason, this function will not be
|
||||
* effective on methods which return scalar values (numbers, strings, booleans).
|
||||
*
|
||||
* Care should be taken to extend the correct object – in most cases, a class'
|
||||
* prototype will be the desired target of extension, not the class itself.
|
||||
*
|
||||
* @example
|
||||
* extend(Discussion.prototype, 'badges', function(badges) {
|
||||
* // do something with `badges`
|
||||
* });
|
||||
*
|
||||
* @param {Object} object The object that owns the method
|
||||
* @param {String} method The name of the method to extend
|
||||
* @param {function} callback A callback which mutates the method's output
|
||||
*/
|
||||
export function extend(object, method, callback) {
|
||||
const original = object[method];
|
||||
|
||||
object[method] = function(...args) {
|
||||
const value = original.apply(this, args);
|
||||
|
||||
callback.apply(this, [value].concat(args));
|
||||
|
||||
return value;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Override an object's method by replacing it with a new function, so that the
|
||||
* new function will be run every time the object's method is called.
|
||||
*
|
||||
* The replacement function accepts the original method as its first argument,
|
||||
* which is like a call to 'super'. Any arguments passed to the original method
|
||||
* are also passed to the replacement.
|
||||
*
|
||||
* Care should be taken to extend the correct object – in most cases, a class'
|
||||
* prototype will be the desired target of extension, not the class itself.
|
||||
*
|
||||
* @example
|
||||
* override(Discussion.prototype, 'badges', function(original) {
|
||||
* const badges = original();
|
||||
* // do something with badges
|
||||
* return badges;
|
||||
* });
|
||||
*
|
||||
* @param {Object} object The object that owns the method
|
||||
* @param {String} method The name of the method to override
|
||||
* @param {function} newMethod The method to replace it with
|
||||
*/
|
||||
export function override(object, method, newMethod) {
|
||||
const original = object[method];
|
||||
|
||||
object[method] = function(...args) {
|
||||
return newMethod.apply(this, [original.bind(this)].concat(args));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a notification type.
|
||||
*
|
||||
* @param {String} name The name of the notification type (equivalent to the
|
||||
* serialized `contentType`)
|
||||
* @param {Object} Component The constructor of the component that this
|
||||
* notification type should be rendered with
|
||||
* @param {String|Array} label vDOM to render a label in the notification
|
||||
* preferences grid for this notification type
|
||||
*/
|
||||
export function notificationType(name, Component, label) {
|
||||
// TODO
|
||||
}
|
Reference in New Issue
Block a user