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

autodoc.js 12KB

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