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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /* AutoDoc Javascript */
  2. var conf = {
  3. default_hash: "home",
  4. openapi: "/v1",
  5. pageinit: {
  6. home: "page_load_home",
  7. document: "page_load_document",
  8. upload: "page_load_upload",
  9. tags: "page_load_tags"
  10. },
  11. home: {
  12. width: 4,
  13. curpage: 0,
  14. end: 0
  15. },
  16. run: {
  17. "upload_handler": 0,
  18. "upload_status": 0
  19. }
  20. };
  21. var temp = {
  22. upload: [],
  23. documentid: null
  24. };
  25. $(document).ready(function () {
  26. // load the current page based on anchor
  27. change_page();
  28. // initialise drop zone
  29. upload_zone_init();
  30. // main page scroll event
  31. $(document).scroll(function () {
  32. page_home_scroll();
  33. });
  34. // modal left paging
  35. $('#autodoc_home_modal_left').mouseenter(function (e) {
  36. $(e.target).fadeTo("slow", 0.8);
  37. })
  38. .mouseleave(function (e) {
  39. $(e.target).fadeTo("slow", 0.15);
  40. })
  41. .click(function (e) {
  42. home_modal_page_prev();
  43. });
  44. // modal right paging
  45. $('#autodoc_home_modal_right')
  46. .mouseenter(function (e) {
  47. $(e.target).fadeTo("slow", 0.8);
  48. })
  49. .mouseleave(function (e) {
  50. $(e.target).fadeTo("slow", 0.15);
  51. })
  52. .click(function (e) {
  53. home_modal_page_next();
  54. });
  55. // modal resize buttons events
  56. $('.autodoc_home_modal_size').click(function (e) {
  57. $('.autodoc_home_modal_size').removeClass("active");
  58. $(e.target).addClass("active");
  59. home_modal_resize($(e.target).html());
  60. });
  61. // click to rename page in modal
  62. $('span.autodoc_home_modal_name').click(function (e) {
  63. $(e.target).addClass('d-none');
  64. $('input.autodoc_home_modal_name').removeClass('d-none');
  65. });
  66. // [enter] event of modal name edit
  67. $('input.autodoc_home_modal_name').change(function (e) {
  68. var docid = $('#autodoc_home_modal').data('documentid');
  69. document_update_name(docid, $(e.target).val(), docid);
  70. });
  71. // modal show typeahead
  72. $('.autodoc_home_modal_tags_show').click(function(e) {
  73. $('.autodoc_home_modal_tags_show').addClass('d-none');
  74. $('.autodoc_home_modal_tags_add').removeClass('d-none');
  75. })
  76. // tag menu select
  77. $('.autodoc_tag_list').change(function (e) {
  78. console.log(e);
  79. var tagId = $('.autodoc_tag_list').val();
  80. var tagName = $('.autodoc_tag_list').children("option:selected").html();
  81. var tagColor = $('.autodoc_tag_list').children("option:selected").attr('data');
  82. if (tagId == "") {
  83. $(".autodoc_tag_list").addClass('d-none');
  84. $(".autodoc_tag_text").removeClass('d-none');
  85. $('.autodoc_tag_text').val('');
  86. $('.autodoc_tag_color.btn-primary').click();
  87. $('.autodoc_tag_id').val('');
  88. }
  89. else {
  90. $(".autodoc_tag_list").addClass('d-none');
  91. $(".autodoc_tag_text").removeClass('d-none');
  92. $('.autodoc_tag_text').val(tagName);
  93. $('.autodoc_tag_color.btn-' + tagColor).click();
  94. $('.autodoc_tag_id').val(tagId);
  95. $('.autodoc_tag_text').keyup();
  96. }
  97. });
  98. // tags add button
  99. $('.autodoc_tag_add').click(function (e) {
  100. var tagId = $('.autodoc_tag_id').val();
  101. var tagName = $('.autodoc_tag_text').val();
  102. var tagColor = $('.autodoc_tag_color.active').attr('data').split('_').pop();
  103. var url = conf.openapi + "/tags/";
  104. var urlparam = "tagName=" + encodeURIComponent(tagName) + "&tagColor=" + encodeURIComponent(tagColor);
  105. var method = "POST";
  106. if (tagId != "") {
  107. method = "PATCH";
  108. url += tagId;
  109. }
  110. $.ajax({
  111. type: method,
  112. url: url + '?' + urlparam,
  113. success: function () {
  114. location.reload();
  115. }
  116. });
  117. });
  118. // tags delete event
  119. $('.autodoc_tag_delete').click(function (e) {
  120. var tagId = $('.autodoc_tag_id').val();
  121. if (tagId != "") {
  122. $.ajax({
  123. type: "DELETE",
  124. url: conf.openapi + "/tags/" + tagId,
  125. success: function () { location.reload(); }
  126. });
  127. }
  128. else {
  129. location.reload();
  130. }
  131. });
  132. // tags add color events
  133. $('.autodoc_tag_color').click(function (e) {
  134. var btn = e.target;
  135. while ($(btn).prop("tagName") != "BUTTON") {
  136. btn = $(btn).parent();
  137. }
  138. $(btn).parent().children().removeClass('active');
  139. $(btn).addClass('active');
  140. $('.autodoc_tag_btn_on').addClass('d-none');
  141. $('.autodoc_tag_btn_off').removeClass('d-none');
  142. $(btn).children('.autodoc_tag_btn_on').removeClass('d-none');
  143. $(btn).children('.autodoc_tag_btn_off').addClass('d-none');
  144. $(btn).parent().children().each(function (id, x) {
  145. var myclass = 'badge-' + $(x).attr('data').split('_').pop();
  146. $('#autodoc_tag_result').removeClass(myclass);
  147. });
  148. var myclass = 'badge-' + $(btn).attr('data').split('_').pop();
  149. $('#autodoc_tag_result').addClass(myclass);
  150. });
  151. // tag add text events
  152. $('.autodoc_tag_text').keyup(function (e) {
  153. $('#autodoc_tag_result').html($(e.target).val());
  154. });
  155. });
  156. // anchor change event
  157. $(window).bind("hashchange", function () {
  158. change_page();
  159. });
  160. function change_page() {
  161. var args = document.location.hash.substr(1).split('_') || conf.default_hash.split('_');
  162. var hash = args.shift();
  163. if (!$("#autodoc_tab_" + hash).length) {
  164. hash = conf.default_hash;
  165. }
  166. /* change menu highlights and content visibility */
  167. $("#autodoc_navbar")
  168. .find("a")
  169. .each(function (id, obj) {
  170. var curhash = $(obj)
  171. .attr("href")
  172. .substr(1);
  173. if (curhash == hash) {
  174. $(obj)
  175. .parent()
  176. .addClass("active");
  177. $("#autodoc_tab_" + curhash).removeClass("d-none");
  178. } else {
  179. $(obj)
  180. .parent()
  181. .removeClass("active");
  182. $("#autodoc_tab_" + curhash).addClass("d-none");
  183. }
  184. });
  185. if (conf.pageinit[hash]) {
  186. var func = conf.pageinit[hash];
  187. console.log(func);
  188. if ( window[func] ) {
  189. window[func](args);
  190. }
  191. }
  192. }
  193. function isvisible(obj) {
  194. var top_of_fileent = $(obj).offset().top;
  195. var bottom_of_fileent = $(obj).offset().top + $(obj).outerHeight();
  196. var bottom_of_screen = $(window).scrollTop() + $(window).innerHeight();
  197. var top_of_screen = $(window).scrollTop();
  198. if (bottom_of_screen > top_of_fileent && top_of_screen < bottom_of_fileent) {
  199. return true;
  200. }
  201. return false;
  202. }
  203. function page_load_home() {
  204. document_load();
  205. }
  206. function page_load_document(args) {
  207. var [docid] = args;
  208. if ( !docid ) { return false; }
  209. $('.autodoc_document_empty').addClass('d-none');
  210. $('.autodoc_document_body').removeClass('d-none');
  211. $.getJSON(
  212. conf.openapi + "/documents/" + docid,
  213. function(doc) {
  214. for(var i=0; i<doc.pageId.length; i++) {
  215. $('.autodoc_document_carousel').append('<div><img /></div>');
  216. $('.autodoc_document_carousel').children().last().children().attr('data-lazy',
  217. conf.openapi + "/pages/" + doc.pageId[i] + "/image?maxWidth=100");
  218. }
  219. $('.autodoc_document_carousel').slick({
  220. lazyLoad: 'ondemand',
  221. slidesToShow: 4,
  222. slidesToScroll: 4
  223. });
  224. }
  225. );
  226. }
  227. function page_load_upload() {
  228. upload_status();
  229. }
  230. function page_load_tags() {
  231. // update tag list
  232. $.getJSON(conf.openapi + "/tags", function (tags) {
  233. $.each(tags, function (n, tag) {
  234. $('.autodoc_tag_list').append(
  235. '<option data="' + tag.color + '" value="' + tag.id + '">' +
  236. tag.name +
  237. '</option>'
  238. );
  239. });
  240. });
  241. }
  242. function document_load() {
  243. if (conf.home.curpage == 0) {
  244. $("#autodoc_home_body").html("");
  245. }
  246. var tmpl = $("#autodoc_template_home");
  247. var row = $(tmpl)
  248. .find(".autodoc_template_home_row")
  249. .clone();
  250. $.getJSON(
  251. conf.openapi +
  252. "/documents?pageSize=" +
  253. conf.home.width +
  254. "&pageIndex=" +
  255. conf.home.curpage,
  256. null,
  257. function (data, textStatus, jqXHR) {
  258. // last document was loaded, stop scrolling
  259. if (data.length < conf.home.width) {
  260. conf.home.end = 1;
  261. }
  262. $.each(data, function (id, doc) {
  263. var col = $(tmpl)
  264. .find(".autodoc_template_home_col")
  265. .clone();
  266. $(col)
  267. .find(".autodoc_template_home_img")
  268. .attr(
  269. "src",
  270. conf.openapi +
  271. "/documents/" +
  272. doc.id +
  273. "/image?maxWidth=" +
  274. Math.floor(1000 / conf.home.width)
  275. );
  276. $(col).find(".autodoc_template_home_img").data("documentId", doc.id);
  277. $(col).find(".autodoc_template_home_img").on('click', function (e) {
  278. window.location.hash = 'document_' + $(e.target).data("documentId");
  279. e.preventDefault();
  280. // home_modal_open($(e.target).data("documentId"));
  281. });
  282. $(col)
  283. .find(".autodoc_template_home_owner")
  284. .html(doc.owner);
  285. $(col)
  286. .find(".autodoc_template_home_created")
  287. .html(doc.created);
  288. $(col)
  289. .find(".autodoc_template_home_name")
  290. .html(doc.name);
  291. $(col)
  292. .find(".autodoc_template_home_pages")
  293. .html(doc.pageId.length);
  294. $.each(doc.tags, function (id, tag) {
  295. $(col)
  296. .find(".autodoc_template_home_tag")
  297. .after(
  298. $(col)
  299. .find(".autodoc_template_home_tag")
  300. .clone()
  301. .html(tag)
  302. );
  303. });
  304. $(row).append(col);
  305. });
  306. $("#autodoc_home_body").append(row);
  307. // continue to load until out of browser.
  308. page_home_scroll();
  309. }
  310. );
  311. }
  312. function page_home_scroll() {
  313. // don't scroll if we've loaded the last document
  314. if (conf.home.end) {
  315. return;
  316. }
  317. if (isvisible($(".autodoc_template_home_col").last())) {
  318. console.log("scroll to page " + conf.home.curpage);
  319. conf.home.curpage++;
  320. document_load();
  321. }
  322. }
  323. function upload_zone_init() {
  324. $(window).on("dragover dragleave drop", function (e) { e.preventDefault() });
  325. $('.autodoc_upload_zone').on({
  326. dragenter: upload_zone_highlight,
  327. dragleave: upload_zone_normal,
  328. dragover: upload_zone_over,
  329. drop: upload_zone_drop
  330. });
  331. }
  332. function upload_zone_highlight(e) {
  333. $(e.target).addClass('autodoc_upload_zone_hover');
  334. }
  335. function upload_zone_over(e) {
  336. return true;
  337. }
  338. function upload_zone_normal(e) {
  339. $(e.target).removeClass('autodoc_upload_zone_hover');
  340. }
  341. function upload_zone_drop(e) {
  342. var ev = e.originalEvent;
  343. $.each(ev.dataTransfer.files, function (id, file) {
  344. console.log(file);
  345. var fileid = temp.upload.length;
  346. temp.upload.push({
  347. progress: 0,
  348. error: null,
  349. obj: file
  350. });
  351. upload_zone_normal(e);
  352. });
  353. if (conf.run.upload_handler == 0) {
  354. upload_handler();
  355. }
  356. if (conf.run.upload_status == 0) {
  357. upload_status();
  358. }
  359. }
  360. function upload_handler() {
  361. var stop = 1;
  362. conf.run.upload_handler = 1;
  363. console.log("upload_handler");
  364. if (temp.documentid == null) {
  365. console.log("creating documentId");
  366. $.post(conf.openapi + "/documents", {}, function (data) {
  367. temp.documentid = data.id;
  368. });
  369. stop = 0;
  370. }
  371. else {
  372. for (var id = 0; id < temp.upload.length; id++) {
  373. var file = temp.upload[id];
  374. if (file.progress != 100) {
  375. if (file.progress == 0) {
  376. if (file.obj.type != 'application/pdf' && file.obj.type != 'image/png' && file.obj.type != 'image/jpeg') {
  377. file['progress'] = 100;
  378. file['error'] = 'invalid file type';
  379. }
  380. else {
  381. var reader = new FileReader();
  382. file['progress'] = 1;
  383. $(reader).on('load', id, upload_read_event);
  384. $(reader).on('loadstart', id, upload_read_event);
  385. $(reader).on('loadend', id, upload_read_event);
  386. $(reader).on('progress', id, upload_read_event);
  387. $(reader).on('error', id, upload_read_event);
  388. $(reader).on('abort', id, upload_read_event);
  389. reader.readAsArrayBuffer(file.obj);
  390. }
  391. }
  392. stop = 0;
  393. break;
  394. }
  395. }
  396. }
  397. if (stop) {
  398. conf.run.upload_handler = 0;
  399. }
  400. else {
  401. setTimeout(function () { upload_handler(); }, 500);
  402. }
  403. }
  404. function upload_read_event(e) {
  405. if (e.type == 'progress') {
  406. if (e.lengthComputable) {
  407. var progress = Math.floor(e.originalEvent.loaded / e.originalEvent.total * 50);
  408. if (progress < 1) { progress = 1; }
  409. if (progress > 49) { progress = 49; }
  410. temp.upload[e.data]['progress'] = progress;
  411. }
  412. }
  413. else if (e.type == 'load') {
  414. temp.upload[e.data]['progress'] = 50;
  415. console.log(e.target.result);
  416. console.log("upload_id", e.data);
  417. $.ajax({
  418. type: 'POST',
  419. url: conf.openapi + "/documents/" + temp.documentid + "/data",
  420. data: e.target.result,
  421. contentType: temp.upload[e.data].obj.type,
  422. context: { id: e.data },
  423. processData: false,
  424. xhr: function () {
  425. var xhr = $.ajaxSettings.xhr();
  426. xhr.upload.addEventListener("progress", (function () {
  427. var id = e.data;
  428. return function (evt) {
  429. if (evt.lengthComputable) {
  430. var progress = Math.floor(evt.loaded / evt.total * 50);
  431. if (progress < 1) { progress = 1; }
  432. if (progress > 49) { progress = 49; }
  433. temp.upload[id].progress = 50 + progress;
  434. // console.log("upload_progress_event", evt);
  435. }
  436. }
  437. })(), false);
  438. return xhr;
  439. },
  440. success: function (data, status, xhr) {
  441. console.log("upload_success_this", this);
  442. temp.upload[this.id].progress = 100;
  443. }
  444. });
  445. }
  446. }
  447. function upload_status() {
  448. var stop = 1;
  449. conf.run.upload_status = 1;
  450. $.each(temp.upload, function (id, file) {
  451. if (!$('.autodoc_upload_status_' + id).length) {
  452. var obj = $(".autodoc_template_progress").children().clone();
  453. $(obj).addClass('autodoc_upload_status_' + id);
  454. $('.autodoc_upload_status').append(obj);
  455. stop = 0;
  456. }
  457. var obj = $('.autodoc_upload_status_' + id).children();
  458. if (file.error != null) {
  459. $(obj).html(file.obj.name + ': ' + file.error);
  460. $(obj).css('width', '100%');
  461. $(obj).addClass('bg-danger');
  462. $(obj).removeClass("progress-bar-animated");
  463. $(obj).removeClass("progress-bar-striped");
  464. }
  465. else if (file.progress == 100) {
  466. $(obj).html(file.obj.name);
  467. $(obj).css('width', '100%');
  468. $(obj).addClass('bg-success');
  469. $(obj).removeClass('progress-bar-animated');
  470. $(obj).removeClass("progress-bar-striped");
  471. }
  472. else if (file.progress < 50) {
  473. $(obj).html(file.obj.name + ': ' + 'reading file');
  474. $(obj).css('width', file.progress + '%');
  475. stop = 0;
  476. }
  477. else {
  478. $(obj).html(file.obj.name + ': ' + 'sending file');
  479. $(obj).css('width', file.progress + '%');
  480. stop = 0;
  481. }
  482. });
  483. if (stop) {
  484. conf.run.upload_status = 0;
  485. }
  486. else {
  487. setTimeout(function () { upload_status(); }, 100);
  488. }
  489. }
  490. function home_modal_page(pageNum) {
  491. var doc = $('#autodoc_home_modal').data('document');
  492. var pageTotal = doc.pageId.length;
  493. if (pageNum < 0) {
  494. pageNum = pageTotal - 1;
  495. }
  496. if (pageNum > pageTotal - 1) {
  497. pageNum = 0;
  498. }
  499. $('.autodoc_home_modal_page').html(pageNum + 1);
  500. $('#autodoc_home_modal').data('pageNum', pageNum);
  501. $('#autodoc_home_modal_img').attr('src',
  502. conf.openapi +
  503. "/pages/" +
  504. doc.pageId[pageNum] +
  505. "/image");
  506. }
  507. function home_modal_resize(size) {
  508. var newclass;
  509. $('#autodoc_home_modal').children().removeClass('modal-sm modal-lg modal-xl');
  510. switch (size) {
  511. case "S": newclass = "modal-sm"; break;
  512. case "M": break;
  513. case "L": newclass = "modal-lg"; break;
  514. case "XL": newclass = "modal-xl"; break;
  515. }
  516. if (newclass) {
  517. $('#autodoc_home_modal').children().addClass(newclass);
  518. }
  519. }
  520. function home_modal_page_prev() {
  521. home_modal_page(
  522. $('#autodoc_home_modal').data('pageNum') - 1);
  523. }
  524. function home_modal_page_next() {
  525. home_modal_page(
  526. $('#autodoc_home_modal').data('pageNum') + 1);
  527. }
  528. function home_modal_open(docid) {
  529. $.getJSON(conf.openapi + "/documents/" + docid, function (data) {
  530. $('#autodoc_home_modal').data('document', data);
  531. $('#autodoc_home_modal').data('documentid', docid);
  532. var pageNum;
  533. for (var i = 0; i < data.pageId.length; i++) {
  534. if (data.pageId[i] == data.primaryPage) {
  535. pageNum = i;
  536. break;
  537. }
  538. }
  539. $('span.autodoc_home_modal_name').html(data.name ? data.name : 'n/a');
  540. $('input.autodoc_home_modal_name').val(data.name ? data.name : '');
  541. $('.autodoc_home_modal_created').html(data.created);
  542. $('.autodoc_home_modal_languages').html(data.languages.join(' '));
  543. $('.autodoc_home_modal_pages').html(data.pageId.length);
  544. $('.autodoc_home_modal_owner').html(data.owner);
  545. home_modal_page(pageNum);
  546. $('#autodoc_home_modal').modal('show');
  547. });
  548. }
  549. function home_modal_close(docid) {
  550. $('#autodoc_home_modal').modal('hide');
  551. }
  552. function document_update_name(docid, name) {
  553. $.ajax({
  554. type: "PATCH",
  555. url: conf.openapi + "/documents/" + docid + "?name=" + encodeURIComponent(name),
  556. success: function (data) {
  557. $('span.autodoc_home_modal_name').removeClass('d-none');
  558. $('input.autodoc_home_modal_name').addClass('d-none');
  559. home_modal_open(docid);
  560. }
  561. });
  562. }