- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / examples / extensions / plugin_settings / domui / js / util.js
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /**
6  * The global object.
7  * @type {!Object}
8  */
9 const global = this;
10
11 /**
12  * Alias for document.getElementById.
13  * @param {string} id The ID of the element to find.
14  * @return {HTMLElement} The found element or null if not found.
15  */
16 function $(id) {
17   return document.getElementById(id);
18 }
19
20 /**
21  * Calls chrome.send with a callback and restores the original afterwards.
22  * @param {string} name The name of the message to send.
23  * @param {!Array} params The parameters to send.
24  * @param {string} callbackName The name of the function that the backend calls.
25  * @param {!Function} The function to call.
26  */
27 function chromeSend(name, params, callbackName, callback) {
28   var old = global[callbackName];
29   global[callbackName] = function() {
30     // restore
31     global[callbackName] = old;
32
33     var args = Array.prototype.slice.call(arguments);
34     return callback.apply(global, args);
35   };
36   chrome.send(name, params);
37 }
38
39 /**
40  * Generates a CSS url string.
41  * @param {string} s The URL to generate the CSS url for.
42  * @return {string} The CSS url string.
43  */
44 function url(s) {
45   // http://www.w3.org/TR/css3-values/#uris
46   // Parentheses, commas, whitespace characters, single quotes (') and double
47   // quotes (") appearing in a URI must be escaped with a backslash
48   var s2 = s.replace(/(\(|\)|\,|\s|\'|\"|\\)/g, '\\$1');
49   // WebKit has a bug when it comes to URLs that end with \
50   // https://bugs.webkit.org/show_bug.cgi?id=28885
51   if (/\\\\$/.test(s2)) {
52     // Add a space to work around the WebKit bug.
53     s2 += ' ';
54   }
55   return 'url("' + s2 + '")';
56 }
57
58 /**
59  * Parses query parameters from Location.
60  * @param {string} s The URL to generate the CSS url for.
61  * @return {object} Dictionary containing name value pairs for URL
62  */
63 function parseQueryParams(location) {
64   var params = {};
65   var query = unescape(location.search.substring(1));
66   var vars = query.split("&");
67   for (var i=0; i < vars.length; i++) {
68     var pair = vars[i].split("=");
69     params[pair[0]] = pair[1];
70   }
71   return params;
72 }
73
74 function findAncestorByClass(el, className) {
75   return findAncestor(el, function(el) {
76     if (el.classList)
77       return el.classList.contains(className);
78     return null;
79   });
80 }
81
82 /**
83  * Return the first ancestor for which the {@code predicate} returns true.
84  * @param {Node} node The node to check.
85  * @param {function(Node) : boolean} predicate The function that tests the
86  *     nodes.
87  * @return {Node} The found ancestor or null if not found.
88  */
89 function findAncestor(node, predicate) {
90   var last = false;
91   while (node != null && !(last = predicate(node))) {
92     node = node.parentNode;
93   }
94   return last ? node : null;
95 }
96
97 function swapDomNodes(a, b) {
98   var afterA = a.nextSibling;
99   if (afterA == b) {
100     swapDomNodes(b, a);
101     return;
102   }
103   var aParent = a.parentNode;
104   b.parentNode.replaceChild(a, b);
105   aParent.insertBefore(b, afterA);
106 }
107
108 /**
109  * Disables text selection and dragging.
110  */
111 function disableTextSelectAndDrag() {
112   // Disable text selection.
113   document.onselectstart = function(e) {
114     e.preventDefault();
115   }
116
117   // Disable dragging.
118   document.ondragstart = function(e) {
119     e.preventDefault();
120   }
121 }
122
123 // Handle click on a link. If the link points to a chrome: or file: url, then
124 // call into the browser to do the navigation.
125 document.addEventListener('click', function(e) {
126   // Allow preventDefault to work.
127   if (!e.returnValue)
128     return;
129
130   var el = e.target;
131   if (el.nodeType == Node.ELEMENT_NODE &&
132       el.webkitMatchesSelector('A, A *')) {
133     while (el.tagName != 'A') {
134       el = el.parentElement;
135     }
136
137     if ((el.protocol == 'file:' || el.protocol == 'about:') &&
138         (e.button == 0 || e.button == 1)) {
139       chrome.send('navigateToUrl', [
140         el.href,
141         el.target,
142         e.button,
143         e.altKey,
144         e.ctrlKey,
145         e.metaKey,
146         e.shiftKey
147       ]);
148       e.preventDefault();
149     }
150   }
151 });