An unfinished system to manage all your paper documentation in an easy way.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

autodoc.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /* AutoDoc Javascript */
  2. var conf = {
  3. default_hash: "home",
  4. openapi: "/v1",
  5. pageinit: {
  6. home: "page_load_home",
  7. document: "page_load_document",
  8. upload: "page_load_upload",
  9. tags: "page_load_tags"
  10. },
  11. home: {
  12. width: 4,
  13. curpage: 0,
  14. end: 0
  15. },
  16. run: {
  17. "upload_handler": 0,
  18. "upload_status": 0
  19. },
  20. busy: 0
  21. };
  22. var temp = {
  23. upload: [],
  24. documentid: null
  25. };
  26. $(document).ready(function () {
  27. // loader icon thread
  28. loader_icon_update();
  29. // load the current page based on anchor
  30. change_page();
  31. // initialise drop zone
  32. upload_zone_init();
  33. // main page scroll event
  34. $(document).scroll(function () {
  35. page_home_scroll();
  36. });
  37. // document page
  38. // make primary button
  39. $('.autodoc_document_btn_primary').click(function(e) {
  40. $(e.target).addClass('disabled');
  41. var docid = $('#autodoc_tab_document').data('docid');
  42. var pageid = $('.autodoc_document_image').data('pageid');
  43. loader_icon_on();
  44. $.ajax({
  45. type: "PATCH",
  46. url: conf.openapi + "/documents/" + docid + "?primarypage=" + pageid,
  47. success: function () {
  48. loader_icon_off();
  49. $('.autodoc_document_btn_primary').removeClass('disabled');
  50. }
  51. });
  52. });
  53. // tag menu select
  54. $('.autodoc_tag_list').change(function (e) {
  55. console.log(e);
  56. var tagId = $('.autodoc_tag_list').val();
  57. var tagName = $('.autodoc_tag_list').children("option:selected").html();
  58. var tagColor = $('.autodoc_tag_list').children("option:selected").attr('data');
  59. if (tagId == "") {
  60. $(".autodoc_tag_list").addClass('d-none');
  61. $(".autodoc_tag_text").removeClass('d-none');
  62. $('.autodoc_tag_text').val('');
  63. $('.autodoc_tag_color.btn-primary').click();
  64. $('.autodoc_tag_id').val('');
  65. }
  66. else {
  67. $(".autodoc_tag_list").addClass('d-none');
  68. $(".autodoc_tag_text").removeClass('d-none');
  69. $('.autodoc_tag_text').val(tagName);
  70. $('.autodoc_tag_color.btn-' + tagColor).click();
  71. $('.autodoc_tag_id').val(tagId);
  72. $('.autodoc_tag_text').keyup();
  73. }
  74. });
  75. // tags add button
  76. $('.autodoc_tag_add').click(function (e) {
  77. var tagId = $('.autodoc_tag_id').val();
  78. var tagName = $('.autodoc_tag_text').val();
  79. var tagColor = $('.autodoc_tag_color.active').attr('data').split('_').pop();
  80. var url = conf.openapi + "/tags/";
  81. var urlparam = "tagName=" + encodeURIComponent(tagName) + "&tagColor=" + encodeURIComponent(tagColor);
  82. var method = "POST";
  83. if (tagId != "") {
  84. method = "PATCH";
  85. url += tagId;
  86. }
  87. loader_icon_on();
  88. $.ajax({
  89. type: method,
  90. url: url + '?' + urlparam,
  91. success: function () {
  92. loader_icon_off();
  93. location.reload();
  94. }
  95. });
  96. });
  97. // tags delete event
  98. $('.autodoc_tag_delete').click(function (e) {
  99. var tagId = $('.autodoc_tag_id').val();
  100. if (tagId != "") {
  101. loader_icon_on();
  102. $.ajax({
  103. type: "DELETE",
  104. url: conf.openapi + "/tags/" + tagId,
  105. success: function () {
  106. loader_icon_off();
  107. location.reload();
  108. }
  109. });
  110. }
  111. else {
  112. location.reload();
  113. }
  114. });
  115. // tags add color events
  116. $('.autodoc_tag_color').click(function (e) {
  117. var btn = e.target;
  118. while ($(btn).prop("tagName") != "BUTTON") {
  119. btn = $(btn).parent();
  120. }
  121. $(btn).parent().children().removeClass('active');
  122. $(btn).addClass('active');
  123. $('.autodoc_tag_btn_on').addClass('d-none');
  124. $('.autodoc_tag_btn_off').removeClass('d-none');
  125. $(btn).children('.autodoc_tag_btn_on').removeClass('d-none');
  126. $(btn).children('.autodoc_tag_btn_off').addClass('d-none');
  127. $(btn).parent().children().each(function (id, x) {
  128. var myclass = 'badge-' + $(x).attr('data').split('_').pop();
  129. $('#autodoc_tag_result').removeClass(myclass);
  130. });
  131. var myclass = 'badge-' + $(btn).attr('data').split('_').pop();
  132. $('#autodoc_tag_result').addClass(myclass);
  133. });
  134. // tag add text events
  135. $('.autodoc_tag_text').keyup(function (e) {
  136. $('#autodoc_tag_result').html($(e.target).val());
  137. });
  138. });
  139. // anchor change event
  140. $(window).bind("hashchange", function () {
  141. change_page();
  142. });
  143. function change_page() {
  144. var args = document.location.hash.substr(1).split('_') || conf.default_hash.split('_');
  145. var hash = args.shift();
  146. if (!$("#autodoc_tab_" + hash).length) {
  147. hash = conf.default_hash;
  148. }
  149. /* change menu highlights and content visibility */
  150. $("#autodoc_navbar")
  151. .find("a")
  152. .each(function (id, obj) {
  153. var curhash = $(obj)
  154. .attr("href")
  155. .substr(1);
  156. if (curhash == hash) {
  157. $(obj)
  158. .parent()
  159. .addClass("active");
  160. $("#autodoc_tab_" + curhash).removeClass("d-none");
  161. } else {
  162. $(obj)
  163. .parent()
  164. .removeClass("active");
  165. $("#autodoc_tab_" + curhash).addClass("d-none");
  166. }
  167. });
  168. if (conf.pageinit[hash]) {
  169. var func = conf.pageinit[hash];
  170. console.log(func);
  171. if (window[func]) {
  172. window[func](args);
  173. }
  174. }
  175. }
  176. function isvisible(obj) {
  177. var top_of_fileent = $(obj).offset().top;
  178. var bottom_of_fileent = $(obj).offset().top + $(obj).outerHeight();
  179. var bottom_of_screen = $(window).scrollTop() + $(window).innerHeight();
  180. var top_of_screen = $(window).scrollTop();
  181. if (bottom_of_screen > top_of_fileent && top_of_screen < bottom_of_fileent) {
  182. return true;
  183. }
  184. return false;
  185. }
  186. function page_load_document(args) {
  187. var [docid] = args;
  188. if (!docid) { return false; }
  189. $('#autodoc_tab_document').data('docid', docid);
  190. $('.autodoc_document_empty').addClass('d-none');
  191. $('.autodoc_document_body').removeClass('d-none');
  192. $('.autodoc_document_image').attr('src','');
  193. loader_icon_on();
  194. $.getJSON(
  195. conf.openapi + "/documents/" + docid,
  196. function (doc) {
  197. loader_icon_off();
  198. $('span.autodoc_document_name').html(doc.name);
  199. $('input.autodoc_document_name').val(doc.name);
  200. $('.autodoc_document_created').html(doc.created);
  201. $('.autodoc_document_languages').html(doc.languages.join(','));
  202. $('.autodoc_document_owner').html(doc.owner);
  203. $('.autodoc_document_pages').html(doc.pageId.length);
  204. for(var i=0; i<doc.tags.length; i++) {
  205. var tag = $('.autodoc_document_tags_template').clone().removeClass('d-none');
  206. $(tag).html(doc.tags[i].name);
  207. $(tag).addClass('badge-' + doc.tags[i].color);
  208. $('.autodoc_document_tags').append(tag);
  209. }
  210. if ($('.slick-slide').length > 0) {
  211. $('.autodoc_document_carousel').slick('unslick');
  212. }
  213. $('.autodoc_document_carousel').children().remove();
  214. for (var i = 0; i < doc.pageId.length; i++) {
  215. var item = $('.autodoc_document_template_carousel').children().clone();
  216. $(item).children('img').attr('data-lazy',
  217. conf.openapi + "/pages/" + doc.pageId[i] + "/image?maxWidth=300");
  218. $(item).data('pageid', doc.pageId[i]);
  219. $(item).click(function(e) {
  220. var pageid = $(e.target).parent().data('pageid');
  221. $('.autodoc_document_image').attr('src',
  222. conf.openapi + "/pages/" + pageid + "/image"
  223. );
  224. $('.autodoc_document_image').data('pageid', pageid);
  225. });
  226. $('.autodoc_document_carousel').append(item);
  227. }
  228. $('.autodoc_document_image').attr('src', conf.openapi + "/pages/" + doc.primaryPage + "/image");
  229. $('.autodoc_document_image').data('pageid', doc.primaryPage);
  230. $('.autodoc_document_carousel').slick({
  231. lazyLoad: 'ondemand',
  232. slidesToShow: 3,
  233. slidesToScroll: 3,
  234. dots: true,
  235. infinite: true
  236. });
  237. }
  238. );
  239. }
  240. function page_load_upload() {
  241. upload_status();
  242. }
  243. function page_load_tags() {
  244. // update tag list
  245. loader_icon_on();
  246. $.getJSON(conf.openapi + "/tags", function (tags) {
  247. loader_icon_off();
  248. $.each(tags, function (n, tag) {
  249. $('.autodoc_tag_list').append(
  250. '<option data="' + tag.color + '" value="' + tag.id + '">' +
  251. tag.name +
  252. '</option>'
  253. );
  254. });
  255. });
  256. }
  257. function page_load_home(filter) {
  258. if (conf.home.curpage == 0) {
  259. $("#autodoc_home_body").html("");
  260. }
  261. if ( ! filter ) {
  262. filter = '';
  263. }
  264. var tmpl = $("#autodoc_template_home");
  265. var row = $(tmpl)
  266. .find(".autodoc_template_home_row")
  267. .clone();
  268. loader_icon_on();
  269. $.getJSON(
  270. conf.openapi +
  271. "/documents?pageSize=" +
  272. conf.home.width +
  273. "&pageIndex=" +
  274. conf.home.curpage +
  275. "&filter=" +
  276. encodeURIComponent(filter),
  277. null,
  278. function (data, textStatus, jqXHR) {
  279. // last document was loaded, stop scrolling
  280. loader_icon_off();
  281. if (data.length < conf.home.width) {
  282. conf.home.end = 1;
  283. }
  284. $.each(data, function (id, doc) {
  285. var col = $(tmpl)
  286. .find(".autodoc_template_home_col")
  287. .clone();
  288. $(col)
  289. .find(".autodoc_template_home_img")
  290. .attr(
  291. "src",
  292. conf.openapi +
  293. "/documents/" +
  294. doc.id +
  295. "/image?maxWidth=" +
  296. Math.floor(1000 / conf.home.width)
  297. );
  298. $(col).find(".autodoc_template_home_img").data("documentId", doc.id);
  299. $(col).find(".autodoc_template_home_img").on('click', function (e) {
  300. window.location.hash = 'document_' + $(e.target).data("documentId");
  301. e.preventDefault();
  302. });
  303. $(col)
  304. .find(".autodoc_template_home_owner")
  305. .html(doc.owner);
  306. $(col)
  307. .find(".autodoc_template_home_created")
  308. .html(doc.created);
  309. $(col)
  310. .find(".autodoc_template_home_name")
  311. .html(doc.name);
  312. $(col)
  313. .find(".autodoc_template_home_pages")
  314. .html(doc.pageId.length);
  315. $.each(doc.tags, function (id, tag) {
  316. $(col)
  317. .find(".autodoc_template_home_tag")
  318. .after(
  319. $(col)
  320. .find(".autodoc_template_home_tag")
  321. .clone()
  322. .html(tag)
  323. );
  324. });
  325. $(row).append(col);
  326. });
  327. $("#autodoc_home_body").append(row);
  328. // continue to load until out of browser.
  329. page_home_scroll();
  330. }
  331. );
  332. }
  333. function page_home_scroll() {
  334. // don't scroll if we've loaded the last document
  335. if (conf.home.end) {
  336. return;
  337. }
  338. if (isvisible($(".autodoc_template_home_col").last())) {
  339. console.log("scroll to page " + conf.home.curpage);
  340. conf.home.curpage++;
  341. document_load();
  342. }
  343. }
  344. function upload_zone_init() {
  345. $(window).on("dragover dragleave drop", function (e) { e.preventDefault() });
  346. $('.autodoc_upload_zone').on({
  347. dragenter: upload_zone_highlight,
  348. dragleave: upload_zone_normal,
  349. dragover: upload_zone_over,
  350. drop: upload_zone_drop
  351. });
  352. }
  353. function upload_zone_highlight(e) {
  354. $(e.target).addClass('autodoc_upload_zone_hover');
  355. }
  356. function upload_zone_over(e) {
  357. return true;
  358. }
  359. function upload_zone_normal(e) {
  360. $(e.target).removeClass('autodoc_upload_zone_hover');
  361. }
  362. function upload_zone_drop(e) {
  363. var ev = e.originalEvent;
  364. $.each(ev.dataTransfer.files, function (id, file) {
  365. console.log(file);
  366. var fileid = temp.upload.length;
  367. temp.upload.push({
  368. progress: 0,
  369. error: null,
  370. obj: file
  371. });
  372. upload_zone_normal(e);
  373. });
  374. if (conf.run.upload_handler == 0) {
  375. upload_handler();
  376. }
  377. if (conf.run.upload_status == 0) {
  378. upload_status();
  379. }
  380. }
  381. function upload_handler() {
  382. var stop = 1;
  383. conf.run.upload_handler = 1;
  384. console.log("upload_handler");
  385. if (temp.documentid == null) {
  386. console.log("creating documentId");
  387. loader_icon_on();
  388. $.post(conf.openapi + "/documents", {}, function (data) {
  389. loader_icon_off();
  390. temp.documentid = data.id;
  391. });
  392. stop = 0;
  393. }
  394. else {
  395. for (var id = 0; id < temp.upload.length; id++) {
  396. var file = temp.upload[id];
  397. if (file.progress != 100) {
  398. if (file.progress == 0) {
  399. if (file.obj.type != 'application/pdf' && file.obj.type != 'image/png' && file.obj.type != 'image/jpeg') {
  400. file['progress'] = 100;
  401. file['error'] = 'invalid file type';
  402. }
  403. else {
  404. var reader = new FileReader();
  405. file['progress'] = 1;
  406. $(reader).on('load', id, upload_read_event);
  407. $(reader).on('loadstart', id, upload_read_event);
  408. $(reader).on('loadend', id, upload_read_event);
  409. $(reader).on('progress', id, upload_read_event);
  410. $(reader).on('error', id, upload_read_event);
  411. $(reader).on('abort', id, upload_read_event);
  412. reader.readAsArrayBuffer(file.obj);
  413. }
  414. }
  415. stop = 0;
  416. break;
  417. }
  418. }
  419. }
  420. if (stop) {
  421. conf.run.upload_handler = 0;
  422. }
  423. else {
  424. setTimeout(function () { upload_handler(); }, 500);
  425. }
  426. }
  427. function upload_read_event(e) {
  428. if (e.type == 'progress') {
  429. if (e.lengthComputable) {
  430. var progress = Math.floor(e.originalEvent.loaded / e.originalEvent.total * 50);
  431. if (progress < 1) { progress = 1; }
  432. if (progress > 49) { progress = 49; }
  433. temp.upload[e.data]['progress'] = progress;
  434. }
  435. }
  436. else if (e.type == 'load') {
  437. temp.upload[e.data]['progress'] = 50;
  438. loader_icon_on();
  439. $.ajax({
  440. type: 'POST',
  441. url: conf.openapi + "/documents/" + temp.documentid + "/data",
  442. data: e.target.result,
  443. contentType: temp.upload[e.data].obj.type,
  444. context: { id: e.data },
  445. processData: false,
  446. xhr: function () {
  447. var xhr = $.ajaxSettings.xhr();
  448. xhr.upload.addEventListener("progress", (function () {
  449. var id = e.data;
  450. return function (evt) {
  451. if (evt.lengthComputable) {
  452. var progress = Math.floor(evt.loaded / evt.total * 50);
  453. if (progress < 1) { progress = 1; }
  454. if (progress > 49) { progress = 49; }
  455. temp.upload[id].progress = 50 + progress;
  456. // console.log("upload_progress_event", evt);
  457. }
  458. }
  459. })(), false);
  460. return xhr;
  461. },
  462. success: function (data, status, xhr) {
  463. loader_icon_off();
  464. temp.upload[this.id].progress = 100;
  465. }
  466. });
  467. }
  468. }
  469. function upload_status() {
  470. var stop = 1;
  471. conf.run.upload_status = 1;
  472. $.each(temp.upload, function (id, file) {
  473. if (!$('.autodoc_upload_status_' + id).length) {
  474. var obj = $(".autodoc_template_progress").children().clone();
  475. $(obj).addClass('autodoc_upload_status_' + id);
  476. $('.autodoc_upload_status').append(obj);
  477. stop = 0;
  478. }
  479. var obj = $('.autodoc_upload_status_' + id).children();
  480. if (file.error != null) {
  481. $(obj).html(file.obj.name + ': ' + file.error);
  482. $(obj).css('width', '100%');
  483. $(obj).addClass('bg-danger');
  484. $(obj).removeClass("progress-bar-animated");
  485. $(obj).removeClass("progress-bar-striped");
  486. }
  487. else if (file.progress == 100) {
  488. $(obj).html(file.obj.name);
  489. $(obj).css('width', '100%');
  490. $(obj).addClass('bg-success');
  491. $(obj).removeClass('progress-bar-animated');
  492. $(obj).removeClass("progress-bar-striped");
  493. }
  494. else if (file.progress < 50) {
  495. $(obj).html(file.obj.name + ': ' + 'reading file');
  496. $(obj).css('width', file.progress + '%');
  497. stop = 0;
  498. }
  499. else {
  500. $(obj).html(file.obj.name + ': ' + 'sending file');
  501. $(obj).css('width', file.progress + '%');
  502. stop = 0;
  503. }
  504. });
  505. if (stop) {
  506. conf.run.upload_status = 0;
  507. }
  508. else {
  509. setTimeout(function () { upload_status(); }, 100);
  510. }
  511. }
  512. function document_update_name(docid, name) {
  513. loader_icon_on();
  514. $.ajax({
  515. type: "PATCH",
  516. url: conf.openapi + "/documents/" + docid + "?name=" + encodeURIComponent(name),
  517. success: function (data) {
  518. loader_icon_off();
  519. $('span.autodoc_home_modal_name').removeClass('d-none');
  520. $('input.autodoc_home_modal_name').addClass('d-none');
  521. home_modal_open(docid);
  522. }
  523. });
  524. }
  525. // handle ajax busy icon
  526. function loader_icon_on() {
  527. conf.busy++;
  528. }
  529. function loader_icon_off() {
  530. setTimeout(function() { conf.busy--; }, 1000);
  531. }
  532. function loader_icon_update() {
  533. if ( conf.busy > 0 ) {
  534. $('.autodoc_ajax_on').removeClass('d-none');
  535. $('.autodoc_ajax_off').addClass('d-none');
  536. }
  537. else {
  538. $('.autodoc_ajax_off').removeClass('d-none');
  539. $('.autodoc_ajax_on').addClass('d-none');
  540. }
  541. setTimeout(loader_icon_update, 100);
  542. }