Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
- /**
- * Copyright 2014 Opera Software ASA
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- **/
- Element.prototype.matchesSelector || (Element.prototype.matchesSelector = Element.prototype.webkitMatchesSelector || function(selector) { // what about other vendor prefixes, is it meant to run only on Webkit? ;)
- if(this.parentNode) {
- var sel = this.parentNode.querySelectorAll(selector);
- var i = 0;
- while (sel[++i] !== this);
- return !!sel[i];
- }
- });
- (function() {
- 'use strict';
- this.createTemplate = function() {
- // A special element name. We don't create an element for a template that
- // starts with this element but instead merge it with its parent context.
- var TEXT_NODE_NAME = '#text';
- var ObjToString = Object.prototype.toString;
- var ATTRS = 1;
- return function(tmpl){
- var ele = null;
- var elementName = tmpl[0];
- var i = 0;
- var attrs = null;
- if (typeof elementName === 'string' && elementName !== TEXT_NODE_NAME) {
- i++;
- ele = document.createElement(elementName);
- if (ObjToString.call(tmpl[ATTRS]) === '[object Object]') {
- i++;
- if (attrs = tmpl[ATTRS]) {
- for (var prop in attrs) {
- if (typeof attrs[prop] === 'string') {
- ele.setAttribute(prop, attrs[prop]);
- }
- }
- }
- }
- } else {
- if (elementName === TEXT_NODE_NAME) {
- i++;
- }
- ele = document.createDocumentFragment();
- }
- for (var len = tmpl.length; i < len; i++) {
- if (typeof tmpl[i] === 'string') {
- ele.appendChild(document.createTextNode(tmpl[i]));
- } else if (tmpl[i]) {
- ele.appendChild(window.bragi.createTemplate(tmpl[i]));
- }
- }
- return ele;
- };
- }();
- /**
- * Creates a template array from the localized string which contains
- * placeholders, replacing placeholders with templates specified in the
- * replacementMap.
- *
- * @param {String} string The localized string to process.
- * @param {Object} replacementMap The map with templates specified for each
- * placeholder. First placeholder in the string is marked with a '$1'.
- * @return {Array} The resulting template.
- */
- this.createTemplateFromStringF = function(stringToProcess, replacementMap) {
- var result = ['#text'];
- var $number = /\$[$1-9]/;
- var matchPlaceholder;
- while (matchPlaceholder = $number.exec(stringToProcess)) {
- var placeholder = matchPlaceholder[0];
- var placeholderPosition = matchPlaceholder.index;
- var currentStringSlice = stringToProcess.substr(0, placeholderPosition);
- // Save remaining for the next iteration.
- stringToProcess = stringToProcess.slice(placeholderPosition + placeholder.length);
- if (placeholder == '$$') {
- currentStringSlice += '$';
- } else if (placeholder != '$$') {
- var placeholderTemplate = replacementMap[placeholder];
- result.push(placeholderTemplate);
- }
- if (currentStringSlice !== '') {
- result.push(currentStringSlice);
- }
- if (stringToProcess !== '') {
- result.push(stringToProcess);
- }
- return result;
- }
- };
- window.EventHandler = function(type, isCapturing, handlerKey) {
- return this.init_(type, isCapturing, handlerKey);
- };
- Element.prototype.appendTemplate = function(tmpl) {
- return this.appendChild(window.bragi.createTemplate(tmpl));
- };
- Element.prototype.cleanAppendTemplate = function(tmpl) {
- this.textContent = '';
- return this.appendTemplate(tmpl);
- };
- Element.prototype.replaceWithTemplate = function() {
- var slice = Array.prototype.slice;
- return function(tmpl){
- var parent = this.parentNode;
- if (parent) {
- var documentFragment = document.createDocumentFragment();
- documentFragment.appendTemplate(tmpl);
- var ret = slice.call(documentFragment.childNodes);
- parent.replaceChild(documentFragment, this);
- return ret;
- }
- return null;
- };
- }();
- Element.prototype.getAncestor = function(selector) {
- var ele = this;
- while (ele) {
- if (ele.matchesSelector(selector)) {
- return ele;
- }
- ele = ele.parentElement;
- }
- return null;
- };
- Element.prototype.getAncestorAttr = function(name) {
- var ele = this;
- while (ele && !ele.hasAttribute(name)) {
- ele = ele.parentElement;
- }
- return ele && ele.hasAttribute(name) ? ele.getAttribute(name) : null;
- };
- var handlers_ = Object.create ? {'true': Object.create(null), 'false': Object.create(null)} : {'true': [], 'false': []};
- var handler_ = function(handler_map, event) {
- var ele = event.target;
- while (ele && !event.cancelBubble) {
- var name = ele.getAttribute('data-handler');
- if (name && handler_map[name])
- handler_map[name](event, ele);
- ele = ele.parentElement;
- }
- };
- function EventHandler(){}
- EventHandler.prototype.init_ = function(type, isCapturing) {
- isCapturing = !!isCapturing;
- if (handlers_[isCapturing][type]) {
- return handlers_[isCapturing][type];
- }
- var handler_map = handlers_[isCapturing][type] = Object.create ? Object.create(null) : {};
- var handler = handler_.bind(this, handler_map);
- document.addEventListener(type, handler, isCapturing);
- return handler_map;
- };
- EventHandler.register = function(type, name, handler, isCapturing) {
- isCapturing = !!isCapturing;
- (handlers_[isCapturing][type] || new EventHandler(type, isCapturing))[name] = handler;
- };
- EventHandler.unregister = function(type, name, handler, isCapturing) {
- var handler_map = handlers_[!!isCapturing][type];
- if (handler_map && handler_map[name] === handler)
- handler_map[name] = null;
- };
- }).call(window.bragi || (window.bragi = {}));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement