Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Unified Diff: chrome/browser/resources/chromeos/login/oobe_screen_network.js

Issue 7520037: [cros] Network dropdown button in WebUI. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: move handle click Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/chromeos/login/oobe_screen_network.js
diff --git a/chrome/browser/resources/chromeos/login/oobe_screen_network.js b/chrome/browser/resources/chromeos/login/oobe_screen_network.js
index 179c1fbfd5b9b841f9ce996127201d886a148a3c..a153ddff14aa6cfb8e817541414aad997c4c5d6e 100644
--- a/chrome/browser/resources/chromeos/login/oobe_screen_network.js
+++ b/chrome/browser/resources/chromeos/login/oobe_screen_network.js
@@ -3,6 +3,146 @@
// found in the LICENSE file.
/**
+ * Creates dropdown element.
+ * @param {!Element} root Root element, where dropdown is inserted to.
+ * @param {string} title Text on dropdown.
+ * @param {Array} items Initial array of dropdown items.
+ */
+function DropDown(root, title, items) {
xiyuan 2011/07/29 21:39:33 we probably want to put this into a cr.define name
xiyuan 2011/07/29 21:39:33 It would be better to merge DropDown a HTMLDivElem
Nikita (slow) 2011/08/05 23:40:26 Done.
Nikita (slow) 2011/08/05 23:40:26 Done.
+ this.root_ = root;
+ this.container_ = document.createElement('div');
+ this.container_.classList.add('dropdown-container');
xiyuan 2011/07/29 21:39:33 this.container_ element seems not used.
Nikita (slow) 2011/08/05 23:40:26 Done.
+ this.isShown = false;
+
+ this.setTitle(title);
+ this.setItems(items);
+}
+
+DropDown.prototype = {
+ /**
+ * Sets title and icon.
+ * @param {string} title Text on dropdown.
+ * @param {string} icon Icon in dataURL format.
+ */
+ setTitle: function(title, icon) {
+ var titleHtml = title;
+ var image = document.createElement('img');
+ if (icon)
+ image.src = icon;
+ this.root_.insertBefore(
+ this.createDiv_('dropdown-title', image, titleHtml, -1, true),
+ this.root_.childNodes[0]);
+ if (this.root_.childNodes.length > 1)
+ this.root_.removeChild(this.root_.childNodes[1]);
xiyuan 2011/07/29 21:39:33 It seems a DropDown has two immediate child: title
Nikita (slow) 2011/08/05 23:40:26 Done.
+ },
+
+ /**
+ * Sets dropdown items.
+ * @param {Array} items Dropdown items array.
+ */
+ setItems: function(items) {
+ if (this.root_.childNodes.length > 1) {
+ this.root_.removeChild(this.root_.childNodes[1]);
xiyuan 2011/07/29 21:39:33 see above. Instead of creating the container every
Nikita (slow) 2011/08/05 23:40:26 Done.
+ }
+
+ var container = document.createElement('div')
+ container.classList.add('dropdown-container');
+
+ for (var i = 0; i < items.length; ++i) {
+ var item = items[i];
+ if ('sub' in item) {
+ // Workaround for submenus, add items on top level.
+ // TODO(altimofeev): support submenus.
+ // item = item.sub[0];
+ for (var j = 0; j < item.sub.length; ++j)
+ this.createItem_(container, item.sub[j]);
+
+ continue;
+ }
+ this.createItem_(container, item);
+ }
+
+ this.root_.appendChild(container);
+ container.style.display = this.isShown ? 'block' : 'none';
xiyuan 2011/07/29 21:39:33 suggest to use container.hidden instead of display
Nikita (slow) 2011/08/05 23:40:26 Done.
+ },
+
+ /**
+ * Creates dropdown element (div/button) for dropdown item / title.
+ * @param {string} className Element class.
+ * @param {HTMLElement} image Image element with icon.
+ * @param {string} innerHtml Inner contents of the element.
+ * @param {number} iid Internal ID of the element.
+ * @param {bool} enabled Whether element is enabled.
+ * @type {HTMLElement}
+ * @private
+ */
+ createDiv_: function(className, image, innerHtml, iid, enabled) {
+ var el = document.createElement(
+ className == 'dropdown-title' ? 'button' : 'div');
+ el.classList.add(className);
+ el.innerHTML = innerHtml;
xiyuan 2011/07/29 21:39:33 we probably should try to get rid of this use.
Nikita (slow) 2011/08/05 23:40:26 Done.
+ el.iid = iid;
+ el.link = this;
xiyuan 2011/07/29 21:39:33 nit: why DropDown is called "link"? Why not use pa
Nikita (slow) 2011/08/05 23:40:26 Done.
+ el.addEventListener('click', function f(e) {
+ if (this.iid < -1 || this.classList.contains('disabled-item'))
+ return;
+ this.link.isShown = !this.link.isShown;
xiyuan 2011/07/29 21:39:33 Do we really toggle isShown here?
Nikita (slow) 2011/08/05 23:40:26 Yes, menu is hidden on a click.
+ this.link.root_.childNodes[1].style.display =
xiyuan 2011/07/29 21:39:33 use hidden attribute.
Nikita (slow) 2011/08/05 23:40:26 Done.
+ this.link.isShown ? 'block' : 'none';
+ if (this.iid >= 0) {
+ chrome.send('networkItemChosen', [this.iid]);
+ }
+ });
+
+ if (!enabled)
+ el.classList.add('disabled-item');
+
+ // TODO(nkostylev): Icon support for title.
+ if (className != 'dropdown-title' && iid > 0) {
+ var wrapperDiv = document.createElement('div');
+ wrapperDiv.classList.add('dropdown-item-container');
+ var imageDiv = document.createElement('div');
+ imageDiv.classList.add('dropdown-image');
+ imageDiv.appendChild(image);
+ wrapperDiv.appendChild(imageDiv);
+ wrapperDiv.appendChild(el);
+ el = wrapperDiv;
+ }
+ return el;
+ },
+
+ /**
+ * Creates dropdown item element and adds into container.
+ * @param {HTMLElement} container Container where item is added.
+ * @param {!Object} item Item to be added.
+ * @private
+ */
+ createItem_ : function(container, item) {
+ var text = item.label;
+ if ('bold' in item && item.bold) {
+ text = '<b>' + text + "</b>";
+ }
+ var itemHtml = '<span>' + text + '</span>';
+
+ var className = 'dropdown-item';
+ // Separator.
+ if (item.id == -2) {
+ className = 'dropdown-divider';
+ itemHtml = "<hr>";
+ }
+
+ var image = document.createElement('img');
+ if (item.icon)
+ image.src = item.icon;
+ var enabled = 'enabled' in item && item.enabled;
+ container.appendChild(
+ this.createDiv_(className, image, itemHtml, item.id, enabled));
+ },
+};
+
+var dropdown = undefined;
xiyuan 2011/07/29 21:39:33 we should avoid using global vars. Let's put this
Nikita (slow) 2011/08/05 23:40:26 Done.
+
+/**
* @fileoverview Oobe network screen implementation.
*/
@@ -21,6 +161,8 @@ cr.define('oobe', function() {
var screen = $('connect');
NetworkScreen.decorate(screen);
Oobe.getInstance().registerScreen(screen);
+
+ dropdown = new DropDown($('networks-list'), "empty", []);
};
NetworkScreen.prototype = {
@@ -64,6 +206,14 @@ cr.define('oobe', function() {
}
};
+ NetworkScreen.updateNetworks = function(data) {
+ dropdown.setItems(data[0].sub);
+ };
+
+ NetworkScreen.updateNetworkTitle = function(title, icon) {
+ dropdown.setTitle(title, icon);
+ };
+
return {
NetworkScreen: NetworkScreen
};

Powered by Google App Engine
This is Rietveld 408576698