Skip to content

Commit

Permalink
Adds Polymer.BasicElement
Browse files Browse the repository at this point in the history
Provides minimal element starting point using PropertyAccessors to create properties using a properties object similar to Polymer.Element. No template or binding support is provided.
  • Loading branch information
Steven Orvell committed Sep 15, 2017
1 parent aa4f186 commit 717a4f4
Show file tree
Hide file tree
Showing 3 changed files with 482 additions and 2 deletions.
100 changes: 100 additions & 0 deletions lib/elements/basic-element.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!--
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../utils/boot.html">
<link rel="import" href="../utils/mixin.html">
<link rel="import" href="../mixins/property-accessors.html">

<script>
(function() {
'use strict';

/**
* Custom element base class that provides minimal starting point
* using PropertyAccessors to create properties.
*
* @customElement
* @polymer
* @memberof Polymer
* @constructor
* @implements {Polymer_PropertyAccessors}
* @extends HTMLElement
* @appliesMixin Polymer.PropertyAccessors
* @summary Custom element base class that provides minimal starting point
* using PropertyAccessors to create properties
*/
class BasicElement extends Polymer.PropertyAccessors(HTMLElement) {

static _ensureFinalized(name) {
const proto = this.prototype;
if (!proto.hasOwnProperty('__finalized')) {
proto.__finalized = true;
this.finalize(name);
}
}

static get observedAttributes() {
const props = this.properties;
return props ? Object.keys(props) : [];
}

/**
* Finalizes an element definition. This includes ensuring property
* accessors exist on the element prototype and parsing the element
* template.
* @param {string} name Name of the element
*/
static finalize(name) {
const props = this.properties;
if (props) {
this.prototype.__propertyInfo = props;
this.createProperties(Object.keys(props));
}
}

/**
* Overrides default behavior and adds a call to `finalize`.
* @override
*/
_initializeProperties() {
this.constructor._ensureFinalized(this.localName);
super._initializeProperties();
}

_attributeToProperty(attribute, value, type) {
if (!type) {
const property = this._propertyNameForAttribute(attribute);
const props = this.__propertyInfo;
const info = props && props[property];
type = info && info.type || info ;
}
super._attributeToProperty(attribute, value, type);
}

connectedCallback() {
this._enableProperties();
}

/**
* Called when the element is removed from a document
*/
disconnectedCallback() {}

}
/**
* @constructor
* @implements {Polymer_PropertyAccessors}
* @extends {HTMLElement}
*/
Polymer.BasicElement = BasicElement;

})();

</script>
24 changes: 22 additions & 2 deletions lib/mixins/property-accessors.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@
}
}

/**
* Returns a property name that corresponds to the given attribute.
* By default, converts dash to camel case, e.g. `foo-bar` to `fooBar`.
* @param {string} attribute Attribute to convert
* @return {string} Property name corresponding to the given attribute.
*/
_propertyNameForAttribute(attribute) {
return caseMap.dashToCamelCase(attribute);
}

/**
* Returns an attribute name that corresponds to the given property.
* By default, converts camel to dash case, e.g. `fooBar` to `foo-bar`.
* @param {string} property Property to convert
* @return {string} Attribute name corresponding to the given property.
*/
_attributeNameForProperty(property) {
return caseMap.camelToDashCase(property);
}

/**
* Initializes the local storage for property accessors.
*
Expand Down Expand Up @@ -245,7 +265,7 @@
_attributeToProperty(attribute, value, type) {
// Don't deserialize back to property if currently reflecting
if (!this.__serializing) {
let property = caseMap.dashToCamelCase(attribute);
let property = this._propertyNameForAttribute(attribute);
this[property] = this._deserializeValue(value, type);
}
}
Expand All @@ -261,7 +281,7 @@
this.__serializing = true;
value = (arguments.length < 3) ? this[property] : value;
this._valueToNodeAttribute(this, value,
attribute || caseMap.camelToDashCase(property));
attribute || this._attributeNameForProperty(property));
this.__serializing = false;
}

Expand Down
Loading

0 comments on commit 717a4f4

Please sign in to comment.