An unfinished system to manage all your paper documentation in an easy way.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autodoc.js 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. });
  30. $(window).bind("hashchange", function () {
  31. change_page();
  32. });
  33. function change_page() {
  34. var hash = document.location.hash.substr(1) || conf.default_hash;
  35. if (!$("#autodoc_tab_" + hash).length) {
  36. hash = conf.default_hash;
  37. }
  38. /* change menu highlights and content visibility */
  39. $("#autodoc_navbar")
  40. .find("a")
  41. .each(function (id, obj) {
  42. var curhash = $(obj)
  43. .attr("href")
  44. .substr(1);
  45. if (curhash == hash) {
  46. $(obj)
  47. .parent()
  48. .addClass("active");
  49. $("#autodoc_tab_" + curhash).removeClass("d-none");
  50. } else {
  51. $(obj)
  52. .parent()
  53. .removeClass("active");
  54. $("#autodoc_tab_" + curhash).addClass("d-none");
  55. }
  56. });
  57. if (conf.pageinit[hash]) {
  58. eval(conf.pageinit[hash]);
  59. }
  60. }
  61. function isvisible(obj) {
  62. var top_of_fileent = $(obj).offset().top;
  63. var bottom_of_fileent = $(obj).offset().top + $(obj).outerHeight();
  64. var bottom_of_screen = $(window).scrollTop() + $(window).innerHeight();
  65. var top_of_screen = $(window).scrollTop();
  66. if (bottom_of_screen > top_of_fileent && top_of_screen < bottom_of_fileent) {
  67. return true;
  68. }
  69. return false;
  70. }
  71. function page_load_home() {
  72. document_load();
  73. }
  74. function page_load_upload() {
  75. upload_status();
  76. }
  77. function document_load() {
  78. if (conf.home.curpage == 0) {
  79. $("#autodoc_home_body").html("");
  80. }
  81. var tmpl = $("#autodoc_template_home");
  82. var row = $(tmpl)
  83. .find(".autodoc_template_home_row")
  84. .clone();
  85. $.getJSON(
  86. conf.openapi +
  87. "/documents?pageSize=" +
  88. conf.home.width +
  89. "&pageIndex=" +
  90. conf.home.curpage,
  91. null,
  92. function (data, textStatus, jqXHR) {
  93. // last document was loaded, stop scrolling
  94. if (data.length < conf.home.width) {
  95. conf.home.end = 1;
  96. }
  97. $.each(data, function (id, doc) {
  98. var col = $(tmpl)
  99. .find(".autodoc_template_home_col")
  100. .clone();
  101. $(col)
  102. .find(".autodoc_template_home_img")
  103. .attr(
  104. "src",
  105. conf.openapi +
  106. "/documents/" +
  107. doc.id +
  108. "/image?maxWidth=" +
  109. Math.floor(1000 / conf.home.width)
  110. );
  111. $(col).find(".autodoc_template_home_img").on('click', function (e) {
  112. console.log(e);
  113. });
  114. $(col)
  115. .find(".autodoc_template_home_owner")
  116. .html(doc.owner);
  117. $(col)
  118. .find(".autodoc_template_home_created")
  119. .html(doc.created);
  120. $(col)
  121. .find(".autodoc_template_home_name")
  122. .html(doc.name);
  123. $(col)
  124. .find(".autodoc_template_home_pages")
  125. .html(doc.pageId.length);
  126. $.each(doc.tags, function (id, tag) {
  127. $(col)
  128. .find(".autodoc_template_home_tag")
  129. .after(
  130. $(col)
  131. .find(".autodoc_template_home_tag")
  132. .clone()
  133. .html(tag)
  134. );
  135. });
  136. $(row).append(col);
  137. });
  138. $("#autodoc_home_body").append(row);
  139. // continue to load until out of browser.
  140. page_home_scroll();
  141. }
  142. );
  143. }
  144. function page_home_scroll() {
  145. // don't scroll if we've loaded the last document
  146. if (conf.home.end) {
  147. return;
  148. }
  149. if (isvisible($(".autodoc_template_home_col").last())) {
  150. console.log("scroll to page " + conf.home.curpage);
  151. conf.home.curpage++;
  152. document_load();
  153. }
  154. }
  155. function upload_zone_init() {
  156. $(window).on("dragover dragleave drop", function (e) { e.preventDefault() });
  157. $('.autodoc_upload_zone').on({
  158. dragenter: upload_zone_highlight,
  159. dragleave: upload_zone_normal,
  160. dragover: upload_zone_over,
  161. drop: upload_zone_drop
  162. });
  163. }
  164. function upload_zone_highlight(e) {
  165. $(e.target).addClass('autodoc_upload_zone_hover');
  166. }
  167. function upload_zone_over(e) {
  168. return true;
  169. }
  170. function upload_zone_normal(e) {
  171. $(e.target).removeClass('autodoc_upload_zone_hover');
  172. }
  173. function upload_zone_drop(e) {
  174. var ev = e.originalEvent;
  175. $.each(ev.dataTransfer.files, function (id, file) {
  176. console.log(file);
  177. var fileid = temp.upload.length;
  178. temp.upload.push({
  179. progress: 0,
  180. error: null,
  181. obj: file
  182. });
  183. upload_zone_normal(e);
  184. });
  185. if (conf.run.upload_handler == 0) {
  186. upload_handler();
  187. }
  188. if (conf.run.upload_status == 0) {
  189. upload_status();
  190. }
  191. }
  192. function upload_handler() {
  193. var stop = 1;
  194. conf.run.upload_handler = 1;
  195. console.log("upload_handler");
  196. if (temp.documentid == null) {
  197. console.log("creating documentId");
  198. $.post(conf.openapi + "/documents", {}, function (data) {
  199. temp.documentid = data.id;
  200. });
  201. stop = 0;
  202. }
  203. else {
  204. for (var id = 0; id < temp.upload.length; id++) {
  205. var file = temp.upload[id];
  206. if (file.progress != 100) {
  207. if (file.progress == 0) {
  208. if (file.obj.type != 'application/pdf' && file.obj.type != 'image/png' && file.obj.type != 'image/jpeg') {
  209. file['progress'] = 100;
  210. file['error'] = 'invalid file type';
  211. }
  212. else {
  213. var reader = new FileReader();
  214. file['progress'] = 1;
  215. $(reader).on('load', id, upload_read_event);
  216. $(reader).on('loadstart', id, upload_read_event);
  217. $(reader).on('loadend', id, upload_read_event);
  218. $(reader).on('progress', id, upload_read_event);
  219. $(reader).on('error', id, upload_read_event);
  220. $(reader).on('abort', id, upload_read_event);
  221. reader.readAsArrayBuffer(file.obj);
  222. }
  223. }
  224. stop = 0;
  225. break;
  226. }
  227. }
  228. }
  229. if (stop) {
  230. conf.run.upload_handler = 0;
  231. }
  232. else {
  233. setTimeout(function () { upload_handler(); }, 500);
  234. }
  235. }
  236. function upload_read_event(e) {
  237. if (e.type == 'progress') {
  238. if (e.lengthComputable) {
  239. var progress = Math.floor(e.originalEvent.loaded / e.originalEvent.total * 50);
  240. if (progress < 1) { progress = 1; }
  241. if (progress > 49) { progress = 49; }
  242. temp.upload[e.data]['progress'] = progress;
  243. }
  244. }
  245. else if (e.type == 'load') {
  246. temp.upload[e.data]['progress'] = 50;
  247. console.log(e.target.result);
  248. console.log("upload_id", e.data);
  249. $.ajax({
  250. type: 'POST',
  251. url: conf.openapi + "/documents/" + temp.documentid + "/data",
  252. data: e.target.result,
  253. contentType: temp.upload[e.data].obj.type,
  254. context: { id: e.data },
  255. processData: false,
  256. xhr: function () {
  257. var xhr = $.ajaxSettings.xhr();
  258. xhr.upload.addEventListener("progress", (function () {
  259. var id = e.data;
  260. return function (evt) {
  261. if (evt.lengthComputable) {
  262. var progress = Math.floor(evt.loaded / evt.total * 50);
  263. if (progress < 1) { progress = 1; }
  264. if (progress > 49) { progress = 49; }
  265. temp.upload[id].progress = 50 + progress;
  266. // console.log("upload_progress_event", evt);
  267. }
  268. }
  269. })(), false);
  270. return xhr;
  271. },
  272. success: function (data, status, xhr) {
  273. console.log("upload_success_this", this);
  274. temp.upload[this.id].progress = 100;
  275. }
  276. });
  277. }
  278. }
  279. function upload_status() {
  280. var stop = 1;
  281. conf.run.upload_status = 1;
  282. $.each(temp.upload, function (id, file) {
  283. if (!$('.autodoc_upload_status_' + id).length) {
  284. var obj = $(".autodoc_template_progress").children().clone();
  285. $(obj).addClass('autodoc_upload_status_' + id);
  286. $('.autodoc_upload_status').append(obj);
  287. stop = 0;
  288. }
  289. var obj = $('.autodoc_upload_status_' + id).children();
  290. if (file.error != null) {
  291. $(obj).html(file.obj.name + ': ' + file.error);
  292. $(obj).css('width', '100%');
  293. $(obj).addClass('bg-danger');
  294. $(obj).removeClass("progress-bar-animated");
  295. $(obj).removeClass("progress-bar-striped");
  296. }
  297. else if (file.progress == 100) {
  298. $(obj).html(file.obj.name);
  299. $(obj).css('width', '100%');
  300. $(obj).addClass('bg-success');
  301. $(obj).removeClass('progress-bar-animated');
  302. $(obj).removeClass("progress-bar-striped");
  303. }
  304. else if (file.progress < 50) {
  305. $(obj).html(file.obj.name + ': ' + 'reading file');
  306. $(obj).css('width', file.progress + '%');
  307. stop = 0;
  308. }
  309. else {
  310. $(obj).html(file.obj.name + ': ' + 'sending file');
  311. $(obj).css('width', file.progress + '%');
  312. stop = 0;
  313. }
  314. });
  315. if (stop) {
  316. conf.run.upload_status = 0;
  317. }
  318. else {
  319. setTimeout(function () { upload_status(); }, 100);
  320. }
  321. }