2
0

Move mpw-js out of the homepage.

This commit is contained in:
Maarten Billemont
2014-11-02 10:27:02 -05:00
parent acf9b4aec8
commit 1c72643aaa
16 changed files with 66 additions and 4 deletions

View File

@@ -0,0 +1,47 @@
// If crypto.subtle is not supported, try crypto.webkitSubtle instead.
if (window.crypto && !window.crypto.subtle && window.crypto.webkitSubtle)
window.crypto.subtle = window.crypto.webkitSubtle;
if ((!window.crypto || !window.crypto.subtle) && window.SubtleCrypto)
window.crypto = { subtle: window.SubtleCrypto }
// If Web Crypto API is not supported we include a JS crypto library
// https://code.google.com/p/crypto-js/
if (!window.crypto || !window.crypto.subtle) {
document.write("<script src=https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha256.js><\/script>");
document.write("<script src=https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/lib-typedarrays-min.js><\/script>");
}
// If Typed Arrays are not supported we include the polyfill
// https://github.com/inexorabletash/polyfill
window.ArrayBuffer || document.write("<script src=js/typedarray-polyfill.js><\/script>");
// If TextEncoder is not supported we include the polyfill
// https://github.com/inexorabletash/text-encoding
window.TextEncoder || document.write("<script src=js/encoding-polyfill.js><\/script>");
// If Promise is not supported we include the polyfill
// https://github.com/taylorhakes/promise-polyfill
window.Promise || document.write("<script src=js/promise-polyfill.js><\/script>");
// Test for required ES6 features
// Use an eval call to avoid a hard-fail on ES5 parsers.
var ES6 = false;
var esdir = "es5/";
try {
// Use ES6 code if the ES6 class, let, destructive assignment and rest arguments are supported.
eval("class $ES6 { constructor() { let b = true; this.b = b; } } var [ES6, esdir] = ((...args) => args)(new $ES6().b, '')");
} catch (e) {
}
// If ES6 is not supported we must include traceur-runtime.js
ES6 || document.write("<script src=js/mpw-js/traceur-runtime.js><\/script>");
// If setImmediate is not implemented we include the polyfill
window.setImmediate || document.write("<script src=js/" + esdir + "setImmediate-polyfill.js><\/script>");
// Include the scrypt implementation
var SCRYPTASM_PATH = (window.location + '').replace(/[^/]*(#[^#]*)?$/, 'js/mpw-js/scrypt-asm.js');
document.write("<script src=js/mpw-js/" + esdir + "scrypt.js><\/script>");
// Include the MPW class
document.write("<script src=js/mpw-js/" + esdir + "mpw.js><\/script>");

File diff suppressed because it is too large Load Diff

105
Site/mpw-js/js/main.js Normal file
View File

@@ -0,0 +1,105 @@
var mpw;
function updateMPW() {
update('identity', 'identity');
mpw = new MPW( $('#userName')[0].value, $('#masterPassword')[0].value );
updateActive();
}
function updateActive() {
if (!mpw)
update('identity');
else
mpw.key.then(
function() {
update('site');
},
function(reason) {
update('identity', null, reason);
}
);
}
function update(active, working, error) {
// Working
if (working == 'identity') {
$('#identity').addClass('working').find('input, select').attr('disabled', 'disabled');
}
else {
$('#userName')[0].value = $('#masterPassword')[0].value = '';
$('#identity').removeClass('working').find('input, select').removeAttr('disabled');
}
if (working == 'site')
$('#site').addClass('working');
else
$('#site').removeClass('working');
// Active
if (active == 'identity') {
$('#identity').addClass('active');
$('#site').removeClass('active');
if (!working)
$('#userName').focus();
}
else {
$('#identity').removeClass('active');
$('#site').addClass('active');
$('#siteName').focus();
if (!working)
$('#siteName').focus();
}
// Error
$('#error').text(error);
}
function updateSite() {
update('site', 'site');
if (!mpw)
updateActive();
else
mpw.generatePassword( $('#siteName')[0].value, $('#siteCounter')[0].valueAsNumber, $('#siteType')[0].value )
.then( function (sitePassword) {
$('#sitePassword').val(sitePassword);
update('site');
}, function (reason) {
update('site', null, reason);
});
}
function selectText(element) {
var doc = document, range, selection;
if (doc.body.createTextRange) { //ms
range = doc.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) { //all others
selection = window.getSelection();
range = doc.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
}
$(function() {
$('#identity form').on('submit', function() {
updateMPW();
return false;
});
$('#site input, #site select').on('change input keyup', function() {
updateSite();
});
$('#logout').on('click', function() {
mpw = null;
updateActive();
});
$('#sitePassword').on('click', function() {
selectText(this);
});
updateActive();
});

1
Site/mpw-js/js/mpw-js Submodule

Submodule Site/mpw-js/js/mpw-js added at 768cfad320

View File

@@ -0,0 +1,186 @@
(function() {
var root;
if (typeof window === 'object' && window) {
root = window;
} else {
root = global;
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = root.Promise ? root.Promise : Promise;
} else if (!root.Promise) {
root.Promise = Promise;
}
// Use polyfill for setImmediate for performance gains
var asap = root.setImmediate || function(fn) { setTimeout(fn, 1); };
// Polyfill for Function.prototype.bind
function bind(fn, thisArg) {
return function() {
fn.apply(thisArg, arguments);
}
}
var isArray = Array.isArray || function(value) { return Object.prototype.toString.call(value) === "[object Array]" };
function Promise(fn) {
if (typeof this !== 'object') throw new TypeError('Promises must be constructed via new');
if (typeof fn !== 'function') throw new TypeError('not a function');
this._state = null;
this._value = null;
this._deferreds = []
doResolve(fn, bind(resolve, this), bind(reject, this))
}
function handle(deferred) {
var me = this;
if (this._state === null) {
this._deferreds.push(deferred);
return
}
asap(function() {
var cb = me._state ? deferred.onFulfilled : deferred.onRejected
if (cb === null) {
(me._state ? deferred.resolve : deferred.reject)(me._value);
return;
}
var ret;
try {
ret = cb(me._value);
}
catch (e) {
deferred.reject(e);
return;
}
deferred.resolve(ret);
})
}
function resolve(newValue) {
try { //Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure
if (newValue === this) throw new TypeError('A promise cannot be resolved with itself.');
if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) {
var then = newValue.then;
if (typeof then === 'function') {
doResolve(bind(then, newValue), bind(resolve, this), bind(reject, this));
return;
}
}
this._state = true;
this._value = newValue;
finale.call(this);
} catch (e) { reject.call(this, e); }
}
function reject(newValue) {
this._state = false;
this._value = newValue;
finale.call(this);
}
function finale() {
for (var i = 0, len = this._deferreds.length; i < len; i++) {
handle.call(this, this._deferreds[i]);
}
this._deferreds = null;
}
function Handler(onFulfilled, onRejected, resolve, reject){
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null;
this.onRejected = typeof onRejected === 'function' ? onRejected : null;
this.resolve = resolve;
this.reject = reject;
}
/**
* Take a potentially misbehaving resolver function and make sure
* onFulfilled and onRejected are only called once.
*
* Makes no guarantees about asynchrony.
*/
function doResolve(fn, onFulfilled, onRejected) {
var done = false;
try {
fn(function (value) {
if (done) return;
done = true;
onFulfilled(value);
}, function (reason) {
if (done) return;
done = true;
onRejected(reason);
})
} catch (ex) {
if (done) return;
done = true;
onRejected(ex);
}
}
Promise.prototype['catch'] = function (onRejected) {
return this.then(null, onRejected);
};
Promise.prototype.then = function(onFulfilled, onRejected) {
var me = this;
return new Promise(function(resolve, reject) {
handle.call(me, new Handler(onFulfilled, onRejected, resolve, reject));
})
};
Promise.all = function () {
var args = Array.prototype.slice.call(arguments.length === 1 && isArray(arguments[0]) ? arguments[0] : arguments);
return new Promise(function (resolve, reject) {
if (args.length === 0) return resolve([]);
var remaining = args.length;
function res(i, val) {
try {
if (val && (typeof val === 'object' || typeof val === 'function')) {
var then = val.then;
if (typeof then === 'function') {
then.call(val, function (val) { res(i, val) }, reject);
return;
}
}
args[i] = val;
if (--remaining === 0) {
resolve(args);
}
} catch (ex) {
reject(ex);
}
}
for (var i = 0; i < args.length; i++) {
res(i, args[i]);
}
});
};
Promise.resolve = function (value) {
if (value && typeof value === 'object' && value.constructor === Promise) {
return value;
}
return new Promise(function (resolve) {
resolve(value);
});
};
Promise.reject = function (value) {
return new Promise(function (resolve, reject) {
reject(value);
});
};
Promise.race = function (values) {
return new Promise(function (resolve, reject) {
for(var i = 0, len = values.length; i < len; i++) {
values[i].then(resolve, reject);
}
});
};
})();

File diff suppressed because it is too large Load Diff

9190
Site/mpw-js/js/vendor/jquery-2.1.1.js vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long