2015-12-27 02:02:17 +03:00
|
|
|
'use strict';
|
2016-05-06 12:46:41 +03:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { isFunction, isString } from '@angular/core/src/facade/lang';
|
|
|
|
import { BrowserDomAdapter } from '@angular/platform-browser/src/browser/browser_adapter';
|
|
|
|
import { global } from '@angular/core/src/facade/lang';
|
2015-12-27 02:02:17 +03:00
|
|
|
|
2016-05-06 12:46:41 +03:00
|
|
|
const defaults = {
|
2016-02-10 16:48:07 +03:00
|
|
|
scrollYOffset: 0,
|
2016-02-11 14:38:44 +03:00
|
|
|
disableLazySchemas: false,
|
2016-05-25 18:34:31 +03:00
|
|
|
debugMode: false//global && global.redocDebugMode
|
2015-12-27 02:02:17 +03:00
|
|
|
};
|
2015-12-30 02:29:29 +03:00
|
|
|
|
2016-05-06 12:46:41 +03:00
|
|
|
const OPTION_NAMES = new Set(['scrollYOffset', 'disableLazySchemas', 'specUrl']);
|
2016-02-11 14:38:44 +03:00
|
|
|
|
2016-05-06 12:46:41 +03:00
|
|
|
@Injectable()
|
|
|
|
export class OptionsService {
|
2016-05-25 18:34:31 +03:00
|
|
|
private _options: any;
|
|
|
|
|
|
|
|
constructor(private dom:BrowserDomAdapter) {
|
2016-02-10 16:48:07 +03:00
|
|
|
this._options = defaults;
|
2016-05-06 12:46:41 +03:00
|
|
|
this.dom = dom;
|
2016-01-24 22:27:15 +03:00
|
|
|
}
|
|
|
|
|
2015-12-30 02:29:29 +03:00
|
|
|
get options() {
|
|
|
|
return this._options;
|
|
|
|
}
|
|
|
|
|
|
|
|
set options(opts) {
|
2016-02-10 16:48:07 +03:00
|
|
|
this._options = Object.assign(this._options, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
parseOptions(el) {
|
|
|
|
let parsedOpts;
|
|
|
|
let attributesMap = this.dom.attributeMap(el);
|
|
|
|
parsedOpts = {};
|
|
|
|
Array.from(attributesMap.keys())
|
|
|
|
//camelCasify
|
|
|
|
.map(k => ({
|
|
|
|
attrName: k,
|
|
|
|
name: k.replace(/-(.)/g, (m, $1) => $1.toUpperCase())
|
|
|
|
})
|
|
|
|
)
|
2016-02-11 14:38:44 +03:00
|
|
|
.filter(option => OPTION_NAMES.has(option.name))
|
2016-02-10 16:48:07 +03:00
|
|
|
.forEach(option => {
|
|
|
|
parsedOpts[option.name] = attributesMap.get(option.attrName);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.options = parsedOpts;
|
|
|
|
this._normalizeOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
_normalizeOptions() {
|
|
|
|
// modify scrollYOffset to always be a function
|
|
|
|
if (!isFunction(this._options.scrollYOffset)) {
|
|
|
|
if (isFinite(this._options.scrollYOffset)) {
|
|
|
|
// if number specified create function that returns this value
|
|
|
|
let numberOffset = parseFloat(this._options.scrollYOffset);
|
|
|
|
this.options.scrollYOffset = () => numberOffset;
|
|
|
|
} else {
|
|
|
|
// if selector or node function that returns bottom offset of this node
|
|
|
|
let el = this._options.scrollYOffset;
|
|
|
|
if (!(el instanceof Node)) {
|
|
|
|
el = this.dom.query(el);
|
|
|
|
}
|
|
|
|
if (!el) {
|
|
|
|
this._options.scrollYOffset = () => 0;
|
|
|
|
} else {
|
|
|
|
this._options.scrollYOffset = () => el.offsetTop + el.offsetHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isString(this._options.disableLazySchemas)) this._options.disableLazySchemas = true;
|
2015-12-30 02:29:29 +03:00
|
|
|
}
|
|
|
|
}
|