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

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