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.

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