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

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