An unfinished system to manage all your paper documentation in an easy way.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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