TableAjax.php.files.js 15 KB

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