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

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