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 /** 6 * @fileoverview Base class for dialogs that require saving preferences on 7 * confirm and resetting preference inputs on cancel. 8 */ 9 10 cr.define('options', function() { 11 /** @const */ var Page = cr.ui.pageManager.Page; 12 /** @const */ var PageManager = cr.ui.pageManager.PageManager; 13 14 /** 15 * Base class for settings dialogs. 16 * @constructor 17 * @param {string} name See Page constructor. 18 * @param {string} title See Page constructor. 19 * @param {string} pageDivName See Page constructor. 20 * @param {HTMLButtonElement} okButton The confirmation button element. 21 * @param {HTMLButtonElement} cancelButton The cancellation button element. 22 * @extends {cr.ui.pageManager.Page} 23 */ 24 function SettingsDialog(name, title, pageDivName, okButton, cancelButton) { 25 Page.call(this, name, title, pageDivName); 26 this.okButton = okButton; 27 this.cancelButton = cancelButton; 28 } 29 30 SettingsDialog.prototype = { 31 __proto__: Page.prototype, 32 33 /** @override */ 34 initializePage: function() { 35 this.okButton.onclick = this.handleConfirm.bind(this); 36 this.cancelButton.onclick = this.handleCancel.bind(this); 37 }, 38 39 /** 40 * Handles the confirm button by saving the dialog preferences. 41 */ 42 handleConfirm: function() { 43 PageManager.closeOverlay(); 44 45 var prefs = Preferences.getInstance(); 46 var els = this.pageDiv.querySelectorAll('[dialog-pref]'); 47 for (var i = 0; i < els.length; i++) { 48 if (els[i].pref) 49 prefs.commitPref(els[i].pref, els[i].metric); 50 } 51 }, 52 53 /** 54 * Handles the cancel button by closing the overlay. 55 */ 56 handleCancel: function() { 57 PageManager.closeOverlay(); 58 59 var prefs = Preferences.getInstance(); 60 var els = this.pageDiv.querySelectorAll('[dialog-pref]'); 61 for (var i = 0; i < els.length; i++) { 62 if (els[i].pref) 63 prefs.rollbackPref(els[i].pref); 64 } 65 }, 66 }; 67 68 return { 69 SettingsDialog: SettingsDialog 70 }; 71 }); 72