1// Copyright (c) 2010 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
5cr.define('options.browser_options', function() {
6  const AutocompleteList = options.AutocompleteList;
7  const InlineEditableItem = options.InlineEditableItem;
8  const InlineEditableItemList = options.InlineEditableItemList;
9
10  /**
11   * Creates a new startup page list item.
12   * @param {Object} pageInfo The page this item represents.
13   * @constructor
14   * @extends {cr.ui.ListItem}
15   */
16  function StartupPageListItem(pageInfo) {
17    var el = cr.doc.createElement('div');
18    el.pageInfo_ = pageInfo;
19    StartupPageListItem.decorate(el);
20    return el;
21  }
22
23  /**
24   * Decorates an element as a startup page list item.
25   * @param {!HTMLElement} el The element to decorate.
26   */
27  StartupPageListItem.decorate = function(el) {
28    el.__proto__ = StartupPageListItem.prototype;
29    el.decorate();
30  };
31
32  StartupPageListItem.prototype = {
33    __proto__: InlineEditableItem.prototype,
34
35    /**
36     * Input field for editing the page url.
37     * @type {HTMLElement}
38     * @private
39     */
40    urlField_: null,
41
42    /** @inheritDoc */
43    decorate: function() {
44      InlineEditableItem.prototype.decorate.call(this);
45
46      var pageInfo = this.pageInfo_;
47
48      if (pageInfo['modelIndex'] == '-1') {
49        this.isPlaceholder = true;
50        pageInfo['title'] = localStrings.getString('startupAddLabel');
51        pageInfo['url'] = '';
52      }
53
54      var titleEl = this.ownerDocument.createElement('div');
55      titleEl.className = 'title';
56      titleEl.classList.add('favicon-cell');
57      titleEl.textContent = pageInfo['title'];
58      if (!this.isPlaceholder) {
59        titleEl.style.backgroundImage = url('chrome://favicon/' +
60                                            pageInfo['url']);
61        titleEl.title = pageInfo['tooltip'];
62      }
63
64      this.contentElement.appendChild(titleEl);
65
66      var urlEl = this.createEditableTextCell(pageInfo['url']);
67      urlEl.className = 'url';
68      this.contentElement.appendChild(urlEl);
69
70      var urlField = urlEl.querySelector('input')
71      urlField.required = true;
72      this.urlField_ = urlField;
73
74      this.addEventListener('commitedit', this.onEditCommitted_);
75
76      var self = this;
77      urlField.addEventListener('focus', function(event) {
78        self.parentNode.autocompleteList.attachToInput(urlField);
79      });
80      urlField.addEventListener('blur', function(event) {
81        self.parentNode.autocompleteList.detach();
82      });
83    },
84
85    /** @inheritDoc */
86    get currentInputIsValid() {
87      return this.urlField_.validity.valid;
88    },
89
90    /** @inheritDoc */
91    get hasBeenEdited() {
92      return this.urlField_.value != this.pageInfo_['url'];
93    },
94
95    /**
96     * Called when committing an edit; updates the model.
97     * @param {Event} e The end event.
98     * @private
99     */
100    onEditCommitted_: function(e) {
101      var url = this.urlField_.value;
102      if (this.isPlaceholder)
103        chrome.send('addStartupPage', [url]);
104      else
105        chrome.send('editStartupPage', [this.pageInfo_['modelIndex'], url]);
106    },
107  };
108
109  var StartupPageList = cr.ui.define('list');
110
111  StartupPageList.prototype = {
112    __proto__: InlineEditableItemList.prototype,
113
114    /**
115     * An autocomplete suggestion list for URL editing.
116     * @type {AutocompleteList}
117     */
118    autocompleteList: null,
119
120    /** @inheritDoc */
121    createItem: function(pageInfo) {
122      return new StartupPageListItem(pageInfo);
123    },
124
125    /** @inheritDoc */
126    deleteItemAtIndex: function(index) {
127      chrome.send('removeStartupPages', [String(index)]);
128    },
129  };
130
131  return {
132    StartupPageList: StartupPageList
133  };
134});
135