cel.default.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* Main object */
  2. const ControlErrorLog = function (config) {
  3. config = config || {};
  4. ControlErrorLog.superclass.constructor.call(this, config);
  5. };
  6. Ext.extend(ControlErrorLog, Ext.Component, {
  7. window: {}, combo: {}, config: {},
  8. fileIndex: 0,
  9. isEmpty: function(object) {
  10. for (let key in object)
  11. if (object.hasOwnProperty(key)) return false;
  12. return true;
  13. },
  14. showLog: function() {
  15. if (controlErrorLog.isEmpty(controlErrorLog.window)) {
  16. controlErrorLog.window = new MODx.Window({
  17. id: "errorlog-window",
  18. height: 500,
  19. width: 1000,
  20. closeAction: 'hide',
  21. title: _("error_log") ? _("error_log") : 'Error log',
  22. stateful: false,
  23. buttonAlign: "left",
  24. tbar: [{
  25. xtype: 'controlerrorlog-combo-logfiles',
  26. width: 250,
  27. emptyText: '',
  28. cls: 'cel-files',
  29. id: 'controlerrorlog-logfiles',
  30. listeners: {
  31. select: {
  32. fn: function (combo, rec, index) {
  33. if (index > 0 && controlErrorLog.config.allow_copy_deletion) {
  34. Ext.getCmp('errorlog-clear-btn').setDeleteText();
  35. } else {
  36. Ext.getCmp('errorlog-clear-btn').setDefaultText();
  37. }
  38. controlErrorLog.fileIndex = index;
  39. combo.baseParams.file = combo.value;
  40. controlErrorLog.window._refresh();
  41. }, scope: this
  42. }
  43. }
  44. }, {
  45. // xtype: "button",
  46. text: '<i class="icon icon-download"></i>',
  47. id: "errorlog-download-btn",
  48. tooltip: _('errorlog_download'),
  49. //hidden: !controlErrorLog.config.tooLarge,
  50. handler: function () {
  51. location.href = controlErrorLog.config.connector_url + "?action=mgr/errorlog/download&HTTP_MODAUTH=" + MODx.siteId + "&file=" + Ext.getCmp('controlerrorlog-logfiles').getValue();
  52. },
  53. scope: this
  54. }, {
  55. xtype: 'label',
  56. id: 'errorlog-filesize-label',
  57. style: 'font-weight: bold'
  58. }],
  59. items: [
  60. {
  61. xtype: "textarea",
  62. name: "log",
  63. hideLabel: true,
  64. id: "window-errorlog-content",
  65. style: 'margin-top: 10px;',
  66. //value: controlErrorLog.config.log,
  67. readOnly: true,
  68. height: "94%",
  69. width: "99%",
  70. hidden: !controlErrorLog.config.tooLarge
  71. }, {
  72. html: _("errorlog_too_large") ? _("errorlog_too_large") : 'The error log is too large to be viewed. You can download it via the button below.',
  73. id: "too-large-text",
  74. style: "margin-top: 10px;",
  75. border: false,
  76. hidden: !controlErrorLog.config.tooLarge
  77. }, {
  78. xtype: 'label',
  79. text: _("errorlog_last_lines") ? _("errorlog_last_lines", {last: controlErrorLog.config.last}) : 'The last '+controlErrorLog.config.last+' lines.',
  80. id: "errorlog-last-lines",
  81. style: "display:block;margin-top: 15px;",
  82. border: false,
  83. hidden: !(controlErrorLog.config.tooLarge && controlErrorLog.config.last > 0)
  84. }, {
  85. xtype: "textarea",
  86. name: "log-last",
  87. hideLabel: true,
  88. id: "errorlog-last-lines-content",
  89. value: controlErrorLog.config.log,
  90. readOnly: true,
  91. width: "99%",
  92. hidden: !(controlErrorLog.config.tooLarge && controlErrorLog.config.last > 0)
  93. }],
  94. buttons: [{
  95. text: '<i class="icon icon-copy"></i> ' + (_("cel_copy") ? _("cel_copy") : 'Make a copy'),
  96. id: "errorlog-copy-btn",
  97. handler: function () {
  98. controlErrorLog.window._copy();
  99. },
  100. scope: this
  101. }, '->' ,{
  102. text: '<i class="icon icon-refresh"></i> ' + (_("cel_refresh") ? _("cel_refresh") : 'Refresh'),
  103. id: "errorlog-refresh-btn",
  104. handler: function () {
  105. controlErrorLog.window._refresh();
  106. },
  107. scope: this
  108. }, {
  109. text: '',
  110. defaultText: '<i class="icon icon-eraser"></i> ' + (_("cel_clear") ? _("cel_clear") : 'Clear'),
  111. deleteText: '<i class="icon icon-remove"></i> ' + (_("cel_delete") ? _("cel_delete") : 'Delete'),
  112. id: "errorlog-clear-btn",
  113. disabled: controlErrorLog.config.empty,
  114. setDefaultText: function() {
  115. this.setText(this.defaultText).removeClass('btn-danger');
  116. return this;
  117. },
  118. setDeleteText: function() {
  119. this.setText(this.deleteText).addClass('btn-danger');
  120. return this;
  121. },
  122. listeners: {
  123. beforerender: {
  124. fn: function (btn) {
  125. btn.setText(btn.defaultText);
  126. }, scope: this
  127. }
  128. },
  129. handler: function () {
  130. MODx.Ajax.request({
  131. url: controlErrorLog.config.connector_url,
  132. params: {
  133. action: "mgr/errorlog/clear",
  134. file: Ext.get('controlerrorlog-logfiles').getValue()
  135. },
  136. listeners: {
  137. "success": {
  138. fn: function (r) {
  139. controlErrorLog.window._reset(r.object);
  140. if (r.object.isDeleted) {
  141. const combo = Ext.getCmp('controlerrorlog-logfiles');
  142. combo.setValue('error.log');
  143. combo.store.removeAt(controlErrorLog.fileIndex);
  144. Ext.getCmp('errorlog-clear-btn').setDefaultText();
  145. controlErrorLog.window._refresh();
  146. } else {
  147. Ext.getCmp('errorlog-filesize-label').setText(r.object.size);
  148. }
  149. if (r.object.name == 'error.log') {
  150. document.getElementById("errorlog-link").className = "errorlog-empty";
  151. }
  152. }
  153. },
  154. "failure": {fn: function (r) {} }
  155. }
  156. });
  157. },
  158. scope: this
  159. }, {
  160. text: '<i class="icon icon-times"></i> ' + (_("cel_close") ? _("cel_close") : 'Close'),
  161. handler: function (w) {
  162. controlErrorLog.window.hide();
  163. },
  164. scope: this
  165. }],
  166. listeners: {
  167. show: {fn: function(w) {
  168. w._refresh();
  169. }, scope: this}
  170. },
  171. _reset: function(r) {
  172. const log = Ext.getCmp("window-errorlog-content");
  173. log.setValue(r.log);
  174. // controlErrorLog.config.log = r.log;
  175. Ext.getCmp("errorlog-clear-btn").disable();
  176. Ext.getCmp("errorlog-copy-btn").disable();
  177. if (controlErrorLog.config.tooLarge) {
  178. log.show();
  179. Ext.getCmp("too-large-text").hide();
  180. // Ext.getCmp("errorlog-download-btn").hide();
  181. Ext.getCmp("errorlog-last-lines").hide();
  182. Ext.getCmp("errorlog-last-lines-content").hide();
  183. controlErrorLog.config.tooLarge = false;
  184. }
  185. },
  186. _refresh: function() {
  187. MODx.Ajax.request({
  188. url: controlErrorLog.config.connector_url,
  189. params: {
  190. action: "mgr/errorlog/get",
  191. file: Ext.get('controlerrorlog-logfiles').getValue()
  192. },
  193. listeners: {
  194. "success": {
  195. fn: function (r) {
  196. controlErrorLog.config = r.object;
  197. //Ext.getCmp("window-errorlog-content").setValue(r.object.log);
  198. Ext.getCmp("errorlog-copy-btn").disable();
  199. if (controlErrorLog.config.empty) {
  200. Ext.getCmp("errorlog-clear-btn").disable();
  201. if (r.object.name == 'error.log') {
  202. document.getElementById("errorlog-link").className = "errorlog-empty";
  203. } else if (controlErrorLog.config.allow_copy_deletion) {
  204. Ext.getCmp("errorlog-clear-btn").enable();
  205. }
  206. } else {
  207. Ext.getCmp("errorlog-clear-btn").enable();
  208. if (r.object.name == 'error.log') {
  209. document.getElementById("errorlog-link").className = "errorlog-notempty";
  210. Ext.getCmp("errorlog-copy-btn").enable();
  211. }
  212. }
  213. Ext.getCmp('errorlog-filesize-label').setText(r.object.size);
  214. if (controlErrorLog.config.tooLarge) {
  215. const errorlog_last_lines = _("errorlog_last_lines") ? _("errorlog_last_lines", {last: controlErrorLog.config.last}) : 'The last '+controlErrorLog.config.last+' lines.';
  216. Ext.getCmp("window-errorlog-content").hide();
  217. Ext.getCmp("too-large-text").show();
  218. Ext.getCmp("errorlog-last-lines").setText(errorlog_last_lines).show();
  219. Ext.getCmp("errorlog-last-lines-content").setValue(r.object.log).show();
  220. } else {
  221. Ext.getCmp("window-errorlog-content").setValue(r.object.log).show();
  222. Ext.getCmp("too-large-text").hide();
  223. Ext.getCmp("errorlog-last-lines").hide();
  224. Ext.getCmp("errorlog-last-lines-content").hide();
  225. }
  226. delete(controlErrorLog.config.log);
  227. }
  228. }
  229. }
  230. });
  231. },
  232. _copy: function() {
  233. MODx.Ajax.request({
  234. url: controlErrorLog.config.connector_url,
  235. params: {
  236. action: "mgr/errorlog/copy"
  237. },
  238. listeners: {
  239. "success": {
  240. fn: function (r) {
  241. let file = r.object.file,
  242. Record = Ext.data.Record.create([{name: "id"}, {name: "name"}]);
  243. Ext.getCmp('controlerrorlog-logfiles').store.add(new Record({id: file, name: file}));
  244. if (r.message) {
  245. MODx.msg.alert(_('success'), r.message, Ext.emptyFn);
  246. }
  247. }
  248. },
  249. "failure": {fn: function (r) {} }
  250. }
  251. });
  252. }
  253. });
  254. }
  255. controlErrorLog.window.hidden ? controlErrorLog.window.show(Ext.EventObject.target) : controlErrorLog.window.hide();
  256. },
  257. getClass: function(empty) {
  258. return empty ? "errorlog-empty" : "errorlog-notempty";
  259. },
  260. refreshLog: function() {
  261. MODx.Ajax.request({
  262. url: controlErrorLog.config.connector_url,
  263. params: {
  264. action: "mgr/errorlog/get"
  265. },
  266. listeners: {
  267. success: {
  268. fn: function (r) {
  269. controlErrorLog.config.empty = r.object.empty;
  270. document.getElementById("errorlog-link").className = controlErrorLog.getClass(controlErrorLog.config.empty);
  271. }
  272. }
  273. }
  274. });
  275. }
  276. });
  277. // Ext.reg('control-errorlog', controlErrorLog);
  278. controlErrorLog = new ControlErrorLog();
  279. /* Combo */
  280. controlErrorLog.combo.Logfiles = function(config) {
  281. config = config || {};
  282. Ext.applyIf(config,{
  283. hideMode: 'offsets',
  284. autoScroll: true,
  285. maxHeight: 200,
  286. url: controlErrorLog.config.connector_url,
  287. baseParams: {
  288. action: 'mgr/errorlog/getfiles'
  289. },
  290. name: 'logfiles',
  291. hiddenName: 'logfiles',
  292. // editable: false,
  293. value: 'error.log'
  294. });
  295. controlErrorLog.combo.Logfiles.superclass.constructor.call(this,config);
  296. };
  297. Ext.extend(controlErrorLog.combo.Logfiles, MODx.combo.ComboBox);
  298. Ext.reg('controlerrorlog-combo-logfiles', controlErrorLog.combo.Logfiles);
  299. Ext.onReady(function() {
  300. let usermenuUl = document.getElementById("modx-user-menu"),
  301. errorlogLi = document.createElement("LI"),
  302. title = _("errors_title") ? _("errors_title") : 'Open the error log in a modal window';
  303. errorlogLi.innerHTML = "<a id=\"errorlog-link\" class=\""+controlErrorLog.getClass(controlErrorLog.config.empty)+"\" href=\"javascript:controlErrorLog.showLog()\" title=\""+ title +"\"><i" +
  304. " class=\"celicon\"></i></a>";
  305. errorlogLi.className = "errorlog-li";
  306. usermenuUl.insertBefore(errorlogLi, usermenuUl.firstChild);
  307. if (controlErrorLog.config.auto_refresh) setInterval(controlErrorLog.refreshLog, controlErrorLog.config.refresh_freq);
  308. });