The extension now has its own dedicated page and embeds core.html.
[skeletonkey.git] / options.js
1 /* Copyright (c) 2012 Robert Sesek <http://robert.sesek.com>
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 */
21
22 (function main() {
23 document.addEventListener('DOMContentLoaded', function() {
24 var doc = null;
25 if (window.location.pathname.indexOf('options.html') != -1)
26 doc = document;
27 var controller = new SkeletonKeyOptions(doc, window);
28 });
29 })();
30
31 /**
32 * SkeletonKeyOptions is a controller for both retrieving settings and for
33 * displaying the view.
34 *
35 * @param {HTMLDocument} doc The document on wich to operate.
36 * @param {Window} win The window to use for localStorage.
37 */
38 var SkeletonKeyOptions = SkeletonKeyOptions || function(doc, win) {
39 if (doc) {
40 this._maxLength = doc.getElementById('maxlength');
41 this._saveButton = doc.getElementById('save');
42 this._saveButton.onclick = this.onSave.bind(this);
43 }
44 if (win) {
45 this._storage = win.localStorage;
46 }
47 if (this._maxLength) {
48 this._maxLength.value = this.getMaximumPasswordLength();
49 }
50 };
51
52 /**
53 * Local storage key constants.
54 * @priate
55 */
56 SkeletonKeyOptions.prototype._MIN_LENGTH_KEY = 'minlength';
57 SkeletonKeyOptions.prototype._MAX_LENGTH_KEY = 'maxlength';
58
59 /**
60 * Gets the minimum password length.
61 * @returns {int}
62 */
63 SkeletonKeyOptions.prototype.getMinimumPasswordLength = function() {
64 if (this._storage) {
65 var setting = this._storage.getItem(this._MIN_LENGTH_KEY);
66 if (setting)
67 return setting;
68 }
69 return 6;
70 };
71
72 /**
73 * Gets the maximum password length.
74 * @returns {int}
75 */
76 SkeletonKeyOptions.prototype.getMaximumPasswordLength = function() {
77 if (this._storage) {
78 var setting = this._storage.getItem(this._MAX_LENGTH_KEY);
79 if (setting)
80 return setting;
81 }
82 return 18;
83 };
84
85 /**
86 * Saves the options. Requires a document.
87 */
88 SkeletonKeyOptions.prototype.onSave = function() {
89 if (!this._storage)
90 return;
91
92 this._storage.setItem(this._MAX_LENGTH_KEY, this._maxLength.value);
93 };