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

142 lines
3.6 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;
});
};
Auth.api('domains.list', {}).then(function (els) {
console.log('domains.list els', els);
vm.domains = 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 () {
var fd = new window.FormData();
var pkg = Auth.oauth3.pkg('www@daplie.com');
//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;
}
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));
});
};
//vm.getDomains();
}]);