An unfinished system to manage all your paper documentation in an easy way.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

autodoc.js 14KB

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