Get minimum password length from options
[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 win = null;
25 if (window.location.pathname.indexOf('options.html') != -1)
26 win = window;
27 var controller = new SkeletonKeyOptions(win);
28 });
29 })();
30
31 /**
32 * SkeletonKeyOptions is a controller for both retrieving settings and for
33 * displaying the view.
34 *
35 * @param {Window} win The window and document on wich to operate.
36 */
37 var SkeletonKeyOptions = SkeletonKeyOptions || function(win) {
38 if (win) {
39 this._storage = win.localStorage;
40 this._maxLength = win.document.getElementById('maxlength');
41 this._saveButton = win.document.getElementById('save');
42 this._saveButton.onclick = this.onSave.bind(this);
43 }
44 };
45
46 /**
47 * Local storage key constants.
48 * @priate
49 */
50 SkeletonKeyOptions.prototype._MIN_LENGTH_KEY = 'minlength';
51 SkeletonKeyOptions.prototype._MAX_LENGTH_KEY = 'maxlength';
52
53 /**
54 * Gets the minimum password length.
55 * @returns {int}
56 */
57 SkeletonKeyOptions.prototype.getMinimumPasswordLength = function() {
58 if (this._storage) {
59 var setting = this._storage.getItem(this._MIN_LENGTH_KEY);
60 if (setting)
61 return setting;
62 }
63 return 6;
64 };
65
66 /**
67 * Gets the maximum password length.
68 * @returns {int}
69 */
70 SkeletonKeyOptions.prototype.getMaximumPasswordLength = function() {
71 if (this._storage) {
72 var setting = this._storage.getItem(this._MAX_LENGTH_KEY);
73 if (setting)
74 return setting;
75 }
76 return 18;
77 };
78
79 /**
80 * Saves the options. Requires a document.
81 */
82 SkeletonKeyOptions.prototype.onSave = function() {
83 if (!this._storage)
84 return;
85
86 this._storage.setItem(this._MAX_LENGTH_KEY, this._maxLength.value);
87 };