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

275 lines
8.1 KiB
JavaScript

//
// 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;
console.log('vm.currentFiles', vm.currentFiles);
}
window.document.body.addEventListener('change', handleFiles);
app.controller('websiteCtrl', [
'$scope', '$q', 'Auth', 'azp@oauth3.org'
, function ($scope, $q, Auth, Oauth3) {
var vm = this;
vm.domains = [];
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;
});
});
});
})).then(function (results) {
return els;
});
};
vm.getDomains = function () {
return Auth.oauth3.api('domains.list', {}).then(function (result) {
vm.domains = result.registrations || result;
});
};
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 };
}
};
vm.selectDomain = function (domain) {
vm.domain = domain;
vm.newDomain = domain.domain;
return Auth.api('dns.list', { }).then(function (records) {
records = records.filter(function (r) {
return /^A(AAA)?$/i.test(r.type) && ((r.sld + '.' + r.tld) === domain || r.zone === domain.domain);
});
vm.records = records;
records.forEach(function (record) {
if (!record.sub) {
record.sub = record.host.replace('.' + record.zone, '');
if (record.host === record.zone) {
record.sub = '@';
}
}
});
console.log('dns records', records);
});
};
vm.selectRecord = function (record) {
vm.record = record;
vm.newRecord = record.sub;
vm.currentHost = record.host; // .replace(new RegExp('\\.' + vm.domain.domain.replace(/\./g, '\\.') + '$', ''));
};
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));
};
vm.createWebsite = function () {
var pkg = Auth.oauth3.pkg('www@daplie.com');
var parts;
var sub;
var sld;
var tld;
console.log('Auth.oauth3', Auth.oauth3);
//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;
}
// 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;
});
};
//vm.getDomains();
Auth.api('domains.list', {}).then(function (els) {
console.log('domains.list els', els);
vm.domains = els;
});
vm.listSites();
$scope.domain = [
{ amount: "0", createdAt: "1498687134570", domain: "asdf.daplie.me", id: "1a38260bac84e0c4f8c1edf3600d98ba", session: "mmIoFLxbobxdhfSV8HWhokUGMRy-i5TIUZ_9c5ywGhYSuRi1KHh_aVSgBixxyHPW@oauth3.org", sld: "asdf", tld: "daplie.me", updatedAt: "1498687134570" },
{ amount: "0", createdAt: "1499722035396", domain: "two.daplie.me", id: "2ea60504fa2a917a5b3736c53a564fa4", session: "mmIoFLxbobxdhfSV8HWhokUGMRy-i5TIUZ_9c5ywGhYSuRi1KHh_aVSgBixxyHPW@oauth3.org", sld: "two", tld: "daplie.me", updatedAt: "1499722035396" },
{ amount: "0", createdAt: "1502725301216", domain: "jon.lambson.daplie.me", id: "23117a47d1ef60a360b46a703afc1c67", session: "mmIoFLxbobxdhfSV8HWhokUGMRy-i5TIUZ_9c5ywGhYSuRi1KHh_aVSgBixxyHPW@oauth3.org", sld: "jon.lambson", tld: "daplie.me", updatedAt: "1502725301216" },
{ amount: "0", createdAt: "1502990579539", domain: "joncloud.daplie.me", id: "ec7e4d8643537d7602fffd61b36e1676", session: "mmIoFLxbobxdhfSV8HWhokUGMRy-i5TIUZ_9c5ywGhYSuRi1KHh_aVSgBixxyHPW@oauth3.org", sld: "joncloud", tld: "daplie.me", updatedAt: "1502990579539" }
];
$scope.localDomainSearch = function(str, domain) {
var matches = [];
domain.forEach(function(domain) {
if ((domain.domain.toLowerCase().indexOf(str.toString().toLowerCase()) >= 0)) {
matches.push(domain);
}
});
return matches;
};
}]);