Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
Typecheck chrome://bookmarks using Closure Compiler
Browse files Browse the repository at this point in the history
[email protected]
BUG=393873
TEST=GYP_GENERATORS=ninja gyp --depth . chrome/browser/resources/bookmark_manager/js/compiled_resources.gyp && ninja -C out/Default

Review URL: https://codereview.chromium.org/543863002

Cr-Commit-Position: refs/heads/master@{#297560}
  • Loading branch information
vitalyp authored and Commit bot committed Oct 1, 2014
1 parent d102d04 commit 5d8e6e2
Show file tree
Hide file tree
Showing 12 changed files with 714 additions and 180 deletions.
4 changes: 2 additions & 2 deletions chrome/browser/resources/bookmark_manager/js/bmm.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ cr.define('bmm', function() {
* Loads the entire bookmark tree and returns a {@code Promise} that will
* be fulfilled when done. This reuses multiple loads so that we do not load
* the same tree more than once at the same time.
* @return {!Promise.<Node>} The future promise for the load.
* @return {!Promise.<!BookmarkTreeNode>} The future promise for the load.
*/
function loadTree() {
return loadSubtree('');
Expand Down Expand Up @@ -169,7 +169,7 @@ cr.define('bmm', function() {
/**
* Callback for when a bookmark node is removed.
* @param {string} id The id of the removed bookmark node.
* @param {!Object} bookmarkNode The information about removed.
* @param {!Object} removeInfo The information about removed.
*/
function handleRemoved(id, removeInfo) {
if (bmm.list)
Expand Down
55 changes: 42 additions & 13 deletions chrome/browser/resources/bookmark_manager/js/bmm/bookmark_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@
// TODO(arv): Now that this is driven by a data model, implement a data model
// that handles the loading and the events from the bookmark backend.

/**
* @typedef {{childIds: Array.<string>}}
*
* @see chrome/common/extensions/api/bookmarks.json
*/
var ReorderInfo;

/**
* @typedef {{parentId: string,
* index: number,
* oldParentId: string,
* oldIndex: number}}
*
* @see chrome/common/extensions/api/bookmarks.json
*/
var MoveInfo;

cr.define('bmm', function() {
var List = cr.ui.List;
var ListItem = cr.ui.ListItem;
var ArrayDataModel = cr.ui.ArrayDataModel;
var ContextMenuButton = cr.ui.ContextMenuButton;

var list;

/**
* Basic array data model for use with bookmarks.
* @param {!Array.<!BookmarkTreeNode>} items The bookmark items.
Expand Down Expand Up @@ -57,7 +72,7 @@ cr.define('bmm', function() {
* Creates a new bookmark list.
* @param {Object=} opt_propertyBag Optional properties.
* @constructor
* @extends {HTMLButtonElement}
* @extends {cr.ui.List}
*/
var BookmarkList = cr.ui.define('list');

Expand All @@ -79,6 +94,10 @@ cr.define('bmm', function() {
bmm.list = this;
},

/**
* @param {!BookmarkTreeNode} bookmarkNode
* @override
*/
createItem: function(bookmarkNode) {
return new BookmarkListItem(bookmarkNode);
},
Expand Down Expand Up @@ -198,9 +217,10 @@ cr.define('bmm', function() {
* Handles mousedown events so that we can prevent the auto scroll as
* necessary.
* @private
* @param {!MouseEvent} e The mousedown event object.
* @param {!Event} e The mousedown event object.
*/
handleMouseDown_: function(e) {
e = /** @type {!MouseEvent} */(e);
if (e.button == 1) {
// WebKit no longer fires click events for middle clicks so we manually
// listen to mouse up to dispatch a click event.
Expand All @@ -220,9 +240,10 @@ cr.define('bmm', function() {
* WebKit no longer dispatches click events for middle clicks so we need
* to emulate it.
* @private
* @param {!MouseEvent} e The mouse up event object.
* @param {!Event} e The mouse up event object.
*/
handleMiddleMouseUp_: function(e) {
e = /** @type {!MouseEvent} */(e);
this.removeEventListener('mouseup', this.handleMiddleMouseUp_);
if (e.button == 1) {
var el = e.target;
Expand Down Expand Up @@ -250,6 +271,10 @@ cr.define('bmm', function() {
}
},

/**
* @param {string} id
* @param {ReorderInfo} reorderInfo
*/
handleChildrenReordered: function(id, reorderInfo) {
if (this.parentId == id) {
// We create a new data model with updated items in the right order.
Expand All @@ -274,6 +299,10 @@ cr.define('bmm', function() {
this.dataModel.splice(bookmarkNode.index, 0, bookmarkNode);
},

/**
* @param {string} id
* @param {MoveInfo} moveInfo
*/
handleMoved: function(id, moveInfo) {
if (moveInfo.parentId == this.parentId ||
moveInfo.oldParentId == this.parentId) {
Expand Down Expand Up @@ -344,7 +373,6 @@ cr.define('bmm', function() {

/**
* The ID of the bookmark folder we are displaying.
* @type {string}
*/
cr.defineProperty(BookmarkList, 'parentId', cr.PropertyKind.JS,
function() {
Expand All @@ -353,9 +381,10 @@ cr.define('bmm', function() {

/**
* The contextMenu property.
* @type {cr.ui.Menu}
*/
cr.ui.contextMenuHandler.addContextMenuProperty(BookmarkList);
/** @type {cr.ui.Menu} */
BookmarkList.prototype.contextMenu;

/**
* Creates a new bookmark list item.
Expand Down Expand Up @@ -507,28 +536,28 @@ cr.define('bmm', function() {
this.setAttribute('editing', '');
this.draggable = false;

labelInput = doc.createElement('input');
labelInput = /** @type {HTMLElement} */(doc.createElement('input'));
labelInput.placeholder =
loadTimeData.getString('name_input_placeholder');
replaceAllChildren(labelEl, labelInput);
labelInput.value = title;

if (!isFolder) {
urlInput = doc.createElement('input');
urlInput = /** @type {HTMLElement} */(doc.createElement('input'));
urlInput.type = 'url';
urlInput.required = true;
urlInput.placeholder =
loadTimeData.getString('url_input_placeholder');

// We also need a name for the input for the CSS to work.
urlInput.name = '-url-input-' + cr.createUid();
replaceAllChildren(urlEl, urlInput);
replaceAllChildren(assert(urlEl), urlInput);
urlInput.value = url;
}

function stopPropagation(e) {
var stopPropagation = function(e) {
e.stopPropagation();
}
};

var eventsToStop =
['mousedown', 'mouseup', 'contextmenu', 'dblclick', 'paste'];
Expand Down Expand Up @@ -601,6 +630,6 @@ cr.define('bmm', function() {

return {
BookmarkList: BookmarkList,
list: list
list: /** @type {Element} */(null), // Set when decorated.
};
});
36 changes: 25 additions & 11 deletions chrome/browser/resources/bookmark_manager/js/bmm/bookmark_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ cr.define('bmm', function() {

/** @const */ var Tree = cr.ui.Tree;
/** @const */ var TreeItem = cr.ui.TreeItem;
/** @const */ var localStorage = window.localStorage;

var treeLookup = {};
var tree;

// Manager for persisting the expanded state.
var expandedManager = {
var expandedManager = /** @type {EventListener} */({
/**
* A map of the collapsed IDs.
* @type {Object}
*/
map: 'bookmarkTreeState' in localStorage ?
JSON.parse(localStorage['bookmarkTreeState']) : {},
/** @type {Object} */(JSON.parse(localStorage['bookmarkTreeState'])) :
{},

/**
* Set the collapsed state for an ID.
* @param {string} The bookmark ID of the tree item that was expanded or
* @param {string} id The bookmark ID of the tree item that was expanded or
* collapsed.
* @param {boolean} expanded Whether the tree item was expanded.
*/
Expand Down Expand Up @@ -83,7 +84,7 @@ cr.define('bmm', function() {
localStorage['bookmarkTreeState'] = JSON.stringify(map);
}, 100);
}
};
});

// Clean up once per session but wait until things settle down a bit.
setTimeout(expandedManager.cleanUp.bind(expandedManager), 1e4);
Expand Down Expand Up @@ -131,12 +132,16 @@ cr.define('bmm', function() {
*
* @param {!cr.ui.TreeItem} parent The parent tree item.
* @param {!cr.ui.TreeItem} treeItem The tree item to add.
* @param {Function=} f A function which gets called after the item has been
* added at the right index.
* @param {Function=} opt_f A function which gets called after the item has
* been added at the right index.
*/
function addTreeItem(parent, treeItem, opt_f) {
chrome.bookmarks.getChildren(parent.bookmarkNode.id, function(children) {
var index = children.filter(bmm.isFolder).map(function(item) {
var isFolder = /**
* @type {function (BookmarkTreeNode, number,
* Array.<(BookmarkTreeNode)>)}
*/(bmm.isFolder);
var index = children.filter(isFolder).map(function(item) {
return item.id;
}).indexOf(treeItem.bookmarkNode.id);
parent.addAt(treeItem, index);
Expand All @@ -151,7 +156,7 @@ cr.define('bmm', function() {
* Creates a new bookmark list.
* @param {Object=} opt_propertyBag Optional properties.
* @constructor
* @extends {HTMLButtonElement}
* @extends {cr.ui.Tree}
*/
var BookmarkTree = cr.ui.define('tree');

Expand All @@ -172,6 +177,10 @@ cr.define('bmm', function() {
treeItem.label = treeItem.bookmarkNode.title = changeInfo.title;
},

/**
* @param {string} id
* @param {ReorderInfo} reorderInfo
*/
handleChildrenReordered: function(id, reorderInfo) {
var parentItem = treeLookup[id];
// The tree only contains folders.
Expand All @@ -190,6 +199,10 @@ cr.define('bmm', function() {
}
},

/**
* @param {string} id
* @param {MoveInfo} moveInfo
*/
handleMoved: function(id, moveInfo) {
var treeItem = treeLookup[id];
if (treeItem) {
Expand Down Expand Up @@ -262,7 +275,8 @@ cr.define('bmm', function() {
hasDirectories = true;
var item = new BookmarkTreeItem(bookmarkNode);
parentTreeItem.add(item);
var anyChildren = buildTreeItems(item, bookmarkNode.children);
var children = assert(bookmarkNode.children);
var anyChildren = buildTreeItems(item, children);
item.expanded = anyChildren && expandedManager.get(bookmarkNode.id);
}
}
Expand Down Expand Up @@ -301,7 +315,7 @@ cr.define('bmm', function() {
BookmarkTree: BookmarkTree,
BookmarkTreeItem: BookmarkTreeItem,
treeLookup: treeLookup,
tree: tree,
tree: /** @type {Element} */(null), // Set when decorated.
ROOT_ID: ROOT_ID
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2014 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.
{
'targets': [
{
'target_name': 'main',
'variables': {
'depends': [
'../../../../../third_party/jstemplate/compiled_resources.gyp:jstemplate',
'../../../../../ui/webui/resources/js/cr.js',
'../../../../../ui/webui/resources/js/cr/event_target.js',
'../../../../../ui/webui/resources/js/cr/link_controller.js',
'../../../../../ui/webui/resources/js/cr/ui.js',
'../../../../../ui/webui/resources/js/cr/ui/array_data_model.js',
'../../../../../ui/webui/resources/js/cr/ui/command.js',
'../../../../../ui/webui/resources/js/cr/ui/context_menu_button.js',
'../../../../../ui/webui/resources/js/cr/ui/context_menu_handler.js',
'../../../../../ui/webui/resources/js/cr/ui/focus_outline_manager.js',
'../../../../../ui/webui/resources/js/cr/ui/list.js',
'../../../../../ui/webui/resources/js/cr/ui/list_item.js',
'../../../../../ui/webui/resources/js/cr/ui/list_selection_controller.js',
'../../../../../ui/webui/resources/js/cr/ui/list_selection_model.js',
'../../../../../ui/webui/resources/js/cr/ui/menu.js',
'../../../../../ui/webui/resources/js/cr/ui/menu_button.js',
'../../../../../ui/webui/resources/js/cr/ui/menu_item.js',
'../../../../../ui/webui/resources/js/cr/ui/position_util.js',
'../../../../../ui/webui/resources/js/cr/ui/splitter.js',
'../../../../../ui/webui/resources/js/cr/ui/touch_handler.js',
'../../../../../ui/webui/resources/js/cr/ui/tree.js',
'../../../../../ui/webui/resources/js/event_tracker.js',
'../../../../../ui/webui/resources/js/i18n_template_no_process.js',
'../../../../../ui/webui/resources/js/load_time_data.js',
'../../../../../ui/webui/resources/js/util.js',
'../../../../../chrome/browser/resources/bookmark_manager/js/bmm.js',
'../../../../../chrome/browser/resources/bookmark_manager/js/bmm/bookmark_list.js',
'../../../../../chrome/browser/resources/bookmark_manager/js/bmm/bookmark_tree.js',
'../../../../../chrome/browser/resources/bookmark_manager/js/dnd.js',
],
'externs': [
'<(CLOSURE_DIR)/externs/bookmark_manager_private.js',
'<(CLOSURE_DIR)/externs/chrome_send_externs.js',
'<(CLOSURE_DIR)/externs/chrome_extensions.js',
'<(CLOSURE_DIR)/externs/metrics_private.js',
'<(CLOSURE_DIR)/externs/system_private.js',
'../../../../../ui/webui/resources/js/template_data_externs.js',
],
},
'includes': ['../../../../../third_party/closure_compiler/compile_js.gypi'],
}
],
}
Loading

0 comments on commit 5d8e6e2

Please sign in to comment.