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

142 lines
3.6 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
};
Auth.api('domains.list', {}).then(function (els) {
console.log('domains.list els', els);
vm.domains = els;
2017-08-16 22:35:04 +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, '\\.') + '$', ''));
};
vm.createWebsite = function () {
var fd = new window.FormData();
2017-08-18 20:12:22 +00:00
var pkg = Auth.oauth3.pkg('www@daplie.com');
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-18 20:12:22 +00:00
console.log('Auth.oauth3', Auth.oauth3);
return pkg.add({ hostname: vm.currentHost, multipart: { site: vm.currentFiles[0] } }).then(function (result) {
window.alert(JSON.stringify(result));
});
2017-08-14 19:45:19 +00:00
};
//vm.getDomains();
2017-08-08 20:54:25 +00:00
}]);