1// Copyright (c) 2012 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// Defines global variables.
6var commands = TaskManagerCommands;
7var taskmanager = undefined;  // This will be instantiated in main.js.
8
9/**
10 * Invoked when a range of items has changed.
11 * @param {Integer} start The start position of tasks to be changed
12 * @param {Integer} length The length of tasks to be changed
13 * @param {Array of task} tasks The array of updated task
14 */
15function taskChanged(start, length, tasks) {
16  var task = {type: 'change', start: start, length: length, tasks: tasks};
17
18  if (taskmanager) taskmanager.processTaskChange(task);
19}
20
21// Cached list of enabled columns to prevent frequent access to localStorage.
22var cachedEnabledColumns;
23
24/**
25 * @return {Dictionary} the dictionary which contains the list of columns and
26 * whether each column is enabled or not.
27 */
28function getEnabledColumns() {
29  // Use the cache after the second time since access to localStorage is slow.
30  if (cachedEnabledColumns)
31    return cachedEnabledColumns;
32
33  var json = window.localStorage.getItem(ENABLED_COLUMNS_KEY);
34  try {
35    cachedEnabledColumns = JSON.parse(json) || {};
36  } catch (e) {
37    cachedEnabledColumns = {};
38  }
39  for (var i = 0; i < DEFAULT_COLUMNS.length; i++) {
40    if (typeof(cachedEnabledColumns[DEFAULT_COLUMNS[i][0]]) == 'undefined')
41      cachedEnabledColumns[DEFAULT_COLUMNS[i][0]] = DEFAULT_COLUMNS[i][3];
42  }
43  return cachedEnabledColumns;
44}
45
46/**
47 * @return {boolean} the given column (at |columnId|) is enabled or not.
48 * @param {string} columnId The ID of the collumn to be checked.
49 */
50function isColumnEnabled(columnId) {
51  return (getEnabledColumns())[columnId];
52}
53
54/**
55 * Sets the given column either enabled or disabled.
56 * @param {string} columnId The ID of the collumn to be set.
57 * @param {boolean} newChecked True, to set the column enable. False otherwise.
58 */
59function setColumnEnabled(columnId, newChecked) {
60  commands.setUpdateColumn(columnId, newChecked);
61  cachedEnabledColumns[columnId] = newChecked;
62  var json = JSON.stringify(cachedEnabledColumns);
63  window.localStorage.setItem(ENABLED_COLUMNS_KEY, json);
64}
65
66// Enable the taskmanager model before the loading of scripts.
67(function() {
68  for (var i = 0; i < DEFAULT_COLUMNS.length; i++) {
69    var columnId = DEFAULT_COLUMNS[i][0];
70    if (isColumnEnabled(columnId))
71      commands.setUpdateColumn(columnId, true);
72  }
73  commands.enableTaskManager();
74  commands.setUpdateColumn('canInspect', true);
75  commands.setUpdateColumn('canActivate', true);
76})();
77