mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
feature(polyfill) add scroll-into-view-if-needed polyfill from npm
This commit is contained in:
parent
e12359fdaa
commit
1ce28fd500
3 changed files with 8 additions and 135 deletions
|
|
@ -185,14 +185,15 @@ function CloudCmdProto(Util, DOM) {
|
|||
exec.with(CloudCmd.route, location.hash),
|
||||
], noop);
|
||||
|
||||
const addPrefix = (a) => `${prefix}${a}`;
|
||||
const funcBefore = (callback) => {
|
||||
const src = prefix + '/join:' + [
|
||||
const srcs = [
|
||||
CloudCmd.DIRCLIENT_MODULES + 'polyfill.js',
|
||||
'/modules/domtokenlist-shim/dist/domtokenlist.min.js',
|
||||
].join(':');
|
||||
].map(addPrefix);
|
||||
|
||||
DOM.loadJquery(() => {
|
||||
DOM.load.js(src, callback);
|
||||
DOM.load.parallel(srcs, callback);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,136 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
/* global Util, DOM, $ */
|
||||
/* global DOM */
|
||||
|
||||
const itype = require('itype/legacy');
|
||||
|
||||
if (!window.XMLHttpRequest || !document.head)
|
||||
DOM.load.ajax = $.ajax;
|
||||
|
||||
if (!Array.isArray)
|
||||
Array.isArray = itype.array.bind();
|
||||
|
||||
/* function polyfill webkit standart function
|
||||
* https://gist.github.com/2581101
|
||||
*/
|
||||
DOM.scrollIntoViewIfNeeded = function(element, centerIfNeeded) {
|
||||
let parent,
|
||||
topWidth,
|
||||
leftWidth,
|
||||
parentComputedStyle,
|
||||
parentBorderTopWidth,
|
||||
parentBorderLeftWidth,
|
||||
overTop,
|
||||
overBottom,
|
||||
overLeft,
|
||||
overRight,
|
||||
alignWithTop;
|
||||
|
||||
if (window.getComputedStyle) {
|
||||
if (arguments.length === 1)
|
||||
centerIfNeeded = false;
|
||||
|
||||
parent = element.parentNode;
|
||||
parentComputedStyle = window.getComputedStyle(parent, null);
|
||||
|
||||
topWidth = parentComputedStyle.getPropertyValue('border-top-width');
|
||||
leftWidth = parentComputedStyle.getPropertyValue('border-left-width');
|
||||
|
||||
parentBorderTopWidth = parseInt(topWidth, 10);
|
||||
parentBorderLeftWidth = parseInt(leftWidth, 10);
|
||||
|
||||
overTop = element.offsetTop - parent.offsetTop < parent.scrollTop,
|
||||
overBottom =
|
||||
(element.offsetTop -
|
||||
parent.offsetTop +
|
||||
element.clientHeight -
|
||||
parentBorderTopWidth) >
|
||||
(parent.scrollTop + parent.clientHeight),
|
||||
|
||||
overLeft = element.offsetLeft -
|
||||
parent.offsetLeft < parent.scrollLeft,
|
||||
|
||||
overRight =
|
||||
(element.offsetLeft -
|
||||
parent.offsetLeft +
|
||||
element.clientWidth -
|
||||
parentBorderLeftWidth) >
|
||||
(parent.scrollLeft + parent.clientWidth),
|
||||
|
||||
alignWithTop = overTop && !overBottom;
|
||||
|
||||
if ((overTop || overBottom) && centerIfNeeded)
|
||||
parent.scrollTop =
|
||||
element.offsetTop -
|
||||
parent.offsetTop -
|
||||
parent.clientHeight / 2 -
|
||||
parentBorderTopWidth +
|
||||
element.clientHeight / 2;
|
||||
|
||||
if ((overLeft || overRight) && centerIfNeeded)
|
||||
parent.scrollLeft =
|
||||
element.offsetLeft -
|
||||
parent.offsetLeft -
|
||||
parent.clientWidth / 2 -
|
||||
parentBorderLeftWidth +
|
||||
element.clientWidth / 2;
|
||||
|
||||
if ((overTop || overBottom || overLeft || overRight) && !centerIfNeeded)
|
||||
element.scrollIntoView(alignWithTop);
|
||||
}
|
||||
};
|
||||
|
||||
if (!window.JSON) {
|
||||
window.JSON = {};
|
||||
|
||||
window.JSON.parse = $.parseJSON;
|
||||
|
||||
/* https://gist.github.com/754454 */
|
||||
window.JSON.stringify = function(obj) {
|
||||
let n, v, has,
|
||||
ret = '',
|
||||
value = '',
|
||||
json = [];
|
||||
|
||||
let isStr = itype.string(obj);
|
||||
let isObj = itype.object(obj);
|
||||
let isArray = itype.array(obj);
|
||||
|
||||
if (!isObj || obj === null) {
|
||||
// simple data type
|
||||
if (isStr)
|
||||
obj = '"' + obj + '"';
|
||||
|
||||
ret += obj;
|
||||
} else {
|
||||
// recurse array or object
|
||||
for (n in obj) {
|
||||
v = obj[n];
|
||||
has = obj.hasOwnProperty(n);
|
||||
|
||||
if (has) {
|
||||
isStr = itype.string(v);
|
||||
isObj = itype.object(v);
|
||||
|
||||
if (isStr)
|
||||
v = '"' + v + '"';
|
||||
else if (v && isObj)
|
||||
v = Util.json.stringify(v);
|
||||
|
||||
if (!isArray)
|
||||
value = '"' + n + '":';
|
||||
|
||||
json.push(value + v);
|
||||
}
|
||||
}
|
||||
|
||||
if (isArray)
|
||||
ret = '[' + json + ']';
|
||||
else
|
||||
ret = '{' + json + '}';
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
}
|
||||
const scrollIntoViewIfNeeded = require('scroll-into-view-if-needed').default;
|
||||
DOM.scrollIntoViewIfNeeded = scrollIntoViewIfNeeded;
|
||||
|
||||
|
|
|
|||
|
|
@ -195,6 +195,7 @@
|
|||
"redrun": "^6.0.0",
|
||||
"request": "^2.76.0",
|
||||
"rimraf": "^2.5.4",
|
||||
"scroll-into-view-if-needed": "^2.2.5",
|
||||
"serviceworker-webpack-plugin": "1.0.0-alpha02",
|
||||
"shortdate": "^1.0.1",
|
||||
"sinon": "^6.0.0",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue