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

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