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

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