TableAjax.php.files.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // @require variables:
  2. if ('undefined' === typeof UNIQ_HASH) throw "Missing UNIQ_HASH"; // $this->_htmlID,
  3. if ('undefined' === typeof FUNCTION_FILE_LIST_UPDATE_AJAX) throw "Missing FUNCTION_FILE_LIST_UPDATE_AJAX"; // "fileListUpdateAjax{$this->_htmlID}", // fileListUpdateAjax
  4. if ('undefined' === typeof FUNCTION_FILE_LIST_UPDATE) throw "Missing FUNCTION_FILE_LIST_UPDATE"; // "fileListUpdate{$this->_htmlID}", // fileListUpdate
  5. if ('undefined' === typeof FUNCTION_CONN_TBL_LIST_UPDATE_AJAX) throw "Missing FUNCTION_CONN_TBL_LIST_UPDATE_AJAX"; // "connTblListUpdateAjax{$this->_htmlID}", // connTblListUpdateAjax
  6. if ('undefined' === typeof FUNCTION_CONN_TBL_LIST_UPDATE) throw "Missing FUNCTION_CONN_TBL_LIST_UPDATE"; // "connTblListUpdate{$this->_htmlID}", // connTblListUpdate
  7. if ('undefined' === typeof FUNCTION_FILE_LIST_ACTIONS) throw "Missing FUNCTION_FILE_LIST_ACTIONS"; // "fileListActions{$this->_htmlID}", // fileListActions
  8. if ('undefined' === typeof CONN_TABLES) throw "Missing CONN_TABLES"; // $this->getConnectedTables(),
  9. if ('undefined' === typeof NODE_ID_FILES_FRM) throw "Missing NODE_ID_FILES_FRM"; // "FILES_FRM_{$this->_htmlID}",
  10. if ('undefined' === typeof NODE_ID_FRM_UPLOAD_RESULTS) throw "Missing NODE_ID_FRM_UPLOAD_RESULTS"; // "FRM_UPLOAD_RESULTS_{$this->_htmlID}",
  11. if ('undefined' === typeof NODE_ID_FILES_TAB) throw "Missing NODE_ID_FILES_TAB"; // "FILES_TAB_{$this->_htmlID}",
  12. if ('undefined' === typeof NODE_ID_FILES_LIST) throw "Missing NODE_ID_FILES_LIST"; // "FILES_LIST_{$this->_htmlID}",
  13. if ('undefined' === typeof NODE_ID_FILES_CONN_TBLS) throw "Missing NODE_ID_FILES_CONN_TBLS"; // "FILES_CONN_TBLS_{$this->_htmlID}",
  14. if ('undefined' === typeof NODE_ID_FILES_LIST_ACTIONS) throw "Missing NODE_ID_FILES_LIST_ACTIONS"; // "FILES_LIST_ACTIONS_{$this->_htmlID}",
  15. if ('undefined' === typeof NODE_ID_FILES_MULTIPLE_UPLOAD) throw "Missing NODE_ID_FILES_MULTIPLE_UPLOAD"; // "FILES_MULTIPLE_UPLOAD_{$this->_htmlID}",
  16. if ('undefined' === typeof URL_FILE_LIST_UPDATE_AJAX) throw "Missing URL_FILE_LIST_UPDATE_AJAX";
  17. if ('undefined' === typeof URL_FILE_REMOVE_AJAX) throw "Missing URL_FILE_REMOVE_AJAX";
  18. if ('undefined' === typeof URL_CONNECTED_TABLE_LIST) throw "Missing URL_CONNECTED_TABLE_LIST";
  19. if ('undefined' === typeof URL_FILE_PERMS_REFRESH) throw "Missing URL_FILE_PERMS_REFRESH";
  20. if ('undefined' === typeof URL_FILE_UPLOAD) throw "Missing URL_FILE_UPLOAD";
  21. if ('undefined' === typeof CAN_WRITE_RECORD) throw "Missing CAN_WRITE_RECORD";
  22. if ('undefined' === typeof SHARE_POINT) throw "Missing SHARE_POINT";
  23. if ('undefined' === typeof JSON_FILES) throw "Missing JSON_FILES";
  24. function fileListUpdateAjax() { // FUNCTION_FILE_LIST_UPDATE_AJAX
  25. var postData = {};
  26. jQuery.ajax({
  27. url: URL_FILE_LIST_UPDATE_AJAX,
  28. type: 'POST',
  29. data: postData,
  30. success: function(data) {
  31. fileListUpdate(data);
  32. },
  33. error: function(jhr, textStatus, errorThrown) {
  34. console.log('request error: ', errorThrown, ' textStatus: ', textStatus);
  35. }
  36. });
  37. }
  38. function fileListUpdate(fileListJson) { // FUNCTION_FILE_LIST_UPDATE
  39. var fileListNode = jQuery('#' + NODE_ID_FILES_LIST);
  40. fileListNode.empty();
  41. fileListJson.map(function(file){
  42. var node = jQuery('<tr></tr>');
  43. var fFun = CAN_WRITE_RECORD
  44. ? jQuery('<i class=\"glyphicon glyphicon-remove\" style=\"cursor:pointer; margin-right:2px\" data-filename=\"' + file.name + '\"></i>')
  45. : null
  46. ;
  47. var fNameCell = jQuery('<td style=\"overflow: hidden;\"></td>');
  48. var fName = jQuery('<div style=\"overflow:hidden; white-space:nowrap;\" title=\"' + file.name + '\"></div>');
  49. if (fFun) fName.append(fFun);
  50. fName.append(file.name);
  51. fName.appendTo(fNameCell);
  52. node.append(fNameCell);
  53. node.append('<td style=\"overflow:hidden; white-space:nowrap; text-align:center;\">' + '<a href=\"' + file.web + '\" target=\"_blank\" class=\"glyphicon glyphicon-download-alt\"></a>' + '</td>');
  54. if (SHARE_POINT) {
  55. node.append('<td style=\"overflow:hidden; white-space:nowrap; text-align:center;\">' + '<a href=\"' + SHARE_POINT + '/' + file.name + '\" target=\"_blank\" class=\"glyphicon glyphicon-folder-open\"></a>' + '</td>');
  56. }
  57. node.append('<td style=\"overflow:hidden; white-space:nowrap; text-align:right;\">' + file.sizeStr + '</td>');
  58. node.append('<td style=\"overflow:hidden; white-space:nowrap;\">' + file.created + '</td>');
  59. node.appendTo(fileListNode);
  60. if (fFun) {
  61. jQuery(fFun).click(function(e){
  62. var n = jQuery(e.target);
  63. var fname= n.data('filename');
  64. if (!fname) {
  65. return false;
  66. }
  67. if (confirm('Czy jesteś pewien, że chcesz usunąć plik o nazwie ' + fname + '?')) {
  68. var postData = new FormData()
  69. postData.append('filename', fname)
  70. window.fetch(URL_FILE_REMOVE_AJAX, {
  71. method: 'POST',
  72. body: postData,
  73. credentials: 'same-origin',
  74. }).then(function (response) {
  75. return response.json()
  76. }).then(function (response) {
  77. p5UI__notifyAjaxCallback(response)
  78. fileListUpdateAjax()
  79. }).catch(function (e) {
  80. p5UI__notifyAjaxCallback({ type: 'error', msg: e })
  81. console.warn(e)
  82. fileListUpdateAjax()
  83. })
  84. }
  85. });
  86. }
  87. });
  88. }
  89. function connTblListUpdateAjax(connTblID) { // FUNCTION_CONN_TBL_LIST_UPDATE_AJAX
  90. var postData = {};
  91. // clear current file list
  92. jQuery('#' + NODE_ID_FILES_CONN_TBLS).find('.files-list').empty();
  93. jQuery.ajax({
  94. url: URL_CONNECTED_TABLE_LIST + '&connTblID=' + connTblID,
  95. type: 'POST',
  96. data: postData,
  97. success: function(data) {
  98. connTblListUpdate(data);
  99. },
  100. error: function(jqXHR, textStatus, errorThrown) {
  101. var txt = jqXHR.responseText || 'Error';
  102. jQuery('#' + NODE_ID_FILES_CONN_TBLS).find('.files-list').html('<tr><td colspan=\"6\"><div class=\"alert alert-danger\">' + txt + '</div></td></tr>');
  103. if (priv.options.debug) console.log('connTblListUpdateAjax error: ', errorThrown, ' textStatus: ', textStatus);
  104. }
  105. });
  106. }
  107. function connTblListUpdate(fileListJson) { // FUNCTION_CONN_TBL_LIST_UPDATE
  108. var fileListNode = jQuery('#' + NODE_ID_FILES_CONN_TBLS).find('.files-list');
  109. fileListNode.empty();
  110. fileListJson.map(function(file){
  111. var node = jQuery('<tr></tr>');
  112. var fNameCell = jQuery('<td style=\"overflow: hidden;\"></td>');
  113. var fName = jQuery('<div style=\"overflow:hidden; white-space:nowrap;\" title=\"' + file.name + '\"></div>');
  114. fName.append(file.name);
  115. fName.appendTo(fNameCell);
  116. node.append(fNameCell);
  117. node.append('<td style=\"overflow:hidden; white-space:nowrap; text-align:center;\">' + '<a href=\"' + file.web + '\" target=\"_blank\" class=\"glyphicon glyphicon-download-alt\"></a>' + '</td>');
  118. node.append('<td style=\"overflow:hidden; white-space:nowrap; text-align:right;\">' + file.sizeStr + '</td>');
  119. node.append('<td style=\"overflow:hidden; white-space:nowrap;\">' + file.created + '</td>');
  120. node.appendTo(fileListNode);
  121. });
  122. }
  123. function fileListActions() { // FUNCTION_FILE_LIST_ACTIONS
  124. var filePermsReload = jQuery('#' + NODE_ID_FILES_LIST_ACTIONS);
  125. var btnReload = jQuery('<button class="btn-link btn-sm" title="odśwież uprawnienia do plików"><span class="glyphicon glyphicon-refresh"></span>Odśwież</button>');
  126. btnReload.on('click', function(e) {
  127. //console.log('TODO: click reload perms...');
  128. function notifyAjaxCallback(data) {
  129. var notify = {};
  130. notify.type = (data && data.type)? data.type : '';
  131. notify.msg = (data && data.msg)? data.msg : '';
  132. switch (notify.type) {
  133. case 'success':
  134. if (!notify.msg) notify.msg = 'OK';
  135. break;
  136. case 'info':
  137. if (!notify.msg) notify.msg = '';
  138. break;
  139. case 'error':
  140. if (!notify.msg) notify.msg = 'Wystąpiły błędy';
  141. break;
  142. case 'warning':
  143. notify.type = 'warn';
  144. if (!notify.msg) notify.msg = 'Wystąpiły błędy';
  145. break;
  146. default:
  147. notify.msg = 'Nieznany błąd';
  148. if (data && data.errorCode) notify.msg += ' ' + data.errorCode;
  149. notify.type = '';
  150. }
  151. jQuery.notify(notify.msg, notify.type);
  152. }
  153. var reqData = {};
  154. jQuery.ajax({
  155. data: reqData,
  156. dataType: 'json',
  157. type: "GET",
  158. url: URL_FILE_PERMS_REFRESH,
  159. })
  160. .done(function(data, textStatus, jqXHR){
  161. notifyAjaxCallback(data);
  162. if (data && data.files) {
  163. fileListUpdate(data.files);
  164. }
  165. })
  166. .fail(function(jqXHR){// jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
  167. if (jqXHR.responseJSON) {
  168. notifyAjaxCallback(jqXHR.responseJSON);
  169. }
  170. else {
  171. var txt = jqXHR.responseText || 'Wystąpiły błędy';
  172. if (jqXHR.status == 404) {
  173. jQuery.notify(jqXHR.responseText, 'error');
  174. } else {
  175. jQuery.notify(jqXHR.responseText, 'warn');
  176. }
  177. }
  178. });
  179. });
  180. filePermsReload.append(btnReload);
  181. }
  182. jQuery(document).ready(function(){
  183. jQuery('#' + NODE_ID_FILES_TAB + ' a').click(function(e) {
  184. e.preventDefault();
  185. jQuery(this).tab('show');
  186. })
  187. .on('shown.bs.tab', function(e) {
  188. var fileSource = jQuery(e.target).data('toggle')
  189. , frm = jQuery('#' + NODE_ID_FILES_FRM).get(0);
  190. if (frm['M_DIST_UPLOAD_SOURCE']) {
  191. frm['M_DIST_UPLOAD_SOURCE'].value = fileSource;
  192. }
  193. })
  194. jQuery('#' + NODE_ID_FILES_FRM).ajaxForm({
  195. url: URL_FILE_UPLOAD,
  196. beforeSubmit: function(args, $form, options) {
  197. //console.log('#' + NODE_ID_FILES_FRM + '.ajaxForm->beforeSubmit()...');
  198. var argsMap = [];
  199. args.map(function(v, k) {
  200. argsMap[v.name] = v.value;
  201. });
  202. // validate
  203. if (argsMap['M_DIST_UPLOAD_SOURCE'] == 'local') {
  204. if (!argsMap['M_DIST_FILES_NAME']) {
  205. //console.log('#' + NODE_ID_FILES_FRM+'.ajaxForm->beforeSubmit() M_DIST_UPLOAD_SOURCE=local && !M_DIST_FILES_NAME');
  206. return false;
  207. }
  208. }
  209. else if (argsMap['M_DIST_UPLOAD_SOURCE'] == 'scan') {
  210. if (!argsMap['SCANS_COLUMN_ADD']) {
  211. //console.log('#' + NODE_ID_FILES_FRM+'.ajaxForm->beforeSubmit() M_DIST_UPLOAD_SOURCE=scan && !SCANS_COLUMN_ADD');
  212. return false;
  213. }
  214. }
  215. else {
  216. return false;
  217. }
  218. jQuery('#' + NODE_ID_FRM_UPLOAD_RESULTS).html('Wysyłanie...');
  219. },
  220. success: function(data) {
  221. //console.log('#' + NODE_ID_FILES_FRM+'.ajaxForm->success()...');
  222. var outJqNode = jQuery('#' + NODE_ID_FRM_UPLOAD_RESULTS);
  223. var btnClose = '<button type="button" class="close" data-dismiss="alert"><i class="glyphicon glyphicon-remove"></i></button>';
  224. if (typeof data == 'object') {
  225. if (data.type == 'error') {
  226. outJqNode.html('<div class="alert alert-danger">' + btnClose + data.msg + '</div>');
  227. } else if (data.type == 'SUCCESS') {
  228. outJqNode.html('<div class="alert alert-success">' + btnClose + data.msg + '</div>');
  229. fileListUpdateAjax();
  230. // TODO: scanFileListUpdateAjax...
  231. }
  232. } else {
  233. if (data.substr(0, 7) == 'WARNING') {
  234. data = data.substr(7);
  235. outJqNode.html('<div class="alert alert-warning">' + btnClose + data + '</div>');
  236. } else if (data.substr(0, 5) == 'error') {
  237. data = data.substr(5);
  238. outJqNode.html('<div class="alert alert-danger">' + btnClose + data + '</div>');
  239. } else if (data.substr(0, 7) == 'SUCCESS') {
  240. data = data.substr(7);
  241. outJqNode.html('<div class="alert alert-success">' + btnClose + data + '</div>');
  242. fileListUpdateAjax();
  243. // TODO: scanFileListUpdateAjax...
  244. } else if (data.substr(0, 4) == 'INFO') {
  245. data = data.substr(4);
  246. outJqNode.html('<div class="alert alert-info">' + btnClose + data + '</div>');
  247. }
  248. }
  249. }
  250. })
  251. fileListActions()
  252. var fileList = JSON_FILES
  253. fileListUpdate(fileList)
  254. var connTbls = CONN_TABLES
  255. if (connTbls) {
  256. var connTblsOut = '';
  257. for(var key in connTbls) {
  258. connTblsOut += '<button class="btn btn-sm btn-default conn-tbl-load" data-zasobid="' + key + '">' + connTbls[key] + '</button>';
  259. };
  260. if (connTblsOut) {
  261. connTblsOut = 'Pliki w powiązanych tabelach: <div class="btn-group">' + connTblsOut + '</div>';
  262. connTblsOut += '<div style="max-height:180px; overflow:auto; border-bottom:1px solid #ddd;">' +
  263. '<table class="table table-bordered table-hover">' +
  264. '<colgroup>' +
  265. '<col style="">' +
  266. '<col style="width:30px;">' +
  267. ( (SHARE_POINT) ? '<col style="width:30px;">' : '' ) +
  268. '<col style="width:80px;">' +
  269. '<col style="width:140px;">' +
  270. '</colgroup>' +
  271. '<tbody class="files-list">' +
  272. '</tbody>' +
  273. '</table>' +
  274. '</div>';
  275. var _connTblsWrap = jQuery('#' + NODE_ID_FILES_CONN_TBLS);
  276. _connTblsWrap.css('marginBottom', '26px');
  277. _connTblsWrap.html(connTblsOut);
  278. _connTblsWrap.find('.conn-tbl-load').each(function(){
  279. jQuery(this).click(function(e){
  280. var tblID = jQuery(e.target).data('zasobid');
  281. if (tblID) {
  282. connTblListUpdateAjax(tblID);
  283. }// TODO: else show error
  284. });
  285. });
  286. }
  287. }
  288. initDateTimePicker(jQuery('#' + NODE_ID_FILES_FRM));
  289. });
  290. function p5TAFiles_onDropMultiple(event, targetNode, pk, ns) {
  291. event.preventDefault()
  292. targetNode.style.backgroundColor = '#eee'
  293. var files = p5Utils__getFilesFromDropEvent(event)
  294. if (!files) {
  295. p5UI__notifyAjaxCallback({
  296. type: 'info',
  297. msg: 'brak plików',
  298. })
  299. return false
  300. }
  301. try {
  302. var initialContent = (NODE_ID_FILES_MULTIPLE_UPLOAD)
  303. ? document.getElementById(NODE_ID_FILES_MULTIPLE_UPLOAD).innerHTML
  304. : ''
  305. ;
  306. p5TA_uploadWithProgress(files, ns, pk, {
  307. onProgress: function (loadedPercent) {
  308. var intPercent = Math.floor(loadedPercent)
  309. if (NODE_ID_FILES_MULTIPLE_UPLOAD) {
  310. document.getElementById(NODE_ID_FILES_MULTIPLE_UPLOAD).innerHTML = '<div class="progress">' +
  311. '<div class="progress-bar" role="progressbar" aria-valuenow="' + intPercent + '" aria-valuemin="0" aria-valuemax="100" style="width: ' + intPercent + '%;">' +
  312. intPercent + '%' +
  313. '</div>' +
  314. '</div>'
  315. } else {
  316. p5UI__notifyAjaxCallback({ type: 'info', msg: 'wgrano ' + intPercent + '%' })
  317. }
  318. },
  319. onLoad: function (response) {
  320. if (NODE_ID_FILES_MULTIPLE_UPLOAD) {
  321. document.getElementById(NODE_ID_FILES_MULTIPLE_UPLOAD).innerHTML = initialContent
  322. }
  323. try {
  324. var respJson = JSON.parse(response)
  325. } catch (e) {
  326. p5UI__notifyAjaxCallback({ type: 'error', msg: 'Wystąpił błąd podczas wgrywania plików' })
  327. return false
  328. }
  329. if (respJson && respJson.type && respJson.msg) {
  330. p5UI__notifyAjaxCallback(respJson)
  331. }
  332. fileListUpdateAjax()
  333. },
  334. onError: function (e) {
  335. p5UI__notifyAjaxCallback({ type: 'error', msg: e })
  336. console.warn('error', e)
  337. }
  338. })
  339. } catch (e) {
  340. console.warn(e)
  341. }
  342. }
  343. function p5TAFiles_onDragOverMultiple(event, targetNode) { // 'ondragover' => "p5TAFiles_onDragOverMultiple", // "event.preventDefault(); this.style.backgroundColor='#D9EDF7'",
  344. event.preventDefault()
  345. targetNode.style.backgroundColor = '#D9EDF7'
  346. }
  347. function p5TAFiles_onDragLeaveMultiple(event, targetNode) { // 'ondragleave' => "p5TAFiles_onDragLeaveMultiple", // "event.preventDefault(); this.style.backgroundColor='#eee'",
  348. event.preventDefault()
  349. targetNode.style.backgroundColor = '#eee'
  350. }
  351. function p5TAFiles_onDragEndMultiple(event, targetNode) { // 'ondragend' => "p5TAFiles_onDragEndMultiple", // "event.preventDefault(); this.style.backgroundColor='#eee'",
  352. event.preventDefault()
  353. targetNode.style.backgroundColor = '#eee'
  354. }
  355. global[FUNCTION_FILE_LIST_UPDATE_AJAX] = fileListUpdateAjax
  356. global[FUNCTION_FILE_LIST_UPDATE] = fileListUpdate
  357. global[FUNCTION_CONN_TBL_LIST_UPDATE_AJAX] = connTblListUpdateAjax
  358. global[FUNCTION_CONN_TBL_LIST_UPDATE] = connTblListUpdate
  359. global[FUNCTION_FILE_LIST_ACTIONS] = fileListActions
  360. global['p5TAFiles_onDropMultiple'] = p5TAFiles_onDropMultiple
  361. global['p5TAFiles_onDragOverMultiple'] = p5TAFiles_onDragOverMultiple
  362. global['p5TAFiles_onDragLeaveMultiple'] = p5TAFiles_onDragLeaveMultiple
  363. global['p5TAFiles_onDragEndMultiple'] = p5TAFiles_onDragEndMultiple