Break window and document up in 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 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 };
48
49 /**
50 * Local storage key constants.
51 * @priate
52 */
53 SkeletonKeyOptions.prototype._MIN_LENGTH_KEY = 'minlength';
54 SkeletonKeyOptions.prototype._MAX_LENGTH_KEY = 'maxlength';
55
56 /**
57 * Gets the minimum password length.
58 * @returns {int}
59 */
60 SkeletonKeyOptions.prototype.getMinimumPasswordLength = function() {
61 if (this._storage) {
62 var setting = this._storage.getItem(this._MIN_LENGTH_KEY);
63 if (setting)
64 return setting;
65 }
66 return 6;
67 };
68
69 /**
70 * Gets the maximum password length.
71 * @returns {int}
72 */
73 SkeletonKeyOptions.prototype.getMaximumPasswordLength = function() {
74 if (this._storage) {
75 var setting = this._storage.getItem(this._MAX_LENGTH_KEY);
76 if (setting)
77 return setting;
78 }
79 return 18;
80 };
81
82 /**
83 * Saves the options. Requires a document.
84 */
85 SkeletonKeyOptions.prototype.onSave = function() {
86 if (!this._storage)
87 return;
88
89 this._storage.setItem(this._MAX_LENGTH_KEY, this._maxLength.value);
90 };