summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/ntp/util.js
diff options
context:
space:
mode:
authorarv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-04-30 00:41:37 +0000
committerarv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-04-30 00:41:37 +0000
commita02afd36072896ba584f32664cc8e4bc1889bae0 (patch)
treed202bf35af23f91f629df2680d3e2af3218f9b8d /chrome/browser/resources/ntp/util.js
parente030e767b4f0fef8ef1de21ebe977c2a4dec840b (diff)
downloadchromium_src-a02afd36072896ba584f32664cc8e4bc1889bae0.zip
chromium_src-a02afd36072896ba584f32664cc8e4bc1889bae0.tar.gz
chromium_src-a02afd36072896ba584f32664cc8e4bc1889bae0.tar.bz2
NTP - Refactor the most visited code to uncouple it from the rest of the NTP.
The goal of this refactoring is to allow splitting the different parts of the NTP into different reusable components. BUG=None TEST=Manually Review URL: http://codereview.chromium.org/1695022 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46021 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/resources/ntp/util.js')
-rw-r--r--chrome/browser/resources/ntp/util.js86
1 files changed, 86 insertions, 0 deletions
diff --git a/chrome/browser/resources/ntp/util.js b/chrome/browser/resources/ntp/util.js
new file mode 100644
index 0000000..ab99ec2
--- /dev/null
+++ b/chrome/browser/resources/ntp/util.js
@@ -0,0 +1,86 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// TODO(arv): Move to shared/js once namespaced and tested.
+
+var global = this;
+const IS_MAC = /$Mac/.test(navigator.platform);
+
+function $(id) {
+ return document.getElementById(id);
+}
+
+function bind(fn, selfObj, var_args) {
+ var boundArgs = Array.prototype.slice.call(arguments, 2);
+ return function() {
+ var args = Array.prototype.slice.call(arguments);
+ args.unshift.apply(args, boundArgs);
+ return fn.apply(selfObj, args);
+ }
+}
+
+function url(s) {
+ // http://www.w3.org/TR/css3-values/#uris
+ // Parentheses, commas, whitespace characters, single quotes (') and double
+ // quotes (") appearing in a URI must be escaped with a backslash
+ var s2 = s.replace(/(\(|\)|\,|\s|\'|\"|\\)/g, '\\$1');
+ // WebKit has a bug when it comes to URLs that end with \
+ // https://bugs.webkit.org/show_bug.cgi?id=28885
+ if (/\\\\$/.test(s2)) {
+ // Add a space to work around the WebKit bug.
+ s2 += ' ';
+ }
+ return 'url("' + s2 + '")';
+}
+
+function findAncestorByClass(el, className) {
+ return findAncestor(el, function(el) {
+ if (el.classList)
+ return el.classList.contains(className);
+ return null;
+ });
+}
+
+/**
+ * Return the first ancestor for which the {@code predicate} returns true.
+ * @param {Node} node The node to check.
+ * @param {function(Node) : boolean} predicate The function that tests the
+ * nodes.
+ * @return {Node} The found ancestor or null if not found.
+ */
+function findAncestor(node, predicate) {
+ var last = false;
+ while (node != null && !(last = predicate(node))) {
+ node = node.parentNode;
+ }
+ return last ? node : null;
+}
+
+function swapDomNodes(a, b) {
+ var afterA = a.nextSibling;
+ if (afterA == b) {
+ swapDomNodes(b, a);
+ return;
+ }
+ var aParent = a.parentNode;
+ b.parentNode.replaceChild(a, b);
+ aParent.insertBefore(b, afterA);
+}
+
+/**
+ * Calls chrome.send with a callback and restores the original afterwards.
+ */
+function chromeSend(name, params, callbackName, callback) {
+ var old = global[callbackName];
+ global[callbackName] = function() {
+ // restore
+ global[callbackName] = old;
+
+ var args = Array.prototype.slice.call(arguments);
+ return callback.apply(global, args);
+ };
+ chrome.send(name, params);
+}
+
+