An unfinished system to manage all your paper documentation in an easy way.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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_tab_document').data('docid', docid);
  210. $('.autodoc_document_empty').addClass('d-none');
  211. $('.autodoc_document_body').removeClass('d-none');
  212. $.getJSON(
  213. conf.openapi + "/documents/" + docid,
  214. function (doc) {
  215. $('span.autodoc_document_name').html(doc.name);
  216. $('input.autodoc_document_name').val(doc.name);
  217. $('.autodoc_document_created').html(doc.created);
  218. $('.autodoc_document_languages').html(doc.languages.join(','));
  219. $('.autodoc_document_owner').html(doc.owner);
  220. $('.autodoc_document_pages').html(doc.pageId.length);
  221. for(var i=0; i<doc.tags.length; i++) {
  222. var tag = $('.autodoc_document_tags_template').clone().removeClass('d-none');
  223. $(tag).html(doc.tags[i].name);
  224. $(tag).addClass('badge-' + doc.tags[i].color);
  225. $('.autodoc_document_tags').append(tag);
  226. }
  227. if ($('.slick-slide').length > 0) {
  228. $('.autodoc_document_carousel').slick('unslick');
  229. }
  230. $('.autodoc_document_carousel').children().remove();
  231. for (var i = 0; i < doc.pageId.length; i++) {
  232. var item = $('.autodoc_document_template_carousel').children().clone();
  233. $(item).children('img').attr('data-lazy',
  234. conf.openapi + "/pages/" + doc.pageId[i] + "/image?maxWidth=300");
  235. $(item).data('pageid', doc.pageId[i]);
  236. $(item).click(function(e) {
  237. console.log(e);
  238. console.log("open image", $(e.target).data('pageid'));
  239. });
  240. console.log(item);
  241. $('.autodoc_document_carousel').append(item);
  242. }
  243. $('.autodoc_document_carousel').slick({
  244. lazyLoad: 'ondemand',
  245. slidesToShow: 3,
  246. slidesToScroll: 3,
  247. dots: true,
  248. infinite: true
  249. });
  250. }
  251. );
  252. }
  253. function page_load_upload() {
  254. upload_status();
  255. }
  256. function page_load_tags() {
  257. // update tag list
  258. $.getJSON(conf.openapi + "/tags", function (tags) {
  259. $.each(tags, function (n, tag) {
  260. $('.autodoc_tag_list').append(
  261. '<option data="' + tag.color + '" value="' + tag.id + '">' +
  262. tag.name +
  263. '</option>'
  264. );
  265. });
  266. });
  267. }
  268. function document_load() {
  269. if (conf.home.curpage == 0) {
  270. $("#autodoc_home_body").html("");
  271. }
  272. var tmpl = $("#autodoc_template_home");
  273. var row = $(tmpl)
  274. .find(".autodoc_template_home_row")
  275. .clone();
  276. $.getJSON(
  277. conf.openapi +
  278. "/documents?pageSize=" +
  279. conf.home.width +
  280. "&pageIndex=" +
  281. conf.home.curpage,
  282. null,
  283. function (data, textStatus, jqXHR) {
  284. // last document was loaded, stop scrolling
  285. if (data.length < conf.home.width) {
  286. conf.home.end = 1;
  287. }
  288. $.each(data, function (id, doc) {
  289. var col = $(tmpl)
  290. .find(".autodoc_template_home_col")
  291. .clone();
  292. $(col)
  293. .find(".autodoc_template_home_img")
  294. .attr(
  295. "src",
  296. conf.openapi +
  297. "/documents/" +
  298. doc.id +
  299. "/image?maxWidth=" +
  300. Math.floor(1000 / conf.home.width)
  301. );
  302. $(col).find(".autodoc_template_home_img").data("documentId", doc.id);
  303. $(col).find(".autodoc_template_home_img").on('click', function (e) {
  304. window.location.hash = 'document_' + $(e.target).data("documentId");
  305. e.preventDefault();
  306. // home_modal_open($(e.target).data("documentId"));
  307. });
  308. $(col)
  309. .find(".autodoc_template_home_owner")
  310. .html(doc.owner);
  311. $(col)
  312. .find(".autodoc_template_home_created")
  313. .html(doc.created);
  314. $(col)
  315. .find(".autodoc_template_home_name")
  316. .html(doc.name);
  317. $(col)
  318. .find(".autodoc_template_home_pages")
  319. .html(doc.pageId.length);
  320. $.each(doc.tags, function (id, tag) {
  321. $(col)
  322. .find(".autodoc_template_home_tag")
  323. .after(
  324. $(col)
  325. .find(".autodoc_template_home_tag")
  326. .clone()
  327. .html(tag)
  328. );
  329. });
  330. $(row).append(col);
  331. });
  332. $("#autodoc_home_body").append(row);
  333. // continue to load until out of browser.
  334. page_home_scroll();
  335. }
  336. );
  337. }
  338. function page_home_scroll() {
  339. // don't scroll if we've loaded the last document
  340. if (conf.home.end) {
  341. return;
  342. }
  343. if (isvisible($(".autodoc_template_home_col").last())) {
  344. console.log("scroll to page " + conf.home.curpage);
  345. conf.home.curpage++;
  346. document_load();
  347. }
  348. }
  349. function upload_zone_init() {
  350. $(window).on("dragover dragleave drop", function (e) { e.preventDefault() });
  351. $('.autodoc_upload_zone').on({
  352. dragenter: upload_zone_highlight,
  353. dragleave: upload_zone_normal,
  354. dragover: upload_zone_over,
  355. drop: upload_zone_drop
  356. });
  357. }
  358. function upload_zone_highlight(e) {
  359. $(e.target).addClass('autodoc_upload_zone_hover');
  360. }
  361. function upload_zone_over(e) {
  362. return true;
  363. }
  364. function upload_zone_normal(e) {
  365. $(e.target).removeClass('autodoc_upload_zone_hover');
  366. }
  367. function upload_zone_drop(e) {
  368. var ev = e.originalEvent;
  369. $.each(ev.dataTransfer.files, function (id, file) {
  370. console.log(file);
  371. var fileid = temp.upload.length;
  372. temp.upload.push({
  373. progress: 0,
  374. error: null,
  375. obj: file
  376. });
  377. upload_zone_normal(e);
  378. });
  379. if (conf.run.upload_handler == 0) {
  380. upload_handler();
  381. }
  382. if (conf.run.upload_status == 0) {
  383. upload_status();
  384. }
  385. }
  386. function upload_handler() {
  387. var stop = 1;
  388. conf.run.upload_handler = 1;
  389. console.log("upload_handler");
  390. if (temp.documentid == null) {
  391. console.log("creating documentId");
  392. $.post(conf.openapi + "/documents", {}, function (data) {
  393. temp.documentid = data.id;
  394. });
  395. stop = 0;
  396. }
  397. else {
  398. for (var id = 0; id < temp.upload.length; id++) {
  399. var file = temp.upload[id];
  400. if (file.progress != 100) {
  401. if (file.progress == 0) {
  402. if (file.obj.type != 'application/pdf' && file.obj.type != 'image/png' && file.obj.type != 'image/jpeg') {
  403. file['progress'] = 100;
  404. file['error'] = 'invalid file type';
  405. }
  406. else {
  407. var reader = new FileReader();
  408. file['progress'] = 1;
  409. $(reader).on('load', id, upload_read_event);
  410. $(reader).on('loadstart', id, upload_read_event);
  411. $(reader).on('loadend', id, upload_read_event);
  412. $(reader).on('progress', id, upload_read_event);
  413. $(reader).on('error', id, upload_read_event);
  414. $(reader).on('abort', id, upload_read_event);
  415. reader.readAsArrayBuffer(file.obj);
  416. }
  417. }
  418. stop = 0;
  419. break;
  420. }
  421. }
  422. }
  423. if (stop) {
  424. conf.run.upload_handler = 0;
  425. }
  426. else {
  427. setTimeout(function () { upload_handler(); }, 500);
  428. }
  429. }
  430. function upload_read_event(e) {
  431. if (e.type == 'progress') {
  432. if (e.lengthComputable) {
  433. var progress = Math.floor(e.originalEvent.loaded / e.originalEvent.total * 50);
  434. if (progress < 1) { progress = 1; }
  435. if (progress > 49) { progress = 49; }
  436. temp.upload[e.data]['progress'] = progress;
  437. }
  438. }
  439. else if (e.type == 'load') {
  440. temp.upload[e.data]['progress'] = 50;
  441. console.log(e.target.result);
  442. console.log("upload_id", e.data);
  443. $.ajax({
  444. type: 'POST',
  445. url: conf.openapi + "/documents/" + temp.documentid + "/data",
  446. data: e.target.result,
  447. contentType: temp.upload[e.data].obj.type,
  448. context: { id: e.data },
  449. processData: false,
  450. xhr: function () {
  451. var xhr = $.ajaxSettings.xhr();
  452. xhr.upload.addEventListener("progress", (function () {
  453. var id = e.data;
  454. return function (evt) {
  455. if (evt.lengthComputable) {
  456. var progress = Math.floor(evt.loaded / evt.total * 50);
  457. if (progress < 1) { progress = 1; }
  458. if (progress > 49) { progress = 49; }
  459. temp.upload[id].progress = 50 + progress;
  460. // console.log("upload_progress_event", evt);
  461. }
  462. }
  463. })(), false);
  464. return xhr;
  465. },
  466. success: function (data, status, xhr) {
  467. console.log("upload_success_this", this);
  468. temp.upload[this.id].progress = 100;
  469. }
  470. });
  471. }
  472. }
  473. function upload_status() {
  474. var stop = 1;
  475. conf.run.upload_status = 1;
  476. $.each(temp.upload, function (id, file) {
  477. if (!$('.autodoc_upload_status_' + id).length) {
  478. var obj = $(".autodoc_template_progress").children().clone();
  479. $(obj).addClass('autodoc_upload_status_' + id);
  480. $('.autodoc_upload_status').append(obj);
  481. stop = 0;
  482. }
  483. var obj = $('.autodoc_upload_status_' + id).children();
  484. if (file.error != null) {
  485. $(obj).html(file.obj.name + ': ' + file.error);
  486. $(obj).css('width', '100%');
  487. $(obj).addClass('bg-danger');
  488. $(obj).removeClass("progress-bar-animated");
  489. $(obj).removeClass("progress-bar-striped");
  490. }
  491. else if (file.progress == 100) {
  492. $(obj).html(file.obj.name);
  493. $(obj).css('width', '100%');
  494. $(obj).addClass('bg-success');
  495. $(obj).removeClass('progress-bar-animated');
  496. $(obj).removeClass("progress-bar-striped");
  497. }
  498. else if (file.progress < 50) {
  499. $(obj).html(file.obj.name + ': ' + 'reading file');
  500. $(obj).css('width', file.progress + '%');
  501. stop = 0;
  502. }
  503. else {
  504. $(obj).html(file.obj.name + ': ' + 'sending file');
  505. $(obj).css('width', file.progress + '%');
  506. stop = 0;
  507. }
  508. });
  509. if (stop) {
  510. conf.run.upload_status = 0;
  511. }
  512. else {
  513. setTimeout(function () { upload_status(); }, 100);
  514. }
  515. }
  516. function home_modal_page(pageNum) {
  517. var doc = $('#autodoc_home_modal').data('document');
  518. var pageTotal = doc.pageId.length;
  519. if (pageNum < 0) {
  520. pageNum = pageTotal - 1;
  521. }
  522. if (pageNum > pageTotal - 1) {
  523. pageNum = 0;
  524. }
  525. $('.autodoc_home_modal_page').html(pageNum + 1);
  526. $('#autodoc_home_modal').data('pageNum', pageNum);
  527. $('#autodoc_home_modal_img').attr('src',
  528. conf.openapi +
  529. "/pages/" +
  530. doc.pageId[pageNum] +
  531. "/image");
  532. }
  533. function home_modal_resize(size) {
  534. var newclass;
  535. $('#autodoc_home_modal').children().removeClass('modal-sm modal-lg modal-xl');
  536. switch (size) {
  537. case "S": newclass = "modal-sm"; break;
  538. case "M": break;
  539. case "L": newclass = "modal-lg"; break;
  540. case "XL": newclass = "modal-xl"; break;
  541. }
  542. if (newclass) {
  543. $('#autodoc_home_modal').children().addClass(newclass);
  544. }
  545. }
  546. function home_modal_page_prev() {
  547. home_modal_page(
  548. $('#autodoc_home_modal').data('pageNum') - 1);
  549. }
  550. function home_modal_page_next() {
  551. home_modal_page(
  552. $('#autodoc_home_modal').data('pageNum') + 1);
  553. }
  554. function home_modal_open(docid) {
  555. $.getJSON(conf.openapi + "/documents/" + docid, function (data) {
  556. $('#autodoc_home_modal').data('document', data);
  557. $('#autodoc_home_modal').data('documentid', docid);
  558. var pageNum;
  559. for (var i = 0; i < data.pageId.length; i++) {
  560. if (data.pageId[i] == data.primaryPage) {
  561. pageNum = i;
  562. break;
  563. }
  564. }
  565. $('span.autodoc_home_modal_name').html(data.name ? data.name : 'n/a');
  566. $('input.autodoc_home_modal_name').val(data.name ? data.name : '');
  567. $('.autodoc_home_modal_created').html(data.created);
  568. $('.autodoc_home_modal_languages').html(data.languages.join(' '));
  569. $('.autodoc_home_modal_pages').html(data.pageId.length);
  570. $('.autodoc_home_modal_owner').html(data.owner);
  571. home_modal_page(pageNum);
  572. $('#autodoc_home_modal').modal('show');
  573. });
  574. }
  575. function home_modal_close(docid) {
  576. $('#autodoc_home_modal').modal('hide');
  577. }
  578. function document_update_name(docid, name) {
  579. $.ajax({
  580. type: "PATCH",
  581. url: conf.openapi + "/documents/" + docid + "?name=" + encodeURIComponent(name),
  582. success: function (data) {
  583. $('span.autodoc_home_modal_name').removeClass('d-none');
  584. $('input.autodoc_home_modal_name').addClass('d-none');
  585. home_modal_open(docid);
  586. }
  587. });
  588. }