-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(params): Add uiOnParamsChanged controller callback
- Loading branch information
1 parent
1541b90
commit 961c96d
Showing
7 changed files
with
407 additions
and
331 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,37 @@ | ||
/** @module params */ /** for typedoc */ | ||
import {forEach, ancestors, extend, copy, pick, omit} from "../common/common"; | ||
import {extend, ancestors} from "../common/common"; | ||
|
||
export class StateParams { | ||
constructor(params: Object = {}) { | ||
extend(this, params); | ||
} | ||
|
||
$digest() {} | ||
$inherit(newParams, $current, $to) {} | ||
$set(params, url) {} | ||
$sync() {} | ||
$off() {} | ||
$raw() {} | ||
$localize(state, params) {} | ||
$observe(key: string, fn: Function) {} | ||
} | ||
|
||
|
||
export function stateParamsFactory() { | ||
let observers = {}, current = {}; | ||
|
||
function unhook(key, func) { | ||
return () => { | ||
forEach(key.split(" "), k => observers[k].splice(observers[k].indexOf(func), 1)); | ||
}; | ||
} | ||
|
||
function observeChange(key, val?: any) { | ||
if (!observers[key] || !observers[key].length) return; | ||
forEach(observers[key], func => func(val)); | ||
} | ||
|
||
|
||
StateParams.prototype.$digest = function() { | ||
forEach(this, (val, key) => { | ||
if (val === current[key] || !this.hasOwnProperty(key)) return; | ||
current[key] = val; | ||
observeChange(key, val); | ||
}); | ||
}; | ||
|
||
/** | ||
* Merges a set of parameters with all parameters inherited between the common parents of the | ||
* current state and a given destination state. | ||
* | ||
* @param {Object} newParams The set of parameters which will be composited with inherited params. | ||
* @param {Object} $current Internal definition of object representing the current state. | ||
* @param {Object} $to Internal definition of object representing state to transition to. | ||
*/ | ||
StateParams.prototype.$inherit = function(newParams, $current, $to) { | ||
let parents = ancestors($current, $to), parentParams, inherited = {}, inheritList = []; | ||
|
||
for (let i in parents) { | ||
if (!parents[i] || !parents[i].params) continue; | ||
parentParams = Object.keys(parents[i].params); | ||
if (!parentParams.length) continue; | ||
|
||
for (let j in parentParams) { | ||
if (inheritList.indexOf(parentParams[j]) >= 0) continue; | ||
inheritList.push(parentParams[j]); | ||
inherited[parentParams[j]] = this[parentParams[j]]; | ||
} | ||
} | ||
return extend({}, inherited, newParams); | ||
}; | ||
|
||
StateParams.prototype.$set = function(params, url) { | ||
let hasChanged = false, abort = false; | ||
|
||
if (url) { | ||
forEach(params, function(val, key) { | ||
if ((url.parameter(key) || {}).dynamic !== true) abort = true; | ||
}); | ||
/** | ||
* Merges a set of parameters with all parameters inherited between the common parents of the | ||
* current state and a given destination state. | ||
* | ||
* @param {Object} newParams The set of parameters which will be composited with inherited params. | ||
* @param {Object} $current Internal definition of object representing the current state. | ||
* @param {Object} $to Internal definition of object representing state to transition to. | ||
*/ | ||
$inherit(newParams, $current, $to) { | ||
let parents = ancestors($current, $to), parentParams, inherited = {}, inheritList = []; | ||
|
||
for (let i in parents) { | ||
if (!parents[i] || !parents[i].params) continue; | ||
parentParams = Object.keys(parents[i].params); | ||
if (!parentParams.length) continue; | ||
|
||
for (let j in parentParams) { | ||
if (inheritList.indexOf(parentParams[j]) >= 0) continue; | ||
inheritList.push(parentParams[j]); | ||
inherited[parentParams[j]] = this[parentParams[j]]; | ||
} | ||
if (abort) return false; | ||
|
||
forEach(params, (val, key) => { | ||
if (val !== this[key]) { | ||
this[key] = val; | ||
observeChange(key); | ||
hasChanged = true; | ||
} | ||
}); | ||
|
||
this.$sync(); | ||
return hasChanged; | ||
}; | ||
|
||
StateParams.prototype.$sync = function() { | ||
copy(this, current); | ||
return this; | ||
}; | ||
|
||
StateParams.prototype.$off = function() { | ||
observers = {}; | ||
return this; | ||
}; | ||
|
||
StateParams.prototype.$raw = function() { | ||
return omit( | ||
this, | ||
Object.keys(this).filter(StateParams.prototype.hasOwnProperty.bind(StateParams.prototype)) | ||
); | ||
}; | ||
|
||
StateParams.prototype.$localize = function(state, params) { | ||
return new StateParams(pick(params || this, Object.keys(state.params))); | ||
}; | ||
|
||
StateParams.prototype.$observe = function(key: string, fn: Function) { | ||
forEach(key.split(" "), k => (observers[k] || (observers[k] = [])).push(fn)); | ||
return unhook(key, fn); | ||
}; | ||
} | ||
return extend({}, inherited, newParams); | ||
}; | ||
} | ||
|
||
return new StateParams(); | ||
} | ||
export function stateParamsFactory() { | ||
return new StateParams(); | ||
} |
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
Oops, something went wrong.