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

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