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.

autodoc.js 16KB

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