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

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