An unfinished system to manage all your paper documentation in an easy way.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

autodoc.js 13KB

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