📄 webcore.js
字号:
var scrollOffset = WebCore.Measure._getScrollOffset(element); this.top = cumulativeOffset.top - scrollOffset.top; this.left = cumulativeOffset.left - scrollOffset.left; } }, /** * toString() implementation for debug purposes. * * @return a string representation of the object * @type String */ toString: function() { return (this.left != null ? (this.left + "," + this.top + " : ") : "") + "[" + this.width + "x" + this.height + "]"; } })};/** * Scheduler namespace. Non-instantiable object. * Provides capability to invoke code at regular intervals, after a delay, * or after the current JavaScript execution context has completed. * Provides an object-oriented means of accomplishing this task. */WebCore.Scheduler = { /** * Collection of runnables to execute. * @private */ _runnables: [], /** * The thread handle returned by setTimeout(). */ _threadHandle: null, /** * Time at which next execution of the scheduler should occur. * When this field is not null, the _threadHandle field contains a * timeout scheduled to occur at this time. */ _nextExecution: null, /** * Enqueues a Runnable to be executed by the scheduler. * * @param {WebCore.Scheduler.Runnable} the runnable to enqueue */ add: function(runnable) { var currentTime = new Date().getTime(); var timeInterval = runnable.timeInterval ? runnable.timeInterval : 0; runnable._enabled = true; runnable._nextExecution = currentTime + timeInterval; WebCore.Scheduler._runnables.push(runnable); WebCore.Scheduler._start(runnable._nextExecution); }, /** * Executes the scheduler, running any runnables that are due. * This method is invoked by the interval/thread. */ _execute: function() { var currentTime = new Date().getTime(); var nextInterval = Number.MAX_VALUE; // Execute pending runnables. for (var i = 0; i < WebCore.Scheduler._runnables.length; ++i) { var runnable = WebCore.Scheduler._runnables[i]; if (runnable._nextExecution && runnable._nextExecution <= currentTime) { runnable._nextExecution = null; try { runnable.run(); } catch (ex) { throw(ex); } } } var newRunnables = []; for (var i = 0; i < WebCore.Scheduler._runnables.length; ++i) { var runnable = WebCore.Scheduler._runnables[i]; if (!runnable._enabled) { continue; } if (runnable._nextExecution) { // Runnable is scheduled for execution: add it to new queue. newRunnables.push(runnable); // Determine time interval of this runnable, if it is the soonest to be executed, use its execution time // as the setTimeout delay. var interval = runnable._nextExecution - currentTime; if (interval < nextInterval) { nextInterval = interval; } // Done processing this runnable. continue; } if (runnable.timeInterval != null && runnable.repeat) { // Runnable is executed at a repeating interval but is not scheduled: schedule it for execution. runnable._nextExecution = currentTime + runnable.timeInterval; newRunnables.push(runnable); // If this is the next runnable to be executed, use its execution time as the setTimeout delay. if (runnable.timeInterval < nextInterval) { nextInterval = runnable.timeInterval; } } } // Store new runnable queue. WebCore.Scheduler._runnables = newRunnables; if (nextInterval < Number.MAX_VALUE) { this._nextExecution = currentTime + nextInterval; WebCore.Scheduler._threadHandle = window.setTimeout(WebCore.Scheduler._execute, nextInterval); } else { WebCore.Scheduler._threadHandle = null; } }, /** * Dequeues a Runnable so it will no longer be executed by the scheduler. * * @param {WebCore.Scheduler.Runnable} the runnable to dequeue */ remove: function(runnable) { runnable._enabled = false; runnable._nextExecution = null; }, /** * Creates a new Runnable that executes the specified method and enqueues it into the scheduler. * * @param {Number} time the time interval, in milleseconds, after which the Runnable should be executed * (may be null/undefined to execute task immediately, in such cases repeat must be false) * @param {Boolean} repeat a flag indicating whether the task should be repeated * @param f a function to invoke, may be null/undefined * @return the created Runnable. * @type WebCore.Scheduler.Runnable */ run: function(f, timeInterval, repeat) { var runnable = new WebCore.Scheduler.MethodRunnable(f, timeInterval, repeat); this.add(runnable); return runnable; }, /** * Starts the scheduler "thread". * If the scheduler is already running, no action is taken. * @private */ _start: function(nextExecution) { var currentTime = new Date().getTime(); if (WebCore.Scheduler._threadHandle == null) { this._nextExecution = nextExecution; WebCore.Scheduler._threadHandle = window.setTimeout(WebCore.Scheduler._execute, nextExecution - currentTime); } else if (this._nextExecution > nextExecution) { // Cancel current timeout, start new timeout. window.clearTimeout(WebCore.Scheduler._threadHandle); this._nextExecution = nextExecution; WebCore.Scheduler._threadHandle = window.setTimeout(WebCore.Scheduler._execute, nextExecution - currentTime); } }, /** * Stops the scheduler "thread". * If the scheduler is not running, no action is taken. * @private */ _stop: function() { if (WebCore.Scheduler._threadHandle == null) { return; } window.clearTimeout(WebCore.Scheduler._threadHandle); WebCore.Scheduler._threadHandle = null; WebCore.Scheduler._nextExecution = null; }};/** * @class A runnable task that may be scheduled with the Scheduler. */WebCore.Scheduler.Runnable = Core.extend({ _enabled: null, _nextExecution: null, $virtual: { /** * Time interval, in milleseconds after which the Runnable should be executed. * @type Number */ timeInterval: null, /** * Flag indicating whether task should be repeated. * @type Boolean */ repeat: false }, $abstract: { run: function() { } }});/** * @class A runnable task implemenation that invokes a function at regular intervals. */WebCore.Scheduler.MethodRunnable = Core.extend(WebCore.Scheduler.Runnable, { f: null, /** * Creates a new Runnable. * * @constructor * @param {Number} time the time interval, in milleseconds, after which the Runnable should be executed * (may be null/undefined to execute task immediately, in such cases repeat must be false) * @param {Boolean} repeat a flag indicating whether the task should be repeated * @param {Function} f a function to invoke, may be null/undefined */ $construct: function(f, timeInterval, repeat) { if (!timeInterval && repeat) { throw new Error("Cannot create repeating runnable without time delay:" + f); } this.f = f; this.timeInterval = timeInterval; this.repeat = !!repeat; }, $virtual: { /** * Default run() implementation. Should be overidden by subclasses. */ run: function() { this.f(); } }});/** * @class * Static object/namespace which provides cross-platform CSS positioning * capabilities. Do not instantiate. * <p> * Internet Explorer 6 is ordinarily handicapped by its lack * of support for setting 'left' and 'right' or 'top' and 'bottom' positions * simultaneously on a single document element. * <p> * To use virtual positioning, simply set the left/right/top/bottom * coordinates of an element and invoke redraw(). The redraw() method * must be invoked whenever the size of the element should be redrawn, * e.g., when the screen or its containing element resizes. */WebCore.VirtualPosition = { _OFFSETS_VERTICAL: ["paddingTop", "paddingBottom", "marginTop", "marginBottom", "borderTopWidth", "borderBottomWidth"], _OFFSETS_HORIZONTAL: ["paddingLeft", "paddingRight", "marginLeft", "marginRight", "borderLeftWidth", "borderRightWidth"], /** Flag indicating whether virtual positioning is required/enabled. */ _enabled: false, /** * Calculates horizontal or vertical padding, border, and margin offsets for a particular style. * * @param offsetNames the names of the offsets styles to calculate, either * _OFFSETS_VERTICAL or _OFFSETS_HORIZONTAL. * @param style the style whose offsets should be calculated * @return the pixel size of the offsets, or -1 if they cannot be calculated */ _calculateOffsets: function(offsetNames, style) { var offsets = 0; for (var i = 0; i < offsetNames.length; ++i) { var value = style[offsetNames[i]]; if (value) { if (value.toString().indexOf("px") == -1) { return -1; } offsets += parseInt(value); } } return offsets; }, /** * Enables and initializes the virtual positioning system. */ _init: function() { this._enabled = true; }, /** * Redraws elements registered with the virtual positioning system. * Adjusts the style.height and style.width attributes of an element to * simulate its specified top, bottom, left, and right CSS position settings * The calculation makes allowances for padding, margin, and border width. * * @param element the element to redraw */ redraw: function(element) { if (!this._enabled) { return; } if (!element || !element.parentNode) { return; } // Adjust 'height' property if 'top' and 'bottom' properties are set, // and if all padding/margin/borders are 0 or set in pixel units. if (this._verifyPixelValue(element.style.top) && this._verifyPixelValue(element.style.bottom)) { // Verify that offsetHeight is valid, and do nothing if it cannot be calculated. // Such a do-nothing scenario is due to a not-up-to-date element cache, where // the element is no longer hierarchy. var offsetHeight = element.parentNode.offsetHeight; if (!isNaN(offsetHeight)) { var offsets = this._calculateOffsets(this._OFFSETS_VERTICAL, element.style); if (offsets != -1) { var calculatedHeight = offsetHeight - parseInt(element.style.top) - parseInt(element.style.bottom) - offsets; if (calculatedHeight <= 0) { element.style.height = 0; } else { if (element.style.height != calculatedHeight + "px") { element.style.height = calculatedHeight + "px"; } } } } } // Adjust 'width' property if 'left' and 'right' properties are set, // and if all padding/margin/borders are 0 or set in pixel units. if (this._verifyPixelValue(element.style.left) && this._verifyPixelValue(element.style.right)) { // Verify that offsetHeight is valid, and do nothing if it cannot be calculated. // Such a do-nothing scenario is due to a not-up-to-date element cache, where // the element is no longer hierarchy. var offsetWidth = element.parentNode.offsetWidth; if (!isNaN(offset
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -