main.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. var isProcessing = false;
  2. function host_check_processing(){
  3. if(isProcessing){
  4. $('.machines-preloader').removeClass('hidden');
  5. $('.btn-search').addClass('btn-preloader');
  6. }else{
  7. $('.machines-preloader').addClass('hidden');
  8. }
  9. }
  10. //CHARGEMENT DE LA PAGE
  11. function init_plugin_host(){
  12. switch($.urlParam('page')){
  13. case 'plan':
  14. host_plan_search();
  15. break;
  16. case 'ping':
  17. host_application_url_search();
  18. default:
  19. break;
  20. }
  21. host_machine_search(function(){
  22. if(!$.urlParam('id')) return;
  23. var element = $('#machines > li[data-id="'+$.urlParam('id')+'"]');
  24. host_machine_edit(element.get(0));
  25. });
  26. $('#machines').sortable_table({
  27. onSort : host_machine_search
  28. });
  29. //Ajout de la fonction save sur les inputs classiques ET le dynamicForm
  30. $('.host-sheet-container').off('change').on('change', 'input, select', function(){
  31. if($(this).closest(".machine-application-container").length == 0)host_machine_save();
  32. });
  33. $('.host-sheet-container').off('blur').on('blur', 'textarea', function(){
  34. if($(this).closest(".machine-application-container").length == 0)host_machine_save();
  35. });
  36. }
  37. //Enregistrement des configurations
  38. function host_setting_save(){
  39. $.action({
  40. action: 'host_setting_save',
  41. fields: $('#host-setting-form').toJson()
  42. },function(){
  43. $.message('success','Enregistré');
  44. });
  45. }
  46. /** MACHINE / ENVIRONNEMENT **/
  47. //Récuperation d'une liste machine / environnement dans le tableau #machines
  48. function host_machine_search(callback,exportMode){
  49. if(isProcessing) return;
  50. var box = new FilterBox('#filters');
  51. if(exportMode) $('.btn-export').addClass('btn-preloader');
  52. isProcessing = true;
  53. host_check_processing();
  54. $('#machines').fill({
  55. action:'host_machine_search',
  56. filters: box.filters(),
  57. sort: $('#machines').sortable_table('get'),
  58. export: !exportMode ? false : exportMode
  59. },function(response){
  60. isProcessing = false;
  61. host_check_processing();
  62. if(!exportMode) $('.results-count span').text(response.pagination.total);
  63. if(callback!=null) callback();
  64. });
  65. }
  66. function host_application_url_search(){
  67. $('#vm-ping-list').fill({
  68. action:'host_application_url_search'
  69. });
  70. }
  71. function host_application_url_ping(){
  72. $('.ping-state').html('<i class="text-muted fas fa-circle-notch fa-spin"></i>');
  73. $('[data-application]').each(function(){
  74. var li = $(this);
  75. $.action({
  76. action : 'host_application_url_ping',
  77. application : li.attr('data-application')
  78. },function(response){
  79. var ping = response.ping;
  80. var state = '<i class="text-danger far fa-times-circle"></i>';
  81. if(ping.code == 200){
  82. state = '<i class="text-success far fa-check-circle"></i>';
  83. }
  84. li.find('.ping-state').html(state);
  85. li.find('.ping-message').html('<strong>'+ping.code+':</strong> '+ping.message);
  86. });
  87. });
  88. }
  89. function host_plan_search(){
  90. $.action({
  91. action:'host_plan_search',
  92. root:$('#root').val()
  93. },function(r){
  94. var table = $('#table-plan');
  95. var tpl = $('#table-plan-template').html();
  96. for(var k in r.rows){
  97. var machine = r.rows[k];
  98. var ip = machine.ip.split('.');
  99. if(ip.length<4) continue;
  100. ip = ip[3];
  101. var cell = table.find('td[data-ip='+ip+']');
  102. cell.html(Mustache.render(tpl,machine));
  103. }
  104. });
  105. }
  106. //Ajout ou modification machine / environnement
  107. function host_machine_save(callback){
  108. var data = $('#machine-form').toJson();
  109. $('.host-preloader').removeClass('hidden');
  110. $.action(data,function(r){
  111. $('#machine-form').attr('data-id',r.id);
  112. $.urlParam('id',r.id);
  113. setTimeout(function(){
  114. $('.host-preloader').addClass('hidden');
  115. },500);
  116. if(callback) callback();
  117. });
  118. }
  119. //Récuperation ou edition machine / environnement
  120. function host_machine_edit(element){
  121. var line = $(element).closest('.item-line');
  122. $('#machines >li').removeClass('active');
  123. line.addClass('active');
  124. $.action({
  125. action: 'host_machine_edit',
  126. id: line.attr('data-id')
  127. },function(r){
  128. $('.host-sheet-container').html(r.html);
  129. $.urlParam('id',r.id);
  130. init_components('#machine-form');
  131. $('#machine-form').readonly(r.readonly).attr('data-readonly',r.readonly);
  132. });
  133. }
  134. //Suppression machine / environnement
  135. function host_machine_delete(element){
  136. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  137. var line = $(element).closest('.item-line');
  138. $.action({
  139. action: 'host_machine_delete',
  140. id: line.attr('data-id')
  141. },function(r){
  142. line.remove();
  143. $.message('info','Item supprimé');
  144. });
  145. }
  146. /** APPLICATION SUR LA MACHINE **/
  147. //Récuperation d'une liste application sur la machine dans le tableau #machineapplications
  148. function host_machine_application_search(callback,exportMode){
  149. if(!$('#machine-form').attr('data-id')) return;
  150. var box = new FilterBox('#host_machine_application-filters');
  151. if(exportMode) $('.btn-export').addClass('btn-preloader');
  152. $('#machine-applications').fill({
  153. action:'host_machine_application_search',
  154. filters: box.filters(),
  155. machine : $('#machine-form').attr('data-id'),
  156. sort: $('#machine-applications').sortable_table('get'),
  157. export: !exportMode ? false : exportMode
  158. },function(response){
  159. if(!exportMode) $('.results-count span').text(response.pagination.total);
  160. init_components('#machine-applications');
  161. if(callback!=null) callback();
  162. });
  163. }
  164. //Ajout ou modification application sur la machine
  165. function host_machine_application_save(){
  166. var data = $('#machine-application-form').toJson();
  167. $('.host-preloader').removeClass('hidden');
  168. data.machine = $('#machine-form').attr('data-id');
  169. data.label = $('#machine-form [data-id="label"]').val();
  170. $.action(data,function(r){
  171. $('#machine-application-form').attr('data-id','');
  172. host_machine_application_search();
  173. $('#machine-application-form').attr('data-id',r.id);
  174. $.urlParam('id',r.id);
  175. $('.host-preloader').addClass('hidden');
  176. });
  177. }
  178. //Récuperation ou edition application sur la machine
  179. function host_machine_application_edit(element){
  180. var editApplication = function(){
  181. var data = {
  182. action: 'host_machine_application_edit',
  183. machine : $('#machine-form').attr('data-id')
  184. };
  185. if(element){
  186. var line = $(element).closest('.item-line');
  187. $('#machine-applications li').removeClass('selected');
  188. line.addClass('selected');
  189. data.id = line.attr('data-id');
  190. }
  191. $.action(data,function(r){
  192. $('.host-application-container').html(r.html);
  193. init_components('.host-application-container');
  194. $('#machine-application-form #label').focus();
  195. });
  196. }
  197. //on s'assure que la fiche vm est enregistrée avant de créer
  198. //de nouvelles applications
  199. if(!$('#machine-form').attr('data-id')){
  200. host_machine_save(editApplication);
  201. }else{
  202. editApplication();
  203. }
  204. }
  205. //Suppression application sur la machine
  206. function host_machine_application_delete(element,event){
  207. event.stopPropagation();
  208. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  209. var line = $(element).closest('.item-line');
  210. $.action({
  211. action: 'host_machine_application_delete',
  212. id: line.attr('data-id')
  213. },function(r){
  214. line.remove();
  215. $.message('info','Item supprimé');
  216. });
  217. }
  218. /** ACCèS à L'APPLICATION **/
  219. //Récuperation d'une liste accès à l'application dans le tableau #applicationaccesss
  220. function host_application_access_search(callback,exportMode){
  221. var box = new FilterBox('#application_access_filters');
  222. if(exportMode) $('.btn-export').addClass('btn-preloader');
  223. $('#application-accesss').fill({
  224. action:'host_application_access_search',
  225. filters: box.filters(),
  226. sort: $('#application-accesss').sortable_table('get'),
  227. export: !exportMode ? false : exportMode
  228. },function(response){
  229. if(!exportMode && response.pagination) $('.plugin-host-access-list .results-count span').text(response.pagination.total);
  230. if(callback!=null) callback();
  231. });
  232. }
  233. function host_application_access_page_search(element,event){
  234. if(event)event.stopPropagation();
  235. var line = $(element).closest('.item-line');
  236. var application = line.attr('data-id');
  237. if(!application) return;
  238. $('#machine-applications li').removeClass('selected');
  239. line.addClass('selected');
  240. var data = {
  241. action: 'host_application_access_search_page',
  242. export:false,
  243. application : application
  244. };
  245. $.action(data,function(r){
  246. $('.host-application-container').html(r.html);
  247. init_components('.host-application-container');
  248. $('#machine-application-form #label').focus();
  249. });
  250. }
  251. //Ajout ou modification accès à l'application
  252. function host_application_access_save(){
  253. var data = $('#application-access-form').toJson();
  254. $.action(data,function(r){
  255. $('#application-access-form').clear().attr('data-id','');
  256. host_application_access_search();
  257. $('#application-access-form').attr('data-id',r.id);
  258. $.urlParam('id',r.id);
  259. $.message('success','Enregistré');
  260. });
  261. }
  262. //Récuperation ou edition accès à l'application
  263. function host_application_access_edit(element){
  264. var line = $(element).closest('.item-line');
  265. $.action({
  266. action: 'host_application_access_edit',
  267. id: line.attr('data-id')
  268. },function(r){
  269. $('#application-access-form').fromJson(r);
  270. init_components('#application-access-form');
  271. $('#application-access-form').attr('data-id',r.id);
  272. });
  273. }
  274. //Suppression accès à l'application
  275. function host_application_access_delete(element){
  276. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  277. var line = $(element).closest('.item-line');
  278. $.action({
  279. action: 'host_application_access_delete',
  280. id: line.attr('data-id')
  281. },function(r){
  282. line.remove();
  283. $.message('info','Item supprimé');
  284. });
  285. }