1 // Copyright (c) 2013 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.
7 tvcm.require('tvcm.ui');
8 tvcm.require('tvcm.settings');
10 tvcm.exportTo('tvcm.ui', function() {
12 function createSpan(opt_dictionary) {
13 var spanEl = document.createElement('span');
15 if (opt_dictionary.className)
16 spanEl.className = opt_dictionary.className;
17 if (opt_dictionary.textContent)
18 spanEl.textContent = opt_dictionary.textContent;
19 if (opt_dictionary.parent)
20 opt_dictionary.parent.appendChild(spanEl);
21 if (opt_dictionary.bold)
22 spanEl.style.fontWeight = 'bold';
27 function createDiv(opt_dictionary) {
28 var divEl = document.createElement('div');
30 if (opt_dictionary.className)
31 divEl.className = opt_dictionary.className;
32 if (opt_dictionary.parent)
33 opt_dictionary.parent.appendChild(divEl);
38 function createScopedStyle(styleContent) {
39 var styleEl = document.createElement('style');
40 styleEl.scoped = true;
41 styleEl.innerHTML = styleContent;
45 function createSelector(
46 targetEl, targetElProperty,
47 settingsKey, defaultValue,
49 var defaultValueIndex;
50 for (var i = 0; i < items.length; i++) {
52 if (item.value == defaultValue) {
53 defaultValueIndex = i;
57 if (defaultValueIndex === undefined)
58 throw new Error('defaultValue must be in the items list');
60 var selectorEl = document.createElement('select');
61 selectorEl.addEventListener('change', onChange);
62 for (var i = 0; i < items.length; i++) {
64 var optionEl = document.createElement('option');
65 optionEl.textContent = item.label;
66 optionEl.targetPropertyValue = item.value;
67 selectorEl.appendChild(optionEl);
69 function onChange(e) {
70 var value = selectorEl.selectedOptions[0].targetPropertyValue;
71 tvcm.Settings.set(settingsKey, value);
72 targetEl[targetElProperty] = value;
74 var oldSetter = targetEl.__lookupSetter__('selectedIndex');
75 selectorEl.__defineGetter__('selectedValue', function(v) {
76 return selectorEl.children[selectorEl.selectedIndex].targetPropertyValue;
78 selectorEl.__defineSetter__('selectedValue', function(v) {
79 for (var i = 0; i < selectorEl.children.length; i++) {
80 var value = selectorEl.children[i].targetPropertyValue;
82 selectorEl.selectedIndex = i;
87 throw new Error('Not a valid value');
90 var initialValue = tvcm.Settings.get(settingsKey, defaultValue);
92 for (var i = 0; i < selectorEl.children.length; i++) {
93 if (selectorEl.children[i].targetPropertyValue == initialValue) {
95 targetEl[targetElProperty] = initialValue;
96 selectorEl.selectedIndex = i;
101 selectorEl.selectedIndex = defaultValueIndex;
102 targetEl[targetElProperty] = defaultValue;
108 var nextCheckboxId = 1;
109 function createCheckBox(targetEl, targetElProperty,
110 settingsKey, defaultValue,
112 var buttonEl = document.createElement('input');
113 buttonEl.type = 'checkbox';
115 var initialValue = tvcm.Settings.get(settingsKey, defaultValue);
116 buttonEl.checked = !!initialValue;
118 targetEl[targetElProperty] = initialValue;
120 function onChange() {
121 tvcm.Settings.set(settingsKey, buttonEl.checked);
123 targetEl[targetElProperty] = buttonEl.checked;
126 buttonEl.addEventListener('change', onChange);
128 var id = '#checkbox-' + nextCheckboxId++;
130 var spanEl = createSpan({className: 'labeled-checkbox'});
131 buttonEl.setAttribute('id', id);
133 var labelEl = document.createElement('label');
134 labelEl.textContent = label;
135 labelEl.setAttribute('for', id);
136 spanEl.appendChild(buttonEl);
137 spanEl.appendChild(labelEl);
139 spanEl.__defineSetter__('checked', function(opt_bool) {
140 buttonEl.checked = !!opt_bool;
143 spanEl.__defineGetter__('checked', function() {
144 return buttonEl.checked;
150 function isElementAttachedToDocument(el) {
152 while (cur.parentNode)
153 cur = cur.parentNode;
154 return cur == el.ownerDocument;
158 createSpan: createSpan,
159 createDiv: createDiv,
160 createScopedStyle: createScopedStyle,
161 createSelector: createSelector,
162 createCheckBox: createCheckBox,
163 isElementAttachedToDocument: isElementAttachedToDocument