PageRenderTime 65ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/angular2/es6/prod/src/platform/browser/browser_adapter.js

https://gitlab.com/remynaps/Vexillarius
JavaScript | 367 lines | 342 code | 0 blank | 25 comment | 47 complexity | c131025120ef96394a642c17951507ae MD5 | raw file
  1. import { ListWrapper } from 'angular2/src/facade/collection';
  2. import { isBlank, isPresent, global, setValueOnPath, DateWrapper } from 'angular2/src/facade/lang';
  3. import { setRootDomAdapter } from 'angular2/src/platform/dom/dom_adapter';
  4. import { GenericBrowserDomAdapter } from './generic_browser_adapter';
  5. var _attrToPropMap = {
  6. 'class': 'className',
  7. 'innerHtml': 'innerHTML',
  8. 'readonly': 'readOnly',
  9. 'tabindex': 'tabIndex'
  10. };
  11. const DOM_KEY_LOCATION_NUMPAD = 3;
  12. // Map to convert some key or keyIdentifier values to what will be returned by getEventKey
  13. var _keyMap = {
  14. // The following values are here for cross-browser compatibility and to match the W3C standard
  15. // cf http://www.w3.org/TR/DOM-Level-3-Events-key/
  16. '\b': 'Backspace',
  17. '\t': 'Tab',
  18. '\x7F': 'Delete',
  19. '\x1B': 'Escape',
  20. 'Del': 'Delete',
  21. 'Esc': 'Escape',
  22. 'Left': 'ArrowLeft',
  23. 'Right': 'ArrowRight',
  24. 'Up': 'ArrowUp',
  25. 'Down': 'ArrowDown',
  26. 'Menu': 'ContextMenu',
  27. 'Scroll': 'ScrollLock',
  28. 'Win': 'OS'
  29. };
  30. // There is a bug in Chrome for numeric keypad keys:
  31. // https://code.google.com/p/chromium/issues/detail?id=155654
  32. // 1, 2, 3 ... are reported as A, B, C ...
  33. var _chromeNumKeyPadMap = {
  34. 'A': '1',
  35. 'B': '2',
  36. 'C': '3',
  37. 'D': '4',
  38. 'E': '5',
  39. 'F': '6',
  40. 'G': '7',
  41. 'H': '8',
  42. 'I': '9',
  43. 'J': '*',
  44. 'K': '+',
  45. 'M': '-',
  46. 'N': '.',
  47. 'O': '/',
  48. '\x60': '0',
  49. '\x90': 'NumLock'
  50. };
  51. /**
  52. * A `DomAdapter` powered by full browser DOM APIs.
  53. */
  54. /* tslint:disable:requireParameterType */
  55. export class BrowserDomAdapter extends GenericBrowserDomAdapter {
  56. parse(templateHtml) { throw new Error("parse not implemented"); }
  57. static makeCurrent() { setRootDomAdapter(new BrowserDomAdapter()); }
  58. hasProperty(element, name) { return name in element; }
  59. setProperty(el, name, value) { el[name] = value; }
  60. getProperty(el, name) { return el[name]; }
  61. invoke(el, methodName, args) {
  62. el[methodName].apply(el, args);
  63. }
  64. // TODO(tbosch): move this into a separate environment class once we have it
  65. logError(error) {
  66. if (window.console.error) {
  67. window.console.error(error);
  68. }
  69. else {
  70. window.console.log(error);
  71. }
  72. }
  73. log(error) { window.console.log(error); }
  74. logGroup(error) {
  75. if (window.console.group) {
  76. window.console.group(error);
  77. this.logError(error);
  78. }
  79. else {
  80. window.console.log(error);
  81. }
  82. }
  83. logGroupEnd() {
  84. if (window.console.groupEnd) {
  85. window.console.groupEnd();
  86. }
  87. }
  88. get attrToPropMap() { return _attrToPropMap; }
  89. query(selector) { return document.querySelector(selector); }
  90. querySelector(el, selector) { return el.querySelector(selector); }
  91. querySelectorAll(el, selector) { return el.querySelectorAll(selector); }
  92. on(el, evt, listener) { el.addEventListener(evt, listener, false); }
  93. onAndCancel(el, evt, listener) {
  94. el.addEventListener(evt, listener, false);
  95. // Needed to follow Dart's subscription semantic, until fix of
  96. // https://code.google.com/p/dart/issues/detail?id=17406
  97. return () => { el.removeEventListener(evt, listener, false); };
  98. }
  99. dispatchEvent(el, evt) { el.dispatchEvent(evt); }
  100. createMouseEvent(eventType) {
  101. var evt = document.createEvent('MouseEvent');
  102. evt.initEvent(eventType, true, true);
  103. return evt;
  104. }
  105. createEvent(eventType) {
  106. var evt = document.createEvent('Event');
  107. evt.initEvent(eventType, true, true);
  108. return evt;
  109. }
  110. preventDefault(evt) {
  111. evt.preventDefault();
  112. evt.returnValue = false;
  113. }
  114. isPrevented(evt) {
  115. return evt.defaultPrevented || isPresent(evt.returnValue) && !evt.returnValue;
  116. }
  117. getInnerHTML(el) { return el.innerHTML; }
  118. getOuterHTML(el) { return el.outerHTML; }
  119. nodeName(node) { return node.nodeName; }
  120. nodeValue(node) { return node.nodeValue; }
  121. type(node) { return node.type; }
  122. content(node) {
  123. if (this.hasProperty(node, "content")) {
  124. return node.content;
  125. }
  126. else {
  127. return node;
  128. }
  129. }
  130. firstChild(el) { return el.firstChild; }
  131. nextSibling(el) { return el.nextSibling; }
  132. parentElement(el) { return el.parentNode; }
  133. childNodes(el) { return el.childNodes; }
  134. childNodesAsList(el) {
  135. var childNodes = el.childNodes;
  136. var res = ListWrapper.createFixedSize(childNodes.length);
  137. for (var i = 0; i < childNodes.length; i++) {
  138. res[i] = childNodes[i];
  139. }
  140. return res;
  141. }
  142. clearNodes(el) {
  143. while (el.firstChild) {
  144. el.removeChild(el.firstChild);
  145. }
  146. }
  147. appendChild(el, node) { el.appendChild(node); }
  148. removeChild(el, node) { el.removeChild(node); }
  149. replaceChild(el, newChild, oldChild) { el.replaceChild(newChild, oldChild); }
  150. remove(node) {
  151. if (node.parentNode) {
  152. node.parentNode.removeChild(node);
  153. }
  154. return node;
  155. }
  156. insertBefore(el, node) { el.parentNode.insertBefore(node, el); }
  157. insertAllBefore(el, nodes) { nodes.forEach(n => el.parentNode.insertBefore(n, el)); }
  158. insertAfter(el, node) { el.parentNode.insertBefore(node, el.nextSibling); }
  159. setInnerHTML(el, value) { el.innerHTML = value; }
  160. getText(el) { return el.textContent; }
  161. // TODO(vicb): removed Element type because it does not support StyleElement
  162. setText(el, value) { el.textContent = value; }
  163. getValue(el) { return el.value; }
  164. setValue(el, value) { el.value = value; }
  165. getChecked(el) { return el.checked; }
  166. setChecked(el, value) { el.checked = value; }
  167. createComment(text) { return document.createComment(text); }
  168. createTemplate(html) {
  169. var t = document.createElement('template');
  170. t.innerHTML = html;
  171. return t;
  172. }
  173. createElement(tagName, doc = document) { return doc.createElement(tagName); }
  174. createElementNS(ns, tagName, doc = document) { return doc.createElementNS(ns, tagName); }
  175. createTextNode(text, doc = document) { return doc.createTextNode(text); }
  176. createScriptTag(attrName, attrValue, doc = document) {
  177. var el = doc.createElement('SCRIPT');
  178. el.setAttribute(attrName, attrValue);
  179. return el;
  180. }
  181. createStyleElement(css, doc = document) {
  182. var style = doc.createElement('style');
  183. this.appendChild(style, this.createTextNode(css));
  184. return style;
  185. }
  186. createShadowRoot(el) { return el.createShadowRoot(); }
  187. getShadowRoot(el) { return el.shadowRoot; }
  188. getHost(el) { return el.host; }
  189. clone(node) { return node.cloneNode(true); }
  190. getElementsByClassName(element, name) {
  191. return element.getElementsByClassName(name);
  192. }
  193. getElementsByTagName(element, name) {
  194. return element.getElementsByTagName(name);
  195. }
  196. classList(element) { return Array.prototype.slice.call(element.classList, 0); }
  197. addClass(element, className) { element.classList.add(className); }
  198. removeClass(element, className) { element.classList.remove(className); }
  199. hasClass(element, className) { return element.classList.contains(className); }
  200. setStyle(element, styleName, styleValue) {
  201. element.style[styleName] = styleValue;
  202. }
  203. removeStyle(element, stylename) { element.style[stylename] = null; }
  204. getStyle(element, stylename) { return element.style[stylename]; }
  205. hasStyle(element, styleName, styleValue = null) {
  206. var value = this.getStyle(element, styleName) || '';
  207. return styleValue ? value == styleValue : value.length > 0;
  208. }
  209. tagName(element) { return element.tagName; }
  210. attributeMap(element) {
  211. var res = new Map();
  212. var elAttrs = element.attributes;
  213. for (var i = 0; i < elAttrs.length; i++) {
  214. var attrib = elAttrs[i];
  215. res.set(attrib.name, attrib.value);
  216. }
  217. return res;
  218. }
  219. hasAttribute(element, attribute) { return element.hasAttribute(attribute); }
  220. hasAttributeNS(element, ns, attribute) {
  221. return element.hasAttributeNS(ns, attribute);
  222. }
  223. getAttribute(element, attribute) { return element.getAttribute(attribute); }
  224. getAttributeNS(element, ns, name) {
  225. return element.getAttributeNS(ns, name);
  226. }
  227. setAttribute(element, name, value) { element.setAttribute(name, value); }
  228. setAttributeNS(element, ns, name, value) {
  229. element.setAttributeNS(ns, name, value);
  230. }
  231. removeAttribute(element, attribute) { element.removeAttribute(attribute); }
  232. removeAttributeNS(element, ns, name) { element.removeAttributeNS(ns, name); }
  233. templateAwareRoot(el) { return this.isTemplateElement(el) ? this.content(el) : el; }
  234. createHtmlDocument() {
  235. return document.implementation.createHTMLDocument('fakeTitle');
  236. }
  237. defaultDoc() { return document; }
  238. getBoundingClientRect(el) {
  239. try {
  240. return el.getBoundingClientRect();
  241. }
  242. catch (e) {
  243. return { top: 0, bottom: 0, left: 0, right: 0, width: 0, height: 0 };
  244. }
  245. }
  246. getTitle() { return document.title; }
  247. setTitle(newTitle) { document.title = newTitle || ''; }
  248. elementMatches(n, selector) {
  249. var matches = false;
  250. if (n instanceof HTMLElement) {
  251. if (n.matches) {
  252. matches = n.matches(selector);
  253. }
  254. else if (n.msMatchesSelector) {
  255. matches = n.msMatchesSelector(selector);
  256. }
  257. else if (n.webkitMatchesSelector) {
  258. matches = n.webkitMatchesSelector(selector);
  259. }
  260. }
  261. return matches;
  262. }
  263. isTemplateElement(el) {
  264. return el instanceof HTMLElement && el.nodeName == "TEMPLATE";
  265. }
  266. isTextNode(node) { return node.nodeType === Node.TEXT_NODE; }
  267. isCommentNode(node) { return node.nodeType === Node.COMMENT_NODE; }
  268. isElementNode(node) { return node.nodeType === Node.ELEMENT_NODE; }
  269. hasShadowRoot(node) { return node instanceof HTMLElement && isPresent(node.shadowRoot); }
  270. isShadowRoot(node) { return node instanceof DocumentFragment; }
  271. importIntoDoc(node) {
  272. var toImport = node;
  273. if (this.isTemplateElement(node)) {
  274. toImport = this.content(node);
  275. }
  276. return document.importNode(toImport, true);
  277. }
  278. adoptNode(node) { return document.adoptNode(node); }
  279. getHref(el) { return el.href; }
  280. getEventKey(event) {
  281. var key = event.key;
  282. if (isBlank(key)) {
  283. key = event.keyIdentifier;
  284. // keyIdentifier is defined in the old draft of DOM Level 3 Events implemented by Chrome and
  285. // Safari
  286. // cf
  287. // http://www.w3.org/TR/2007/WD-DOM-Level-3-Events-20071221/events.html#Events-KeyboardEvents-Interfaces
  288. if (isBlank(key)) {
  289. return 'Unidentified';
  290. }
  291. if (key.startsWith('U+')) {
  292. key = String.fromCharCode(parseInt(key.substring(2), 16));
  293. if (event.location === DOM_KEY_LOCATION_NUMPAD && _chromeNumKeyPadMap.hasOwnProperty(key)) {
  294. // There is a bug in Chrome for numeric keypad keys:
  295. // https://code.google.com/p/chromium/issues/detail?id=155654
  296. // 1, 2, 3 ... are reported as A, B, C ...
  297. key = _chromeNumKeyPadMap[key];
  298. }
  299. }
  300. }
  301. if (_keyMap.hasOwnProperty(key)) {
  302. key = _keyMap[key];
  303. }
  304. return key;
  305. }
  306. getGlobalEventTarget(target) {
  307. if (target == "window") {
  308. return window;
  309. }
  310. else if (target == "document") {
  311. return document;
  312. }
  313. else if (target == "body") {
  314. return document.body;
  315. }
  316. }
  317. getHistory() { return window.history; }
  318. getLocation() { return window.location; }
  319. getBaseHref() {
  320. var href = getBaseElementHref();
  321. if (isBlank(href)) {
  322. return null;
  323. }
  324. return relativePath(href);
  325. }
  326. resetBaseElement() { baseElement = null; }
  327. getUserAgent() { return window.navigator.userAgent; }
  328. setData(element, name, value) {
  329. this.setAttribute(element, 'data-' + name, value);
  330. }
  331. getData(element, name) { return this.getAttribute(element, 'data-' + name); }
  332. getComputedStyle(element) { return getComputedStyle(element); }
  333. // TODO(tbosch): move this into a separate environment class once we have it
  334. setGlobalVar(path, value) { setValueOnPath(global, path, value); }
  335. requestAnimationFrame(callback) { return window.requestAnimationFrame(callback); }
  336. cancelAnimationFrame(id) { window.cancelAnimationFrame(id); }
  337. performanceNow() {
  338. // performance.now() is not available in all browsers, see
  339. // http://caniuse.com/#search=performance.now
  340. if (isPresent(window.performance) && isPresent(window.performance.now)) {
  341. return window.performance.now();
  342. }
  343. else {
  344. return DateWrapper.toMillis(DateWrapper.now());
  345. }
  346. }
  347. }
  348. var baseElement = null;
  349. function getBaseElementHref() {
  350. if (isBlank(baseElement)) {
  351. baseElement = document.querySelector('base');
  352. if (isBlank(baseElement)) {
  353. return null;
  354. }
  355. }
  356. return baseElement.getAttribute('href');
  357. }
  358. // based on urlUtils.js in AngularJS 1
  359. var urlParsingNode = null;
  360. function relativePath(url) {
  361. if (isBlank(urlParsingNode)) {
  362. urlParsingNode = document.createElement("a");
  363. }
  364. urlParsingNode.setAttribute('href', url);
  365. return (urlParsingNode.pathname.charAt(0) === '/') ? urlParsingNode.pathname :
  366. '/' + urlParsingNode.pathname;
  367. }