atm-command-generate
Version:
atm-command-generate
492 lines (475 loc) • 15.2 kB
JavaScript
var main = require('../../main.js'),
$ = main.$,
popup = main.popup,
template = main.template,
ls = localStorage,
$doc = main.$doc,
loadingPopup,
$manageWrapper = $('#manage-wrapper'),
manageTpl = __inline('../../tpl/manage.html'),
requiresTpl = __inline('../../tpl/requires.html'),
errorTpl = __inline('../../tpl/error.html'),
tasksTpl = __inline('../../tpl/tasks.html'),
idsTpl = __inline('../../tpl/ids.html'),
viewsTpl = __inline('../../tpl/views.html'),
familySelect = ls.getItem('familySelect'),
versionSelect = ls.getItem('versionSelect'),
destinationSelect = ls.getItem('destinationSelect'),
manageReturnData;
function refreshManage () {
$.getJSON('/atm-gui/get-datas', {
ts: $.now()
}, function (ret) {
manageReturnData = ret;
ret.familySelect = familySelect;
ret.versionSelect = versionSelect;
ret.destinationSelect = destinationSelect;
// ret.showFamilys存放需要显示的family
ret.showFamilys = {};
if (familySelect && familySelect !== '0' && ret.datas[familySelect]) {
ret.showFamilys = {};
ret.showFamilys[familySelect] = true;
} else {
ret.showFamilys = ret.datas;
}
// 删除多余的版本
if (versionSelect && versionSelect !== '0') {
ret = deleteLowVersions(ret);
}
// ret.showDestinations存放需要现实的destination
if (destinationSelect && destinationSelect !== '0') {
ret.showDestinations = [destinationSelect];
} else {
ret.showDestinations = ret.destinations;
}
var html = template(manageTpl, ret);
$manageWrapper.html(html);
familySelect && $('#filter-family').val(familySelect);
versionSelect && $('#filter-version').val(versionSelect);
destinationSelect && $('#filter-destination').val(destinationSelect);
// tooltip
$('[data-toggle="tooltip"]').tooltip();
resetBatchStatus();
});
}
refreshManage();
$doc
// 点击刷新
.on('click', '.atm-manage-refresh', function (e) {
e.preventDefault();
refreshManage();
})
// 点击table里面的行,给行加深或取消加深
//.on('click', '#manage-table tbody tr', function () {
// var $this = $(this);
// if ($this.is('.info')) {
// $this.removeClass('info');
// } else {
// $this.siblings('tr').removeClass('info');
// $this.addClass('info');
// }
//})
// 切换显示全部/单个family
.on('change', '#filter-family', function () {
var $this = $(this),
val = $this.val();
if (val) {
familySelect = val;
ls.setItem('familySelect', val);
refreshManage();
} else {
ls.removeItem('familySelect');
}
})
// 切换显示最新/全部版本
.on('change', '#filter-version', function () {
var $this = $(this),
val = $this.val();
if (val) {
versionSelect = val;
ls.setItem('versionSelect', val);
refreshManage();
} else {
ls.removeItem('versionSelect');
}
})
// 切换显示全部/单个任务
.on('change', '#filter-destination', function () {
var $this = $(this),
val = $this.val();
if (val) {
destinationSelect = val;
ls.setItem('destinationSelect', val);
refreshManage();
} else {
ls.removeItem('destinationSelect');
}
})
// 点击查看依赖项
.on('click', '.atm-get-requires', function (e) {
e.preventDefault();
var $this = $(this),
$tr = $this.closest('tr'),
route = $tr.data('route');
loading();
$.getJSON('/atm-gui/list-depends', {
route: route,
ts: $.now()
}, function (ret) {
if (ret.status) {
createRequires($.extend(true, {
title: '[' + route + ']及依赖项相关操作',
routes: ret.data.list
}, manageReturnData));
} else {
clearLoading();
showError(ret);
}
});
})
.on('click', '.atm-get-ids', function (e) {
e.preventDefault();
var $this = $(this),
$tr = $this.closest('tr'),
route = $tr.data('route');
loading();
$.getJSON('/atm-gui/list-ids', {
route: route,
ts: $.now()
}, function (ret) {
if (ret.status) {
var html = template(idsTpl, ret.data);
popup.show({
content: html
});
} else {
clearLoading();
showError(ret);
}
});
})
.on('click', '.atm-get-views', function (e) {
e.preventDefault();
var $this = $(this),
$tr = $this.closest('tr'),
route = $tr.data('route');
loading();
$.getJSON('/atm-gui/list-views', {
route: route,
ts: $.now()
}, function (ret) {
if (ret.status) {
var html = template(viewsTpl, ret.data);
popup.show({
content: html
});
} else {
clearLoading();
showError(ret);
}
});
})
// 点击构建单个
.on('click', '.btn-build', function (e) {
e.preventDefault();
var $this = $(this),
$tr = $this.closest('tr'),
destination = $this.data('destination'),
route = $tr.data('route');
var ins = createTasks({
title: '构建[' + route + ']',
task: 'build',
destination: destination,
routes: [route]
});
popupAjax(ins);
})
.on('click', '.btn-deploy', function (e) {
e.preventDefault();
var $this = $(this),
$tr = $this.closest('tr'),
destination = $this.data('destination'),
route = $tr.data('route');
var ins = createTasks({
title: '构建+发布[' + route + ']',
task: 'build-deploy',
destination: destination,
routes: [route]
});
popupAjax(ins);
})
.on('click', '.btn-output', function (e) {
e.preventDefault();
var $this = $(this),
$tr = $this.closest('tr'),
destination = $this.data('destination'),
route = $tr.data('route');
var ins = createTasks({
title: '构建+发布+地图[' + route + ']',
task: 'build-deploy-output',
destination: destination,
routes: [route]
});
popupAjax(ins);
})
.on('click', '.btn-online', function (e) {
e.preventDefault();
var $this = $(this),
$tr = $this.closest('tr'),
destination = $this.data('destination'),
route = $tr.data('route');
var ins = createTasks({
title: '构建+发布+地图+上线[' + route + ']',
task: 'build-deploy-output-online',
destination: destination,
routes: [route]
});
popupAjax(ins);
})
.on('click', '.batch-build', function (e) {
e.preventDefault();
var $this = $(this),
destination = $this.data('destination'),
routes = getRoutes($this);
var ins = createTasks({
title: '构建',
task: 'build',
destination: destination,
routes: routes
});
popupAjax(ins);
})
.on('click', '.batch-deploy', function (e) {
e.preventDefault();
var $this = $(this),
destination = $this.data('destination'),
routes = getRoutes($this);
var ins = createTasks({
title: '发布样式',
task: 'deploy',
destination: destination,
routes: routes
});
popupAjax(ins);
})
.on('click', '.batch-output', function (e) {
e.preventDefault();
var $this = $(this),
destination = $this.data('destination'),
routes = getRoutes($this);
var ins = createTasks({
title: '产出地图',
task: 'output',
destination: destination,
routes: routes
});
popupAjax(ins);
})
.on('click', '.batch-online', function (e) {
e.preventDefault();
var $this = $(this),
destination = $this.data('destination'),
routes = getRoutes($this);
var ins = createTasks({
title: '上线json文件',
task: 'online',
destination: destination,
routes: routes
});
popupAjax(ins);
})
.on('click', '.batch-build-deploy', function (e) {
e.preventDefault();
var $this = $(this),
destination = $this.data('destination'),
routes = getRoutes($this);
var ins = createTasks({
title: '构建+发布样式',
task: 'build-deploy',
destination: destination,
routes: routes
});
popupAjax(ins);
})
.on('click', '.batch-build-deploy-output', function (e) {
e.preventDefault();
var $this = $(this),
destination = $this.data('destination'),
routes = getRoutes($this);
var ins = createTasks({
title: '构建+发布样式+产出地图',
task: 'build-deploy-output',
destination: destination,
routes: routes
});
popupAjax(ins);
})
.on('click', '.batch-build-deploy-output-online', function (e) {
e.preventDefault();
var $this = $(this),
destination = $this.data('destination'),
routes = getRoutes($this);
var ins = createTasks({
title: '构建+发布样式',
task: 'build-deploy-output-online',
destination: destination,
routes: routes
});
popupAjax(ins);
});
__inline('./checkbox.inline.js');
function getRoutes($batchBtn) {
var $table = $batchBtn.closest('table'),
routes = [];
$table.find('[data-checkbox-type="version"]:checked').each(function () {
routes.push( $(this).val() )
});
return routes;
}
function showError(ret) {
var content = template(errorTpl, ret);
popup.show({
content: content
})
}
function popupAjax(ins) {
var $wrap = $(ins.DOM.main),
$build = $wrap.find('.tobe-build'),
$deploy = $wrap.find('.tobe-deploy'),
$output = $wrap.find('.tobe-output'),
$online = $wrap.find('.tobe-online'),
$node,
$td,
route,
destination,
ajaxUrl,
needAjax = true;
if ($build.length) {
$node = $build.eq(0);
ajaxUrl = '/atm-gui/build';
} else if ($deploy.length) {
$node = $deploy.eq(0);
ajaxUrl = '/atm-gui/deploy';
} else if ($output.length) {
$node = $output.eq(0);
ajaxUrl = '/atm-gui/output';
} else if ($online.length) {
$node = $online.eq(0);
ajaxUrl = '/atm-gui/online';
} else {
needAjax = false;
}
if (needAjax) {
$td = $node.closest('td');
route = $td.data('route');
destination = $td.data('destination');
$.getJSON(ajaxUrl, {
route: route,
destination: destination,
ts: $.now()
}, function (ret) {
if (ret.status){
$td.html('<i class="fa fa-check text-success"></i>');
} else {
var $tr = $td.closest('tr');
var len = $tr.children('td').length;
$td.html('<i class="fa fa-close text-danger"></i>');
$('<tr><td class="text-danger" colspan="' + len + '"><h5>' + ret.message + '</h5><pre class="popup-pre">' + ret.suggest + '</pre></td></tr>').insertAfter($tr);
}
popupAjax(ins);
});
} else {
var $popupStatus = $wrap.find('.popup-status'),
errorLength = $wrap.find('tbody .text-danger').length,
successLength = $wrap.find('tbody .text-success').length;
if (errorLength) {
if (successLength) {
$popupStatus.replaceWith('<i class="fa fa-minus text-warning"></i>');
refreshManage();
} else {
$popupStatus.replaceWith('<i class="fa fa-close text-danger"></i>');
}
} else {
$popupStatus.replaceWith('<i class="fa fa-check text-success"></i>');
refreshManage();
}
ins.center();
}
}
function createRequires(data) {
var content = template(requiresTpl, data);
var ins = popup.show({
content: content
});
ins.DOM.main.find('table');
resetBatchStatus(ins.DOM.main.find('table'));
ins.center();
return ins;
}
function createTasks(data) {
var content = template(tasksTpl, data);
var ins = popup.show({
content: content
});
ins.center();
return ins;
}
function loading() {
loadingPopup = popup.show({
closeButton: false,
content: '<div class="atm-loading"></div>',
fixed: true,
button: ''
});
}
function clearLoading() {
loadingPopup.hide();
}
// 删除低版本数据
function deleteLowVersions (ret) {
var familys = ret.datas,
familyName,
familyValue,
moduleName,
moduleValue,
versions,
version,
maxVersion;
for (familyName in familys) {
familyValue = familys[familyName];
for (moduleName in familyValue) {
moduleValue = familyValue[moduleName];
versions = [];
for (version in moduleValue) {
versions.push(version);
}
if (versions.length > 1) {
maxVersion = getLatestVersion(versions);
for (version in moduleValue) {
if (version !== maxVersion) {
delete ret.datas[familyName][moduleName][version];
}
}
}
}
}
return ret;
}
function getLatestVersion (versions) {
var maxArr = [0, 0, 0];
versions.forEach(function (version) {
var currentArr = version.split('.');
if (currentArr[0] > maxArr[0]) {
maxArr = currentArr;
} else if (currentArr[0] === maxArr[0]) {
if (currentArr[1] > maxArr[1]) {
maxArr = currentArr;
} else if (currentArr[1] === maxArr[1]) {
if (currentArr[2] > maxArr[2]) {
maxArr = currentArr;
}
}
}
});
return maxArr.join('.');
}