settings.js revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1// Copyright 2014 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 5settings = new (function() { 6 7this.onDomReady_ = function() { 8 $('#settings-store').button({icons: {primary: 'ui-icon-disk'}}) 9 .click(this.store.bind(this)); 10}; 11 12this.reload = function() { 13 $('#settings-container').empty(); 14 15 // Query the backend-specific settings (e.g., adb path). 16 devices.getAllBackends().forEach(function(backend) { 17 webservice.ajaxRequest( 18 '/settings/' + backend, 19 this.onGetSettingsAjaxResponse_.bind( 20 this, 'backend ' + backend, backend)); 21 }, this); 22 23 // Also query the device-specific settings (e.g., symbol path). 24 devices.getAllDevices().forEach(function(device) { 25 var deviceUri = device.backend + '/' + device.id; 26 var deviceTitle = 'device ' + device.name + ' [' + device.id + ']'; 27 webservice.ajaxRequest( 28 '/settings/' + deviceUri, 29 this.onGetSettingsAjaxResponse_.bind(this, deviceTitle, deviceUri)); 30 }, this); 31}; 32 33this.store = function() { 34 var container = $('#settings-container'); 35 var targetsSettings = {}; 36 37 $('input[type="text"]', container).each(function(_, field) { 38 field = $(field); 39 var key = field.data('key'); 40 var target = field.data('target'); 41 var value = field.val(); 42 if (!(target in targetsSettings)) 43 targetsSettings[target] = {} 44 targetsSettings[target][key] = value 45 }); 46 47 for (var target in targetsSettings) { 48 var targetsSetting = targetsSettings[target]; 49 webservice.ajaxRequest('/settings/' + target, 50 this.onSetSettingsAjaxResponse_.bind(this, target), 51 this.onSetSettingsAjaxError_.bind(this), 52 targetsSetting); 53 } 54}; 55 56this.onGetSettingsAjaxResponse_ = function(title, target, data) { 57 var container = $('#settings-container'); 58 container.append($('<h2/>').text('Settings for ' + title)); 59 var table = $('<table/>'); 60 Object.keys(data).forEach(function(key) { 61 var setting = data[key]; 62 var row = $('<tr/>'); 63 var textfield = $('<input type="text"/>'); 64 textfield.data('target', target); 65 textfield.data('key', key); 66 textfield.val(setting.value); 67 row.append($('<td/>').text(setting.description)); 68 row.append($('<td/>').append(textfield)); 69 table.append(row); 70 }, this); 71 container.append(table); 72}; 73 74this.onSetSettingsAjaxResponse_ = function(target, data) { 75 var container = $('#settings-container'); 76 $('input[type="text"]', container).each(function(_, field) { 77 field = $(field); 78 if (field.data('target') == target) 79 field.addClass('saved'); 80 }); 81}; 82 83this.onSetSettingsAjaxError_ = function(httpStatus, errorMsg) { 84 rootUi.showDialog( 85 'Error ' + httpStatus + ' while storing settings: ' + errorMsg); 86}; 87 88$(document).ready(this.onDomReady_.bind(this)); 89 90})();