📄 core.web.js
字号:
/** * @fileoverview * Provides low-level web-client-related APIs. Features include: * <ul> * <li>Provides cross-platform API for accessing web client features that have * inconsistent implementations on various browser platforms.</li> * <li>Provides HTTP Connection object (wrapper for XMLHttpRequest).</li> * <li>Provides HTML DOM manipulation capabilities.</li> * <li>Provides DOM event management facility, enabling capturing/bubbling phases * on all browsers, including Internet Explorer 6.</li> * <li>Provides "virtual positioning" capability for Internet Explorer 6 to * render proper top/left/right/bottom CSS positioning.</li> * <li>Provides facilities to convert dimensions (e.g., in/cm/pc) to pixels.</li> * <li>Provides capabilities to measure rendered size of DOM fragments.</li> * <li> Provides capabilities to asynchronously load and install JavaScript modules.</li> * </ul> * Requires Core. *//** * Namespace for Web Core. * @namespace */Core.Web = { /** * Flag indicating that a drag-and-drop operation is in process. * Setting this flag will prevent text selections within the browser. * It must be un-set when the drag operation completes. * * @type Boolean */ dragInProgress: false, /** * Initializes the Web Core. This method must be executed prior to using any Web Core capabilities. */ init: function() { if (Core.Web.initialized) { // Already initialized. return; } Core.Web.Env._init(); Core.Web.Measure._calculateExtentSizes(); Core.Web.Measure.Bounds._initMeasureContainer(); if (Core.Web.Env.QUIRK_CSS_POSITIONING_ONE_SIDE_ONLY) { // Enable virtual positioning. Core.Web.VirtualPosition._init(); } if (Core.Web.Env.BROWSER_INTERNET_EXPLORER) { Core.Web.DOM.addEventListener(document, "selectstart", Core.Web._selectStartListener, false); } Core.Web.initialized = true; }, /** * Internet Explorer-specific event listener to deny selection. * * @param {Event} e the selection event */ _selectStartListener: function(e) { e = e ? e : window.event; if (Core.Web.dragInProgress) { Core.Web.DOM.preventEventDefault(e); } }};/** * DOM manipulation utility method namespace. * @class */Core.Web.DOM = { /** * Temporary storage for the element about to be focused (for clients that require 'delayed' focusing). */ _focusPendingElement: null, /** * Adds an event listener to an object, using the client's supported event * model. This method does NOT support method references. * * @param {Element} eventSource the event source * @param {String} eventType the type of event (the 'on' prefix should NOT be included * in the event type, i.e., for mouse rollover events, "mouseover" would * be specified instead of "onmouseover") * @param {Function} eventListener the event listener to be invoked when the event occurs * @param {Boolean} useCapture a flag indicating whether the event listener should capture * events in the final phase of propagation (only supported by * DOM Level 2 event model, not available on Internet Explorer) */ addEventListener: function(eventSource, eventType, eventListener, useCapture) { if (eventSource.addEventListener) { eventSource.addEventListener(eventType, eventListener, useCapture); } else if (eventSource.attachEvent) { eventSource.attachEvent("on" + eventType, eventListener); } }, /** * Creates a new XML DOM. * * @param {String} namespaceUri the unique URI of the namespace of the root element in * the created document (not supported for * Internet Explorer 6 clients, null may be specified for all clients) * @param {String} qualifiedName the name of the root element of the new document (this * element will be created automatically) * @type Document * @return the created DOM */ createDocument: function(namespaceUri, qualifiedName) { if (document.implementation && document.implementation.createDocument) { // DOM Level 2 Browsers var dom; if (Core.Web.Env.BROWSER_FIREFOX && Core.Web.Env.BROWSER_VERSION_MAJOR == 3 && Core.Web.Env.BROWSER_VERSION_MINOR === 0) { // https://bugzilla.mozilla.org/show_bug.cgi?id=431701 dom = new DOMParser().parseFromString("<?xml version='1.0' encoding='UTF-8'?><" + qualifiedName + "/>", "application/xml"); } else { dom = document.implementation.createDocument(namespaceUri, qualifiedName, null); } if (!dom.documentElement) { dom.appendChild(dom.createElement(qualifiedName)); } return dom; } else if (window.ActiveXObject) { // Internet Explorer var createdDocument = new ActiveXObject("Microsoft.XMLDOM"); var documentElement = createdDocument.createElement(qualifiedName); createdDocument.appendChild(documentElement); return createdDocument; } else { throw new Error("XML DOM creation not supported by browser environment."); } }, /** * Focuses the given DOM element. * The focus operation may be placed in the scheduler if the browser requires the focus * operation to be performed outside of current JavaScript context (i.e., in the case * where the element to be focused was just rendered in this context). * * @param {Element} element the DOM element to focus */ focusElement: function(element) { if (Core.Web.Env.QUIRK_DELAYED_FOCUS_REQUIRED) { Core.Web.DOM._focusPendingElement = element; Core.Web.Scheduler.run(Core.method(window, this._focusElementImpl)); } else { this._focusElementImpl(element); } }, /** * Focus element implementation. * * @param {Element} element the DOM element to focus */ _focusElementImpl: function(element) { if (!element) { element = Core.Web.DOM._focusPendingElement; Core.Web.DOM._focusPendingElement = null; } if (element && element.focus) { try { element.focus(); } catch (ex) { // Silently digest IE focus exceptions. } } }, /** * Returns the first immediate child element of parentElement with the specified tag name. * * @param {Element} parentElement the parent element * @param tagName the tag name * @return the first child element of parentElement with the specified tag name, * or null if no elements match * @type Element */ getChildElementByTagName: function(parentElement, tagName) { var element = parentElement.firstChild; while (element) { if (element.nodeType == 1 && element.nodeName == tagName) { return element; } element = element.nextSibling; } return null; }, /** * Returns an array containing all immediate child element of parentElement with the specified tag name. * * @param {Element} parentElement the parent element * @param tagName the tag name * @return the child elements * @type Array */ getChildElementsByTagName: function(parentElement, tagName) { var elements = []; var element = parentElement.firstChild; while (element) { if (element.nodeType == 1 && element.nodeName == tagName) { elements.push(element); } element = element.nextSibling; } return elements; }, /** * Returns x/y coordinates of mouse relative to the element which fired an event. * * @param {Event} e the event * @return object containing 'x' and 'y' properties specifying the numeric pixel * coordinates of the mouse relative to the element, with {x: 0, y: 0} * indicating its upper-left corner */ getEventOffset: function(e) { if (typeof e.offsetX == "number") { return { x: e.offsetX, y: e.offsetY }; } else { var bounds = new Core.Web.Measure.Bounds(this.getEventTarget(e)); return { x: e.clientX - bounds.left, y: e.clientY - bounds.top }; } }, /** * Returns the target of an event, using the client's supported event model. * On clients which support the W3C DOM Level 2 event specification, * the <code>target</code> property of the event is returned. * On clients which support only the Internet Explorer event model, * the <code>srcElement</code> property of the event is returned. * * @param {Event} e the event * @return the target * @type Element */ getEventTarget: function(e) { return e.target ? e.target : e.srcElement; }, /** * Returns the related target of an event, using the client's supported event model. * On clients which support the W3C DOM Level 2 event specification, * the <code>relatedTarget</code> property of the event is returned. * On clients which support only the Internet Explorer event model, * the <code>toElement</code> property of the event is returned. * * @param {Event} e the event * @return the target * @type Element */ getEventRelatedTarget: function(e) { return e.relatedTarget ? e.relatedTarget : e.toElement; }, /** * Determines if <code>ancestorNode</code> is or is an ancestor of * <code>descendantNode</code>. * * @param {Node} ancestorNode the potential ancestor node * @param {Node} descendantNode the potential descendant node * @return true if <code>ancestorNode</code> is or is an ancestor of * <code>descendantNode</code> * @type Boolean */ isAncestorOf: function(ancestorNode, descendantNode) { var testNode = descendantNode; while (testNode !== null) { if (testNode == ancestorNode) { return true; } testNode = testNode.parentNode; } return false; }, /** * Prevents the default action of an event from occurring, using the * client's supported event model. * On clients which support the W3C DOM Level 2 event specification, * the preventDefault() method of the event is invoked. * On clients which support only the Internet Explorer event model, * the 'returnValue' property of the event is set to false. * * @param {Event} e the event */ preventEventDefault: function(e) { if (e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; } }, /** * Removes all child nodes from the specified DOM node. * * @param {Node} node the parent node whose children should be deleted */ removeAllChildren: function(node) { while (node.firstChild) { node.removeChild(node.firstChild); } }, /** * Removes an event listener from an object, using the client's supported event * model. This method does NOT support method references. * * @param {Element} eventSource the event source * @param {String} eventType the type of event (the 'on' prefix should NOT be included * in the event type, i.e., for mouse rollover events, "mouseover" would * be specified instead of "onmouseover") * @param {Function} eventListener the event listener to be invoked when the event occurs * @param {Boolean}useCapture a flag indicating whether the event listener should capture * events in the final phase of propagation (only supported by * DOM Level 2 event model, not available on Internet Explorer) */ removeEventListener: function(eventSource, eventType, eventListener, useCapture) { if (eventSource.removeEventListener) { eventSource.removeEventListener(eventType, eventListener, useCapture); } else if (eventSource.detachEvent) { eventSource.detachEvent("on" + eventType, eventListener); } }, /** * Removes the specified DOM node from the DOM tree. This method employs a workaround for the * <code>QUIRK_PERFORMANCE_LARGE_DOM_REMOVE</code> quirk. * * @param {Node} node the node which should be deleted */ removeNode: function(node) { var parentNode = node.parentNode; if (!parentNode) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -