An unfinished system to manage all your paper documentation in an easy way.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

autodoc.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* AutoDoc Javascript */
  2. var conf = {
  3. default_hash: "home",
  4. openapi: "/v1",
  5. pageinit: {
  6. home: "page_load_home()",
  7. upload: "page_load_upload()"
  8. },
  9. home: {
  10. width: 4,
  11. curpage: 0,
  12. end: 0
  13. },
  14. run: {
  15. "upload_handler": 0,
  16. "upload_status": 0
  17. }
  18. };
  19. var temp = {
  20. upload: [],
  21. documentid: null
  22. };
  23. $(document).ready(function () {
  24. change_page();
  25. upload_zone_init();
  26. $(document).scroll(function () {
  27. page_home_scroll();
  28. });
  29. $('#autodoc_home_modal_left').mouseenter(function (e) {
  30. $(e.target).fadeTo("slow", 0.8);
  31. })
  32. .mouseleave(function (e) {
  33. $(e.target).fadeTo("slow", 0.15);
  34. })
  35. .click(function (e) {
  36. home_modal_page_prev();
  37. });
  38. $('#autodoc_home_modal_right').mouseenter(function (e) {
  39. $(e.target).fadeTo("slow", 0.8);
  40. })
  41. .mouseleave(function (e) {
  42. $(e.target).fadeTo("slow", 0.15);
  43. })
  44. .click(function (e) {
  45. home_modal_page_next();
  46. });
  47. $('.autodoc_home_modal_size').click(function(e) {
  48. $('.autodoc_home_modal_size').removeClass("active");
  49. $(e.target).addClass("active");
  50. home_modal_resize($(e.target).html());
  51. });
  52. $('span.autodoc_home_modal_name').click(function(e) {
  53. $(e.target).addClass('d-none');
  54. $('input.autodoc_home_modal_name').removeClass('d-none');
  55. });
  56. $('input.autodoc_home_modal_name').change(function(e) {
  57. var docid = $('#autodoc_home_modal').data('documentid');
  58. document_update_name(docid, $(e.target).val(), docid);
  59. });
  60. $('.autodoc_tag_color').click(function(e) {
  61. $(e.target).parent().children().children().html('');
  62. $(e.target).children().html(' X');
  63. return false;
  64. })
  65. $('.autodoc_tag_text').keypress(function(e) {
  66. $('#autodoc_tag_result').html($(e.target).val());
  67. });
  68. });
  69. $(window).bind("hashchange", function () {
  70. change_page();
  71. });
  72. function change_page() {
  73. var hash = document.location.hash.substr(1) || conf.default_hash;
  74. if (!$("#autodoc_tab_" + hash).length) {
  75. hash = conf.default_hash;
  76. }
  77. /* change menu highlights and content visibility */
  78. $("#autodoc_navbar")
  79. .find("a")
  80. .each(function (id, obj) {
  81. var curhash = $(obj)
  82. .attr("href")
  83. .substr(1);
  84. if (curhash == hash) {
  85. $(obj)
  86. .parent()
  87. .addClass("active");
  88. $("#autodoc_tab_" + curhash).removeClass("d-none");
  89. } else {
  90. $(obj)
  91. .parent()
  92. .removeClass("active");
  93. $("#autodoc_tab_" + curhash).addClass("d-none");
  94. }
  95. });
  96. if (conf.pageinit[hash]) {
  97. eval(conf.pageinit[hash]);
  98. }
  99. }
  100. function isvisible(obj) {
  101. var top_of_fileent = $(obj).offset().top;
  102. var bottom_of_fileent = $(obj).offset().top + $(obj).outerHeight();
  103. var bottom_of_screen = $(window).scrollTop() + $(window).innerHeight();
  104. var top_of_screen = $(window).scrollTop();
  105. if (bottom_of_screen > top_of_fileent && top_of_screen < bottom_of_fileent) {
  106. return true;
  107. }
  108. return false;
  109. }
  110. function page_load_home() {
  111. document_load();
  112. }
  113. function page_load_upload() {
  114. upload_status();
  115. }
  116. function document_load() {
  117. if (conf.home.curpage == 0) {
  118. $("#autodoc_home_body").html("");
  119. }
  120. var tmpl = $("#autodoc_template_home");
  121. var row = $(tmpl)
  122. .find(".autodoc_template_home_row")
  123. .clone();
  124. $.getJSON(
  125. conf.openapi +
  126. "/documents?pageSize=" +
  127. conf.home.width +
  128. "&pageIndex=" +
  129. conf.home.curpage,
  130. null,
  131. function (data, textStatus, jqXHR) {
  132. // last document was loaded, stop scrolling
  133. if (data.length < conf.home.width) {
  134. conf.home.end = 1;
  135. }
  136. $.each(data, function (id, doc) {
  137. var col = $(tmpl)
  138. .find(".autodoc_template_home_col")
  139. .clone();
  140. $(col)
  141. .find(".autodoc_template_home_img")
  142. .attr(
  143. "src",
  144. conf.openapi +
  145. "/documents/" +
  146. doc.id +
  147. "/image?maxWidth=" +
  148. Math.floor(1000 / conf.home.width)
  149. );
  150. $(col).find(".autodoc_template_home_img").data("documentId", doc.id);
  151. $(col).find(".autodoc_template_home_img").on('click', function (e) {
  152. home_modal_open($(e.target).data("documentId"));
  153. });
  154. $(col)
  155. .find(".autodoc_template_home_owner")
  156. .html(doc.owner);
  157. $(col)
  158. .find(".autodoc_template_home_created")
  159. .html(doc.created);
  160. $(col)
  161. .find(".autodoc_template_home_name")
  162. .html(doc.name);
  163. $(col)
  164. .find(".autodoc_template_home_pages")
  165. .html(doc.pageId.length);
  166. $.each(doc.tags, function (id, tag) {
  167. $(col)
  168. .find(".autodoc_template_home_tag")
  169. .after(
  170. $(col)
  171. .find(".autodoc_template_home_tag")
  172. .clone()
  173. .html(tag)
  174. );
  175. });
  176. $(row).append(col);
  177. });
  178. $("#autodoc_home_body").append(row);
  179. // continue to load until out of browser.
  180. page_home_scroll();
  181. }
  182. );
  183. }
  184. function page_home_scroll() {
  185. // don't scroll if we've loaded the last document
  186. if (conf.home.end) {
  187. return;
  188. }
  189. if (isvisible($(".autodoc_template_home_col").last())) {
  190. console.log("scroll to page " + conf.home.curpage);
  191. conf.home.curpage++;
  192. document_load();
  193. }
  194. }
  195. function upload_zone_init() {
  196. $(window).on("dragover dragleave drop", function (e) { e.preventDefault() });
  197. $('.autodoc_upload_zone').on({
  198. dragenter: upload_zone_highlight,
  199. dragleave: upload_zone_normal,
  200. dragover: upload_zone_over,
  201. drop: upload_zone_drop
  202. });
  203. }
  204. function upload_zone_highlight(e) {
  205. $(e.target).addClass('autodoc_upload_zone_hover');
  206. }
  207. function upload_zone_over(e) {
  208. return true;
  209. }
  210. function upload_zone_normal(e) {
  211. $(e.target).removeClass('autodoc_upload_zone_hover');
  212. }
  213. function upload_zone_drop(e) {
  214. var ev = e.originalEvent;
  215. $.each(ev.dataTransfer.files, function (id, file) {
  216. console.log(file);
  217. var fileid = temp.upload.length;
  218. temp.upload.push({
  219. progress: 0,
  220. error: null,
  221. obj: file
  222. });
  223. upload_zone_normal(e);
  224. });
  225. if (conf.run.upload_handler == 0) {
  226. upload_handler();
  227. }
  228. if (conf.run.upload_status == 0) {
  229. upload_status();
  230. }
  231. }
  232. function upload_handler() {
  233. var stop = 1;
  234. conf.run.upload_handler = 1;
  235. console.log("upload_handler");
  236. if (temp.documentid == null) {
  237. console.log("creating documentId");
  238. $.post(conf.openapi + "/documents", {}, function (data) {
  239. temp.documentid = data.id;
  240. });
  241. stop = 0;
  242. }
  243. else {
  244. for (var id = 0; id < temp.upload.length; id++) {
  245. var file = temp.upload[id];
  246. if (file.progress != 100) {
  247. if (file.progress == 0) {
  248. if (file.obj.type != 'application/pdf' && file.obj.type != 'image/png' && file.obj.type != 'image/jpeg') {
  249. file['progress'] = 100;
  250. file['error'] = 'invalid file type';
  251. }
  252. else {
  253. var reader = new FileReader();
  254. file['progress'] = 1;
  255. $(reader).on('load', id, upload_read_event);
  256. $(reader).on('loadstart', id, upload_read_event);
  257. $(reader).on('loadend', id, upload_read_event);
  258. $(reader).on('progress', id, upload_read_event);
  259. $(reader).on('error', id, upload_read_event);
  260. $(reader).on('abort', id, upload_read_event);
  261. reader.readAsArrayBuffer(file.obj);
  262. }
  263. }
  264. stop = 0;
  265. break;
  266. }
  267. }
  268. }
  269. if (stop) {
  270. conf.run.upload_handler = 0;
  271. }
  272. else {
  273. setTimeout(function () { upload_handler(); }, 500);
  274. }
  275. }
  276. function upload_read_event(e) {
  277. if (e.type == 'progress') {
  278. if (e.lengthComputable) {
  279. var progress = Math.floor(e.originalEvent.loaded / e.originalEvent.total * 50);
  280. if (progress < 1) { progress = 1; }
  281. if (progress > 49) { progress = 49; }
  282. temp.upload[e.data]['progress'] = progress;
  283. }
  284. }
  285. else if (e.type == 'load') {
  286. temp.upload[e.data]['progress'] = 50;
  287. console.log(e.target.result);
  288. console.log("upload_id", e.data);
  289. $.ajax({
  290. type: 'POST',
  291. url: conf.openapi + "/documents/" + temp.documentid + "/data",
  292. data: e.target.result,
  293. contentType: temp.upload[e.data].obj.type,
  294. context: { id: e.data },
  295. processData: false,
  296. xhr: function () {
  297. var xhr = $.ajaxSettings.xhr();
  298. xhr.upload.addEventListener("progress", (function () {
  299. var id = e.data;
  300. return function (evt) {
  301. if (evt.lengthComputable) {
  302. var progress = Math.floor(evt.loaded / evt.total * 50);
  303. if (progress < 1) { progress = 1; }
  304. if (progress > 49) { progress = 49; }
  305. temp.upload[id].progress = 50 + progress;
  306. // console.log("upload_progress_event", evt);
  307. }
  308. }
  309. })(), false);
  310. return xhr;
  311. },
  312. success: function (data, status, xhr) {
  313. console.log("upload_success_this", this);
  314. temp.upload[this.id].progress = 100;
  315. }
  316. });
  317. }
  318. }
  319. function upload_status() {
  320. var stop = 1;
  321. conf.run.upload_status = 1;
  322. $.each(temp.upload, function (id, file) {
  323. if (!$('.autodoc_upload_status_' + id).length) {
  324. var obj = $(".autodoc_template_progress").children().clone();
  325. $(obj).addClass('autodoc_upload_status_' + id);
  326. $('.autodoc_upload_status').append(obj);
  327. stop = 0;
  328. }
  329. var obj = $('.autodoc_upload_status_' + id).children();
  330. if (file.error != null) {
  331. $(obj).html(file.obj.name + ': ' + file.error);
  332. $(obj).css('width', '100%');
  333. $(obj).addClass('bg-danger');
  334. $(obj).removeClass("progress-bar-animated");
  335. $(obj).removeClass("progress-bar-striped");
  336. }
  337. else if (file.progress == 100) {
  338. $(obj).html(file.obj.name);
  339. $(obj).css('width', '100%');
  340. $(obj).addClass('bg-success');
  341. $(obj).removeClass('progress-bar-animated');
  342. $(obj).removeClass("progress-bar-striped");
  343. }
  344. else if (file.progress < 50) {
  345. $(obj).html(file.obj.name + ': ' + 'reading file');
  346. $(obj).css('width', file.progress + '%');
  347. stop = 0;
  348. }
  349. else {
  350. $(obj).html(file.obj.name + ': ' + 'sending file');
  351. $(obj).css('width', file.progress + '%');
  352. stop = 0;
  353. }
  354. });
  355. if (stop) {
  356. conf.run.upload_status = 0;
  357. }
  358. else {
  359. setTimeout(function () { upload_status(); }, 100);
  360. }
  361. }
  362. function home_modal_page(pageNum) {
  363. var doc = $('#autodoc_home_modal').data('document');
  364. var pageTotal = doc.pageId.length;
  365. if ( pageNum < 0 ) {
  366. pageNum=pageTotal-1;
  367. }
  368. if ( pageNum > pageTotal - 1 ) {
  369. pageNum = 0;
  370. }
  371. $('.autodoc_home_modal_page').html(pageNum+1);
  372. $('#autodoc_home_modal').data('pageNum', pageNum);
  373. $('#autodoc_home_modal_img').attr('src',
  374. conf.openapi +
  375. "/pages/" +
  376. doc.pageId[pageNum] +
  377. "/image");
  378. }
  379. function home_modal_resize(size) {
  380. var newclass;
  381. $('#autodoc_home_modal').children().removeClass('modal-sm modal-lg modal-xl');
  382. switch(size) {
  383. case "S": newclass = "modal-sm"; break;
  384. case "M": break;
  385. case "L": newclass = "modal-lg"; break;
  386. case "XL": newclass = "modal-xl"; break;
  387. }
  388. if ( newclass ) {
  389. $('#autodoc_home_modal').children().addClass(newclass);
  390. }
  391. }
  392. function home_modal_page_prev() {
  393. home_modal_page(
  394. $('#autodoc_home_modal').data('pageNum') - 1);
  395. }
  396. function home_modal_page_next() {
  397. home_modal_page(
  398. $('#autodoc_home_modal').data('pageNum') + 1);
  399. }
  400. function home_modal_open(docid) {
  401. $.getJSON(conf.openapi + "/documents/" + docid, function (data) {
  402. $('#autodoc_home_modal').data('document', data);
  403. $('#autodoc_home_modal').data('documentid', docid);
  404. var pageNum;
  405. for (var i = 0; i < data.pageId.length; i++) {
  406. if (data.pageId[i] == data.primaryPage) {
  407. pageNum = i;
  408. break;
  409. }
  410. }
  411. $('span.autodoc_home_modal_name').html(data.name ? data.name : 'n/a');
  412. $('input.autodoc_home_modal_name').val(data.name ? data.name : '');
  413. $('.autodoc_home_modal_created').html(data.created);
  414. $('.autodoc_home_modal_languages').html(data.languages.join(' '));
  415. $('.autodoc_home_modal_pages').html(data.pageId.length);
  416. $('.autodoc_home_modal_owner').html(data.owner);
  417. home_modal_page(pageNum);
  418. $('#autodoc_home_modal').modal('show');
  419. });
  420. }
  421. function home_modal_close(docid) {
  422. $('#autodoc_home_modal').modal('hide');
  423. }
  424. function document_update_name(docid, name) {
  425. $.ajax({
  426. type: "PATCH",
  427. url: conf.openapi + "/documents/" + docid + "?name=" + encodeURIComponent(name),
  428. success: function(data) {
  429. $('span.autodoc_home_modal_name').removeClass('d-none');
  430. $('input.autodoc_home_modal_name').addClass('d-none');
  431. home_modal_open(docid);
  432. }
  433. });
  434. }