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.

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