webcomponent
Version:
lightweight helpers for constructing web components
90 lines (69 loc) • 3.95 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _extendableHtmlElement = require('./extendable-html-element');
var _extendableHtmlElement2 = _interopRequireDefault(_extendableHtmlElement);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
// WebComponent = thin wrapper around HTMLElement with convenience methods
var WebComponent = function (_ExtendableHTMLElemen) {
_inherits(WebComponent, _ExtendableHTMLElemen);
function WebComponent() {
_classCallCheck(this, WebComponent);
return _possibleConstructorReturn(this, (WebComponent.__proto__ || Object.getPrototypeOf(WebComponent)).apply(this, arguments));
}
_createClass(WebComponent, [{
key: 'getJSONAttribute',
// parse an attribute which has been serialized as JSON
// pass an optional errorHandler in case JSON.parse() fails
value: function getJSONAttribute(attrName) {
var errorHandler = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {
return null;
};
try {
return JSON.parse(this.getAttribute(attrName));
} catch (e) {
return errorHandler(attrName, e);
}
}
// Parse number attributes. Examples:
// null -> null
// "" -> null
// "asd" -> null
// "123asd" -> null
// "0" -> 0
// "123" -> 123
// "-123" -> -123
// "123.45" -> 123.45
}, {
key: 'getNumberAttribute',
value: function getNumberAttribute(attrName) {
var attrVal = this.getAttribute(attrName);
if (!attrVal) {
return null;
} else {
var num = Number(attrVal);
return Number.isNaN(num) ? null : num;
}
}
// check whether a boolean attribute is 'enabled' in an element instance
// taking into account usages such as:
// <my-element myattr="true"> -> enabled
// <my-element myattr> -> enabled
// <my-element myattr="myattr"> -> enabled
// <my-element myattr="false"> -> disabled
// <my-element> -> disabled
}, {
key: 'isAttributeEnabled',
value: function isAttributeEnabled(attrName) {
var attrVal = this.getAttribute(attrName);
return typeof attrVal === 'string' && ['', 'true', attrName.toLowerCase()].indexOf(attrVal.toLowerCase()) !== -1;
}
}]);
return WebComponent;
}(_extendableHtmlElement2.default);
exports.default = WebComponent;
;