Guest User

Untitled

a guest
Oct 25th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 113.70 KB | None | 0 0
  1. <!--
  2. @license
  3. Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
  4. This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
  5. The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
  6. The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
  7. Code distributed by Google as part of the polymer project is also
  8. subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
  9. --><!--
  10. @license
  11. Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
  12. This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
  13. The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
  14. The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
  15. Code distributed by Google as part of the polymer project is also
  16. subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
  17. --><link rel="import" href="polymer-mini.html">
  18.  
  19. <script>Polymer.nar = [];
  20. Polymer.Annotations = {
  21. parseAnnotations: function (template) {
  22. var list = [];
  23. var content = template._content || template.content;
  24. this._parseNodeAnnotations(content, list);
  25. return list;
  26. },
  27. _parseNodeAnnotations: function (node, list) {
  28. return node.nodeType === Node.TEXT_NODE ? this._parseTextNodeAnnotation(node, list) : this._parseElementAnnotations(node, list);
  29. },
  30. _bindingRegex: /([^{[]*)({{|\[\[)([^}\]]*)(?:]]|}})/g,
  31. _parseBindings: function (text) {
  32. var re = this._bindingRegex;
  33. var parts = [];
  34. var m, lastIndex;
  35. while ((m = re.exec(text)) !== null) {
  36. if (m[1]) {
  37. parts.push({ literal: m[1] });
  38. }
  39. var mode = m[2][0];
  40. var value = m[3].trim();
  41. var negate = false;
  42. if (value[0] == '!') {
  43. negate = true;
  44. value = value.substring(1).trim();
  45. }
  46. var customEvent, notifyEvent, colon;
  47. if (mode == '{' && (colon = value.indexOf('::')) > 0) {
  48. notifyEvent = value.substring(colon + 2);
  49. value = value.substring(0, colon);
  50. customEvent = true;
  51. }
  52. parts.push({
  53. compoundIndex: parts.length,
  54. value: value,
  55. mode: mode,
  56. negate: negate,
  57. event: notifyEvent,
  58. customEvent: customEvent
  59. });
  60. lastIndex = re.lastIndex;
  61. }
  62. if (lastIndex && lastIndex < text.length) {
  63. var literal = text.substring(lastIndex);
  64. if (literal) {
  65. parts.push({ literal: literal });
  66. }
  67. }
  68. if (parts.length) {
  69. return parts;
  70. }
  71. },
  72. _literalFromParts: function (parts) {
  73. var s = '';
  74. for (var i = 0; i < parts.length; i++) {
  75. var literal = parts[i].literal;
  76. s += literal || '';
  77. }
  78. return s;
  79. },
  80. _parseTextNodeAnnotation: function (node, list) {
  81. var parts = this._parseBindings(node.textContent);
  82. if (parts) {
  83. node.textContent = this._literalFromParts(parts) || ' ';
  84. var annote = {
  85. bindings: [{
  86. kind: 'text',
  87. name: 'textContent',
  88. parts: parts,
  89. isCompound: parts.length !== 1
  90. }]
  91. };
  92. list.push(annote);
  93. return annote;
  94. }
  95. },
  96. _parseElementAnnotations: function (element, list) {
  97. var annote = {
  98. bindings: [],
  99. events: []
  100. };
  101. if (element.localName === 'content') {
  102. list._hasContent = true;
  103. }
  104. this._parseChildNodesAnnotations(element, annote, list);
  105. if (element.attributes) {
  106. this._parseNodeAttributeAnnotations(element, annote, list);
  107. if (this.prepElement) {
  108. this.prepElement(element);
  109. }
  110. }
  111. if (annote.bindings.length || annote.events.length || annote.id) {
  112. list.push(annote);
  113. }
  114. return annote;
  115. },
  116. _parseChildNodesAnnotations: function (root, annote, list, callback) {
  117. if (root.firstChild) {
  118. for (var i = 0, node = root.firstChild; node; node = node.nextSibling, i++) {
  119. if (node.localName === 'template' && !node.hasAttribute('preserve-content')) {
  120. this._parseTemplate(node, i, list, annote);
  121. }
  122. if (node.nodeType === Node.TEXT_NODE) {
  123. var n = node.nextSibling;
  124. while (n && n.nodeType === Node.TEXT_NODE) {
  125. node.textContent += n.textContent;
  126. root.removeChild(n);
  127. n = n.nextSibling;
  128. }
  129. }
  130. var childAnnotation = this._parseNodeAnnotations(node, list, callback);
  131. if (childAnnotation) {
  132. childAnnotation.parent = annote;
  133. childAnnotation.index = i;
  134. }
  135. }
  136. }
  137. },
  138. _parseTemplate: function (node, index, list, parent) {
  139. var content = document.createDocumentFragment();
  140. content._notes = this.parseAnnotations(node);
  141. content.appendChild(node.content);
  142. list.push({
  143. bindings: Polymer.nar,
  144. events: Polymer.nar,
  145. templateContent: content,
  146. parent: parent,
  147. index: index
  148. });
  149. },
  150. _parseNodeAttributeAnnotations: function (node, annotation) {
  151. var attrs = Array.prototype.slice.call(node.attributes);
  152. for (var i = attrs.length - 1, a; a = attrs[i]; i--) {
  153. var n = a.name;
  154. var v = a.value;
  155. var b;
  156. if (n.slice(0, 3) === 'on-') {
  157. node.removeAttribute(n);
  158. annotation.events.push({
  159. name: n.slice(3),
  160. value: v
  161. });
  162. } else if (b = this._parseNodeAttributeAnnotation(node, n, v)) {
  163. annotation.bindings.push(b);
  164. } else if (n === 'id') {
  165. annotation.id = v;
  166. }
  167. }
  168. },
  169. _parseNodeAttributeAnnotation: function (node, name, value) {
  170. var parts = this._parseBindings(value);
  171. if (parts) {
  172. var origName = name;
  173. var kind = 'property';
  174. if (name[name.length - 1] == '$') {
  175. name = name.slice(0, -1);
  176. kind = 'attribute';
  177. }
  178. var literal = this._literalFromParts(parts);
  179. if (literal && kind == 'attribute') {
  180. node.setAttribute(name, literal);
  181. }
  182. if (node.localName == 'input' && name == 'value') {
  183. node.setAttribute(origName, '');
  184. }
  185. node.removeAttribute(origName);
  186. if (kind === 'property') {
  187. name = Polymer.CaseMap.dashToCamelCase(name);
  188. }
  189. return {
  190. kind: kind,
  191. name: name,
  192. parts: parts,
  193. literal: literal,
  194. isCompound: parts.length !== 1
  195. };
  196. }
  197. },
  198. _localSubTree: function (node, host) {
  199. return node === host ? node.childNodes : node._lightChildren || node.childNodes;
  200. },
  201. findAnnotatedNode: function (root, annote) {
  202. var parent = annote.parent && Polymer.Annotations.findAnnotatedNode(root, annote.parent);
  203. return !parent ? root : Polymer.Annotations._localSubTree(parent, root)[annote.index];
  204. }
  205. };
  206. (function () {
  207. function resolveCss(cssText, ownerDocument) {
  208. return cssText.replace(CSS_URL_RX, function (m, pre, url, post) {
  209. return pre + '\'' + resolve(url.replace(/["']/g, ''), ownerDocument) + '\'' + post;
  210. });
  211. }
  212. function resolveAttrs(element, ownerDocument) {
  213. for (var name in URL_ATTRS) {
  214. var a$ = URL_ATTRS[name];
  215. for (var i = 0, l = a$.length, a, at, v; i < l && (a = a$[i]); i++) {
  216. if (name === '*' || element.localName === name) {
  217. at = element.attributes[a];
  218. v = at && at.value;
  219. if (v && v.search(BINDING_RX) < 0) {
  220. at.value = a === 'style' ? resolveCss(v, ownerDocument) : resolve(v, ownerDocument);
  221. }
  222. }
  223. }
  224. }
  225. }
  226. function resolve(url, ownerDocument) {
  227. if (url && url[0] === '#') {
  228. return url;
  229. }
  230. var resolver = getUrlResolver(ownerDocument);
  231. resolver.href = url;
  232. return resolver.href || url;
  233. }
  234. var tempDoc;
  235. var tempDocBase;
  236. function resolveUrl(url, baseUri) {
  237. if (!tempDoc) {
  238. tempDoc = document.implementation.createHTMLDocument('temp');
  239. tempDocBase = tempDoc.createElement('base');
  240. tempDoc.head.appendChild(tempDocBase);
  241. }
  242. tempDocBase.href = baseUri;
  243. return resolve(url, tempDoc);
  244. }
  245. function getUrlResolver(ownerDocument) {
  246. return ownerDocument.__urlResolver || (ownerDocument.__urlResolver = ownerDocument.createElement('a'));
  247. }
  248. var CSS_URL_RX = /(url\()([^)]*)(\))/g;
  249. var URL_ATTRS = {
  250. '*': [
  251. 'href',
  252. 'src',
  253. 'style',
  254. 'url'
  255. ],
  256. form: ['action']
  257. };
  258. var BINDING_RX = /\{\{|\[\[/;
  259. Polymer.ResolveUrl = {
  260. resolveCss: resolveCss,
  261. resolveAttrs: resolveAttrs,
  262. resolveUrl: resolveUrl
  263. };
  264. }());
  265. Polymer.Base._addFeature({
  266. _prepAnnotations: function () {
  267. if (!this._template) {
  268. this._notes = [];
  269. } else {
  270. Polymer.Annotations.prepElement = this._prepElement.bind(this);
  271. if (this._template._content && this._template._content._notes) {
  272. this._notes = this._template._content._notes;
  273. } else {
  274. this._notes = Polymer.Annotations.parseAnnotations(this._template);
  275. }
  276. this._processAnnotations(this._notes);
  277. Polymer.Annotations.prepElement = null;
  278. }
  279. },
  280. _processAnnotations: function (notes) {
  281. for (var i = 0; i < notes.length; i++) {
  282. var note = notes[i];
  283. for (var j = 0; j < note.bindings.length; j++) {
  284. var b = note.bindings[j];
  285. for (var k = 0; k < b.parts.length; k++) {
  286. var p = b.parts[k];
  287. if (!p.literal) {
  288. p.signature = this._parseMethod(p.value);
  289. if (!p.signature) {
  290. p.model = this._modelForPath(p.value);
  291. }
  292. }
  293. }
  294. }
  295. if (note.templateContent) {
  296. this._processAnnotations(note.templateContent._notes);
  297. var pp = note.templateContent._parentProps = this._discoverTemplateParentProps(note.templateContent._notes);
  298. var bindings = [];
  299. for (var prop in pp) {
  300. bindings.push({
  301. index: note.index,
  302. kind: 'property',
  303. name: '_parent_' + prop,
  304. parts: [{
  305. mode: '{',
  306. model: prop,
  307. value: prop
  308. }]
  309. });
  310. }
  311. note.bindings = note.bindings.concat(bindings);
  312. }
  313. }
  314. },
  315. _discoverTemplateParentProps: function (notes) {
  316. var pp = {};
  317. notes.forEach(function (n) {
  318. n.bindings.forEach(function (b) {
  319. b.parts.forEach(function (p) {
  320. if (p.signature) {
  321. var args = p.signature.args;
  322. for (var k = 0; k < args.length; k++) {
  323. pp[args[k].model] = true;
  324. }
  325. } else {
  326. pp[p.model] = true;
  327. }
  328. });
  329. });
  330. if (n.templateContent) {
  331. var tpp = n.templateContent._parentProps;
  332. Polymer.Base.mixin(pp, tpp);
  333. }
  334. });
  335. return pp;
  336. },
  337. _prepElement: function (element) {
  338. Polymer.ResolveUrl.resolveAttrs(element, this._template.ownerDocument);
  339. },
  340. _findAnnotatedNode: Polymer.Annotations.findAnnotatedNode,
  341. _marshalAnnotationReferences: function () {
  342. if (this._template) {
  343. this._marshalIdNodes();
  344. this._marshalAnnotatedNodes();
  345. this._marshalAnnotatedListeners();
  346. }
  347. },
  348. _configureAnnotationReferences: function (config) {
  349. var notes = this._notes;
  350. var nodes = this._nodes;
  351. for (var i = 0; i < notes.length; i++) {
  352. var note = notes[i];
  353. var node = nodes[i];
  354. this._configureTemplateContent(note, node);
  355. this._configureCompoundBindings(note, node);
  356. }
  357. },
  358. _configureTemplateContent: function (note, node) {
  359. if (note.templateContent) {
  360. node._content = note.templateContent;
  361. }
  362. },
  363. _configureCompoundBindings: function (note, node) {
  364. var bindings = note.bindings;
  365. for (var i = 0; i < bindings.length; i++) {
  366. var binding = bindings[i];
  367. if (binding.isCompound) {
  368. var storage = node.__compoundStorage__ || (node.__compoundStorage__ = {});
  369. var parts = binding.parts;
  370. var literals = new Array(parts.length);
  371. for (var j = 0; j < parts.length; j++) {
  372. literals[j] = parts[j].literal;
  373. }
  374. var name = binding.name;
  375. storage[name] = literals;
  376. if (binding.literal && binding.kind == 'property') {
  377. if (node._configValue) {
  378. node._configValue(name, binding.literal);
  379. } else {
  380. node[name] = binding.literal;
  381. }
  382. }
  383. }
  384. }
  385. },
  386. _marshalIdNodes: function () {
  387. this.$ = {};
  388. this._notes.forEach(function (a) {
  389. if (a.id) {
  390. this.$[a.id] = this._findAnnotatedNode(this.root, a);
  391. }
  392. }, this);
  393. },
  394. _marshalAnnotatedNodes: function () {
  395. if (this._nodes) {
  396. this._nodes = this._nodes.map(function (a) {
  397. return this._findAnnotatedNode(this.root, a);
  398. }, this);
  399. }
  400. },
  401. _marshalAnnotatedListeners: function () {
  402. this._notes.forEach(function (a) {
  403. if (a.events && a.events.length) {
  404. var node = this._findAnnotatedNode(this.root, a);
  405. a.events.forEach(function (e) {
  406. this.listen(node, e.name, e.value);
  407. }, this);
  408. }
  409. }, this);
  410. }
  411. });
  412. Polymer.Base._addFeature({
  413. listeners: {},
  414. _listenListeners: function (listeners) {
  415. var node, name, key;
  416. for (key in listeners) {
  417. if (key.indexOf('.') < 0) {
  418. node = this;
  419. name = key;
  420. } else {
  421. name = key.split('.');
  422. node = this.$[name[0]];
  423. name = name[1];
  424. }
  425. this.listen(node, name, listeners[key]);
  426. }
  427. },
  428. listen: function (node, eventName, methodName) {
  429. var handler = this._recallEventHandler(this, eventName, node, methodName);
  430. if (!handler) {
  431. handler = this._createEventHandler(node, eventName, methodName);
  432. }
  433. if (handler._listening) {
  434. return;
  435. }
  436. this._listen(node, eventName, handler);
  437. handler._listening = true;
  438. },
  439. _boundListenerKey: function (eventName, methodName) {
  440. return eventName + ':' + methodName;
  441. },
  442. _recordEventHandler: function (host, eventName, target, methodName, handler) {
  443. var hbl = host.__boundListeners;
  444. if (!hbl) {
  445. hbl = host.__boundListeners = new WeakMap();
  446. }
  447. var bl = hbl.get(target);
  448. if (!bl) {
  449. bl = {};
  450. hbl.set(target, bl);
  451. }
  452. var key = this._boundListenerKey(eventName, methodName);
  453. bl[key] = handler;
  454. },
  455. _recallEventHandler: function (host, eventName, target, methodName) {
  456. var hbl = host.__boundListeners;
  457. if (!hbl) {
  458. return;
  459. }
  460. var bl = hbl.get(target);
  461. if (!bl) {
  462. return;
  463. }
  464. var key = this._boundListenerKey(eventName, methodName);
  465. return bl[key];
  466. },
  467. _createEventHandler: function (node, eventName, methodName) {
  468. var host = this;
  469. var handler = function (e) {
  470. if (host[methodName]) {
  471. host[methodName](e, e.detail);
  472. } else {
  473. host._warn(host._logf('_createEventHandler', 'listener method `' + methodName + '` not defined'));
  474. }
  475. };
  476. handler._listening = false;
  477. this._recordEventHandler(host, eventName, node, methodName, handler);
  478. return handler;
  479. },
  480. unlisten: function (node, eventName, methodName) {
  481. var handler = this._recallEventHandler(this, eventName, node, methodName);
  482. if (handler) {
  483. this._unlisten(node, eventName, handler);
  484. handler._listening = false;
  485. }
  486. },
  487. _listen: function (node, eventName, handler) {
  488. node.addEventListener(eventName, handler);
  489. },
  490. _unlisten: function (node, eventName, handler) {
  491. node.removeEventListener(eventName, handler);
  492. }
  493. });
  494. (function () {
  495. 'use strict';
  496. var HAS_NATIVE_TA = typeof document.head.style.touchAction === 'string';
  497. var GESTURE_KEY = '__polymerGestures';
  498. var HANDLED_OBJ = '__polymerGesturesHandled';
  499. var TOUCH_ACTION = '__polymerGesturesTouchAction';
  500. var TAP_DISTANCE = 25;
  501. var TRACK_DISTANCE = 5;
  502. var TRACK_LENGTH = 2;
  503. var MOUSE_TIMEOUT = 2500;
  504. var MOUSE_EVENTS = [
  505. 'mousedown',
  506. 'mousemove',
  507. 'mouseup',
  508. 'click'
  509. ];
  510. var MOUSE_WHICH_TO_BUTTONS = [
  511. 0,
  512. 1,
  513. 4,
  514. 2
  515. ];
  516. var MOUSE_HAS_BUTTONS = function () {
  517. try {
  518. return new MouseEvent('test', { buttons: 1 }).buttons === 1;
  519. } catch (e) {
  520. return false;
  521. }
  522. }();
  523. var IS_TOUCH_ONLY = navigator.userAgent.match(/iP(?:[oa]d|hone)|Android/);
  524. var mouseCanceller = function (mouseEvent) {
  525. mouseEvent[HANDLED_OBJ] = { skip: true };
  526. if (mouseEvent.type === 'click') {
  527. var path = Polymer.dom(mouseEvent).path;
  528. for (var i = 0; i < path.length; i++) {
  529. if (path[i] === POINTERSTATE.mouse.target) {
  530. return;
  531. }
  532. }
  533. mouseEvent.preventDefault();
  534. mouseEvent.stopPropagation();
  535. }
  536. };
  537. function setupTeardownMouseCanceller(setup) {
  538. for (var i = 0, en; i < MOUSE_EVENTS.length; i++) {
  539. en = MOUSE_EVENTS[i];
  540. if (setup) {
  541. document.addEventListener(en, mouseCanceller, true);
  542. } else {
  543. document.removeEventListener(en, mouseCanceller, true);
  544. }
  545. }
  546. }
  547. function ignoreMouse() {
  548. if (IS_TOUCH_ONLY) {
  549. return;
  550. }
  551. if (!POINTERSTATE.mouse.mouseIgnoreJob) {
  552. setupTeardownMouseCanceller(true);
  553. }
  554. var unset = function () {
  555. setupTeardownMouseCanceller();
  556. POINTERSTATE.mouse.target = null;
  557. POINTERSTATE.mouse.mouseIgnoreJob = null;
  558. };
  559. POINTERSTATE.mouse.mouseIgnoreJob = Polymer.Debounce(POINTERSTATE.mouse.mouseIgnoreJob, unset, MOUSE_TIMEOUT);
  560. }
  561. function hasLeftMouseButton(ev) {
  562. var type = ev.type;
  563. if (MOUSE_EVENTS.indexOf(type) === -1) {
  564. return false;
  565. }
  566. if (type === 'mousemove') {
  567. var buttons = ev.buttons === undefined ? 1 : ev.buttons;
  568. if (ev instanceof window.MouseEvent && !MOUSE_HAS_BUTTONS) {
  569. buttons = MOUSE_WHICH_TO_BUTTONS[ev.which] || 0;
  570. }
  571. return Boolean(buttons & 1);
  572. } else {
  573. var button = ev.button === undefined ? 0 : ev.button;
  574. return button === 0;
  575. }
  576. }
  577. function isSyntheticClick(ev) {
  578. if (ev.type === 'click') {
  579. if (ev.detail === 0) {
  580. return true;
  581. }
  582. var t = Gestures.findOriginalTarget(ev);
  583. var bcr = t.getBoundingClientRect();
  584. var x = ev.pageX, y = ev.pageY;
  585. return !(x >= bcr.left && x <= bcr.right && (y >= bcr.top && y <= bcr.bottom));
  586. }
  587. return false;
  588. }
  589. var POINTERSTATE = {
  590. mouse: {
  591. target: null,
  592. mouseIgnoreJob: null
  593. },
  594. touch: {
  595. x: 0,
  596. y: 0,
  597. id: -1,
  598. scrollDecided: false
  599. }
  600. };
  601. function firstTouchAction(ev) {
  602. var path = Polymer.dom(ev).path;
  603. var ta = 'auto';
  604. for (var i = 0, n; i < path.length; i++) {
  605. n = path[i];
  606. if (n[TOUCH_ACTION]) {
  607. ta = n[TOUCH_ACTION];
  608. break;
  609. }
  610. }
  611. return ta;
  612. }
  613. function trackDocument(stateObj, movefn, upfn) {
  614. stateObj.movefn = movefn;
  615. stateObj.upfn = upfn;
  616. document.addEventListener('mousemove', movefn);
  617. document.addEventListener('mouseup', upfn);
  618. }
  619. function untrackDocument(stateObj) {
  620. document.removeEventListener('mousemove', stateObj.movefn);
  621. document.removeEventListener('mouseup', stateObj.upfn);
  622. }
  623. var Gestures = {
  624. gestures: {},
  625. recognizers: [],
  626. deepTargetFind: function (x, y) {
  627. var node = document.elementFromPoint(x, y);
  628. var next = node;
  629. while (next && next.shadowRoot) {
  630. next = next.shadowRoot.elementFromPoint(x, y);
  631. if (next) {
  632. node = next;
  633. }
  634. }
  635. return node;
  636. },
  637. findOriginalTarget: function (ev) {
  638. if (ev.path) {
  639. return ev.path[0];
  640. }
  641. return ev.target;
  642. },
  643. handleNative: function (ev) {
  644. var handled;
  645. var type = ev.type;
  646. var node = ev.currentTarget;
  647. var gobj = node[GESTURE_KEY];
  648. var gs = gobj[type];
  649. if (!gs) {
  650. return;
  651. }
  652. if (!ev[HANDLED_OBJ]) {
  653. ev[HANDLED_OBJ] = {};
  654. if (type.slice(0, 5) === 'touch') {
  655. var t = ev.changedTouches[0];
  656. if (type === 'touchstart') {
  657. if (ev.touches.length === 1) {
  658. POINTERSTATE.touch.id = t.identifier;
  659. }
  660. }
  661. if (POINTERSTATE.touch.id !== t.identifier) {
  662. return;
  663. }
  664. if (!HAS_NATIVE_TA) {
  665. if (type === 'touchstart' || type === 'touchmove') {
  666. Gestures.handleTouchAction(ev);
  667. }
  668. }
  669. if (type === 'touchend') {
  670. POINTERSTATE.mouse.target = Polymer.dom(ev).rootTarget;
  671. ignoreMouse(true);
  672. }
  673. }
  674. }
  675. handled = ev[HANDLED_OBJ];
  676. if (handled.skip) {
  677. return;
  678. }
  679. var recognizers = Gestures.recognizers;
  680. for (var i = 0, r; i < recognizers.length; i++) {
  681. r = recognizers[i];
  682. if (gs[r.name] && !handled[r.name]) {
  683. if (r.flow && r.flow.start.indexOf(ev.type) > -1) {
  684. if (r.reset) {
  685. r.reset();
  686. }
  687. }
  688. }
  689. }
  690. for (var i = 0, r; i < recognizers.length; i++) {
  691. r = recognizers[i];
  692. if (gs[r.name] && !handled[r.name]) {
  693. handled[r.name] = true;
  694. r[type](ev);
  695. }
  696. }
  697. },
  698. handleTouchAction: function (ev) {
  699. var t = ev.changedTouches[0];
  700. var type = ev.type;
  701. if (type === 'touchstart') {
  702. POINTERSTATE.touch.x = t.clientX;
  703. POINTERSTATE.touch.y = t.clientY;
  704. POINTERSTATE.touch.scrollDecided = false;
  705. } else if (type === 'touchmove') {
  706. if (POINTERSTATE.touch.scrollDecided) {
  707. return;
  708. }
  709. POINTERSTATE.touch.scrollDecided = true;
  710. var ta = firstTouchAction(ev);
  711. var prevent = false;
  712. var dx = Math.abs(POINTERSTATE.touch.x - t.clientX);
  713. var dy = Math.abs(POINTERSTATE.touch.y - t.clientY);
  714. if (!ev.cancelable) {
  715. } else if (ta === 'none') {
  716. prevent = true;
  717. } else if (ta === 'pan-x') {
  718. prevent = dy > dx;
  719. } else if (ta === 'pan-y') {
  720. prevent = dx > dy;
  721. }
  722. if (prevent) {
  723. ev.preventDefault();
  724. } else {
  725. Gestures.prevent('track');
  726. }
  727. }
  728. },
  729. add: function (node, evType, handler) {
  730. var recognizer = this.gestures[evType];
  731. var deps = recognizer.deps;
  732. var name = recognizer.name;
  733. var gobj = node[GESTURE_KEY];
  734. if (!gobj) {
  735. node[GESTURE_KEY] = gobj = {};
  736. }
  737. for (var i = 0, dep, gd; i < deps.length; i++) {
  738. dep = deps[i];
  739. if (IS_TOUCH_ONLY && MOUSE_EVENTS.indexOf(dep) > -1) {
  740. continue;
  741. }
  742. gd = gobj[dep];
  743. if (!gd) {
  744. gobj[dep] = gd = { _count: 0 };
  745. }
  746. if (gd._count === 0) {
  747. node.addEventListener(dep, this.handleNative);
  748. }
  749. gd[name] = (gd[name] || 0) + 1;
  750. gd._count = (gd._count || 0) + 1;
  751. }
  752. node.addEventListener(evType, handler);
  753. if (recognizer.touchAction) {
  754. this.setTouchAction(node, recognizer.touchAction);
  755. }
  756. },
  757. remove: function (node, evType, handler) {
  758. var recognizer = this.gestures[evType];
  759. var deps = recognizer.deps;
  760. var name = recognizer.name;
  761. var gobj = node[GESTURE_KEY];
  762. if (gobj) {
  763. for (var i = 0, dep, gd; i < deps.length; i++) {
  764. dep = deps[i];
  765. gd = gobj[dep];
  766. if (gd && gd[name]) {
  767. gd[name] = (gd[name] || 1) - 1;
  768. gd._count = (gd._count || 1) - 1;
  769. if (gd._count === 0) {
  770. node.removeEventListener(dep, this.handleNative);
  771. }
  772. }
  773. }
  774. }
  775. node.removeEventListener(evType, handler);
  776. },
  777. register: function (recog) {
  778. this.recognizers.push(recog);
  779. for (var i = 0; i < recog.emits.length; i++) {
  780. this.gestures[recog.emits[i]] = recog;
  781. }
  782. },
  783. findRecognizerByEvent: function (evName) {
  784. for (var i = 0, r; i < this.recognizers.length; i++) {
  785. r = this.recognizers[i];
  786. for (var j = 0, n; j < r.emits.length; j++) {
  787. n = r.emits[j];
  788. if (n === evName) {
  789. return r;
  790. }
  791. }
  792. }
  793. return null;
  794. },
  795. setTouchAction: function (node, value) {
  796. if (HAS_NATIVE_TA) {
  797. node.style.touchAction = value;
  798. }
  799. node[TOUCH_ACTION] = value;
  800. },
  801. fire: function (target, type, detail) {
  802. var ev = Polymer.Base.fire(type, detail, {
  803. node: target,
  804. bubbles: true,
  805. cancelable: true
  806. });
  807. if (ev.defaultPrevented) {
  808. var se = detail.sourceEvent;
  809. if (se && se.preventDefault) {
  810. se.preventDefault();
  811. }
  812. }
  813. },
  814. prevent: function (evName) {
  815. var recognizer = this.findRecognizerByEvent(evName);
  816. if (recognizer.info) {
  817. recognizer.info.prevent = true;
  818. }
  819. }
  820. };
  821. Gestures.register({
  822. name: 'downup',
  823. deps: [
  824. 'mousedown',
  825. 'touchstart',
  826. 'touchend'
  827. ],
  828. flow: {
  829. start: [
  830. 'mousedown',
  831. 'touchstart'
  832. ],
  833. end: [
  834. 'mouseup',
  835. 'touchend'
  836. ]
  837. },
  838. emits: [
  839. 'down',
  840. 'up'
  841. ],
  842. info: {
  843. movefn: function () {
  844. },
  845. upfn: function () {
  846. }
  847. },
  848. reset: function () {
  849. untrackDocument(this.info);
  850. },
  851. mousedown: function (e) {
  852. if (!hasLeftMouseButton(e)) {
  853. return;
  854. }
  855. var t = Gestures.findOriginalTarget(e);
  856. var self = this;
  857. var movefn = function movefn(e) {
  858. if (!hasLeftMouseButton(e)) {
  859. self.fire('up', t, e);
  860. untrackDocument(self.info);
  861. }
  862. };
  863. var upfn = function upfn(e) {
  864. if (hasLeftMouseButton(e)) {
  865. self.fire('up', t, e);
  866. }
  867. untrackDocument(self.info);
  868. };
  869. trackDocument(this.info, movefn, upfn);
  870. this.fire('down', t, e);
  871. },
  872. touchstart: function (e) {
  873. this.fire('down', Gestures.findOriginalTarget(e), e.changedTouches[0]);
  874. },
  875. touchend: function (e) {
  876. this.fire('up', Gestures.findOriginalTarget(e), e.changedTouches[0]);
  877. },
  878. fire: function (type, target, event) {
  879. var self = this;
  880. Gestures.fire(target, type, {
  881. x: event.clientX,
  882. y: event.clientY,
  883. sourceEvent: event,
  884. prevent: Gestures.prevent.bind(Gestures)
  885. });
  886. }
  887. });
  888. Gestures.register({
  889. name: 'track',
  890. touchAction: 'none',
  891. deps: [
  892. 'mousedown',
  893. 'touchstart',
  894. 'touchmove',
  895. 'touchend'
  896. ],
  897. flow: {
  898. start: [
  899. 'mousedown',
  900. 'touchstart'
  901. ],
  902. end: [
  903. 'mouseup',
  904. 'touchend'
  905. ]
  906. },
  907. emits: ['track'],
  908. info: {
  909. x: 0,
  910. y: 0,
  911. state: 'start',
  912. started: false,
  913. moves: [],
  914. addMove: function (move) {
  915. if (this.moves.length > TRACK_LENGTH) {
  916. this.moves.shift();
  917. }
  918. this.moves.push(move);
  919. },
  920. movefn: function () {
  921. },
  922. upfn: function () {
  923. },
  924. prevent: false
  925. },
  926. reset: function () {
  927. this.info.state = 'start';
  928. this.info.started = false;
  929. this.info.moves = [];
  930. this.info.x = 0;
  931. this.info.y = 0;
  932. this.info.prevent = false;
  933. untrackDocument(this.info);
  934. },
  935. hasMovedEnough: function (x, y) {
  936. if (this.info.prevent) {
  937. return false;
  938. }
  939. if (this.info.started) {
  940. return true;
  941. }
  942. var dx = Math.abs(this.info.x - x);
  943. var dy = Math.abs(this.info.y - y);
  944. return dx >= TRACK_DISTANCE || dy >= TRACK_DISTANCE;
  945. },
  946. mousedown: function (e) {
  947. if (!hasLeftMouseButton(e)) {
  948. return;
  949. }
  950. var t = Gestures.findOriginalTarget(e);
  951. var self = this;
  952. var movefn = function movefn(e) {
  953. var x = e.clientX, y = e.clientY;
  954. if (self.hasMovedEnough(x, y)) {
  955. self.info.state = self.info.started ? e.type === 'mouseup' ? 'end' : 'track' : 'start';
  956. self.info.addMove({
  957. x: x,
  958. y: y
  959. });
  960. if (!hasLeftMouseButton(e)) {
  961. self.info.state = 'end';
  962. untrackDocument(self.info);
  963. }
  964. self.fire(t, e);
  965. self.info.started = true;
  966. }
  967. };
  968. var upfn = function upfn(e) {
  969. if (self.info.started) {
  970. Gestures.prevent('tap');
  971. movefn(e);
  972. }
  973. untrackDocument(self.info);
  974. };
  975. trackDocument(this.info, movefn, upfn);
  976. this.info.x = e.clientX;
  977. this.info.y = e.clientY;
  978. },
  979. touchstart: function (e) {
  980. var ct = e.changedTouches[0];
  981. this.info.x = ct.clientX;
  982. this.info.y = ct.clientY;
  983. },
  984. touchmove: function (e) {
  985. var t = Gestures.findOriginalTarget(e);
  986. var ct = e.changedTouches[0];
  987. var x = ct.clientX, y = ct.clientY;
  988. if (this.hasMovedEnough(x, y)) {
  989. this.info.addMove({
  990. x: x,
  991. y: y
  992. });
  993. this.fire(t, ct);
  994. this.info.state = 'track';
  995. this.info.started = true;
  996. }
  997. },
  998. touchend: function (e) {
  999. var t = Gestures.findOriginalTarget(e);
  1000. var ct = e.changedTouches[0];
  1001. if (this.info.started) {
  1002. Gestures.prevent('tap');
  1003. this.info.state = 'end';
  1004. this.info.addMove({
  1005. x: ct.clientX,
  1006. y: ct.clientY
  1007. });
  1008. this.fire(t, ct);
  1009. }
  1010. },
  1011. fire: function (target, touch) {
  1012. var secondlast = this.info.moves[this.info.moves.length - 2];
  1013. var lastmove = this.info.moves[this.info.moves.length - 1];
  1014. var dx = lastmove.x - this.info.x;
  1015. var dy = lastmove.y - this.info.y;
  1016. var ddx, ddy = 0;
  1017. if (secondlast) {
  1018. ddx = lastmove.x - secondlast.x;
  1019. ddy = lastmove.y - secondlast.y;
  1020. }
  1021. return Gestures.fire(target, 'track', {
  1022. state: this.info.state,
  1023. x: touch.clientX,
  1024. y: touch.clientY,
  1025. dx: dx,
  1026. dy: dy,
  1027. ddx: ddx,
  1028. ddy: ddy,
  1029. sourceEvent: touch,
  1030. hover: function () {
  1031. return Gestures.deepTargetFind(touch.clientX, touch.clientY);
  1032. }
  1033. });
  1034. }
  1035. });
  1036. Gestures.register({
  1037. name: 'tap',
  1038. deps: [
  1039. 'mousedown',
  1040. 'click',
  1041. 'touchstart',
  1042. 'touchend'
  1043. ],
  1044. flow: {
  1045. start: [
  1046. 'mousedown',
  1047. 'touchstart'
  1048. ],
  1049. end: [
  1050. 'click',
  1051. 'touchend'
  1052. ]
  1053. },
  1054. emits: ['tap'],
  1055. info: {
  1056. x: NaN,
  1057. y: NaN,
  1058. prevent: false
  1059. },
  1060. reset: function () {
  1061. this.info.x = NaN;
  1062. this.info.y = NaN;
  1063. this.info.prevent = false;
  1064. },
  1065. save: function (e) {
  1066. this.info.x = e.clientX;
  1067. this.info.y = e.clientY;
  1068. },
  1069. mousedown: function (e) {
  1070. if (hasLeftMouseButton(e)) {
  1071. this.save(e);
  1072. }
  1073. },
  1074. click: function (e) {
  1075. if (hasLeftMouseButton(e)) {
  1076. this.forward(e);
  1077. }
  1078. },
  1079. touchstart: function (e) {
  1080. this.save(e.changedTouches[0]);
  1081. },
  1082. touchend: function (e) {
  1083. this.forward(e.changedTouches[0]);
  1084. },
  1085. forward: function (e) {
  1086. var dx = Math.abs(e.clientX - this.info.x);
  1087. var dy = Math.abs(e.clientY - this.info.y);
  1088. var t = Gestures.findOriginalTarget(e);
  1089. if (isNaN(dx) || isNaN(dy) || dx <= TAP_DISTANCE && dy <= TAP_DISTANCE || isSyntheticClick(e)) {
  1090. if (!this.info.prevent) {
  1091. Gestures.fire(t, 'tap', {
  1092. x: e.clientX,
  1093. y: e.clientY,
  1094. sourceEvent: e
  1095. });
  1096. }
  1097. }
  1098. }
  1099. });
  1100. var DIRECTION_MAP = {
  1101. x: 'pan-x',
  1102. y: 'pan-y',
  1103. none: 'none',
  1104. all: 'auto'
  1105. };
  1106. Polymer.Base._addFeature({
  1107. _listen: function (node, eventName, handler) {
  1108. if (Gestures.gestures[eventName]) {
  1109. Gestures.add(node, eventName, handler);
  1110. } else {
  1111. node.addEventListener(eventName, handler);
  1112. }
  1113. },
  1114. _unlisten: function (node, eventName, handler) {
  1115. if (Gestures.gestures[eventName]) {
  1116. Gestures.remove(node, eventName, handler);
  1117. } else {
  1118. node.removeEventListener(eventName, handler);
  1119. }
  1120. },
  1121. setScrollDirection: function (direction, node) {
  1122. node = node || this;
  1123. Gestures.setTouchAction(node, DIRECTION_MAP[direction] || 'auto');
  1124. }
  1125. });
  1126. Polymer.Gestures = Gestures;
  1127. }());
  1128. Polymer.Async = {
  1129. _currVal: 0,
  1130. _lastVal: 0,
  1131. _callbacks: [],
  1132. _twiddleContent: 0,
  1133. _twiddle: document.createTextNode(''),
  1134. run: function (callback, waitTime) {
  1135. if (waitTime > 0) {
  1136. return ~setTimeout(callback, waitTime);
  1137. } else {
  1138. this._twiddle.textContent = this._twiddleContent++;
  1139. this._callbacks.push(callback);
  1140. return this._currVal++;
  1141. }
  1142. },
  1143. cancel: function (handle) {
  1144. if (handle < 0) {
  1145. clearTimeout(~handle);
  1146. } else {
  1147. var idx = handle - this._lastVal;
  1148. if (idx >= 0) {
  1149. if (!this._callbacks[idx]) {
  1150. throw 'invalid async handle: ' + handle;
  1151. }
  1152. this._callbacks[idx] = null;
  1153. }
  1154. }
  1155. },
  1156. _atEndOfMicrotask: function () {
  1157. var len = this._callbacks.length;
  1158. for (var i = 0; i < len; i++) {
  1159. var cb = this._callbacks[i];
  1160. if (cb) {
  1161. try {
  1162. cb();
  1163. } catch (e) {
  1164. i++;
  1165. this._callbacks.splice(0, i);
  1166. this._lastVal += i;
  1167. this._twiddle.textContent = this._twiddleContent++;
  1168. throw e;
  1169. }
  1170. }
  1171. }
  1172. this._callbacks.splice(0, len);
  1173. this._lastVal += len;
  1174. }
  1175. };
  1176. new window.MutationObserver(function () {
  1177. Polymer.Async._atEndOfMicrotask();
  1178. }).observe(Polymer.Async._twiddle, { characterData: true });
  1179. Polymer.Debounce = function () {
  1180. var Async = Polymer.Async;
  1181. var Debouncer = function (context) {
  1182. this.context = context;
  1183. this.boundComplete = this.complete.bind(this);
  1184. };
  1185. Debouncer.prototype = {
  1186. go: function (callback, wait) {
  1187. var h;
  1188. this.finish = function () {
  1189. Async.cancel(h);
  1190. };
  1191. h = Async.run(this.boundComplete, wait);
  1192. this.callback = callback;
  1193. },
  1194. stop: function () {
  1195. if (this.finish) {
  1196. this.finish();
  1197. this.finish = null;
  1198. }
  1199. },
  1200. complete: function () {
  1201. if (this.finish) {
  1202. this.stop();
  1203. this.callback.call(this.context);
  1204. }
  1205. }
  1206. };
  1207. function debounce(debouncer, callback, wait) {
  1208. if (debouncer) {
  1209. debouncer.stop();
  1210. } else {
  1211. debouncer = new Debouncer(this);
  1212. }
  1213. debouncer.go(callback, wait);
  1214. return debouncer;
  1215. }
  1216. return debounce;
  1217. }();
  1218. Polymer.Base._addFeature({
  1219. $$: function (slctr) {
  1220. return Polymer.dom(this.root).querySelector(slctr);
  1221. },
  1222. toggleClass: function (name, bool, node) {
  1223. node = node || this;
  1224. if (arguments.length == 1) {
  1225. bool = !node.classList.contains(name);
  1226. }
  1227. if (bool) {
  1228. Polymer.dom(node).classList.add(name);
  1229. } else {
  1230. Polymer.dom(node).classList.remove(name);
  1231. }
  1232. },
  1233. toggleAttribute: function (name, bool, node) {
  1234. node = node || this;
  1235. if (arguments.length == 1) {
  1236. bool = !node.hasAttribute(name);
  1237. }
  1238. if (bool) {
  1239. Polymer.dom(node).setAttribute(name, '');
  1240. } else {
  1241. Polymer.dom(node).removeAttribute(name);
  1242. }
  1243. },
  1244. classFollows: function (name, toElement, fromElement) {
  1245. if (fromElement) {
  1246. Polymer.dom(fromElement).classList.remove(name);
  1247. }
  1248. if (toElement) {
  1249. Polymer.dom(toElement).classList.add(name);
  1250. }
  1251. },
  1252. attributeFollows: function (name, toElement, fromElement) {
  1253. if (fromElement) {
  1254. Polymer.dom(fromElement).removeAttribute(name);
  1255. }
  1256. if (toElement) {
  1257. Polymer.dom(toElement).setAttribute(name, '');
  1258. }
  1259. },
  1260. getEffectiveChildNodes: function () {
  1261. return Polymer.dom(this).getEffectiveChildNodes();
  1262. },
  1263. getEffectiveChildren: function () {
  1264. var list = Polymer.dom(this).getEffectiveChildNodes();
  1265. return list.filter(function (n) {
  1266. return n.nodeType === Node.ELEMENT_NODE;
  1267. });
  1268. },
  1269. getEffectiveTextContent: function () {
  1270. var cn = this.getEffectiveChildNodes();
  1271. var tc = [];
  1272. for (var i = 0, c; c = cn[i]; i++) {
  1273. if (c.nodeType !== Node.COMMENT_NODE) {
  1274. tc.push(Polymer.dom(c).textContent);
  1275. }
  1276. }
  1277. return tc.join('');
  1278. },
  1279. queryEffectiveChildren: function (slctr) {
  1280. var e$ = Polymer.dom(this).queryDistributedElements(slctr);
  1281. return e$ && e$[0];
  1282. },
  1283. queryAllEffectiveChildren: function (slctr) {
  1284. return Polymer.dom(this).queryAllDistributedElements(slctr);
  1285. },
  1286. getContentChildNodes: function (slctr) {
  1287. var content = Polymer.dom(this.root).querySelector(slctr || 'content');
  1288. return content ? Polymer.dom(content).getDistributedNodes() : [];
  1289. },
  1290. getContentChildren: function (slctr) {
  1291. return this.getContentChildNodes(slctr).filter(function (n) {
  1292. return n.nodeType === Node.ELEMENT_NODE;
  1293. });
  1294. },
  1295. fire: function (type, detail, options) {
  1296. options = options || Polymer.nob;
  1297. var node = options.node || this;
  1298. var detail = detail === null || detail === undefined ? Polymer.nob : detail;
  1299. var bubbles = options.bubbles === undefined ? true : options.bubbles;
  1300. var cancelable = Boolean(options.cancelable);
  1301. var event = new CustomEvent(type, {
  1302. bubbles: Boolean(bubbles),
  1303. cancelable: cancelable,
  1304. detail: detail
  1305. });
  1306. node.dispatchEvent(event);
  1307. return event;
  1308. },
  1309. async: function (callback, waitTime) {
  1310. return Polymer.Async.run(callback.bind(this), waitTime);
  1311. },
  1312. cancelAsync: function (handle) {
  1313. Polymer.Async.cancel(handle);
  1314. },
  1315. arrayDelete: function (path, item) {
  1316. var index;
  1317. if (Array.isArray(path)) {
  1318. index = path.indexOf(item);
  1319. if (index >= 0) {
  1320. return path.splice(index, 1);
  1321. }
  1322. } else {
  1323. var arr = this._get(path);
  1324. index = arr.indexOf(item);
  1325. if (index >= 0) {
  1326. return this.splice(path, index, 1);
  1327. }
  1328. }
  1329. },
  1330. transform: function (transform, node) {
  1331. node = node || this;
  1332. node.style.webkitTransform = transform;
  1333. node.style.transform = transform;
  1334. },
  1335. translate3d: function (x, y, z, node) {
  1336. node = node || this;
  1337. this.transform('translate3d(' + x + ',' + y + ',' + z + ')', node);
  1338. },
  1339. importHref: function (href, onload, onerror) {
  1340. var l = document.createElement('link');
  1341. l.rel = 'import';
  1342. l.href = href;
  1343. if (onload) {
  1344. l.onload = onload.bind(this);
  1345. }
  1346. if (onerror) {
  1347. l.onerror = onerror.bind(this);
  1348. }
  1349. document.head.appendChild(l);
  1350. return l;
  1351. },
  1352. create: function (tag, props) {
  1353. var elt = document.createElement(tag);
  1354. if (props) {
  1355. for (var n in props) {
  1356. elt[n] = props[n];
  1357. }
  1358. }
  1359. return elt;
  1360. },
  1361. isLightDescendant: function (node) {
  1362. return this.contains(node) && Polymer.dom(this).getOwnerRoot() === Polymer.dom(node).getOwnerRoot();
  1363. },
  1364. isLocalDescendant: function (node) {
  1365. return this.root === Polymer.dom(node).getOwnerRoot();
  1366. }
  1367. });
  1368. Polymer.Bind = {
  1369. prepareModel: function (model) {
  1370. model._propertyEffects = {};
  1371. model._bindListeners = [];
  1372. Polymer.Base.mixin(model, this._modelApi);
  1373. },
  1374. _modelApi: {
  1375. _notifyChange: function (property) {
  1376. var eventName = Polymer.CaseMap.camelToDashCase(property) + '-changed';
  1377. Polymer.Base.fire(eventName, { value: this[property] }, {
  1378. bubbles: false,
  1379. node: this
  1380. });
  1381. },
  1382. _propertySetter: function (property, value, effects, fromAbove) {
  1383. var old = this.__data__[property];
  1384. if (old !== value && (old === old || value === value)) {
  1385. this.__data__[property] = value;
  1386. if (typeof value == 'object') {
  1387. this._clearPath(property);
  1388. }
  1389. if (this._propertyChanged) {
  1390. this._propertyChanged(property, value, old);
  1391. }
  1392. if (effects) {
  1393. this._effectEffects(property, value, effects, old, fromAbove);
  1394. }
  1395. }
  1396. return old;
  1397. },
  1398. __setProperty: function (property, value, quiet, node) {
  1399. node = node || this;
  1400. var effects = node._propertyEffects && node._propertyEffects[property];
  1401. if (effects) {
  1402. node._propertySetter(property, value, effects, quiet);
  1403. } else {
  1404. node[property] = value;
  1405. }
  1406. },
  1407. _effectEffects: function (property, value, effects, old, fromAbove) {
  1408. effects.forEach(function (fx) {
  1409. var fn = Polymer.Bind['_' + fx.kind + 'Effect'];
  1410. if (fn) {
  1411. fn.call(this, property, value, fx.effect, old, fromAbove);
  1412. }
  1413. }, this);
  1414. },
  1415. _clearPath: function (path) {
  1416. for (var prop in this.__data__) {
  1417. if (prop.indexOf(path + '.') === 0) {
  1418. this.__data__[prop] = undefined;
  1419. }
  1420. }
  1421. }
  1422. },
  1423. ensurePropertyEffects: function (model, property) {
  1424. var fx = model._propertyEffects[property];
  1425. if (!fx) {
  1426. fx = model._propertyEffects[property] = [];
  1427. }
  1428. return fx;
  1429. },
  1430. addPropertyEffect: function (model, property, kind, effect) {
  1431. var fx = this.ensurePropertyEffects(model, property);
  1432. fx.push({
  1433. kind: kind,
  1434. effect: effect
  1435. });
  1436. },
  1437. createBindings: function (model) {
  1438. var fx$ = model._propertyEffects;
  1439. if (fx$) {
  1440. for (var n in fx$) {
  1441. var fx = fx$[n];
  1442. fx.sort(this._sortPropertyEffects);
  1443. this._createAccessors(model, n, fx);
  1444. }
  1445. }
  1446. },
  1447. _sortPropertyEffects: function () {
  1448. var EFFECT_ORDER = {
  1449. 'compute': 0,
  1450. 'annotation': 1,
  1451. 'computedAnnotation': 2,
  1452. 'reflect': 3,
  1453. 'notify': 4,
  1454. 'observer': 5,
  1455. 'complexObserver': 6,
  1456. 'function': 7
  1457. };
  1458. return function (a, b) {
  1459. return EFFECT_ORDER[a.kind] - EFFECT_ORDER[b.kind];
  1460. };
  1461. }(),
  1462. _createAccessors: function (model, property, effects) {
  1463. var defun = {
  1464. get: function () {
  1465. return this.__data__[property];
  1466. }
  1467. };
  1468. var setter = function (value) {
  1469. this._propertySetter(property, value, effects);
  1470. };
  1471. var info = model.getPropertyInfo && model.getPropertyInfo(property);
  1472. if (info && info.readOnly) {
  1473. if (!info.computed) {
  1474. model['_set' + this.upper(property)] = setter;
  1475. }
  1476. } else {
  1477. defun.set = setter;
  1478. }
  1479. Object.defineProperty(model, property, defun);
  1480. },
  1481. upper: function (name) {
  1482. return name[0].toUpperCase() + name.substring(1);
  1483. },
  1484. _addAnnotatedListener: function (model, index, property, path, event) {
  1485. var fn = this._notedListenerFactory(property, path, this._isStructured(path), this._isEventBogus);
  1486. var eventName = event || Polymer.CaseMap.camelToDashCase(property) + '-changed';
  1487. model._bindListeners.push({
  1488. index: index,
  1489. property: property,
  1490. path: path,
  1491. changedFn: fn,
  1492. event: eventName
  1493. });
  1494. },
  1495. _isStructured: function (path) {
  1496. return path.indexOf('.') > 0;
  1497. },
  1498. _isEventBogus: function (e, target) {
  1499. return e.path && e.path[0] !== target;
  1500. },
  1501. _notedListenerFactory: function (property, path, isStructured, bogusTest) {
  1502. return function (e, target) {
  1503. if (!bogusTest(e, target)) {
  1504. if (e.detail && e.detail.path) {
  1505. this._notifyPath(this._fixPath(path, property, e.detail.path), e.detail.value);
  1506. } else {
  1507. var value = target[property];
  1508. if (!isStructured) {
  1509. this[path] = target[property];
  1510. } else {
  1511. if (this.__data__[path] != value) {
  1512. this.set(path, value);
  1513. }
  1514. }
  1515. }
  1516. }
  1517. };
  1518. },
  1519. prepareInstance: function (inst) {
  1520. inst.__data__ = Object.create(null);
  1521. },
  1522. setupBindListeners: function (inst) {
  1523. inst._bindListeners.forEach(function (info) {
  1524. var node = inst._nodes[info.index];
  1525. node.addEventListener(info.event, inst._notifyListener.bind(inst, info.changedFn));
  1526. });
  1527. }
  1528. };
  1529. Polymer.Base.extend(Polymer.Bind, {
  1530. _shouldAddListener: function (effect) {
  1531. return effect.name && effect.kind != 'attribute' && effect.kind != 'text' && !effect.isCompound && effect.parts[0].mode === '{' && !effect.parts[0].negate;
  1532. },
  1533. _annotationEffect: function (source, value, effect) {
  1534. if (source != effect.value) {
  1535. value = this._get(effect.value);
  1536. this.__data__[effect.value] = value;
  1537. }
  1538. var calc = effect.negate ? !value : value;
  1539. if (!effect.customEvent || this._nodes[effect.index][effect.name] !== calc) {
  1540. return this._applyEffectValue(effect, calc);
  1541. }
  1542. },
  1543. _reflectEffect: function (source) {
  1544. this.reflectPropertyToAttribute(source);
  1545. },
  1546. _notifyEffect: function (source, value, effect, old, fromAbove) {
  1547. if (!fromAbove) {
  1548. this._notifyChange(source);
  1549. }
  1550. },
  1551. _functionEffect: function (source, value, fn, old, fromAbove) {
  1552. fn.call(this, source, value, old, fromAbove);
  1553. },
  1554. _observerEffect: function (source, value, effect, old) {
  1555. var fn = this[effect.method];
  1556. if (fn) {
  1557. fn.call(this, value, old);
  1558. } else {
  1559. this._warn(this._logf('_observerEffect', 'observer method `' + effect.method + '` not defined'));
  1560. }
  1561. },
  1562. _complexObserverEffect: function (source, value, effect) {
  1563. var fn = this[effect.method];
  1564. if (fn) {
  1565. var args = Polymer.Bind._marshalArgs(this.__data__, effect, source, value);
  1566. if (args) {
  1567. fn.apply(this, args);
  1568. }
  1569. } else {
  1570. this._warn(this._logf('_complexObserverEffect', 'observer method `' + effect.method + '` not defined'));
  1571. }
  1572. },
  1573. _computeEffect: function (source, value, effect) {
  1574. var args = Polymer.Bind._marshalArgs(this.__data__, effect, source, value);
  1575. if (args) {
  1576. var fn = this[effect.method];
  1577. if (fn) {
  1578. this.__setProperty(effect.name, fn.apply(this, args));
  1579. } else {
  1580. this._warn(this._logf('_computeEffect', 'compute method `' + effect.method + '` not defined'));
  1581. }
  1582. }
  1583. },
  1584. _annotatedComputationEffect: function (source, value, effect) {
  1585. var computedHost = this._rootDataHost || this;
  1586. var fn = computedHost[effect.method];
  1587. if (fn) {
  1588. var args = Polymer.Bind._marshalArgs(this.__data__, effect, source, value);
  1589. if (args) {
  1590. var computedvalue = fn.apply(computedHost, args);
  1591. if (effect.negate) {
  1592. computedvalue = !computedvalue;
  1593. }
  1594. this._applyEffectValue(effect, computedvalue);
  1595. }
  1596. } else {
  1597. computedHost._warn(computedHost._logf('_annotatedComputationEffect', 'compute method `' + effect.method + '` not defined'));
  1598. }
  1599. },
  1600. _marshalArgs: function (model, effect, path, value) {
  1601. var values = [];
  1602. var args = effect.args;
  1603. for (var i = 0, l = args.length; i < l; i++) {
  1604. var arg = args[i];
  1605. var name = arg.name;
  1606. var v;
  1607. if (arg.literal) {
  1608. v = arg.value;
  1609. } else if (arg.structured) {
  1610. v = Polymer.Base._get(name, model);
  1611. } else {
  1612. v = model[name];
  1613. }
  1614. if (args.length > 1 && v === undefined) {
  1615. return;
  1616. }
  1617. if (arg.wildcard) {
  1618. var baseChanged = name.indexOf(path + '.') === 0;
  1619. var matches = effect.trigger.name.indexOf(name) === 0 && !baseChanged;
  1620. values[i] = {
  1621. path: matches ? path : name,
  1622. value: matches ? value : v,
  1623. base: v
  1624. };
  1625. } else {
  1626. values[i] = v;
  1627. }
  1628. }
  1629. return values;
  1630. }
  1631. });
  1632. Polymer.Base._addFeature({
  1633. _addPropertyEffect: function (property, kind, effect) {
  1634. Polymer.Bind.addPropertyEffect(this, property, kind, effect);
  1635. },
  1636. _prepEffects: function () {
  1637. Polymer.Bind.prepareModel(this);
  1638. this._addAnnotationEffects(this._notes);
  1639. },
  1640. _prepBindings: function () {
  1641. Polymer.Bind.createBindings(this);
  1642. },
  1643. _addPropertyEffects: function (properties) {
  1644. if (properties) {
  1645. for (var p in properties) {
  1646. var prop = properties[p];
  1647. if (prop.observer) {
  1648. this._addObserverEffect(p, prop.observer);
  1649. }
  1650. if (prop.computed) {
  1651. prop.readOnly = true;
  1652. this._addComputedEffect(p, prop.computed);
  1653. }
  1654. if (prop.notify) {
  1655. this._addPropertyEffect(p, 'notify');
  1656. }
  1657. if (prop.reflectToAttribute) {
  1658. this._addPropertyEffect(p, 'reflect');
  1659. }
  1660. if (prop.readOnly) {
  1661. Polymer.Bind.ensurePropertyEffects(this, p);
  1662. }
  1663. }
  1664. }
  1665. },
  1666. _addComputedEffect: function (name, expression) {
  1667. var sig = this._parseMethod(expression);
  1668. sig.args.forEach(function (arg) {
  1669. this._addPropertyEffect(arg.model, 'compute', {
  1670. method: sig.method,
  1671. args: sig.args,
  1672. trigger: arg,
  1673. name: name
  1674. });
  1675. }, this);
  1676. },
  1677. _addObserverEffect: function (property, observer) {
  1678. this._addPropertyEffect(property, 'observer', {
  1679. method: observer,
  1680. property: property
  1681. });
  1682. },
  1683. _addComplexObserverEffects: function (observers) {
  1684. if (observers) {
  1685. observers.forEach(function (observer) {
  1686. this._addComplexObserverEffect(observer);
  1687. }, this);
  1688. }
  1689. },
  1690. _addComplexObserverEffect: function (observer) {
  1691. var sig = this._parseMethod(observer);
  1692. sig.args.forEach(function (arg) {
  1693. this._addPropertyEffect(arg.model, 'complexObserver', {
  1694. method: sig.method,
  1695. args: sig.args,
  1696. trigger: arg
  1697. });
  1698. }, this);
  1699. },
  1700. _addAnnotationEffects: function (notes) {
  1701. this._nodes = [];
  1702. notes.forEach(function (note) {
  1703. var index = this._nodes.push(note) - 1;
  1704. note.bindings.forEach(function (binding) {
  1705. this._addAnnotationEffect(binding, index);
  1706. }, this);
  1707. }, this);
  1708. },
  1709. _addAnnotationEffect: function (note, index) {
  1710. if (Polymer.Bind._shouldAddListener(note)) {
  1711. Polymer.Bind._addAnnotatedListener(this, index, note.name, note.parts[0].value, note.parts[0].event);
  1712. }
  1713. for (var i = 0; i < note.parts.length; i++) {
  1714. var part = note.parts[i];
  1715. if (part.signature) {
  1716. this._addAnnotatedComputationEffect(note, part, index);
  1717. } else if (!part.literal) {
  1718. this._addPropertyEffect(part.model, 'annotation', {
  1719. kind: note.kind,
  1720. index: index,
  1721. name: note.name,
  1722. value: part.value,
  1723. isCompound: note.isCompound,
  1724. compoundIndex: part.compoundIndex,
  1725. event: part.event,
  1726. customEvent: part.customEvent,
  1727. negate: part.negate
  1728. });
  1729. }
  1730. }
  1731. },
  1732. _addAnnotatedComputationEffect: function (note, part, index) {
  1733. var sig = part.signature;
  1734. if (sig.static) {
  1735. this.__addAnnotatedComputationEffect('__static__', index, note, part, null);
  1736. } else {
  1737. sig.args.forEach(function (arg) {
  1738. if (!arg.literal) {
  1739. this.__addAnnotatedComputationEffect(arg.model, index, note, part, arg);
  1740. }
  1741. }, this);
  1742. }
  1743. },
  1744. __addAnnotatedComputationEffect: function (property, index, note, part, trigger) {
  1745. this._addPropertyEffect(property, 'annotatedComputation', {
  1746. index: index,
  1747. isCompound: note.isCompound,
  1748. compoundIndex: part.compoundIndex,
  1749. kind: note.kind,
  1750. name: note.name,
  1751. negate: part.negate,
  1752. method: part.signature.method,
  1753. args: part.signature.args,
  1754. trigger: trigger
  1755. });
  1756. },
  1757. _parseMethod: function (expression) {
  1758. var m = expression.match(/([^\s]+)\((.*)\)/);
  1759. if (m) {
  1760. var sig = {
  1761. method: m[1],
  1762. static: true
  1763. };
  1764. if (m[2].trim()) {
  1765. var args = m[2].replace(/\\,/g, '&comma;').split(',');
  1766. return this._parseArgs(args, sig);
  1767. } else {
  1768. sig.args = Polymer.nar;
  1769. return sig;
  1770. }
  1771. }
  1772. },
  1773. _parseArgs: function (argList, sig) {
  1774. sig.args = argList.map(function (rawArg) {
  1775. var arg = this._parseArg(rawArg);
  1776. if (!arg.literal) {
  1777. sig.static = false;
  1778. }
  1779. return arg;
  1780. }, this);
  1781. return sig;
  1782. },
  1783. _parseArg: function (rawArg) {
  1784. var arg = rawArg.trim().replace(/&comma;/g, ',').replace(/\\(.)/g, '$1');
  1785. var a = {
  1786. name: arg,
  1787. model: this._modelForPath(arg)
  1788. };
  1789. var fc = arg[0];
  1790. if (fc === '-') {
  1791. fc = arg[1];
  1792. }
  1793. if (fc >= '0' && fc <= '9') {
  1794. fc = '#';
  1795. }
  1796. switch (fc) {
  1797. case '\'':
  1798. case '"':
  1799. a.value = arg.slice(1, -1);
  1800. a.literal = true;
  1801. break;
  1802. case '#':
  1803. a.value = Number(arg);
  1804. a.literal = true;
  1805. break;
  1806. }
  1807. if (!a.literal) {
  1808. a.structured = arg.indexOf('.') > 0;
  1809. if (a.structured) {
  1810. a.wildcard = arg.slice(-2) == '.*';
  1811. if (a.wildcard) {
  1812. a.name = arg.slice(0, -2);
  1813. }
  1814. }
  1815. }
  1816. return a;
  1817. },
  1818. _marshalInstanceEffects: function () {
  1819. Polymer.Bind.prepareInstance(this);
  1820. Polymer.Bind.setupBindListeners(this);
  1821. },
  1822. _applyEffectValue: function (info, value) {
  1823. var node = this._nodes[info.index];
  1824. var property = info.name;
  1825. if (info.isCompound) {
  1826. var storage = node.__compoundStorage__[property];
  1827. storage[info.compoundIndex] = value;
  1828. value = storage.join('');
  1829. }
  1830. if (info.kind == 'attribute') {
  1831. this.serializeValueToAttribute(value, property, node);
  1832. } else {
  1833. if (property === 'className') {
  1834. value = this._scopeElementClass(node, value);
  1835. }
  1836. if (property === 'textContent' || node.localName == 'input' && property == 'value') {
  1837. value = value == undefined ? '' : value;
  1838. }
  1839. return node[property] = value;
  1840. }
  1841. },
  1842. _executeStaticEffects: function () {
  1843. if (this._propertyEffects.__static__) {
  1844. this._effectEffects('__static__', null, this._propertyEffects.__static__);
  1845. }
  1846. }
  1847. });
  1848. Polymer.Base._addFeature({
  1849. _setupConfigure: function (initialConfig) {
  1850. this._config = {};
  1851. for (var i in initialConfig) {
  1852. if (initialConfig[i] !== undefined) {
  1853. this._config[i] = initialConfig[i];
  1854. }
  1855. }
  1856. this._handlers = [];
  1857. },
  1858. _marshalAttributes: function () {
  1859. this._takeAttributesToModel(this._config);
  1860. },
  1861. _attributeChangedImpl: function (name) {
  1862. var model = this._clientsReadied ? this : this._config;
  1863. this._setAttributeToProperty(model, name);
  1864. },
  1865. _configValue: function (name, value) {
  1866. this._config[name] = value;
  1867. },
  1868. _beforeClientsReady: function () {
  1869. this._configure();
  1870. },
  1871. _configure: function () {
  1872. this._configureAnnotationReferences();
  1873. this._aboveConfig = this.mixin({}, this._config);
  1874. var config = {};
  1875. this.behaviors.forEach(function (b) {
  1876. this._configureProperties(b.properties, config);
  1877. }, this);
  1878. this._configureProperties(this.properties, config);
  1879. this._mixinConfigure(config, this._aboveConfig);
  1880. this._config = config;
  1881. this._distributeConfig(this._config);
  1882. },
  1883. _configureProperties: function (properties, config) {
  1884. for (var i in properties) {
  1885. var c = properties[i];
  1886. if (c.value !== undefined) {
  1887. var value = c.value;
  1888. if (typeof value == 'function') {
  1889. value = value.call(this, this._config);
  1890. }
  1891. config[i] = value;
  1892. }
  1893. }
  1894. },
  1895. _mixinConfigure: function (a, b) {
  1896. for (var prop in b) {
  1897. if (!this.getPropertyInfo(prop).readOnly) {
  1898. a[prop] = b[prop];
  1899. }
  1900. }
  1901. },
  1902. _distributeConfig: function (config) {
  1903. var fx$ = this._propertyEffects;
  1904. if (fx$) {
  1905. for (var p in config) {
  1906. var fx = fx$[p];
  1907. if (fx) {
  1908. for (var i = 0, l = fx.length, x; i < l && (x = fx[i]); i++) {
  1909. if (x.kind === 'annotation' && !x.isCompound) {
  1910. var node = this._nodes[x.effect.index];
  1911. if (node._configValue) {
  1912. var value = p === x.effect.value ? config[p] : this._get(x.effect.value, config);
  1913. node._configValue(x.effect.name, value);
  1914. }
  1915. }
  1916. }
  1917. }
  1918. }
  1919. }
  1920. },
  1921. _afterClientsReady: function () {
  1922. this._executeStaticEffects();
  1923. this._applyConfig(this._config, this._aboveConfig);
  1924. this._flushHandlers();
  1925. },
  1926. _applyConfig: function (config, aboveConfig) {
  1927. for (var n in config) {
  1928. if (this[n] === undefined) {
  1929. this.__setProperty(n, config[n], n in aboveConfig);
  1930. }
  1931. }
  1932. },
  1933. _notifyListener: function (fn, e) {
  1934. if (!this._clientsReadied) {
  1935. this._queueHandler([
  1936. fn,
  1937. e,
  1938. e.target
  1939. ]);
  1940. } else {
  1941. return fn.call(this, e, e.target);
  1942. }
  1943. },
  1944. _queueHandler: function (args) {
  1945. this._handlers.push(args);
  1946. },
  1947. _flushHandlers: function () {
  1948. var h$ = this._handlers;
  1949. for (var i = 0, l = h$.length, h; i < l && (h = h$[i]); i++) {
  1950. h[0].call(this, h[1], h[2]);
  1951. }
  1952. this._handlers = [];
  1953. }
  1954. });
  1955. (function () {
  1956. 'use strict';
  1957. Polymer.Base._addFeature({
  1958. notifyPath: function (path, value, fromAbove) {
  1959. var info = {};
  1960. path = this._get(path, this, info);
  1961. this._notifyPath(info.path, value, fromAbove);
  1962. },
  1963. _notifyPath: function (path, value, fromAbove) {
  1964. var old = this._propertySetter(path, value);
  1965. if (old !== value && (old === old || value === value)) {
  1966. this._pathEffector(path, value);
  1967. if (!fromAbove) {
  1968. this._notifyPathUp(path, value);
  1969. }
  1970. return true;
  1971. }
  1972. },
  1973. _getPathParts: function (path) {
  1974. if (Array.isArray(path)) {
  1975. var parts = [];
  1976. for (var i = 0; i < path.length; i++) {
  1977. var args = path[i].toString().split('.');
  1978. for (var j = 0; j < args.length; j++) {
  1979. parts.push(args[j]);
  1980. }
  1981. }
  1982. return parts;
  1983. } else {
  1984. return path.toString().split('.');
  1985. }
  1986. },
  1987. set: function (path, value, root) {
  1988. var prop = root || this;
  1989. var parts = this._getPathParts(path);
  1990. var array;
  1991. var last = parts[parts.length - 1];
  1992. if (parts.length > 1) {
  1993. for (var i = 0; i < parts.length - 1; i++) {
  1994. var part = parts[i];
  1995. if (array && part[0] == '#') {
  1996. prop = Polymer.Collection.get(array).getItem(part);
  1997. } else {
  1998. prop = prop[part];
  1999. if (array && parseInt(part, 10) == part) {
  2000. parts[i] = Polymer.Collection.get(array).getKey(prop);
  2001. }
  2002. }
  2003. if (!prop) {
  2004. return;
  2005. }
  2006. array = Array.isArray(prop) ? prop : null;
  2007. }
  2008. if (array) {
  2009. var coll = Polymer.Collection.get(array);
  2010. if (last[0] == '#') {
  2011. var key = last;
  2012. var old = coll.getItem(key);
  2013. last = array.indexOf(old);
  2014. coll.setItem(key, value);
  2015. } else if (parseInt(last, 10) == last) {
  2016. var old = prop[last];
  2017. var key = coll.getKey(old);
  2018. parts[i] = key;
  2019. coll.setItem(key, value);
  2020. }
  2021. }
  2022. prop[last] = value;
  2023. if (!root) {
  2024. this._notifyPath(parts.join('.'), value);
  2025. }
  2026. } else {
  2027. prop[path] = value;
  2028. }
  2029. },
  2030. get: function (path, root) {
  2031. return this._get(path, root);
  2032. },
  2033. _get: function (path, root, info) {
  2034. var prop = root || this;
  2035. var parts = this._getPathParts(path);
  2036. var array;
  2037. for (var i = 0; i < parts.length; i++) {
  2038. if (!prop) {
  2039. return;
  2040. }
  2041. var part = parts[i];
  2042. if (array && part[0] == '#') {
  2043. prop = Polymer.Collection.get(array).getItem(part);
  2044. } else {
  2045. prop = prop[part];
  2046. if (info && array && parseInt(part, 10) == part) {
  2047. parts[i] = Polymer.Collection.get(array).getKey(prop);
  2048. }
  2049. }
  2050. array = Array.isArray(prop) ? prop : null;
  2051. }
  2052. if (info) {
  2053. info.path = parts.join('.');
  2054. }
  2055. return prop;
  2056. },
  2057. _pathEffector: function (path, value) {
  2058. var model = this._modelForPath(path);
  2059. var fx$ = this._propertyEffects[model];
  2060. if (fx$) {
  2061. fx$.forEach(function (fx) {
  2062. var fxFn = this['_' + fx.kind + 'PathEffect'];
  2063. if (fxFn) {
  2064. fxFn.call(this, path, value, fx.effect);
  2065. }
  2066. }, this);
  2067. }
  2068. if (this._boundPaths) {
  2069. this._notifyBoundPaths(path, value);
  2070. }
  2071. },
  2072. _annotationPathEffect: function (path, value, effect) {
  2073. if (effect.value === path || effect.value.indexOf(path + '.') === 0) {
  2074. Polymer.Bind._annotationEffect.call(this, path, value, effect);
  2075. } else if (path.indexOf(effect.value + '.') === 0 && !effect.negate) {
  2076. var node = this._nodes[effect.index];
  2077. if (node && node.notifyPath) {
  2078. var p = this._fixPath(effect.name, effect.value, path);
  2079. node.notifyPath(p, value, true);
  2080. }
  2081. }
  2082. },
  2083. _complexObserverPathEffect: function (path, value, effect) {
  2084. if (this._pathMatchesEffect(path, effect)) {
  2085. Polymer.Bind._complexObserverEffect.call(this, path, value, effect);
  2086. }
  2087. },
  2088. _computePathEffect: function (path, value, effect) {
  2089. if (this._pathMatchesEffect(path, effect)) {
  2090. Polymer.Bind._computeEffect.call(this, path, value, effect);
  2091. }
  2092. },
  2093. _annotatedComputationPathEffect: function (path, value, effect) {
  2094. if (this._pathMatchesEffect(path, effect)) {
  2095. Polymer.Bind._annotatedComputationEffect.call(this, path, value, effect);
  2096. }
  2097. },
  2098. _pathMatchesEffect: function (path, effect) {
  2099. var effectArg = effect.trigger.name;
  2100. return effectArg == path || effectArg.indexOf(path + '.') === 0 || effect.trigger.wildcard && path.indexOf(effectArg) === 0;
  2101. },
  2102. linkPaths: function (to, from) {
  2103. this._boundPaths = this._boundPaths || {};
  2104. if (from) {
  2105. this._boundPaths[to] = from;
  2106. } else {
  2107. this.unlinkPaths(to);
  2108. }
  2109. },
  2110. unlinkPaths: function (path) {
  2111. if (this._boundPaths) {
  2112. delete this._boundPaths[path];
  2113. }
  2114. },
  2115. _notifyBoundPaths: function (path, value) {
  2116. for (var a in this._boundPaths) {
  2117. var b = this._boundPaths[a];
  2118. if (path.indexOf(a + '.') == 0) {
  2119. this.notifyPath(this._fixPath(b, a, path), value);
  2120. } else if (path.indexOf(b + '.') == 0) {
  2121. this.notifyPath(this._fixPath(a, b, path), value);
  2122. }
  2123. }
  2124. },
  2125. _fixPath: function (property, root, path) {
  2126. return property + path.slice(root.length);
  2127. },
  2128. _notifyPathUp: function (path, value) {
  2129. var rootName = this._modelForPath(path);
  2130. var dashCaseName = Polymer.CaseMap.camelToDashCase(rootName);
  2131. var eventName = dashCaseName + this._EVENT_CHANGED;
  2132. this.fire(eventName, {
  2133. path: path,
  2134. value: value
  2135. }, { bubbles: false });
  2136. },
  2137. _modelForPath: function (path) {
  2138. var dot = path.indexOf('.');
  2139. return dot < 0 ? path : path.slice(0, dot);
  2140. },
  2141. _EVENT_CHANGED: '-changed',
  2142. notifySplices: function (path, splices) {
  2143. var info = {};
  2144. var array = this._get(path, this, info);
  2145. this._notifySplices(array, info.path, splices);
  2146. },
  2147. _notifySplices: function (array, path, splices) {
  2148. var change = {
  2149. keySplices: Polymer.Collection.applySplices(array, splices),
  2150. indexSplices: splices
  2151. };
  2152. if (!array.hasOwnProperty('splices')) {
  2153. Object.defineProperty(array, 'splices', {
  2154. configurable: true,
  2155. writable: true
  2156. });
  2157. }
  2158. array.splices = change;
  2159. this._notifyPath(path + '.splices', change);
  2160. this._notifyPath(path + '.length', array.length);
  2161. change.keySplices = null;
  2162. change.indexSplices = null;
  2163. },
  2164. _notifySplice: function (array, path, index, added, removed) {
  2165. this._notifySplices(array, path, [{
  2166. index: index,
  2167. addedCount: added,
  2168. removed: removed,
  2169. object: array,
  2170. type: 'splice'
  2171. }]);
  2172. },
  2173. push: function (path) {
  2174. var info = {};
  2175. var array = this._get(path, this, info);
  2176. var args = Array.prototype.slice.call(arguments, 1);
  2177. var len = array.length;
  2178. var ret = array.push.apply(array, args);
  2179. if (args.length) {
  2180. this._notifySplice(array, info.path, len, args.length, []);
  2181. }
  2182. return ret;
  2183. },
  2184. pop: function (path) {
  2185. var info = {};
  2186. var array = this._get(path, this, info);
  2187. var hadLength = Boolean(array.length);
  2188. var args = Array.prototype.slice.call(arguments, 1);
  2189. var ret = array.pop.apply(array, args);
  2190. if (hadLength) {
  2191. this._notifySplice(array, info.path, array.length, 0, [ret]);
  2192. }
  2193. return ret;
  2194. },
  2195. splice: function (path, start, deleteCount) {
  2196. var info = {};
  2197. var array = this._get(path, this, info);
  2198. if (start < 0) {
  2199. start = array.length - Math.floor(-start);
  2200. } else {
  2201. start = Math.floor(start);
  2202. }
  2203. if (!start) {
  2204. start = 0;
  2205. }
  2206. var args = Array.prototype.slice.call(arguments, 1);
  2207. var ret = array.splice.apply(array, args);
  2208. var addedCount = Math.max(args.length - 2, 0);
  2209. if (addedCount || ret.length) {
  2210. this._notifySplice(array, info.path, start, addedCount, ret);
  2211. }
  2212. return ret;
  2213. },
  2214. shift: function (path) {
  2215. var info = {};
  2216. var array = this._get(path, this, info);
  2217. var hadLength = Boolean(array.length);
  2218. var args = Array.prototype.slice.call(arguments, 1);
  2219. var ret = array.shift.apply(array, args);
  2220. if (hadLength) {
  2221. this._notifySplice(array, info.path, 0, 0, [ret]);
  2222. }
  2223. return ret;
  2224. },
  2225. unshift: function (path) {
  2226. var info = {};
  2227. var array = this._get(path, this, info);
  2228. var args = Array.prototype.slice.call(arguments, 1);
  2229. var ret = array.unshift.apply(array, args);
  2230. if (args.length) {
  2231. this._notifySplice(array, info.path, 0, args.length, []);
  2232. }
  2233. return ret;
  2234. },
  2235. prepareModelNotifyPath: function (model) {
  2236. this.mixin(model, {
  2237. fire: Polymer.Base.fire,
  2238. notifyPath: Polymer.Base.notifyPath,
  2239. _get: Polymer.Base._get,
  2240. _EVENT_CHANGED: Polymer.Base._EVENT_CHANGED,
  2241. _notifyPath: Polymer.Base._notifyPath,
  2242. _notifyPathUp: Polymer.Base._notifyPathUp,
  2243. _pathEffector: Polymer.Base._pathEffector,
  2244. _annotationPathEffect: Polymer.Base._annotationPathEffect,
  2245. _complexObserverPathEffect: Polymer.Base._complexObserverPathEffect,
  2246. _annotatedComputationPathEffect: Polymer.Base._annotatedComputationPathEffect,
  2247. _computePathEffect: Polymer.Base._computePathEffect,
  2248. _modelForPath: Polymer.Base._modelForPath,
  2249. _pathMatchesEffect: Polymer.Base._pathMatchesEffect,
  2250. _notifyBoundPaths: Polymer.Base._notifyBoundPaths,
  2251. _getPathParts: Polymer.Base._getPathParts
  2252. });
  2253. }
  2254. });
  2255. }());
  2256. Polymer.Base._addFeature({
  2257. resolveUrl: function (url) {
  2258. var module = Polymer.DomModule.import(this.is);
  2259. var root = '';
  2260. if (module) {
  2261. var assetPath = module.getAttribute('assetpath') || '';
  2262. root = Polymer.ResolveUrl.resolveUrl(assetPath, module.ownerDocument.baseURI);
  2263. }
  2264. return Polymer.ResolveUrl.resolveUrl(url, root);
  2265. }
  2266. });
  2267. Polymer.CssParse = function () {
  2268. var api = {
  2269. parse: function (text) {
  2270. text = this._clean(text);
  2271. return this._parseCss(this._lex(text), text);
  2272. },
  2273. _clean: function (cssText) {
  2274. return cssText.replace(this._rx.comments, '').replace(this._rx.port, '');
  2275. },
  2276. _lex: function (text) {
  2277. var root = {
  2278. start: 0,
  2279. end: text.length
  2280. };
  2281. var n = root;
  2282. for (var i = 0, s = 0, l = text.length; i < l; i++) {
  2283. switch (text[i]) {
  2284. case this.OPEN_BRACE:
  2285. if (!n.rules) {
  2286. n.rules = [];
  2287. }
  2288. var p = n;
  2289. var previous = p.rules[p.rules.length - 1];
  2290. n = {
  2291. start: i + 1,
  2292. parent: p,
  2293. previous: previous
  2294. };
  2295. p.rules.push(n);
  2296. break;
  2297. case this.CLOSE_BRACE:
  2298. n.end = i + 1;
  2299. n = n.parent || root;
  2300. break;
  2301. }
  2302. }
  2303. return root;
  2304. },
  2305. _parseCss: function (node, text) {
  2306. var t = text.substring(node.start, node.end - 1);
  2307. node.parsedCssText = node.cssText = t.trim();
  2308. if (node.parent) {
  2309. var ss = node.previous ? node.previous.end : node.parent.start;
  2310. t = text.substring(ss, node.start - 1);
  2311. t = t.substring(t.lastIndexOf(';') + 1);
  2312. var s = node.parsedSelector = node.selector = t.trim();
  2313. node.atRule = s.indexOf(this.AT_START) === 0;
  2314. if (node.atRule) {
  2315. if (s.indexOf(this.MEDIA_START) === 0) {
  2316. node.type = this.types.MEDIA_RULE;
  2317. } else if (s.match(this._rx.keyframesRule)) {
  2318. node.type = this.types.KEYFRAMES_RULE;
  2319. }
  2320. } else {
  2321. if (s.indexOf(this.VAR_START) === 0) {
  2322. node.type = this.types.MIXIN_RULE;
  2323. } else {
  2324. node.type = this.types.STYLE_RULE;
  2325. }
  2326. }
  2327. }
  2328. var r$ = node.rules;
  2329. if (r$) {
  2330. for (var i = 0, l = r$.length, r; i < l && (r = r$[i]); i++) {
  2331. this._parseCss(r, text);
  2332. }
  2333. }
  2334. return node;
  2335. },
  2336. stringify: function (node, preserveProperties, text) {
  2337. text = text || '';
  2338. var cssText = '';
  2339. if (node.cssText || node.rules) {
  2340. var r$ = node.rules;
  2341. if (r$ && (preserveProperties || !this._hasMixinRules(r$))) {
  2342. for (var i = 0, l = r$.length, r; i < l && (r = r$[i]); i++) {
  2343. cssText = this.stringify(r, preserveProperties, cssText);
  2344. }
  2345. } else {
  2346. cssText = preserveProperties ? node.cssText : this.removeCustomProps(node.cssText);
  2347. cssText = cssText.trim();
  2348. if (cssText) {
  2349. cssText = ' ' + cssText + '\n';
  2350. }
  2351. }
  2352. }
  2353. if (cssText) {
  2354. if (node.selector) {
  2355. text += node.selector + ' ' + this.OPEN_BRACE + '\n';
  2356. }
  2357. text += cssText;
  2358. if (node.selector) {
  2359. text += this.CLOSE_BRACE + '\n\n';
  2360. }
  2361. }
  2362. return text;
  2363. },
  2364. _hasMixinRules: function (rules) {
  2365. return rules[0].selector.indexOf(this.VAR_START) >= 0;
  2366. },
  2367. removeCustomProps: function (cssText) {
  2368. cssText = this.removeCustomPropAssignment(cssText);
  2369. return this.removeCustomPropApply(cssText);
  2370. },
  2371. removeCustomPropAssignment: function (cssText) {
  2372. return cssText.replace(this._rx.customProp, '').replace(this._rx.mixinProp, '');
  2373. },
  2374. removeCustomPropApply: function (cssText) {
  2375. return cssText.replace(this._rx.mixinApply, '').replace(this._rx.varApply, '');
  2376. },
  2377. types: {
  2378. STYLE_RULE: 1,
  2379. KEYFRAMES_RULE: 7,
  2380. MEDIA_RULE: 4,
  2381. MIXIN_RULE: 1000
  2382. },
  2383. OPEN_BRACE: '{',
  2384. CLOSE_BRACE: '}',
  2385. _rx: {
  2386. comments: /\/\*[^*]*\*+([^\/*][^*]*\*+)*\//gim,
  2387. port: /@import[^;]*;/gim,
  2388. customProp: /(?:^|[\s;])--[^;{]*?:[^{};]*?(?:[;\n]|$)/gim,
  2389. mixinProp: /(?:^|[\s;])--[^;{]*?:[^{;]*?{[^}]*?}(?:[;\n]|$)?/gim,
  2390. mixinApply: /@apply[\s]*\([^)]*?\)[\s]*(?:[;\n]|$)?/gim,
  2391. varApply: /[^;:]*?:[^;]*var[^;]*(?:[;\n]|$)?/gim,
  2392. keyframesRule: /^@[^\s]*keyframes/
  2393. },
  2394. VAR_START: '--',
  2395. MEDIA_START: '@media',
  2396. AT_START: '@'
  2397. };
  2398. return api;
  2399. }();
  2400. Polymer.StyleUtil = function () {
  2401. return {
  2402. MODULE_STYLES_SELECTOR: 'style, link[rel=import][type~=css], template',
  2403. INCLUDE_ATTR: 'include',
  2404. toCssText: function (rules, callback, preserveProperties) {
  2405. if (typeof rules === 'string') {
  2406. rules = this.parser.parse(rules);
  2407. }
  2408. if (callback) {
  2409. this.forEachStyleRule(rules, callback);
  2410. }
  2411. return this.parser.stringify(rules, preserveProperties);
  2412. },
  2413. forRulesInStyles: function (styles, callback) {
  2414. if (styles) {
  2415. for (var i = 0, l = styles.length, s; i < l && (s = styles[i]); i++) {
  2416. this.forEachStyleRule(this.rulesForStyle(s), callback);
  2417. }
  2418. }
  2419. },
  2420. rulesForStyle: function (style) {
  2421. if (!style.__cssRules && style.textContent) {
  2422. style.__cssRules = this.parser.parse(style.textContent);
  2423. }
  2424. return style.__cssRules;
  2425. },
  2426. clearStyleRules: function (style) {
  2427. style.__cssRules = null;
  2428. },
  2429. forEachStyleRule: function (node, callback) {
  2430. if (!node) {
  2431. return;
  2432. }
  2433. var s = node.parsedSelector;
  2434. var skipRules = false;
  2435. if (node.type === this.ruleTypes.STYLE_RULE) {
  2436. callback(node);
  2437. } else if (node.type === this.ruleTypes.KEYFRAMES_RULE || node.type === this.ruleTypes.MIXIN_RULE) {
  2438. skipRules = true;
  2439. }
  2440. var r$ = node.rules;
  2441. if (r$ && !skipRules) {
  2442. for (var i = 0, l = r$.length, r; i < l && (r = r$[i]); i++) {
  2443. this.forEachStyleRule(r, callback);
  2444. }
  2445. }
  2446. },
  2447. applyCss: function (cssText, moniker, target, afterNode) {
  2448. var style = document.createElement('style');
  2449. if (moniker) {
  2450. style.setAttribute('scope', moniker);
  2451. }
  2452. style.textContent = cssText;
  2453. target = target || document.head;
  2454. if (!afterNode) {
  2455. var n$ = target.querySelectorAll('style[scope]');
  2456. afterNode = n$[n$.length - 1];
  2457. }
  2458. target.insertBefore(style, afterNode && afterNode.nextSibling || target.firstChild);
  2459. return style;
  2460. },
  2461. cssFromModules: function (moduleIds, warnIfNotFound) {
  2462. var modules = moduleIds.trim().split(' ');
  2463. var cssText = '';
  2464. for (var i = 0; i < modules.length; i++) {
  2465. cssText += this.cssFromModule(modules[i], warnIfNotFound);
  2466. }
  2467. return cssText;
  2468. },
  2469. cssFromModule: function (moduleId, warnIfNotFound) {
  2470. var m = Polymer.DomModule.import(moduleId);
  2471. if (m && !m._cssText) {
  2472. m._cssText = this._cssFromElement(m);
  2473. }
  2474. if (!m && warnIfNotFound) {
  2475. console.warn('Could not find style data in module named', moduleId);
  2476. }
  2477. return m && m._cssText || '';
  2478. },
  2479. _cssFromElement: function (element) {
  2480. var cssText = '';
  2481. var content = element.content || element;
  2482. var e$ = Array.prototype.slice.call(content.querySelectorAll(this.MODULE_STYLES_SELECTOR));
  2483. for (var i = 0, e; i < e$.length; i++) {
  2484. e = e$[i];
  2485. if (e.localName === 'template') {
  2486. cssText += this._cssFromElement(e);
  2487. } else {
  2488. if (e.localName === 'style') {
  2489. var include = e.getAttribute(this.INCLUDE_ATTR);
  2490. if (include) {
  2491. cssText += this.cssFromModules(include, true);
  2492. }
  2493. e = e.__appliedElement || e;
  2494. e.parentNode.removeChild(e);
  2495. cssText += this.resolveCss(e.textContent, element.ownerDocument);
  2496. } else if (e.import && e.import.body) {
  2497. cssText += this.resolveCss(e.import.body.textContent, e.import);
  2498. }
  2499. }
  2500. }
  2501. return cssText;
  2502. },
  2503. resolveCss: Polymer.ResolveUrl.resolveCss,
  2504. parser: Polymer.CssParse,
  2505. ruleTypes: Polymer.CssParse.types
  2506. };
  2507. }();
  2508. Polymer.StyleTransformer = function () {
  2509. var nativeShadow = Polymer.Settings.useNativeShadow;
  2510. var styleUtil = Polymer.StyleUtil;
  2511. var api = {
  2512. dom: function (node, scope, useAttr, shouldRemoveScope) {
  2513. this._transformDom(node, scope || '', useAttr, shouldRemoveScope);
  2514. },
  2515. _transformDom: function (node, selector, useAttr, shouldRemoveScope) {
  2516. if (node.setAttribute) {
  2517. this.element(node, selector, useAttr, shouldRemoveScope);
  2518. }
  2519. var c$ = Polymer.dom(node).childNodes;
  2520. for (var i = 0; i < c$.length; i++) {
  2521. this._transformDom(c$[i], selector, useAttr, shouldRemoveScope);
  2522. }
  2523. },
  2524. element: function (element, scope, useAttr, shouldRemoveScope) {
  2525. if (useAttr) {
  2526. if (shouldRemoveScope) {
  2527. element.removeAttribute(SCOPE_NAME);
  2528. } else {
  2529. element.setAttribute(SCOPE_NAME, scope);
  2530. }
  2531. } else {
  2532. if (scope) {
  2533. if (element.classList) {
  2534. if (shouldRemoveScope) {
  2535. element.classList.remove(SCOPE_NAME);
  2536. element.classList.remove(scope);
  2537. } else {
  2538. element.classList.add(SCOPE_NAME);
  2539. element.classList.add(scope);
  2540. }
  2541. } else if (element.getAttribute) {
  2542. var c = element.getAttribute(CLASS);
  2543. if (shouldRemoveScope) {
  2544. if (c) {
  2545. element.setAttribute(CLASS, c.replace(SCOPE_NAME, '').replace(scope, ''));
  2546. }
  2547. } else {
  2548. element.setAttribute(CLASS, c + (c ? ' ' : '') + SCOPE_NAME + ' ' + scope);
  2549. }
  2550. }
  2551. }
  2552. }
  2553. },
  2554. elementStyles: function (element, callback) {
  2555. var styles = element._styles;
  2556. var cssText = '';
  2557. for (var i = 0, l = styles.length, s, text; i < l && (s = styles[i]); i++) {
  2558. var rules = styleUtil.rulesForStyle(s);
  2559. cssText += nativeShadow ? styleUtil.toCssText(rules, callback) : this.css(rules, element.is, element.extends, callback, element._scopeCssViaAttr) + '\n\n';
  2560. }
  2561. return cssText.trim();
  2562. },
  2563. css: function (rules, scope, ext, callback, useAttr) {
  2564. var hostScope = this._calcHostScope(scope, ext);
  2565. scope = this._calcElementScope(scope, useAttr);
  2566. var self = this;
  2567. return styleUtil.toCssText(rules, function (rule) {
  2568. if (!rule.isScoped) {
  2569. self.rule(rule, scope, hostScope);
  2570. rule.isScoped = true;
  2571. }
  2572. if (callback) {
  2573. callback(rule, scope, hostScope);
  2574. }
  2575. });
  2576. },
  2577. _calcElementScope: function (scope, useAttr) {
  2578. if (scope) {
  2579. return useAttr ? CSS_ATTR_PREFIX + scope + CSS_ATTR_SUFFIX : CSS_CLASS_PREFIX + scope;
  2580. } else {
  2581. return '';
  2582. }
  2583. },
  2584. _calcHostScope: function (scope, ext) {
  2585. return ext ? '[is=' + scope + ']' : scope;
  2586. },
  2587. rule: function (rule, scope, hostScope) {
  2588. this._transformRule(rule, this._transformComplexSelector, scope, hostScope);
  2589. },
  2590. _transformRule: function (rule, transformer, scope, hostScope) {
  2591. var p$ = rule.selector.split(COMPLEX_SELECTOR_SEP);
  2592. for (var i = 0, l = p$.length, p; i < l && (p = p$[i]); i++) {
  2593. p$[i] = transformer.call(this, p, scope, hostScope);
  2594. }
  2595. rule.selector = rule.transformedSelector = p$.join(COMPLEX_SELECTOR_SEP);
  2596. },
  2597. _transformComplexSelector: function (selector, scope, hostScope) {
  2598. var stop = false;
  2599. var hostContext = false;
  2600. var self = this;
  2601. selector = selector.replace(SIMPLE_SELECTOR_SEP, function (m, c, s) {
  2602. if (!stop) {
  2603. var info = self._transformCompoundSelector(s, c, scope, hostScope);
  2604. stop = stop || info.stop;
  2605. hostContext = hostContext || info.hostContext;
  2606. c = info.combinator;
  2607. s = info.value;
  2608. } else {
  2609. s = s.replace(SCOPE_JUMP, ' ');
  2610. }
  2611. return c + s;
  2612. });
  2613. if (hostContext) {
  2614. selector = selector.replace(HOST_CONTEXT_PAREN, function (m, pre, paren, post) {
  2615. return pre + paren + ' ' + hostScope + post + COMPLEX_SELECTOR_SEP + ' ' + pre + hostScope + paren + post;
  2616. });
  2617. }
  2618. return selector;
  2619. },
  2620. _transformCompoundSelector: function (selector, combinator, scope, hostScope) {
  2621. var jumpIndex = selector.search(SCOPE_JUMP);
  2622. var hostContext = false;
  2623. if (selector.indexOf(HOST_CONTEXT) >= 0) {
  2624. hostContext = true;
  2625. } else if (selector.indexOf(HOST) >= 0) {
  2626. selector = selector.replace(HOST_PAREN, function (m, host, paren) {
  2627. return hostScope + paren;
  2628. });
  2629. selector = selector.replace(HOST, hostScope);
  2630. } else if (jumpIndex !== 0) {
  2631. selector = scope ? this._transformSimpleSelector(selector, scope) : selector;
  2632. }
  2633. if (selector.indexOf(CONTENT) >= 0) {
  2634. combinator = '';
  2635. }
  2636. var stop;
  2637. if (jumpIndex >= 0) {
  2638. selector = selector.replace(SCOPE_JUMP, ' ');
  2639. stop = true;
  2640. }
  2641. return {
  2642. value: selector,
  2643. combinator: combinator,
  2644. stop: stop,
  2645. hostContext: hostContext
  2646. };
  2647. },
  2648. _transformSimpleSelector: function (selector, scope) {
  2649. var p$ = selector.split(PSEUDO_PREFIX);
  2650. p$[0] += scope;
  2651. return p$.join(PSEUDO_PREFIX);
  2652. },
  2653. documentRule: function (rule) {
  2654. rule.selector = rule.parsedSelector;
  2655. this.normalizeRootSelector(rule);
  2656. if (!nativeShadow) {
  2657. this._transformRule(rule, this._transformDocumentSelector);
  2658. }
  2659. },
  2660. normalizeRootSelector: function (rule) {
  2661. if (rule.selector === ROOT) {
  2662. rule.selector = 'body';
  2663. }
  2664. },
  2665. _transformDocumentSelector: function (selector) {
  2666. return selector.match(SCOPE_JUMP) ? this._transformComplexSelector(selector, SCOPE_DOC_SELECTOR) : this._transformSimpleSelector(selector.trim(), SCOPE_DOC_SELECTOR);
  2667. },
  2668. SCOPE_NAME: 'style-scope'
  2669. };
  2670. var SCOPE_NAME = api.SCOPE_NAME;
  2671. var SCOPE_DOC_SELECTOR = ':not([' + SCOPE_NAME + '])' + ':not(.' + SCOPE_NAME + ')';
  2672. var COMPLEX_SELECTOR_SEP = ',';
  2673. var SIMPLE_SELECTOR_SEP = /(^|[\s>+~]+)([^\s>+~]+)/g;
  2674. var HOST = ':host';
  2675. var ROOT = ':root';
  2676. var HOST_PAREN = /(\:host)(?:\(((?:\([^)(]*\)|[^)(]*)+?)\))/g;
  2677. var HOST_CONTEXT = ':host-context';
  2678. var HOST_CONTEXT_PAREN = /(.*)(?:\:host-context)(?:\(((?:\([^)(]*\)|[^)(]*)+?)\))(.*)/;
  2679. var CONTENT = '::content';
  2680. var SCOPE_JUMP = /\:\:content|\:\:shadow|\/deep\//;
  2681. var CSS_CLASS_PREFIX = '.';
  2682. var CSS_ATTR_PREFIX = '[' + SCOPE_NAME + '~=';
  2683. var CSS_ATTR_SUFFIX = ']';
  2684. var PSEUDO_PREFIX = ':';
  2685. var CLASS = 'class';
  2686. return api;
  2687. }();
  2688. Polymer.StyleExtends = function () {
  2689. var styleUtil = Polymer.StyleUtil;
  2690. return {
  2691. hasExtends: function (cssText) {
  2692. return Boolean(cssText.match(this.rx.EXTEND));
  2693. },
  2694. transform: function (style) {
  2695. var rules = styleUtil.rulesForStyle(style);
  2696. var self = this;
  2697. styleUtil.forEachStyleRule(rules, function (rule) {
  2698. var map = self._mapRule(rule);
  2699. if (rule.parent) {
  2700. var m;
  2701. while (m = self.rx.EXTEND.exec(rule.cssText)) {
  2702. var extend = m[1];
  2703. var extendor = self._findExtendor(extend, rule);
  2704. if (extendor) {
  2705. self._extendRule(rule, extendor);
  2706. }
  2707. }
  2708. }
  2709. rule.cssText = rule.cssText.replace(self.rx.EXTEND, '');
  2710. });
  2711. return styleUtil.toCssText(rules, function (rule) {
  2712. if (rule.selector.match(self.rx.STRIP)) {
  2713. rule.cssText = '';
  2714. }
  2715. }, true);
  2716. },
  2717. _mapRule: function (rule) {
  2718. if (rule.parent) {
  2719. var map = rule.parent.map || (rule.parent.map = {});
  2720. var parts = rule.selector.split(',');
  2721. for (var i = 0, p; i < parts.length; i++) {
  2722. p = parts[i];
  2723. map[p.trim()] = rule;
  2724. }
  2725. return map;
  2726. }
  2727. },
  2728. _findExtendor: function (extend, rule) {
  2729. return rule.parent && rule.parent.map && rule.parent.map[extend] || this._findExtendor(extend, rule.parent);
  2730. },
  2731. _extendRule: function (target, source) {
  2732. if (target.parent !== source.parent) {
  2733. this._cloneAndAddRuleToParent(source, target.parent);
  2734. }
  2735. target.extends = target.extends || (target.extends = []);
  2736. target.extends.push(source);
  2737. source.selector = source.selector.replace(this.rx.STRIP, '');
  2738. source.selector = (source.selector && source.selector + ',\n') + target.selector;
  2739. if (source.extends) {
  2740. source.extends.forEach(function (e) {
  2741. this._extendRule(target, e);
  2742. }, this);
  2743. }
  2744. },
  2745. _cloneAndAddRuleToParent: function (rule, parent) {
  2746. rule = Object.create(rule);
  2747. rule.parent = parent;
  2748. if (rule.extends) {
  2749. rule.extends = rule.extends.slice();
  2750. }
  2751. parent.rules.push(rule);
  2752. },
  2753. rx: {
  2754. EXTEND: /@extends\(([^)]*)\)\s*?;/gim,
  2755. STRIP: /%[^,]*$/
  2756. }
  2757. };
  2758. }();
  2759. (function () {
  2760. var prepElement = Polymer.Base._prepElement;
  2761. var nativeShadow = Polymer.Settings.useNativeShadow;
  2762. var styleUtil = Polymer.StyleUtil;
  2763. var styleTransformer = Polymer.StyleTransformer;
  2764. var styleExtends = Polymer.StyleExtends;
  2765. Polymer.Base._addFeature({
  2766. _prepElement: function (element) {
  2767. if (this._encapsulateStyle) {
  2768. styleTransformer.element(element, this.is, this._scopeCssViaAttr);
  2769. }
  2770. prepElement.call(this, element);
  2771. },
  2772. _prepStyles: function () {
  2773. if (this._encapsulateStyle === undefined) {
  2774. this._encapsulateStyle = !nativeShadow && Boolean(this._template);
  2775. }
  2776. this._styles = this._collectStyles();
  2777. var cssText = styleTransformer.elementStyles(this);
  2778. if (cssText && this._template) {
  2779. var style = styleUtil.applyCss(cssText, this.is, nativeShadow ? this._template.content : null);
  2780. if (!nativeShadow) {
  2781. this._scopeStyle = style;
  2782. }
  2783. }
  2784. },
  2785. _collectStyles: function () {
  2786. var styles = [];
  2787. var cssText = '', m$ = this.styleModules;
  2788. if (m$) {
  2789. for (var i = 0, l = m$.length, m; i < l && (m = m$[i]); i++) {
  2790. cssText += styleUtil.cssFromModule(m);
  2791. }
  2792. }
  2793. cssText += styleUtil.cssFromModule(this.is);
  2794. if (cssText) {
  2795. var style = document.createElement('style');
  2796. style.textContent = cssText;
  2797. if (styleExtends.hasExtends(style.textContent)) {
  2798. cssText = styleExtends.transform(style);
  2799. }
  2800. styles.push(style);
  2801. }
  2802. return styles;
  2803. },
  2804. _elementAdd: function (node) {
  2805. if (this._encapsulateStyle) {
  2806. if (node.__styleScoped) {
  2807. node.__styleScoped = false;
  2808. } else {
  2809. styleTransformer.dom(node, this.is, this._scopeCssViaAttr);
  2810. }
  2811. }
  2812. },
  2813. _elementRemove: function (node) {
  2814. if (this._encapsulateStyle) {
  2815. styleTransformer.dom(node, this.is, this._scopeCssViaAttr, true);
  2816. }
  2817. },
  2818. scopeSubtree: function (container, shouldObserve) {
  2819. if (nativeShadow) {
  2820. return;
  2821. }
  2822. var self = this;
  2823. var scopify = function (node) {
  2824. if (node.nodeType === Node.ELEMENT_NODE) {
  2825. node.className = self._scopeElementClass(node, node.className);
  2826. var n$ = node.querySelectorAll('*');
  2827. Array.prototype.forEach.call(n$, function (n) {
  2828. n.className = self._scopeElementClass(n, n.className);
  2829. });
  2830. }
  2831. };
  2832. scopify(container);
  2833. if (shouldObserve) {
  2834. var mo = new MutationObserver(function (mxns) {
  2835. mxns.forEach(function (m) {
  2836. if (m.addedNodes) {
  2837. for (var i = 0; i < m.addedNodes.length; i++) {
  2838. scopify(m.addedNodes[i]);
  2839. }
  2840. }
  2841. });
  2842. });
  2843. mo.observe(container, {
  2844. childList: true,
  2845. subtree: true
  2846. });
  2847. return mo;
  2848. }
  2849. }
  2850. });
  2851. }());
  2852. Polymer.StyleProperties = function () {
  2853. 'use strict';
  2854. var nativeShadow = Polymer.Settings.useNativeShadow;
  2855. var matchesSelector = Polymer.DomApi.matchesSelector;
  2856. var styleUtil = Polymer.StyleUtil;
  2857. var styleTransformer = Polymer.StyleTransformer;
  2858. return {
  2859. decorateStyles: function (styles) {
  2860. var self = this, props = {};
  2861. styleUtil.forRulesInStyles(styles, function (rule) {
  2862. self.decorateRule(rule);
  2863. self.collectPropertiesInCssText(rule.propertyInfo.cssText, props);
  2864. });
  2865. var names = [];
  2866. for (var i in props) {
  2867. names.push(i);
  2868. }
  2869. return names;
  2870. },
  2871. decorateRule: function (rule) {
  2872. if (rule.propertyInfo) {
  2873. return rule.propertyInfo;
  2874. }
  2875. var info = {}, properties = {};
  2876. var hasProperties = this.collectProperties(rule, properties);
  2877. if (hasProperties) {
  2878. info.properties = properties;
  2879. rule.rules = null;
  2880. }
  2881. info.cssText = this.collectCssText(rule);
  2882. rule.propertyInfo = info;
  2883. return info;
  2884. },
  2885. collectProperties: function (rule, properties) {
  2886. var info = rule.propertyInfo;
  2887. if (info) {
  2888. if (info.properties) {
  2889. Polymer.Base.mixin(properties, info.properties);
  2890. return true;
  2891. }
  2892. } else {
  2893. var m, rx = this.rx.VAR_ASSIGN;
  2894. var cssText = rule.parsedCssText;
  2895. var any;
  2896. while (m = rx.exec(cssText)) {
  2897. properties[m[1]] = (m[2] || m[3]).trim();
  2898. any = true;
  2899. }
  2900. return any;
  2901. }
  2902. },
  2903. collectCssText: function (rule) {
  2904. var customCssText = '';
  2905. var cssText = rule.parsedCssText;
  2906. cssText = cssText.replace(this.rx.BRACKETED, '').replace(this.rx.VAR_ASSIGN, '');
  2907. var parts = cssText.split(';');
  2908. for (var i = 0, p; i < parts.length; i++) {
  2909. p = parts[i];
  2910. if (p.match(this.rx.MIXIN_MATCH) || p.match(this.rx.VAR_MATCH)) {
  2911. customCssText += p + ';\n';
  2912. }
  2913. }
  2914. return customCssText;
  2915. },
  2916. collectPropertiesInCssText: function (cssText, props) {
  2917. var m;
  2918. while (m = this.rx.VAR_CAPTURE.exec(cssText)) {
  2919. props[m[1]] = true;
  2920. var def = m[2];
  2921. if (def && def.match(this.rx.IS_VAR)) {
  2922. props[def] = true;
  2923. }
  2924. }
  2925. },
  2926. reify: function (props) {
  2927. var names = Object.getOwnPropertyNames(props);
  2928. for (var i = 0, n; i < names.length; i++) {
  2929. n = names[i];
  2930. props[n] = this.valueForProperty(props[n], props);
  2931. }
  2932. },
  2933. valueForProperty: function (property, props) {
  2934. if (property) {
  2935. if (property.indexOf(';') >= 0) {
  2936. property = this.valueForProperties(property, props);
  2937. } else {
  2938. var self = this;
  2939. var fn = function (all, prefix, value, fallback) {
  2940. var propertyValue = self.valueForProperty(props[value], props) || (props[fallback] ? self.valueForProperty(props[fallback], props) : fallback);
  2941. return prefix + (propertyValue || '');
  2942. };
  2943. property = property.replace(this.rx.VAR_MATCH, fn);
  2944. }
  2945. }
  2946. return property && property.trim() || '';
  2947. },
  2948. valueForProperties: function (property, props) {
  2949. var parts = property.split(';');
  2950. for (var i = 0, p, m; i < parts.length; i++) {
  2951. if (p = parts[i]) {
  2952. m = p.match(this.rx.MIXIN_MATCH);
  2953. if (m) {
  2954. p = this.valueForProperty(props[m[1]], props);
  2955. } else {
  2956. var pp = p.split(':');
  2957. if (pp[1]) {
  2958. pp[1] = pp[1].trim();
  2959. pp[1] = this.valueForProperty(pp[1], props) || pp[1];
  2960. }
  2961. p = pp.join(':');
  2962. }
  2963. parts[i] = p && p.lastIndexOf(';') === p.length - 1 ? p.slice(0, -1) : p || '';
  2964. }
  2965. }
  2966. return parts.join(';');
  2967. },
  2968. applyProperties: function (rule, props) {
  2969. var output = '';
  2970. if (!rule.propertyInfo) {
  2971. this.decorateRule(rule);
  2972. }
  2973. if (rule.propertyInfo.cssText) {
  2974. output = this.valueForProperties(rule.propertyInfo.cssText, props);
  2975. }
  2976. rule.cssText = output;
  2977. },
  2978. propertyDataFromStyles: function (styles, element) {
  2979. var props = {}, self = this;
  2980. var o = [], i = 0;
  2981. styleUtil.forRulesInStyles(styles, function (rule) {
  2982. if (!rule.propertyInfo) {
  2983. self.decorateRule(rule);
  2984. }
  2985. if (element && rule.propertyInfo.properties && matchesSelector.call(element, rule.transformedSelector || rule.parsedSelector)) {
  2986. self.collectProperties(rule, props);
  2987. addToBitMask(i, o);
  2988. }
  2989. i++;
  2990. });
  2991. return {
  2992. properties: props,
  2993. key: o
  2994. };
  2995. },
  2996. scopePropertiesFromStyles: function (styles) {
  2997. if (!styles._scopeStyleProperties) {
  2998. styles._scopeStyleProperties = this.selectedPropertiesFromStyles(styles, this.SCOPE_SELECTORS);
  2999. }
  3000. return styles._scopeStyleProperties;
  3001. },
  3002. hostPropertiesFromStyles: function (styles) {
  3003. if (!styles._hostStyleProperties) {
  3004. styles._hostStyleProperties = this.selectedPropertiesFromStyles(styles, this.HOST_SELECTORS);
  3005. }
  3006. return styles._hostStyleProperties;
  3007. },
  3008. selectedPropertiesFromStyles: function (styles, selectors) {
  3009. var props = {}, self = this;
  3010. styleUtil.forRulesInStyles(styles, function (rule) {
  3011. if (!rule.propertyInfo) {
  3012. self.decorateRule(rule);
  3013. }
  3014. for (var i = 0; i < selectors.length; i++) {
  3015. if (rule.parsedSelector === selectors[i]) {
  3016. self.collectProperties(rule, props);
  3017. return;
  3018. }
  3019. }
  3020. });
  3021. return props;
  3022. },
  3023. transformStyles: function (element, properties, scopeSelector) {
  3024. var self = this;
  3025. var hostSelector = styleTransformer._calcHostScope(element.is, element.extends);
  3026. var rxHostSelector = element.extends ? '\\' + hostSelector.slice(0, -1) + '\\]' : hostSelector;
  3027. var hostRx = new RegExp(this.rx.HOST_PREFIX + rxHostSelector + this.rx.HOST_SUFFIX);
  3028. return styleTransformer.elementStyles(element, function (rule) {
  3029. self.applyProperties(rule, properties);
  3030. if (rule.cssText && !nativeShadow) {
  3031. self._scopeSelector(rule, hostRx, hostSelector, element._scopeCssViaAttr, scopeSelector);
  3032. }
  3033. });
  3034. },
  3035. _scopeSelector: function (rule, hostRx, hostSelector, viaAttr, scopeId) {
  3036. rule.transformedSelector = rule.transformedSelector || rule.selector;
  3037. var selector = rule.transformedSelector;
  3038. var scope = viaAttr ? '[' + styleTransformer.SCOPE_NAME + '~=' + scopeId + ']' : '.' + scopeId;
  3039. var parts = selector.split(',');
  3040. for (var i = 0, l = parts.length, p; i < l && (p = parts[i]); i++) {
  3041. parts[i] = p.match(hostRx) ? p.replace(hostSelector, hostSelector + scope) : scope + ' ' + p;
  3042. }
  3043. rule.selector = parts.join(',');
  3044. },
  3045. applyElementScopeSelector: function (element, selector, old, viaAttr) {
  3046. var c = viaAttr ? element.getAttribute(styleTransformer.SCOPE_NAME) : element.className;
  3047. var v = old ? c.replace(old, selector) : (c ? c + ' ' : '') + this.XSCOPE_NAME + ' ' + selector;
  3048. if (c !== v) {
  3049. if (viaAttr) {
  3050. element.setAttribute(styleTransformer.SCOPE_NAME, v);
  3051. } else {
  3052. element.className = v;
  3053. }
  3054. }
  3055. },
  3056. applyElementStyle: function (element, properties, selector, style) {
  3057. var cssText = style ? style.textContent || '' : this.transformStyles(element, properties, selector);
  3058. var s = element._customStyle;
  3059. if (s && !nativeShadow && s !== style) {
  3060. s._useCount--;
  3061. if (s._useCount <= 0 && s.parentNode) {
  3062. s.parentNode.removeChild(s);
  3063. }
  3064. }
  3065. if (nativeShadow || (!style || !style.parentNode)) {
  3066. if (nativeShadow && element._customStyle) {
  3067. element._customStyle.textContent = cssText;
  3068. style = element._customStyle;
  3069. } else if (cssText) {
  3070. style = styleUtil.applyCss(cssText, selector, nativeShadow ? element.root : null, element._scopeStyle);
  3071. }
  3072. }
  3073. if (style) {
  3074. style._useCount = style._useCount || 0;
  3075. if (element._customStyle != style) {
  3076. style._useCount++;
  3077. }
  3078. element._customStyle = style;
  3079. }
  3080. return style;
  3081. },
  3082. mixinCustomStyle: function (props, customStyle) {
  3083. var v;
  3084. for (var i in customStyle) {
  3085. v = customStyle[i];
  3086. if (v || v === 0) {
  3087. props[i] = v;
  3088. }
  3089. }
  3090. },
  3091. rx: {
  3092. VAR_ASSIGN: /(?:^|[;\n]\s*)(--[\w-]*?):\s*(?:([^;{]*)|{([^}]*)})(?:(?=[;\n])|$)/gi,
  3093. MIXIN_MATCH: /(?:^|\W+)@apply[\s]*\(([^)]*)\)/i,
  3094. VAR_MATCH: /(^|\W+)var\([\s]*([^,)]*)[\s]*,?[\s]*((?:[^,)]*)|(?:[^;]*\([^;)]*\)))[\s]*?\)/gi,
  3095. VAR_CAPTURE: /\([\s]*(--[^,\s)]*)(?:,[\s]*(--[^,\s)]*))?(?:\)|,)/gi,
  3096. IS_VAR: /^--/,
  3097. BRACKETED: /\{[^}]*\}/g,
  3098. HOST_PREFIX: '(?:^|[^.#[:])',
  3099. HOST_SUFFIX: '($|[.:[\\s>+~])'
  3100. },
  3101. HOST_SELECTORS: [':host'],
  3102. SCOPE_SELECTORS: [':root'],
  3103. XSCOPE_NAME: 'x-scope'
  3104. };
  3105. function addToBitMask(n, bits) {
  3106. var o = parseInt(n / 32);
  3107. var v = 1 << n % 32;
  3108. bits[o] = (bits[o] || 0) | v;
  3109. }
  3110. }();
  3111. (function () {
  3112. Polymer.StyleCache = function () {
  3113. this.cache = {};
  3114. };
  3115. Polymer.StyleCache.prototype = {
  3116. MAX: 100,
  3117. store: function (is, data, keyValues, keyStyles) {
  3118. data.keyValues = keyValues;
  3119. data.styles = keyStyles;
  3120. var s$ = this.cache[is] = this.cache[is] || [];
  3121. s$.push(data);
  3122. if (s$.length > this.MAX) {
  3123. s$.shift();
  3124. }
  3125. },
  3126. retrieve: function (is, keyValues, keyStyles) {
  3127. var cache = this.cache[is];
  3128. if (cache) {
  3129. for (var i = cache.length - 1, data; i >= 0; i--) {
  3130. data = cache[i];
  3131. if (keyStyles === data.styles && this._objectsEqual(keyValues, data.keyValues)) {
  3132. return data;
  3133. }
  3134. }
  3135. }
  3136. },
  3137. clear: function () {
  3138. this.cache = {};
  3139. },
  3140. _objectsEqual: function (target, source) {
  3141. var t, s;
  3142. for (var i in target) {
  3143. t = target[i], s = source[i];
  3144. if (!(typeof t === 'object' && t ? this._objectsStrictlyEqual(t, s) : t === s)) {
  3145. return false;
  3146. }
  3147. }
  3148. if (Array.isArray(target)) {
  3149. return target.length === source.length;
  3150. }
  3151. return true;
  3152. },
  3153. _objectsStrictlyEqual: function (target, source) {
  3154. return this._objectsEqual(target, source) && this._objectsEqual(source, target);
  3155. }
  3156. };
  3157. }());
  3158. Polymer.StyleDefaults = function () {
  3159. var styleProperties = Polymer.StyleProperties;
  3160. var styleUtil = Polymer.StyleUtil;
  3161. var StyleCache = Polymer.StyleCache;
  3162. var api = {
  3163. _styles: [],
  3164. _properties: null,
  3165. customStyle: {},
  3166. _styleCache: new StyleCache(),
  3167. addStyle: function (style) {
  3168. this._styles.push(style);
  3169. this._properties = null;
  3170. },
  3171. get _styleProperties() {
  3172. if (!this._properties) {
  3173. styleProperties.decorateStyles(this._styles);
  3174. this._styles._scopeStyleProperties = null;
  3175. this._properties = styleProperties.scopePropertiesFromStyles(this._styles);
  3176. styleProperties.mixinCustomStyle(this._properties, this.customStyle);
  3177. styleProperties.reify(this._properties);
  3178. }
  3179. return this._properties;
  3180. },
  3181. _needsStyleProperties: function () {
  3182. },
  3183. _computeStyleProperties: function () {
  3184. return this._styleProperties;
  3185. },
  3186. updateStyles: function (properties) {
  3187. this._properties = null;
  3188. if (properties) {
  3189. Polymer.Base.mixin(this.customStyle, properties);
  3190. }
  3191. this._styleCache.clear();
  3192. for (var i = 0, s; i < this._styles.length; i++) {
  3193. s = this._styles[i];
  3194. s = s.__importElement || s;
  3195. s._apply();
  3196. }
  3197. }
  3198. };
  3199. return api;
  3200. }();
  3201. (function () {
  3202. 'use strict';
  3203. var serializeValueToAttribute = Polymer.Base.serializeValueToAttribute;
  3204. var propertyUtils = Polymer.StyleProperties;
  3205. var styleTransformer = Polymer.StyleTransformer;
  3206. var styleUtil = Polymer.StyleUtil;
  3207. var styleDefaults = Polymer.StyleDefaults;
  3208. var nativeShadow = Polymer.Settings.useNativeShadow;
  3209. Polymer.Base._addFeature({
  3210. _prepStyleProperties: function () {
  3211. this._ownStylePropertyNames = this._styles ? propertyUtils.decorateStyles(this._styles) : [];
  3212. },
  3213. customStyle: {},
  3214. _setupStyleProperties: function () {
  3215. this.customStyle = {};
  3216. },
  3217. _needsStyleProperties: function () {
  3218. return Boolean(this._ownStylePropertyNames && this._ownStylePropertyNames.length);
  3219. },
  3220. _beforeAttached: function () {
  3221. if (!this._scopeSelector && this._needsStyleProperties()) {
  3222. this._updateStyleProperties();
  3223. }
  3224. },
  3225. _findStyleHost: function () {
  3226. var e = this, root;
  3227. while (root = Polymer.dom(e).getOwnerRoot()) {
  3228. if (Polymer.isInstance(root.host)) {
  3229. return root.host;
  3230. }
  3231. e = root.host;
  3232. }
  3233. return styleDefaults;
  3234. },
  3235. _updateStyleProperties: function () {
  3236. var info, scope = this._findStyleHost();
  3237. if (!scope._styleCache) {
  3238. scope._styleCache = new Polymer.StyleCache();
  3239. }
  3240. var scopeData = propertyUtils.propertyDataFromStyles(scope._styles, this);
  3241. scopeData.key.customStyle = this.customStyle;
  3242. info = scope._styleCache.retrieve(this.is, scopeData.key, this._styles);
  3243. var scopeCached = Boolean(info);
  3244. if (scopeCached) {
  3245. this._styleProperties = info._styleProperties;
  3246. } else {
  3247. this._computeStyleProperties(scopeData.properties);
  3248. }
  3249. this._computeOwnStyleProperties();
  3250. if (!scopeCached) {
  3251. info = styleCache.retrieve(this.is, this._ownStyleProperties, this._styles);
  3252. }
  3253. var globalCached = Boolean(info) && !scopeCached;
  3254. var style = this._applyStyleProperties(info);
  3255. if (!scopeCached) {
  3256. style = style && nativeShadow ? style.cloneNode(true) : style;
  3257. info = {
  3258. style: style,
  3259. _scopeSelector: this._scopeSelector,
  3260. _styleProperties: this._styleProperties
  3261. };
  3262. scopeData.key.customStyle = {};
  3263. this.mixin(scopeData.key.customStyle, this.customStyle);
  3264. scope._styleCache.store(this.is, info, scopeData.key, this._styles);
  3265. if (!globalCached) {
  3266. styleCache.store(this.is, Object.create(info), this._ownStyleProperties, this._styles);
  3267. }
  3268. }
  3269. },
  3270. _computeStyleProperties: function (scopeProps) {
  3271. var scope = this._findStyleHost();
  3272. if (!scope._styleProperties) {
  3273. scope._computeStyleProperties();
  3274. }
  3275. var props = Object.create(scope._styleProperties);
  3276. this.mixin(props, propertyUtils.hostPropertiesFromStyles(this._styles));
  3277. scopeProps = scopeProps || propertyUtils.propertyDataFromStyles(scope._styles, this).properties;
  3278. this.mixin(props, scopeProps);
  3279. this.mixin(props, propertyUtils.scopePropertiesFromStyles(this._styles));
  3280. propertyUtils.mixinCustomStyle(props, this.customStyle);
  3281. propertyUtils.reify(props);
  3282. this._styleProperties = props;
  3283. },
  3284. _computeOwnStyleProperties: function () {
  3285. var props = {};
  3286. for (var i = 0, n; i < this._ownStylePropertyNames.length; i++) {
  3287. n = this._ownStylePropertyNames[i];
  3288. props[n] = this._styleProperties[n];
  3289. }
  3290. this._ownStyleProperties = props;
  3291. },
  3292. _scopeCount: 0,
  3293. _applyStyleProperties: function (info) {
  3294. var oldScopeSelector = this._scopeSelector;
  3295. this._scopeSelector = info ? info._scopeSelector : this.is + '-' + this.__proto__._scopeCount++;
  3296. var style = propertyUtils.applyElementStyle(this, this._styleProperties, this._scopeSelector, info && info.style);
  3297. if (!nativeShadow) {
  3298. propertyUtils.applyElementScopeSelector(this, this._scopeSelector, oldScopeSelector, this._scopeCssViaAttr);
  3299. }
  3300. return style;
  3301. },
  3302. serializeValueToAttribute: function (value, attribute, node) {
  3303. node = node || this;
  3304. if (attribute === 'class' && !nativeShadow) {
  3305. var host = node === this ? this.domHost || this.dataHost : this;
  3306. if (host) {
  3307. value = host._scopeElementClass(node, value);
  3308. }
  3309. }
  3310. node = Polymer.dom(node);
  3311. serializeValueToAttribute.call(this, value, attribute, node);
  3312. },
  3313. _scopeElementClass: function (element, selector) {
  3314. if (!nativeShadow && !this._scopeCssViaAttr) {
  3315. selector += (selector ? ' ' : '') + SCOPE_NAME + ' ' + this.is + (element._scopeSelector ? ' ' + XSCOPE_NAME + ' ' + element._scopeSelector : '');
  3316. }
  3317. return selector;
  3318. },
  3319. updateStyles: function (properties) {
  3320. if (this.isAttached) {
  3321. if (properties) {
  3322. this.mixin(this.customStyle, properties);
  3323. }
  3324. if (this._needsStyleProperties()) {
  3325. this._updateStyleProperties();
  3326. } else {
  3327. this._styleProperties = null;
  3328. }
  3329. if (this._styleCache) {
  3330. this._styleCache.clear();
  3331. }
  3332. this._updateRootStyles();
  3333. }
  3334. },
  3335. _updateRootStyles: function (root) {
  3336. root = root || this.root;
  3337. var c$ = Polymer.dom(root)._query(function (e) {
  3338. return e.shadyRoot || e.shadowRoot;
  3339. });
  3340. for (var i = 0, l = c$.length, c; i < l && (c = c$[i]); i++) {
  3341. if (c.updateStyles) {
  3342. c.updateStyles();
  3343. }
  3344. }
  3345. }
  3346. });
  3347. Polymer.updateStyles = function (properties) {
  3348. styleDefaults.updateStyles(properties);
  3349. Polymer.Base._updateRootStyles(document);
  3350. };
  3351. var styleCache = new Polymer.StyleCache();
  3352. Polymer.customStyleCache = styleCache;
  3353. var SCOPE_NAME = styleTransformer.SCOPE_NAME;
  3354. var XSCOPE_NAME = propertyUtils.XSCOPE_NAME;
  3355. }());
  3356. Polymer.Base._addFeature({
  3357. _registerFeatures: function () {
  3358. this._prepIs();
  3359. this._prepAttributes();
  3360. this._prepConstructor();
  3361. this._prepTemplate();
  3362. this._prepStyles();
  3363. this._prepStyleProperties();
  3364. this._prepAnnotations();
  3365. this._prepEffects();
  3366. this._prepBehaviors();
  3367. this._prepBindings();
  3368. this._prepShady();
  3369. },
  3370. _prepBehavior: function (b) {
  3371. this._addPropertyEffects(b.properties);
  3372. this._addComplexObserverEffects(b.observers);
  3373. this._addHostAttributes(b.hostAttributes);
  3374. },
  3375. _initFeatures: function () {
  3376. this._poolContent();
  3377. this._setupConfigure();
  3378. this._setupStyleProperties();
  3379. this._pushHost();
  3380. this._stampTemplate();
  3381. this._popHost();
  3382. this._marshalAnnotationReferences();
  3383. this._setupDebouncers();
  3384. this._marshalInstanceEffects();
  3385. this._marshalHostAttributes();
  3386. this._marshalBehaviors();
  3387. this._marshalAttributes();
  3388. this._tryReady();
  3389. },
  3390. _marshalBehavior: function (b) {
  3391. this._listenListeners(b.listeners);
  3392. }
  3393. });
  3394. (function () {
  3395. var nativeShadow = Polymer.Settings.useNativeShadow;
  3396. var propertyUtils = Polymer.StyleProperties;
  3397. var styleUtil = Polymer.StyleUtil;
  3398. var cssParse = Polymer.CssParse;
  3399. var styleDefaults = Polymer.StyleDefaults;
  3400. var styleTransformer = Polymer.StyleTransformer;
  3401. Polymer({
  3402. is: 'custom-style',
  3403. extends: 'style',
  3404. properties: { include: String },
  3405. ready: function () {
  3406. this._tryApply();
  3407. },
  3408. attached: function () {
  3409. this._tryApply();
  3410. },
  3411. _tryApply: function () {
  3412. if (!this._appliesToDocument) {
  3413. if (this.parentNode && this.parentNode.localName !== 'dom-module') {
  3414. this._appliesToDocument = true;
  3415. var e = this.__appliedElement || this;
  3416. styleDefaults.addStyle(e);
  3417. if (e.textContent || this.include) {
  3418. this._apply();
  3419. } else {
  3420. var observer = new MutationObserver(function () {
  3421. observer.disconnect();
  3422. this._apply();
  3423. }.bind(this));
  3424. observer.observe(e, { childList: true });
  3425. }
  3426. }
  3427. }
  3428. },
  3429. _apply: function () {
  3430. var e = this.__appliedElement || this;
  3431. if (this.include) {
  3432. e.textContent = styleUtil.cssFromModules(this.include, true) + e.textContent;
  3433. }
  3434. if (e.textContent) {
  3435. styleUtil.forEachStyleRule(styleUtil.rulesForStyle(e), function (rule) {
  3436. styleTransformer.documentRule(rule);
  3437. });
  3438. this._applyCustomProperties(e);
  3439. }
  3440. },
  3441. _applyCustomProperties: function (element) {
  3442. this._computeStyleProperties();
  3443. var props = this._styleProperties;
  3444. var rules = styleUtil.rulesForStyle(element);
  3445. element.textContent = styleUtil.toCssText(rules, function (rule) {
  3446. var css = rule.cssText = rule.parsedCssText;
  3447. if (rule.propertyInfo && rule.propertyInfo.cssText) {
  3448. css = cssParse.removeCustomPropAssignment(css);
  3449. rule.cssText = propertyUtils.valueForProperties(css, props);
  3450. }
  3451. });
  3452. }
  3453. });
  3454. }());
  3455. Polymer.Templatizer = {
  3456. properties: { __hideTemplateChildren__: { observer: '_showHideChildren' } },
  3457. _instanceProps: Polymer.nob,
  3458. _parentPropPrefix: '_parent_',
  3459. templatize: function (template) {
  3460. this._templatized = template;
  3461. if (!template._content) {
  3462. template._content = template.content;
  3463. }
  3464. if (template._content._ctor) {
  3465. this.ctor = template._content._ctor;
  3466. this._prepParentProperties(this.ctor.prototype, template);
  3467. return;
  3468. }
  3469. var archetype = Object.create(Polymer.Base);
  3470. this._customPrepAnnotations(archetype, template);
  3471. this._prepParentProperties(archetype, template);
  3472. archetype._prepEffects();
  3473. this._customPrepEffects(archetype);
  3474. archetype._prepBehaviors();
  3475. archetype._prepBindings();
  3476. archetype._notifyPathUp = this._notifyPathUpImpl;
  3477. archetype._scopeElementClass = this._scopeElementClassImpl;
  3478. archetype.listen = this._listenImpl;
  3479. archetype._showHideChildren = this._showHideChildrenImpl;
  3480. var _constructor = this._constructorImpl;
  3481. var ctor = function TemplateInstance(model, host) {
  3482. _constructor.call(this, model, host);
  3483. };
  3484. ctor.prototype = archetype;
  3485. archetype.constructor = ctor;
  3486. template._content._ctor = ctor;
  3487. this.ctor = ctor;
  3488. },
  3489. _getRootDataHost: function () {
  3490. return this.dataHost && this.dataHost._rootDataHost || this.dataHost;
  3491. },
  3492. _showHideChildrenImpl: function (hide) {
  3493. var c = this._children;
  3494. for (var i = 0; i < c.length; i++) {
  3495. var n = c[i];
  3496. if (Boolean(hide) != Boolean(n.__hideTemplateChildren__)) {
  3497. if (n.nodeType === Node.TEXT_NODE) {
  3498. if (hide) {
  3499. n.__polymerTextContent__ = n.textContent;
  3500. n.textContent = '';
  3501. } else {
  3502. n.textContent = n.__polymerTextContent__;
  3503. }
  3504. } else if (n.style) {
  3505. if (hide) {
  3506. n.__polymerDisplay__ = n.style.display;
  3507. n.style.display = 'none';
  3508. } else {
  3509. n.style.display = n.__polymerDisplay__;
  3510. }
  3511. }
  3512. }
  3513. n.__hideTemplateChildren__ = hide;
  3514. }
  3515. },
  3516. _debounceTemplate: function (fn) {
  3517. Polymer.dom.addDebouncer(this.debounce('_debounceTemplate', fn));
  3518. },
  3519. _flushTemplates: function (debouncerExpired) {
  3520. Polymer.dom.flush();
  3521. },
  3522. _customPrepEffects: function (archetype) {
  3523. var parentProps = archetype._parentProps;
  3524. for (var prop in parentProps) {
  3525. archetype._addPropertyEffect(prop, 'function', this._createHostPropEffector(prop));
  3526. }
  3527. for (var prop in this._instanceProps) {
  3528. archetype._addPropertyEffect(prop, 'function', this._createInstancePropEffector(prop));
  3529. }
  3530. },
  3531. _customPrepAnnotations: function (archetype, template) {
  3532. archetype._template = template;
  3533. var c = template._content;
  3534. if (!c._notes) {
  3535. var rootDataHost = archetype._rootDataHost;
  3536. if (rootDataHost) {
  3537. Polymer.Annotations.prepElement = rootDataHost._prepElement.bind(rootDataHost);
  3538. }
  3539. c._notes = Polymer.Annotations.parseAnnotations(template);
  3540. Polymer.Annotations.prepElement = null;
  3541. this._processAnnotations(c._notes);
  3542. }
  3543. archetype._notes = c._notes;
  3544. archetype._parentProps = c._parentProps;
  3545. },
  3546. _prepParentProperties: function (archetype, template) {
  3547. var parentProps = this._parentProps = archetype._parentProps;
  3548. if (this._forwardParentProp && parentProps) {
  3549. var proto = archetype._parentPropProto;
  3550. var prop;
  3551. if (!proto) {
  3552. for (prop in this._instanceProps) {
  3553. delete parentProps[prop];
  3554. }
  3555. proto = archetype._parentPropProto = Object.create(null);
  3556. if (template != this) {
  3557. Polymer.Bind.prepareModel(proto);
  3558. Polymer.Base.prepareModelNotifyPath(proto);
  3559. }
  3560. for (prop in parentProps) {
  3561. var parentProp = this._parentPropPrefix + prop;
  3562. var effects = [
  3563. {
  3564. kind: 'function',
  3565. effect: this._createForwardPropEffector(prop)
  3566. },
  3567. { kind: 'notify' }
  3568. ];
  3569. Polymer.Bind._createAccessors(proto, parentProp, effects);
  3570. }
  3571. }
  3572. if (template != this) {
  3573. Polymer.Bind.prepareInstance(template);
  3574. template._forwardParentProp = this._forwardParentProp.bind(this);
  3575. }
  3576. this._extendTemplate(template, proto);
  3577. template._pathEffector = this._pathEffectorImpl.bind(this);
  3578. }
  3579. },
  3580. _createForwardPropEffector: function (prop) {
  3581. return function (source, value) {
  3582. this._forwardParentProp(prop, value);
  3583. };
  3584. },
  3585. _createHostPropEffector: function (prop) {
  3586. var prefix = this._parentPropPrefix;
  3587. return function (source, value) {
  3588. this.dataHost._templatized[prefix + prop] = value;
  3589. };
  3590. },
  3591. _createInstancePropEffector: function (prop) {
  3592. return function (source, value, old, fromAbove) {
  3593. if (!fromAbove) {
  3594. this.dataHost._forwardInstanceProp(this, prop, value);
  3595. }
  3596. };
  3597. },
  3598. _extendTemplate: function (template, proto) {
  3599. Object.getOwnPropertyNames(proto).forEach(function (n) {
  3600. var val = template[n];
  3601. var pd = Object.getOwnPropertyDescriptor(proto, n);
  3602. Object.defineProperty(template, n, pd);
  3603. if (val !== undefined) {
  3604. template._propertySetter(n, val);
  3605. }
  3606. });
  3607. },
  3608. _showHideChildren: function (hidden) {
  3609. },
  3610. _forwardInstancePath: function (inst, path, value) {
  3611. },
  3612. _forwardInstanceProp: function (inst, prop, value) {
  3613. },
  3614. _notifyPathUpImpl: function (path, value) {
  3615. var dataHost = this.dataHost;
  3616. var dot = path.indexOf('.');
  3617. var root = dot < 0 ? path : path.slice(0, dot);
  3618. dataHost._forwardInstancePath.call(dataHost, this, path, value);
  3619. if (root in dataHost._parentProps) {
  3620. dataHost._templatized.notifyPath(dataHost._parentPropPrefix + path, value);
  3621. }
  3622. },
  3623. _pathEffectorImpl: function (path, value, fromAbove) {
  3624. if (this._forwardParentPath) {
  3625. if (path.indexOf(this._parentPropPrefix) === 0) {
  3626. var subPath = path.substring(this._parentPropPrefix.length);
  3627. this._forwardParentPath(subPath, value);
  3628. }
  3629. }
  3630. Polymer.Base._pathEffector.call(this._templatized, path, value, fromAbove);
  3631. },
  3632. _constructorImpl: function (model, host) {
  3633. this._rootDataHost = host._getRootDataHost();
  3634. this._setupConfigure(model);
  3635. this._pushHost(host);
  3636. this.root = this.instanceTemplate(this._template);
  3637. this.root.__noContent = !this._notes._hasContent;
  3638. this.root.__styleScoped = true;
  3639. this._popHost();
  3640. this._marshalAnnotatedNodes();
  3641. this._marshalInstanceEffects();
  3642. this._marshalAnnotatedListeners();
  3643. var children = [];
  3644. for (var n = this.root.firstChild; n; n = n.nextSibling) {
  3645. children.push(n);
  3646. n._templateInstance = this;
  3647. }
  3648. this._children = children;
  3649. if (host.__hideTemplateChildren__) {
  3650. this._showHideChildren(true);
  3651. }
  3652. this._tryReady();
  3653. },
  3654. _listenImpl: function (node, eventName, methodName) {
  3655. var model = this;
  3656. var host = this._rootDataHost;
  3657. var handler = host._createEventHandler(node, eventName, methodName);
  3658. var decorated = function (e) {
  3659. e.model = model;
  3660. handler(e);
  3661. };
  3662. host._listen(node, eventName, decorated);
  3663. },
  3664. _scopeElementClassImpl: function (node, value) {
  3665. var host = this._rootDataHost;
  3666. if (host) {
  3667. return host._scopeElementClass(node, value);
  3668. }
  3669. },
  3670. stamp: function (model) {
  3671. model = model || {};
  3672. if (this._parentProps) {
  3673. var templatized = this._templatized;
  3674. for (var prop in this._parentProps) {
  3675. model[prop] = templatized[this._parentPropPrefix + prop];
  3676. }
  3677. }
  3678. return new this.ctor(model, this);
  3679. },
  3680. modelForElement: function (el) {
  3681. var model;
  3682. while (el) {
  3683. if (model = el._templateInstance) {
  3684. if (model.dataHost != this) {
  3685. el = model.dataHost;
  3686. } else {
  3687. return model;
  3688. }
  3689. } else {
  3690. el = el.parentNode;
  3691. }
  3692. }
  3693. }
  3694. };
  3695. Polymer({
  3696. is: 'dom-template',
  3697. extends: 'template',
  3698. behaviors: [Polymer.Templatizer],
  3699. ready: function () {
  3700. this.templatize(this);
  3701. }
  3702. });
  3703. Polymer._collections = new WeakMap();
  3704. Polymer.Collection = function (userArray) {
  3705. Polymer._collections.set(userArray, this);
  3706. this.userArray = userArray;
  3707. this.store = userArray.slice();
  3708. this.initMap();
  3709. };
  3710. Polymer.Collection.prototype = {
  3711. constructor: Polymer.Collection,
  3712. initMap: function () {
  3713. var omap = this.omap = new WeakMap();
  3714. var pmap = this.pmap = {};
  3715. var s = this.store;
  3716. for (var i = 0; i < s.length; i++) {
  3717. var item = s[i];
  3718. if (item && typeof item == 'object') {
  3719. omap.set(item, i);
  3720. } else {
  3721. pmap[item] = i;
  3722. }
  3723. }
  3724. },
  3725. add: function (item) {
  3726. var key = this.store.push(item) - 1;
  3727. if (item && typeof item == 'object') {
  3728. this.omap.set(item, key);
  3729. } else {
  3730. this.pmap[item] = key;
  3731. }
  3732. return '#' + key;
  3733. },
  3734. removeKey: function (key) {
  3735. key = this._parseKey(key);
  3736. this._removeFromMap(this.store[key]);
  3737. delete this.store[key];
  3738. },
  3739. _removeFromMap: function (item) {
  3740. if (item && typeof item == 'object') {
  3741. this.omap.delete(item);
  3742. } else {
  3743. delete this.pmap[item];
  3744. }
  3745. },
  3746. remove: function (item) {
  3747. var key = this.getKey(item);
  3748. this.removeKey(key);
  3749. return key;
  3750. },
  3751. getKey: function (item) {
  3752. var key;
  3753. if (item && typeof item == 'object') {
  3754. key = this.omap.get(item);
  3755. } else {
  3756. key = this.pmap[item];
  3757. }
  3758. if (key != undefined) {
  3759. return '#' + key;
  3760. }
  3761. },
  3762. getKeys: function () {
  3763. return Object.keys(this.store).map(function (key) {
  3764. return '#' + key;
  3765. });
  3766. },
  3767. _parseKey: function (key) {
  3768. if (key[0] == '#') {
  3769. return key.slice(1);
  3770. }
  3771. throw new Error('unexpected key ' + key);
  3772. },
  3773. setItem: function (key, item) {
  3774. key = this._parseKey(key);
  3775. var old = this.store[key];
  3776. if (old) {
  3777. this._removeFromMap(old);
  3778. }
  3779. if (item && typeof item == 'object') {
  3780. this.omap.set(item, key);
  3781. } else {
  3782. this.pmap[item] = key;
  3783. }
  3784. this.store[key] = item;
  3785. },
  3786. getItem: function (key) {
  3787. key = this._parseKey(key);
  3788. return this.store[key];
  3789. },
  3790. getItems: function () {
  3791. var items = [], store = this.store;
  3792. for (var key in store) {
  3793. items.push(store[key]);
  3794. }
  3795. return items;
  3796. },
  3797. _applySplices: function (splices) {
  3798. var keyMap = {}, key, i;
  3799. splices.forEach(function (s) {
  3800. s.addedKeys = [];
  3801. for (i = 0; i < s.removed.length; i++) {
  3802. key = this.getKey(s.removed[i]);
  3803. keyMap[key] = keyMap[key] ? null : -1;
  3804. }
  3805. for (i = 0; i < s.addedCount; i++) {
  3806. var item = this.userArray[s.index + i];
  3807. key = this.getKey(item);
  3808. key = key === undefined ? this.add(item) : key;
  3809. keyMap[key] = keyMap[key] ? null : 1;
  3810. s.addedKeys.push(key);
  3811. }
  3812. }, this);
  3813. var removed = [];
  3814. var added = [];
  3815. for (var key in keyMap) {
  3816. if (keyMap[key] < 0) {
  3817. this.removeKey(key);
  3818. removed.push(key);
  3819. }
  3820. if (keyMap[key] > 0) {
  3821. added.push(key);
  3822. }
  3823. }
  3824. return [{
  3825. removed: removed,
  3826. added: added
  3827. }];
  3828. }
  3829. };
  3830. Polymer.Collection.get = function (userArray) {
  3831. return Polymer._collections.get(userArray) || new Polymer.Collection(userArray);
  3832. };
  3833. Polymer.Collection.applySplices = function (userArray, splices) {
  3834. var coll = Polymer._collections.get(userArray);
  3835. return coll ? coll._applySplices(splices) : null;
  3836. };
  3837. Polymer({
  3838. is: 'dom-repeat',
  3839. extends: 'template',
  3840. properties: {
  3841. items: { type: Array },
  3842. as: {
  3843. type: String,
  3844. value: 'item'
  3845. },
  3846. indexAs: {
  3847. type: String,
  3848. value: 'index'
  3849. },
  3850. sort: {
  3851. type: Function,
  3852. observer: '_sortChanged'
  3853. },
  3854. filter: {
  3855. type: Function,
  3856. observer: '_filterChanged'
  3857. },
  3858. observe: {
  3859. type: String,
  3860. observer: '_observeChanged'
  3861. },
  3862. delay: Number
  3863. },
  3864. behaviors: [Polymer.Templatizer],
  3865. observers: ['_itemsChanged(items.*)'],
  3866. created: function () {
  3867. this._instances = [];
  3868. },
  3869. detached: function () {
  3870. for (var i = 0; i < this._instances.length; i++) {
  3871. this._detachRow(i);
  3872. }
  3873. },
  3874. attached: function () {
  3875. var parentNode = Polymer.dom(this).parentNode;
  3876. for (var i = 0; i < this._instances.length; i++) {
  3877. Polymer.dom(parentNode).insertBefore(this._instances[i].root, this);
  3878. }
  3879. },
  3880. ready: function () {
  3881. this._instanceProps = { __key__: true };
  3882. this._instanceProps[this.as] = true;
  3883. this._instanceProps[this.indexAs] = true;
  3884. if (!this.ctor) {
  3885. this.templatize(this);
  3886. }
  3887. },
  3888. _sortChanged: function () {
  3889. var dataHost = this._getRootDataHost();
  3890. var sort = this.sort;
  3891. this._sortFn = sort && (typeof sort == 'function' ? sort : function () {
  3892. return dataHost[sort].apply(dataHost, arguments);
  3893. });
  3894. this._needFullRefresh = true;
  3895. if (this.items) {
  3896. this._debounceTemplate(this._render);
  3897. }
  3898. },
  3899. _filterChanged: function () {
  3900. var dataHost = this._getRootDataHost();
  3901. var filter = this.filter;
  3902. this._filterFn = filter && (typeof filter == 'function' ? filter : function () {
  3903. return dataHost[filter].apply(dataHost, arguments);
  3904. });
  3905. this._needFullRefresh = true;
  3906. if (this.items) {
  3907. this._debounceTemplate(this._render);
  3908. }
  3909. },
  3910. _observeChanged: function () {
  3911. this._observePaths = this.observe && this.observe.replace('.*', '.').split(' ');
  3912. },
  3913. _itemsChanged: function (change) {
  3914. if (change.path == 'items') {
  3915. if (Array.isArray(this.items)) {
  3916. this.collection = Polymer.Collection.get(this.items);
  3917. } else if (!this.items) {
  3918. this.collection = null;
  3919. } else {
  3920. this._error(this._logf('dom-repeat', 'expected array for `items`,' + ' found', this.items));
  3921. }
  3922. this._keySplices = [];
  3923. this._indexSplices = [];
  3924. this._needFullRefresh = true;
  3925. this._debounceTemplate(this._render);
  3926. } else if (change.path == 'items.splices') {
  3927. this._keySplices = this._keySplices.concat(change.value.keySplices);
  3928. this._indexSplices = this._indexSplices.concat(change.value.indexSplices);
  3929. this._debounceTemplate(this._render);
  3930. } else {
  3931. var subpath = change.path.slice(6);
  3932. this._forwardItemPath(subpath, change.value);
  3933. this._checkObservedPaths(subpath);
  3934. }
  3935. },
  3936. _checkObservedPaths: function (path) {
  3937. if (this._observePaths) {
  3938. path = path.substring(path.indexOf('.') + 1);
  3939. var paths = this._observePaths;
  3940. for (var i = 0; i < paths.length; i++) {
  3941. if (path.indexOf(paths[i]) === 0) {
  3942. this._needFullRefresh = true;
  3943. if (this.delay) {
  3944. this.debounce('render', this._render, this.delay);
  3945. } else {
  3946. this._debounceTemplate(this._render);
  3947. }
  3948. return;
  3949. }
  3950. }
  3951. }
  3952. },
  3953. render: function () {
  3954. this._needFullRefresh = true;
  3955. this._debounceTemplate(this._render);
  3956. this._flushTemplates();
  3957. },
  3958. _render: function () {
  3959. var c = this.collection;
  3960. if (this._needFullRefresh) {
  3961. this._applyFullRefresh();
  3962. this._needFullRefresh = false;
  3963. } else {
  3964. if (this._sortFn) {
  3965. this._applySplicesUserSort(this._keySplices);
  3966. } else {
  3967. if (this._filterFn) {
  3968. this._applyFullRefresh();
  3969. } else {
  3970. this._applySplicesArrayOrder(this._indexSplices);
  3971. }
  3972. }
  3973. }
  3974. this._keySplices = [];
  3975. this._indexSplices = [];
  3976. var keyToIdx = this._keyToInstIdx = {};
  3977. for (var i = 0; i < this._instances.length; i++) {
  3978. var inst = this._instances[i];
  3979. keyToIdx[inst.__key__] = i;
  3980. inst.__setProperty(this.indexAs, i, true);
  3981. }
  3982. this.fire('dom-change');
  3983. },
  3984. _applyFullRefresh: function () {
  3985. var c = this.collection;
  3986. var keys;
  3987. if (this._sortFn) {
  3988. keys = c ? c.getKeys() : [];
  3989. } else {
  3990. keys = [];
  3991. var items = this.items;
  3992. if (items) {
  3993. for (var i = 0; i < items.length; i++) {
  3994. keys.push(c.getKey(items[i]));
  3995. }
  3996. }
  3997. }
  3998. if (this._filterFn) {
  3999. keys = keys.filter(function (a) {
  4000. return this._filterFn(c.getItem(a));
  4001. }, this);
  4002. }
  4003. if (this._sortFn) {
  4004. keys.sort(function (a, b) {
  4005. return this._sortFn(c.getItem(a), c.getItem(b));
  4006. }.bind(this));
  4007. }
  4008. for (var i = 0; i < keys.length; i++) {
  4009. var key = keys[i];
  4010. var inst = this._instances[i];
  4011. if (inst) {
  4012. inst.__setProperty('__key__', key, true);
  4013. inst.__setProperty(this.as, c.getItem(key), true);
  4014. } else {
  4015. this._instances.push(this._insertRow(i, key));
  4016. }
  4017. }
  4018. for (; i < this._instances.length; i++) {
  4019. this._detachRow(i);
  4020. }
  4021. this._instances.splice(keys.length, this._instances.length - keys.length);
  4022. },
  4023. _keySort: function (a, b) {
  4024. return this.collection.getKey(a) - this.collection.getKey(b);
  4025. },
  4026. _numericSort: function (a, b) {
  4027. return a - b;
  4028. },
  4029. _applySplicesUserSort: function (splices) {
  4030. var c = this.collection;
  4031. var instances = this._instances;
  4032. var keyMap = {};
  4033. var pool = [];
  4034. var sortFn = this._sortFn || this._keySort.bind(this);
  4035. splices.forEach(function (s) {
  4036. for (var i = 0; i < s.removed.length; i++) {
  4037. var key = s.removed[i];
  4038. keyMap[key] = keyMap[key] ? null : -1;
  4039. }
  4040. for (var i = 0; i < s.added.length; i++) {
  4041. var key = s.added[i];
  4042. keyMap[key] = keyMap[key] ? null : 1;
  4043. }
  4044. }, this);
  4045. var removedIdxs = [];
  4046. var addedKeys = [];
  4047. for (var key in keyMap) {
  4048. if (keyMap[key] === -1) {
  4049. removedIdxs.push(this._keyToInstIdx[key]);
  4050. }
  4051. if (keyMap[key] === 1) {
  4052. addedKeys.push(key);
  4053. }
  4054. }
  4055. if (removedIdxs.length) {
  4056. removedIdxs.sort(this._numericSort);
  4057. for (var i = removedIdxs.length - 1; i >= 0; i--) {
  4058. var idx = removedIdxs[i];
  4059. if (idx !== undefined) {
  4060. pool.push(this._detachRow(idx));
  4061. instances.splice(idx, 1);
  4062. }
  4063. }
  4064. }
  4065. if (addedKeys.length) {
  4066. if (this._filterFn) {
  4067. addedKeys = addedKeys.filter(function (a) {
  4068. return this._filterFn(c.getItem(a));
  4069. }, this);
  4070. }
  4071. addedKeys.sort(function (a, b) {
  4072. return this._sortFn(c.getItem(a), c.getItem(b));
  4073. }.bind(this));
  4074. var start = 0;
  4075. for (var i = 0; i < addedKeys.length; i++) {
  4076. start = this._insertRowUserSort(start, addedKeys[i], pool);
  4077. }
  4078. }
  4079. },
  4080. _insertRowUserSort: function (start, key, pool) {
  4081. var c = this.collection;
  4082. var item = c.getItem(key);
  4083. var end = this._instances.length - 1;
  4084. var idx = -1;
  4085. var sortFn = this._sortFn || this._keySort.bind(this);
  4086. while (start <= end) {
  4087. var mid = start + end >> 1;
  4088. var midKey = this._instances[mid].__key__;
  4089. var cmp = sortFn(c.getItem(midKey), item);
  4090. if (cmp < 0) {
  4091. start = mid + 1;
  4092. } else if (cmp > 0) {
  4093. end = mid - 1;
  4094. } else {
  4095. idx = mid;
  4096. break;
  4097. }
  4098. }
  4099. if (idx < 0) {
  4100. idx = end + 1;
  4101. }
  4102. this._instances.splice(idx, 0, this._insertRow(idx, key, pool));
  4103. return idx;
  4104. },
  4105. _applySplicesArrayOrder: function (splices) {
  4106. var pool = [];
  4107. var c = this.collection;
  4108. splices.forEach(function (s) {
  4109. for (var i = 0; i < s.removed.length; i++) {
  4110. var inst = this._detachRow(s.index + i);
  4111. if (!inst.isPlaceholder) {
  4112. pool.push(inst);
  4113. }
  4114. }
  4115. this._instances.splice(s.index, s.removed.length);
  4116. for (var i = 0; i < s.addedKeys.length; i++) {
  4117. var inst = {
  4118. isPlaceholder: true,
  4119. key: s.addedKeys[i]
  4120. };
  4121. this._instances.splice(s.index + i, 0, inst);
  4122. }
  4123. }, this);
  4124. for (var i = this._instances.length - 1; i >= 0; i--) {
  4125. var inst = this._instances[i];
  4126. if (inst.isPlaceholder) {
  4127. this._instances[i] = this._insertRow(i, inst.key, pool, true);
  4128. }
  4129. }
  4130. },
  4131. _detachRow: function (idx) {
  4132. var inst = this._instances[idx];
  4133. if (!inst.isPlaceholder) {
  4134. var parentNode = Polymer.dom(this).parentNode;
  4135. for (var i = 0; i < inst._children.length; i++) {
  4136. var el = inst._children[i];
  4137. Polymer.dom(inst.root).appendChild(el);
  4138. }
  4139. }
  4140. return inst;
  4141. },
  4142. _insertRow: function (idx, key, pool, replace) {
  4143. var inst;
  4144. if (inst = pool && pool.pop()) {
  4145. inst.__setProperty(this.as, this.collection.getItem(key), true);
  4146. inst.__setProperty('__key__', key, true);
  4147. } else {
  4148. inst = this._generateRow(idx, key);
  4149. }
  4150. var beforeRow = this._instances[replace ? idx + 1 : idx];
  4151. var beforeNode = beforeRow ? beforeRow._children[0] : this;
  4152. var parentNode = Polymer.dom(this).parentNode;
  4153. Polymer.dom(parentNode).insertBefore(inst.root, beforeNode);
  4154. return inst;
  4155. },
  4156. _generateRow: function (idx, key) {
  4157. var model = { __key__: key };
  4158. model[this.as] = this.collection.getItem(key);
  4159. model[this.indexAs] = idx;
  4160. var inst = this.stamp(model);
  4161. return inst;
  4162. },
  4163. _showHideChildren: function (hidden) {
  4164. for (var i = 0; i < this._instances.length; i++) {
  4165. this._instances[i]._showHideChildren(hidden);
  4166. }
  4167. },
  4168. _forwardInstanceProp: function (inst, prop, value) {
  4169. if (prop == this.as) {
  4170. var idx;
  4171. if (this._sortFn || this._filterFn) {
  4172. idx = this.items.indexOf(this.collection.getItem(inst.__key__));
  4173. } else {
  4174. idx = inst[this.indexAs];
  4175. }
  4176. this.set('items.' + idx, value);
  4177. }
  4178. },
  4179. _forwardInstancePath: function (inst, path, value) {
  4180. if (path.indexOf(this.as + '.') === 0) {
  4181. this._notifyPath('items.' + inst.__key__ + '.' + path.slice(this.as.length + 1), value);
  4182. }
  4183. },
  4184. _forwardParentProp: function (prop, value) {
  4185. this._instances.forEach(function (inst) {
  4186. inst.__setProperty(prop, value, true);
  4187. }, this);
  4188. },
  4189. _forwardParentPath: function (path, value) {
  4190. this._instances.forEach(function (inst) {
  4191. inst._notifyPath(path, value, true);
  4192. }, this);
  4193. },
  4194. _forwardItemPath: function (path, value) {
  4195. if (this._keyToInstIdx) {
  4196. var dot = path.indexOf('.');
  4197. var key = path.substring(0, dot < 0 ? path.length : dot);
  4198. var idx = this._keyToInstIdx[key];
  4199. var inst = this._instances[idx];
  4200. if (inst) {
  4201. if (dot >= 0) {
  4202. path = this.as + '.' + path.substring(dot + 1);
  4203. inst._notifyPath(path, value, true);
  4204. } else {
  4205. inst.__setProperty(this.as, value, true);
  4206. }
  4207. }
  4208. }
  4209. },
  4210. itemForElement: function (el) {
  4211. var instance = this.modelForElement(el);
  4212. return instance && instance[this.as];
  4213. },
  4214. keyForElement: function (el) {
  4215. var instance = this.modelForElement(el);
  4216. return instance && instance.__key__;
  4217. },
  4218. indexForElement: function (el) {
  4219. var instance = this.modelForElement(el);
  4220. return instance && instance[this.indexAs];
  4221. }
  4222. });
  4223. Polymer({
  4224. is: 'array-selector',
  4225. properties: {
  4226. items: {
  4227. type: Array,
  4228. observer: 'clearSelection'
  4229. },
  4230. multi: {
  4231. type: Boolean,
  4232. value: false,
  4233. observer: 'clearSelection'
  4234. },
  4235. selected: {
  4236. type: Object,
  4237. notify: true
  4238. },
  4239. selectedItem: {
  4240. type: Object,
  4241. notify: true
  4242. },
  4243. toggle: {
  4244. type: Boolean,
  4245. value: false
  4246. }
  4247. },
  4248. clearSelection: function () {
  4249. if (Array.isArray(this.selected)) {
  4250. for (var i = 0; i < this.selected.length; i++) {
  4251. this.unlinkPaths('selected.' + i);
  4252. }
  4253. } else {
  4254. this.unlinkPaths('selected');
  4255. this.unlinkPaths('selectedItem');
  4256. }
  4257. if (this.multi) {
  4258. if (!this.selected || this.selected.length) {
  4259. this.selected = [];
  4260. this._selectedColl = Polymer.Collection.get(this.selected);
  4261. }
  4262. } else {
  4263. this.selected = null;
  4264. this._selectedColl = null;
  4265. }
  4266. this.selectedItem = null;
  4267. },
  4268. isSelected: function (item) {
  4269. if (this.multi) {
  4270. return this._selectedColl.getKey(item) !== undefined;
  4271. } else {
  4272. return this.selected == item;
  4273. }
  4274. },
  4275. deselect: function (item) {
  4276. if (this.multi) {
  4277. if (this.isSelected(item)) {
  4278. var skey = this._selectedColl.getKey(item);
  4279. this.arrayDelete('selected', item);
  4280. this.unlinkPaths('selected.' + skey);
  4281. }
  4282. } else {
  4283. this.selected = null;
  4284. this.selectedItem = null;
  4285. this.unlinkPaths('selected');
  4286. this.unlinkPaths('selectedItem');
  4287. }
  4288. },
  4289. select: function (item) {
  4290. var icol = Polymer.Collection.get(this.items);
  4291. var key = icol.getKey(item);
  4292. if (this.multi) {
  4293. if (this.isSelected(item)) {
  4294. if (this.toggle) {
  4295. this.deselect(item);
  4296. }
  4297. } else {
  4298. this.push('selected', item);
  4299. var skey = this._selectedColl.getKey(item);
  4300. this.linkPaths('selected.' + skey, 'items.' + key);
  4301. }
  4302. } else {
  4303. if (this.toggle && item == this.selected) {
  4304. this.deselect();
  4305. } else {
  4306. this.selected = item;
  4307. this.selectedItem = item;
  4308. this.linkPaths('selected', 'items.' + key);
  4309. this.linkPaths('selectedItem', 'items.' + key);
  4310. }
  4311. }
  4312. }
  4313. });
  4314. Polymer({
  4315. is: 'dom-if',
  4316. extends: 'template',
  4317. properties: {
  4318. 'if': {
  4319. type: Boolean,
  4320. value: false,
  4321. observer: '_queueRender'
  4322. },
  4323. restamp: {
  4324. type: Boolean,
  4325. value: false,
  4326. observer: '_queueRender'
  4327. }
  4328. },
  4329. behaviors: [Polymer.Templatizer],
  4330. _queueRender: function () {
  4331. this._debounceTemplate(this._render);
  4332. },
  4333. detached: function () {
  4334. this._teardownInstance();
  4335. },
  4336. attached: function () {
  4337. if (this.if && this.ctor) {
  4338. this.async(this._ensureInstance);
  4339. }
  4340. },
  4341. render: function () {
  4342. this._flushTemplates();
  4343. },
  4344. _render: function () {
  4345. if (this.if) {
  4346. if (!this.ctor) {
  4347. this.templatize(this);
  4348. }
  4349. this._ensureInstance();
  4350. this._showHideChildren();
  4351. } else if (this.restamp) {
  4352. this._teardownInstance();
  4353. }
  4354. if (!this.restamp && this._instance) {
  4355. this._showHideChildren();
  4356. }
  4357. if (this.if != this._lastIf) {
  4358. this.fire('dom-change');
  4359. this._lastIf = this.if;
  4360. }
  4361. },
  4362. _ensureInstance: function () {
  4363. if (!this._instance) {
  4364. this._instance = this.stamp();
  4365. var root = this._instance.root;
  4366. var parent = Polymer.dom(Polymer.dom(this).parentNode);
  4367. parent.insertBefore(root, this);
  4368. }
  4369. },
  4370. _teardownInstance: function () {
  4371. if (this._instance) {
  4372. var c = this._instance._children;
  4373. if (c) {
  4374. var parent = Polymer.dom(Polymer.dom(c[0]).parentNode);
  4375. c.forEach(function (n) {
  4376. parent.removeChild(n);
  4377. });
  4378. }
  4379. this._instance = null;
  4380. }
  4381. },
  4382. _showHideChildren: function () {
  4383. var hidden = this.__hideTemplateChildren__ || !this.if;
  4384. if (this._instance) {
  4385. this._instance._showHideChildren(hidden);
  4386. }
  4387. },
  4388. _forwardParentProp: function (prop, value) {
  4389. if (this._instance) {
  4390. this._instance[prop] = value;
  4391. }
  4392. },
  4393. _forwardParentPath: function (path, value) {
  4394. if (this._instance) {
  4395. this._instance._notifyPath(path, value, true);
  4396. }
  4397. }
  4398. });
  4399. Polymer({
  4400. is: 'dom-bind',
  4401. extends: 'template',
  4402. created: function () {
  4403. Polymer.RenderStatus.whenReady(this._markImportsReady.bind(this));
  4404. },
  4405. _ensureReady: function () {
  4406. if (!this._readied) {
  4407. this._readySelf();
  4408. }
  4409. },
  4410. _markImportsReady: function () {
  4411. this._importsReady = true;
  4412. this._ensureReady();
  4413. },
  4414. _registerFeatures: function () {
  4415. this._prepConstructor();
  4416. },
  4417. _insertChildren: function () {
  4418. var parentDom = Polymer.dom(Polymer.dom(this).parentNode);
  4419. parentDom.insertBefore(this.root, this);
  4420. },
  4421. _removeChildren: function () {
  4422. if (this._children) {
  4423. for (var i = 0; i < this._children.length; i++) {
  4424. this.root.appendChild(this._children[i]);
  4425. }
  4426. }
  4427. },
  4428. _initFeatures: function () {
  4429. },
  4430. _scopeElementClass: function (element, selector) {
  4431. if (this.dataHost) {
  4432. return this.dataHost._scopeElementClass(element, selector);
  4433. } else {
  4434. return selector;
  4435. }
  4436. },
  4437. _prepConfigure: function () {
  4438. var config = {};
  4439. for (var prop in this._propertyEffects) {
  4440. config[prop] = this[prop];
  4441. }
  4442. this._setupConfigure = this._setupConfigure.bind(this, config);
  4443. },
  4444. attached: function () {
  4445. if (this._importsReady) {
  4446. this.render();
  4447. }
  4448. },
  4449. detached: function () {
  4450. this._removeChildren();
  4451. },
  4452. render: function () {
  4453. this._ensureReady();
  4454. if (!this._children) {
  4455. this._template = this;
  4456. this._prepAnnotations();
  4457. this._prepEffects();
  4458. this._prepBehaviors();
  4459. this._prepConfigure();
  4460. this._prepBindings();
  4461. Polymer.Base._initFeatures.call(this);
  4462. this._children = Array.prototype.slice.call(this.root.childNodes);
  4463. }
  4464. this._insertChildren();
  4465. this.fire('dom-change');
  4466. }
  4467. });</script>
Add Comment
Please, Sign In to add comment