walnut_launchpad.html/js/controllers/website-controller.js

459 lines
12 KiB
JavaScript
Raw Normal View History

2017-08-18 19:41:38 +00:00
//
// Angular file upload hack
//
// TODO modularize for reuse
2017-09-01 23:35:04 +00:00
function analyzeFile(file, vm) {
vm.isZip = /\.zip$/.test(file.name);
vm.unzip = vm.isZip;
vm.stripZip = vm.isZip;
return vm;
}
2017-08-18 19:41:38 +00:00
function handleFiles(ev) {
var selector = 'js-file-upload';
var $scope;
var vm;
if ('INPUT' !== ev.target.tagName || 'file' !== ev.target.type || -1 === ev.target.className.indexOf(selector)) {
return;
}
$scope = angular.element(ev.target).scope();
2017-08-18 19:41:38 +00:00
// 'vm' is the Controller As name.
vm = $scope.vm;
vm.currentFiles = ev.target.files;
2017-09-01 23:35:04 +00:00
analyzeFile(vm.currentFiles[0], vm);
$scope.$digest();
console.log('[handleFiles] vm.currentFiles:');
console.log(vm.currentFiles);
2017-08-18 19:41:38 +00:00
}
window.document.body.addEventListener('change', handleFiles);
2017-09-01 22:39:21 +00:00
app.directive('daplieFileChange', function () {
return {
restrict: 'A',
require:"ngModel",
link: function (scope, element, attrs, ngModel) {
element.bind('change', function (event) {
var files = event.target.files;
ngModel.$setViewValue(files[0]);
scope.$eval(attrs.daplieFileChange);
});
}
};
});
app.controller('websiteCtrl', [
2017-08-24 17:02:31 +00:00
'$scope', '$q', 'Auth', 'azp@oauth3.org', '$timeout'
, function ($scope, $q, Auth, Oauth3, $timeout) {
2017-08-08 06:35:11 +00:00
2017-08-15 16:56:46 +00:00
var vm = this;
2017-08-16 22:35:04 +00:00
vm.domains = [];
//vm.unzipPath = '/';
2017-09-01 23:35:04 +00:00
vm.uploadPath = '/';
2017-08-16 22:35:04 +00:00
2017-08-18 19:41:38 +00:00
Auth.api = function (apiname, opts) {
var els = [];
return $q.all(Auth.sessions.map(function (session) {
2017-08-23 18:54:50 +00:00
return Auth.get(session).then(function (oauth3) {
2017-08-18 19:41:38 +00:00
return oauth3.api(apiname, {}).then(function (collection) {
if (collection.error) {
// not all tokens support all apis
return;
}
if (!Array.isArray(collection)) {
console.error('collection is not an array');
console.error(collection);
return;
}
collection.forEach(function (el) {
els.push(el);
el.session = session.token.sub + '@' + session.token.iss;
});
2017-08-16 22:35:04 +00:00
});
});
2017-08-18 19:41:38 +00:00
})).then(function (results) {
return els;
2017-08-16 22:35:04 +00:00
});
2017-08-18 19:41:38 +00:00
};
2017-08-14 19:45:19 +00:00
vm.getDomains = function () {
2017-08-18 20:12:22 +00:00
return Auth.oauth3.api('domains.list', {}).then(function (result) {
2017-08-14 19:45:19 +00:00
vm.domains = result.registrations || result;
});
};
2017-08-16 22:35:04 +00:00
vm.setDomain = function () {
if (!vm.domains || !vm.domains.length) {
vm.domain = { domain: vm.newDomain };
return;
}
vm.domains.some(function (domain) {
if (domain.domain === vm.newDomain) {
vm.domain = domain;
return true;
}
});
if (!vm.domain) {
vm.domain = { domain: vm.newDomain };
}
if (!vm.domain.tld) {
var parts = vm.domain.domain.split('.');
vm.domain.sld = parts.shift();
vm.domain.tld = parts.join('.');
}
vm.setRecord();
2017-08-16 22:35:04 +00:00
};
2017-08-14 19:45:19 +00:00
vm.selectDomain = function (domain) {
vm.domain = domain; //vm.selectedDomain.description;
vm.newDomain = vm.domain.domain;
2017-08-18 19:41:38 +00:00
return Auth.api('dns.list', { }).then(function (records) {
2017-08-14 19:45:19 +00:00
records = records.filter(function (r) {
return /^A(AAA)?$/i.test(r.type) && ((r.sld + '.' + r.tld) === vm.domain.domain || r.zone === vm.domain.domain);
2017-08-14 19:45:19 +00:00
});
vm.records = records;
2017-08-18 19:41:38 +00:00
records.forEach(function (record) {
if (!record.sub) {
record.sub = record.host.replace('.' + record.zone, '');
if (record.host === record.zone) {
record.sub = '@';
}
}
});
console.log('[selectDomain] dns records:');
console.log(records);
2017-08-14 19:45:19 +00:00
});
};
vm.setRecord = function () {
// TODO set record based on (record.host === sub + domain.domain)
var sub = vm.newRecord;
if ('@' === sub) {
sub = '';
}
vm.record = { sub: sub, host: (sub ? sub + '.' : '') + vm.domain.domain };
vm.currentHost = vm.record.host;
console.log('[setRecord] vm.record:');
console.log(vm.record);
};
2017-08-14 19:45:19 +00:00
vm.selectRecord = function (record) {
vm.record = record;
2017-08-18 19:41:38 +00:00
vm.newRecord = record.sub;
2017-08-14 19:45:19 +00:00
vm.currentHost = record.host; // .replace(new RegExp('\\.' + vm.domain.domain.replace(/\./g, '\\.') + '$', ''));
};
2017-09-01 23:35:04 +00:00
vm._uploadFileVm = function (pkg, opts) {
return vm._uploadFile(pkg, {
domain: vm.currentHost
, tld: vm.domain.tld
, sld: vm.domain.sld
, sub: vm.domain.sub
, newFile: vm.currentFiles[0]
, uploadPath: vm.uploadPath
, progress: vm
});
};
2017-09-01 22:39:21 +00:00
vm._uploadFile = function (pkg, opts) {
2017-09-01 23:35:04 +00:00
opts.progress = opts.progress || opts;
2017-08-22 18:37:57 +00:00
return pkg.add({
2017-09-01 23:35:04 +00:00
hostname: opts.domain
, domain: opts.domain
, tld: opts.tld
, sld: opts.sld
2017-09-01 22:39:21 +00:00
//, sub: opts.record.sub
2017-09-01 23:35:04 +00:00
, multipart: { site: opts.newFile }
2017-08-25 23:48:14 +00:00
, progress: function (ev) {
// TODO must digest
2017-09-01 23:35:04 +00:00
opts.progress.uploadPercent = Math.round((ev.loaded / ev.total) * 100);
2017-08-25 23:48:14 +00:00
// TODO GiB, MiB, KiB, etc
2017-09-01 23:35:04 +00:00
opts.progress.uploadTotal = (ev.total / (1024 * 1024)).toFixed(2);
opts.progress.uploadProgress = (ev.loaded / (1024 * 1024)).toFixed(2);
2017-08-25 23:48:14 +00:00
}
2017-09-01 22:39:21 +00:00
, unzip: opts.unzip
, strip: opts.stripZip
2017-09-01 23:35:04 +00:00
, path: opts.uploadPath
2017-08-22 18:37:57 +00:00
}).then(function (result) {
2017-09-01 23:35:04 +00:00
opts.progress.uploadTotal = 0;
2017-08-22 18:37:57 +00:00
window.alert(JSON.stringify(result));
});
};
vm._isSubDomain = function (sub, domain) {
return -1 !== ('.' + sub).indexOf(('.' + domain));
2017-08-22 18:37:57 +00:00
};
2017-08-24 17:26:29 +00:00
vm.Sites = {};
vm.Sites.create = function () {
2017-08-18 20:12:22 +00:00
var pkg = Auth.oauth3.pkg('www@daplie.com');
2017-08-22 18:37:57 +00:00
var parts;
var sub;
var sld;
var tld;
2017-08-18 19:41:38 +00:00
//vm.unlock('webpreneur');
if (!vm.currentFiles || !vm.currentFiles.length) {
window.alert('No files chosen.');
return;
}
if (1 !== vm.currentFiles.length) {
window.alert('Too many files chosen.');
return;
}
if (!vm.currentHost) {
window.alert('No hostname chosen.');
return;
}
2017-08-14 19:45:19 +00:00
2017-08-22 18:37:57 +00:00
if (vm.domain) {
if (!vm.domain.tld || !vm.domain.sld) {
parts = vm.domain.domain.split('.');
sld = parts.shift();
tld = parts.join('.');
} else {
sld = vm.domain.sld;
tld = vm.domain.tld;
}
2017-08-22 18:37:57 +00:00
} else {
parts = vm.currentHost.split('.');
// TODO get list of tlds
tld = parts.pop();
sld = parts.pop();
sub = parts.join('.');
}
// already validated
if (vm.sites.some(function (r) {
return -1 !== ('.' + vm.currentHost).indexOf(('.' + r.domain));
})) {
2017-09-01 23:35:04 +00:00
vm._uploadFileVm(pkg, vm);
return;
}
2017-08-22 18:37:57 +00:00
// We're making a request to claim a domain
// (because two users could both claim a single domain)
// We're claiming it at the top level (i.e. example.com)
// but we could also claim it at the subdomain level (needs UI update)
var domainReq = { sld: sld, tld: tld, sub: undefined };
return pkg.request(domainReq).then(function (result) {
2017-08-22 18:37:57 +00:00
var sess;
var prom;
var def;
// can validate automatically
if (vm.domain.session && vm._isSubDomain(vm.currentHost, vm.domain.domain)) {
// this should always succeed
Auth.sessions.some(function (session) {
if (vm.domain.session === (session.token.sub + '@' + session.token.iss)) {
sess = session;
return session;
}
});
if (sess) {
2017-08-23 18:54:50 +00:00
prom = Auth.get(sess).then(function (oauth3) {
2017-08-22 18:37:57 +00:00
return oauth3.api('dns.set', { sld: sld, tld: tld, sub: ('' + result.data.prefix), type: 'TXT', ttl: 300, value: result.data.challenge });
});
}
}
if (!prom) {
def = $q.defer();
// must validate manually
window.alert(
"Please set a TXT record for '"
+ ('' + result.data.prefix) + '.' + sld + '.' + tld
+ "' with the value '" + result.data.challenge + "' and then continue."
);
def.resolve();
prom = def.promise;
}
return prom.then(function () {
return pkg.claim(domainReq).then(function (result) {
2017-09-01 23:35:04 +00:00
return vm._uploadFileVm(pkg, vm);
});
2017-08-22 18:37:57 +00:00
});
});
};
2017-09-01 22:39:21 +00:00
vm.Sites.setUpload = function (r) {
console.log("Hey! At least it can tell if there's a change!");
2017-09-01 23:35:04 +00:00
analyzeFile(r.newFile, r);
2017-09-01 22:39:21 +00:00
console.log(r);
};
vm.Sites.upload = function (r) {
var pkg = Auth.oauth3.pkg('www@daplie.com');
2017-09-01 23:35:04 +00:00
//analyzeFile(r.newFile, r);
2017-09-01 22:39:21 +00:00
vm._uploadFile(pkg, r);
};
2017-08-24 17:26:29 +00:00
vm.Sites.archive = function (r) {
var pkg = Auth.oauth3.pkg('www@daplie.com');
return pkg.archive({
hostname: r.domain
, domain: r.domain
, tld: r.tld
, sld: r.sld
//, sub: vm.record.sub
2017-09-01 23:35:04 +00:00
//, path: vm.uploadPath
2017-08-24 17:26:29 +00:00
}).then(function (result) {
window.alert(JSON.stringify(result));
// TODO use iframe to initiate download?
window.open(result.data.url);
});
};
vm.Sites.remove = function (r) {
if (!window.confirm("Delete files for this site?")) {
return;
}
var pkg = Auth.oauth3.pkg('www@daplie.com');
return pkg.remove({
hostname: r.domain
, domain: r.domain
, tld: r.tld
, sld: r.sld
//, sub: vm.record.sub
2017-09-01 23:35:04 +00:00
//, path: vm.uploadPath
2017-08-24 17:26:29 +00:00
}).then(function (result) {
window.alert(JSON.stringify(result));
});
};
2017-08-31 07:42:59 +00:00
vm.Sites.contents = function (r) {
var pkg = Auth.oauth3.pkg('www@daplie.com');
return pkg.contents({
hostname: r.domain
, domain: r.domain
, tld: r.tld
, sld: r.sld
2017-09-01 20:50:33 +00:00
//, sub: r.sub
2017-08-31 07:42:59 +00:00
, path: r.newPath
}).then(function (result) {
window.alert(JSON.stringify(result));
});
};
2017-08-22 18:37:57 +00:00
2017-09-01 20:50:33 +00:00
vm.Shares = {};
vm.Shares.invite = function (r) {
var pkg = Auth.oauth3.pkg('www@daplie.com');
return pkg.invite({
2017-09-06 00:25:00 +00:00
comment: r.shareEmail
, email: r.shareEmail
2017-09-01 20:50:33 +00:00
, domain: r.domain
, tld: r.tld
, sld: r.sld
//, sub: r.sub
, path: r.sharePath
, mode: r.shareMode
}).then(function (result) {
window.alert(JSON.stringify(result));
});
};
vm.Shares.accept = function () {
var pkg = Auth.oauth3.pkg('www@daplie.com');
return pkg.acceptInvite({
token: vm.Shares.inviteToken
}).then(function (result) {
window.alert(JSON.stringify(result));
vm.listSites();
});
};
2017-09-06 00:25:00 +00:00
vm.Shares.list = function (r) {
var pkg = Auth.oauth3.pkg('www@daplie.com');
console.log('actual record:');
console.log(r);
return pkg.listShares({
domain: r.domain
, hostname: r.domain
, tld: r.tld
, sld: r.sld
//, sub: r.sub
, path: r.sharePath
}).then(function (result) {
console.log('list shares result:');
console.log(result);
r.sharedWith = result.data;
//r.usersnames = r.sharedWith.map().join(',');
});
};
2017-09-01 20:50:33 +00:00
2017-08-22 18:37:57 +00:00
vm.listSites = function () {
var sites = [];
return $q.all(Auth.sessions.map(function (session) {
2017-08-23 18:54:50 +00:00
return Auth.get(session).then(function (oauth3) {
2017-08-22 18:37:57 +00:00
var pkg = oauth3.pkg('www@daplie.com');
return pkg.list().then(function (result) {
var _sites = result.data;
2017-08-22 18:37:57 +00:00
if (Array.isArray(_sites)) {
sites = _sites.concat(sites);
2017-08-22 18:37:57 +00:00
return;
}
console.error('_sites is not an array');
console.error(_sites);
2017-08-22 18:37:57 +00:00
}, function (err) {
console.error('_sites had an error');
2017-08-22 18:37:57 +00:00
console.error(err);
});
});
})).then(function () {
console.log('[listSites] sites:');
2017-08-24 17:26:29 +00:00
console.log(sites);
2017-08-22 18:37:57 +00:00
vm.sites = sites;
2017-08-18 20:12:22 +00:00
});
2017-08-14 19:45:19 +00:00
};
//vm.getDomains();
2017-08-22 18:37:57 +00:00
Auth.api('domains.list', {}).then(function (els) {
console.log('[init] domains.list els:');
console.log(els);
2017-08-22 18:37:57 +00:00
vm.domains = els;
2017-08-23 23:56:18 +00:00
$scope.domain = vm.domains;
2017-08-22 18:37:57 +00:00
});
vm.listSites();
2017-08-23 19:26:31 +00:00
2017-08-24 17:02:31 +00:00
vm.triggerDropdown = function() {
$timeout(function() {
var el = document.querySelector('.trigger-dropdown');
angular.element(el).triggerHandler('focus');
}, 0);
};
$scope.$watch('vm.selectedDomain', function (domainSelected) {
2017-08-24 17:02:31 +00:00
if (domainSelected !== undefined) {
vm.selectDomain(vm.selectedDomain.description);
2017-08-24 17:02:31 +00:00
}
});
2017-08-23 19:26:31 +00:00
$scope.localDomainSearch = function(str, domain) {
var matches = [];
domain.forEach(function(domain) {
2017-08-23 23:07:15 +00:00
if ((domain.domain.toLowerCase().indexOf(str.toString().toLowerCase()) >= 0)) {
2017-08-23 19:26:31 +00:00
matches.push(domain);
}
});
return matches;
};
2017-08-08 20:54:25 +00:00
}]);