354 lines
9.2 KiB
JavaScript
354 lines
9.2 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;
|
|
var file = vm.currentFiles[0];
|
|
vm.isZip = /\.zip$/.test(file.name);
|
|
vm.unzip = vm.isZip;
|
|
vm.stripZip = vm.isZip;
|
|
$scope.$digest();
|
|
console.log('[handleFiles] vm.currentFiles:');
|
|
console.log(vm.currentFiles);
|
|
}
|
|
window.document.body.addEventListener('change', handleFiles);
|
|
|
|
app.controller('websiteCtrl', [
|
|
'$scope', '$q', 'Auth', 'azp@oauth3.org', '$timeout'
|
|
, function ($scope, $q, Auth, Oauth3, $timeout) {
|
|
|
|
var vm = this;
|
|
vm.domains = [];
|
|
//vm.unzipPath = '/';
|
|
vm.webPath = '/';
|
|
|
|
Auth.api = function (apiname, opts) {
|
|
var els = [];
|
|
|
|
return $q.all(Auth.sessions.map(function (session) {
|
|
|
|
return Auth.get(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 };
|
|
}
|
|
|
|
if (!vm.domain.tld) {
|
|
var parts = vm.domain.domain.split('.');
|
|
vm.domain.sld = parts.shift();
|
|
vm.domain.tld = parts.join('.');
|
|
}
|
|
|
|
vm.setRecord();
|
|
};
|
|
|
|
vm.selectDomain = function (domain) {
|
|
vm.domain = domain; //vm.selectedDomain.description;
|
|
vm.newDomain = vm.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) === vm.domain.domain || r.zone === vm.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('[selectDomain] dns records:');
|
|
console.log(records);
|
|
});
|
|
};
|
|
|
|
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);
|
|
};
|
|
|
|
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] }
|
|
, unzip: vm.unzip
|
|
, strip: vm.stripZip
|
|
, path: vm.webPath
|
|
}).then(function (result) {
|
|
window.alert(JSON.stringify(result));
|
|
});
|
|
};
|
|
|
|
vm._isSubDomain = function (sub, domain) {
|
|
return -1 !== ('.' + sub).indexOf(('.' + domain));
|
|
};
|
|
|
|
vm.Sites = {};
|
|
vm.Sites.create = function () {
|
|
var pkg = Auth.oauth3.pkg('www@daplie.com');
|
|
var parts;
|
|
var sub;
|
|
var sld;
|
|
var tld;
|
|
|
|
//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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
} 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));
|
|
})) {
|
|
vm._createWebsite(pkg);
|
|
return;
|
|
}
|
|
|
|
// 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) {
|
|
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) {
|
|
prom = Auth.get(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 () {
|
|
return pkg.claim(domainReq).then(function (result) {
|
|
return vm._createWebsite(pkg);
|
|
});
|
|
});
|
|
|
|
});
|
|
};
|
|
vm.createWebsite = vm.Sites.create;
|
|
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
|
|
//, path: vm.webPath
|
|
}).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
|
|
//, path: vm.webPath
|
|
}).then(function (result) {
|
|
window.alert(JSON.stringify(result));
|
|
});
|
|
};
|
|
|
|
vm.listSites = function () {
|
|
var sites = [];
|
|
|
|
return $q.all(Auth.sessions.map(function (session) {
|
|
|
|
return Auth.get(session).then(function (oauth3) {
|
|
var pkg = oauth3.pkg('www@daplie.com');
|
|
|
|
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 () {
|
|
console.log('[listSites] sites:');
|
|
console.log(sites);
|
|
vm.sites = sites;
|
|
});
|
|
};
|
|
|
|
//vm.getDomains();
|
|
Auth.api('domains.list', {}).then(function (els) {
|
|
console.log('[init] domains.list els:');
|
|
console.log(els);
|
|
vm.domains = els;
|
|
$scope.domain = vm.domains;
|
|
});
|
|
vm.listSites();
|
|
|
|
vm.triggerDropdown = function() {
|
|
$timeout(function() {
|
|
var el = document.querySelector('.trigger-dropdown');
|
|
angular.element(el).triggerHandler('focus');
|
|
}, 0);
|
|
};
|
|
|
|
$scope.$watch('vm.selectedDomain', function (domainSelected) {
|
|
if (domainSelected !== undefined) {
|
|
vm.selectDomain(vm.selectedDomain.description);
|
|
}
|
|
});
|
|
|
|
$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;
|
|
};
|
|
}]);
|