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

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