summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/options2/chromeos/cellular_plan_element.js
blob: 2eaa7d34e18e5f21c5e1db8fca303043ff253c7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) 2011 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.

cr.define('options.internet', function() {
  /**
   * Creates a new network list div.
   * @param {Object=} opt_propertyBag Optional properties.
   * @constructor
   * @extends {HTMLDivElement}
   */
  var CellularPlanElement = cr.ui.define('div');

  CellularPlanElement.prototype = {
    __proto__: HTMLDivElement.prototype,

    /** @inheritDoc */
    decorate: function() {
    },

    /**
     * Loads given network list.
     * @param {Array} networks An array of network object.
     */
    load: function(plans) {
      this.textContent = '';
      if (!plans || !plans.length) {
        var noplansDiv = this.ownerDocument.createElement('div');
        noplansDiv.textContent = localStrings.getString('noPlansFound');
        this.appendChild(detailsTable);
      } else {
        for (var i = 0; i < plans.length; ++i) {
          this.appendChild(new CellularPlanItem(i, plans[i]));
        }
      }
    }
  };

  /**
   * Creates a new network item.
   * @param {Object} network The network this represents.
   * @constructor
   * @extends {HTMLDivElement}
   */
  function CellularPlanItem(idx, plan) {
    var el = cr.doc.createElement('div');
    el.data = {
      idx: idx,
      planType: plan.planType,
      name: plan.name,
      planSummary: plan.planSummary,
      dataRemaining: plan.dataRemaining,
      planExpires: plan.planExpires,
      warning: plan.warning
    };
    CellularPlanItem.decorate(el);
    return el;
  }


  /**
   * Decorates an element as a network item.
   * @param {!HTMLElement} el The element to decorate.
   */
  CellularPlanItem.decorate = function(el) {
    el.__proto__ = CellularPlanItem.prototype;
    el.decorate();
  };

  CellularPlanItem.prototype = {
    __proto__: HTMLDivElement.prototype,

    /** @inheritDoc */
    decorate: function() {
      this.className = 'cellular-plan';
      var detailsTable = this.createTable_('details-plan-table',
                                           'option-control-table');
      this.addRow_(detailsTable, 'plan-details-info',
          'option-name', 'planSummary', this.data.planSummary);
      this.addRow_(detailsTable, 'plan-details-info',
          'option-name', null, localStrings.getString('planName'),
          'option-value', 'planName', this.data.name);
      this.addRow_(detailsTable, 'plan-details-info',
        'option-name', null, localStrings.getString('dataRemaining'),
        'option-value', 'dataRemaining', this.data.dataRemaining);
      this.addRow_(detailsTable, 'plan-details-info',
        'option-name', null, localStrings.getString('planExpires'),
        'option-value', 'dataRemaining', this.data.planExpires);
      if (this.data.warning && this.data.warning != "") {
        this.addRow_(detailsTable, 'plan-details-info',
            'option-name', 'planWarning', this.data.warning);
      }
      this.appendChild(detailsTable);
      this.appendChild(this.ownerDocument.createElement('hr'));
    },

    createTable_: function(tableId, tableClass) {
      var table = this.ownerDocument.createElement('table');
      table.id = tableId;
      table.className = tableClass;
      return table;
    },

    addRow_: function(table, rowClass, col1Class, col1Id, col1Value,
            col2Class, col2Id, col2Value) {
      var row = this.ownerDocument.createElement('tr');
      if (rowClass)
        row.className = rowClass;
      var col1 = this.ownerDocument.createElement('td');
      col1.className = col1Class;
      if (col1Id)
        col1.id = col1Id;
      col1.textContent = col1Value;
      if (!col2Class)
        col1.setAttribute('colspan','2');
      row.appendChild(col1);
      if (col2Class) {
        var col2 = this.ownerDocument.createElement('td');
        col2.className = col2Class;
        if (col2Id)
          col2.id = col2Id;
        col2.textContent = col2Value;
        row.appendChild(col2);
      }
      table.appendChild(row);
    }
  };

  return {
    CellularPlanElement: CellularPlanElement
  };
});