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.

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