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

autodoc.js 13KB

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