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

256 lines
6.7 KiB
JavaScript
Raw Normal View History

2017-08-18 19:41:38 +00:00
//
// Angular file upload hack
//
// TODO modularize for reuse
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();
// 'vm' is the Controller As name.
vm = $scope.vm;
vm.currentFiles = ev.target.files;
2017-08-18 20:12:22 +00:00
console.log('vm.currentFiles', vm.currentFiles);
2017-08-18 19:41:38 +00:00
}
window.document.body.addEventListener('change', handleFiles);
app.controller('websiteCtrl', [
2017-08-18 19:41:38 +00:00
'$scope', '$q', 'Auth', 'azp@oauth3.org'
, function ($scope, $q, Auth, Oauth3) {
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 = [];
2017-08-18 19:41:38 +00:00
Auth.api = function (apiname, opts) {
var els = [];
return $q.all(Auth.sessions.map(function (session) {
return Auth.select(session).then(function (oauth3) {
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 };
}
};
2017-08-14 19:45:19 +00:00
vm.selectDomain = function (domain) {
vm.domain = domain;
2017-08-16 22:35:04 +00:00
vm.newDomain = 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) === domain || r.zone === domain.domain);
});
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 = '@';
}
}
});
2017-08-18 20:12:22 +00:00
console.log('dns records', records);
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-08-22 18:37:57 +00:00
vm._createWebsite = function (pkg) {
return pkg.add({
hostname: vm.currentHost
, domain: vm.currentHost
, tld: vm.domain.tld
, sld: vm.domain.sld
//, sub: vm.record.sub
, multipart: { site: vm.currentFiles[0] }
}).then(function (result) {
window.alert(JSON.stringify(result));
});
};
vm._isSubDomain = function (sub, domain) {
return -1 === ('.' + sub).indexOf(('.' + domain));
};
2017-08-14 19:45:19 +00:00
vm.createWebsite = 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;
console.log('Auth.oauth3', Auth.oauth3);
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
// already validated
if (vm.sites.some(function (r) {
return -1 !== ('.' + vm.currentHost).indexOf(('.' + r.domain));
})) {
vm._createWebsite(pkg);
}
if (vm.domain) {
parts = vm.domain.domain.split('.');
sld = parts.shift();
tld = parts.join('.');
} else {
parts = vm.currentHost.split('.');
// TODO get list of tlds
tld = parts.pop();
sld = parts.pop();
sub = parts.join('.');
}
// 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)
return pkg.request({ sld: sld, tld: tld, sub: undefined }).then(function (result) {
var sess;
var prom;
var def;
console.log('[pkg www] request domain');
console.log(result);
// 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) {
prom = Auth.select(sess).then(function (oauth3) {
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 () {
vm._createWebsite(pkg);
});
});
};
vm.listSites = function () {
var sites = [];
return $q.all(Auth.sessions.map(function (session) {
console.log('[www] select');
return Auth.select(session).then(function (oauth3) {
console.log('[www] oauth3', oauth3._resourceProviderDirectives);
var pkg = oauth3.pkg('www@daplie.com');
console.log('[www] post pkg', oauth3._resourceProviderDirectives);
return pkg.list().then(function (result) {
var sites = result.data;
if (Array.isArray(sites)) {
sites = sites.concat(sites);
return;
}
console.error('sites is not an array');
console.error(sites);
}, function (err) {
console.error('sites had an error');
console.error(err);
});
});
})).then(function () {
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('domains.list els', els);
vm.domains = els;
});
vm.listSites();
2017-08-08 20:54:25 +00:00
}]);