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.6KB

5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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)
  112. .find(".autodoc_template_home_owner")
  113. .html(doc.owner);
  114. $(col)
  115. .find(".autodoc_template_home_created")
  116. .html(doc.created);
  117. $(col)
  118. .find(".autodoc_template_home_name")
  119. .html(doc.name);
  120. $.each(doc.tags, function (id, tag) {
  121. $(col)
  122. .find(".autodoc_template_home_tag")
  123. .after(
  124. $(col)
  125. .find(".autodoc_template_home_tag")
  126. .clone()
  127. .html(tag)
  128. );
  129. });
  130. $(row).append(col);
  131. });
  132. $("#autodoc_home_body").append(row);
  133. // continue to load until out of browser.
  134. page_home_scroll();
  135. }
  136. );
  137. }
  138. function page_home_scroll() {
  139. // don't scroll if we've loaded the last document
  140. if (conf.home.end) {
  141. return;
  142. }
  143. if (isvisible($(".autodoc_template_home_col").last())) {
  144. console.log("scroll to page " + conf.home.curpage);
  145. conf.home.curpage++;
  146. document_load();
  147. }
  148. }
  149. function upload_zone_init() {
  150. $(window).on("dragover dragleave drop", function (e) { e.preventDefault() });
  151. $('.autodoc_upload_zone').on({
  152. dragenter: upload_zone_highlight,
  153. dragleave: upload_zone_normal,
  154. dragover: upload_zone_over,
  155. drop: upload_zone_drop
  156. });
  157. }
  158. function upload_zone_highlight(e) {
  159. $(e.target).addClass('autodoc_upload_zone_hover');
  160. }
  161. function upload_zone_over(e) {
  162. return true;
  163. }
  164. function upload_zone_normal(e) {
  165. $(e.target).removeClass('autodoc_upload_zone_hover');
  166. }
  167. function upload_zone_drop(e) {
  168. var ev = e.originalEvent;
  169. $.each(ev.dataTransfer.files, function (id, file) {
  170. console.log(file);
  171. var fileid = temp.upload.length;
  172. temp.upload.push({
  173. progress: 0,
  174. error: null,
  175. obj: file
  176. });
  177. });
  178. if (conf.run.upload_handler == 0) {
  179. upload_handler();
  180. }
  181. if (conf.run.upload_status == 0) {
  182. upload_status();
  183. }
  184. }
  185. function upload_handler() {
  186. var stop = 1;
  187. conf.run.upload_handler = 1;
  188. console.log("upload_handler");
  189. if (temp.documentid == null) {
  190. console.log("creating documentId");
  191. $.post(conf.openapi + "/documents", {}, function (data) {
  192. temp.documentid = data.id;
  193. });
  194. stop = 0;
  195. }
  196. else {
  197. for (var id = 0; id < temp.upload.length; id++) {
  198. var file = temp.upload[id];
  199. if (file.progress != 100) {
  200. if (file.progress == 0) {
  201. if (file.obj.type != 'application/pdf' && file.obj.type != 'image/png' && file.obj.type != 'image/jpeg') {
  202. file['progress'] = 100;
  203. file['error'] = 'invalid file type';
  204. }
  205. else {
  206. var reader = new FileReader();
  207. file['progress'] = 1;
  208. $(reader).on('load', id, upload_read_event);
  209. $(reader).on('loadstart', id, upload_read_event);
  210. $(reader).on('loadend', id, upload_read_event);
  211. $(reader).on('progress', id, upload_read_event);
  212. $(reader).on('error', id, upload_read_event);
  213. $(reader).on('abort', id, upload_read_event);
  214. reader.readAsArrayBuffer(file.obj);
  215. }
  216. }
  217. stop = 0;
  218. break;
  219. }
  220. }
  221. }
  222. if (stop) {
  223. conf.run.upload_handler = 0;
  224. }
  225. else {
  226. setTimeout(function () { upload_handler(); }, 500);
  227. }
  228. }
  229. function upload_read_event(e) {
  230. if (e.type == 'progress') {
  231. if (e.lengthComputable) {
  232. var progress = Math.floor(e.originalEvent.loaded / e.originalEvent.total * 50);
  233. if (progress < 1) { progress = 1; }
  234. if (progress > 49) { progress = 49; }
  235. temp.upload[e.data]['progress'] = progress;
  236. }
  237. }
  238. else if (e.type == 'load') {
  239. temp.upload[e.data]['progress'] = 50;
  240. console.log(e.target.result);
  241. console.log("upload_id", e.data);
  242. $.ajax({
  243. type: 'POST',
  244. url: conf.openapi + "/documents/" + temp.documentid + "/data",
  245. data: e.target.result,
  246. contentType: temp.upload[e.data].obj.type,
  247. context: { id: e.data },
  248. processData: false,
  249. xhr: function () {
  250. var xhr = $.ajaxSettings.xhr();
  251. xhr.upload.addEventListener("progress", (function () {
  252. var id = e.data;
  253. return function (evt) {
  254. if (evt.lengthComputable) {
  255. var progress = Math.floor(evt.loaded / evt.total * 50);
  256. if (progress < 1) { progress = 1; }
  257. if (progress > 49) { progress = 49; }
  258. temp.upload[id].progress = 50 + progress;
  259. // console.log("upload_progress_event", evt);
  260. }
  261. }
  262. })(), false);
  263. return xhr;
  264. },
  265. success: function (data, status, xhr) {
  266. console.log("upload_success_this", this);
  267. temp.upload[this.id].progress = 100;
  268. }
  269. });
  270. }
  271. }
  272. function upload_status() {
  273. var stop = 1;
  274. conf.run.upload_status = 1;
  275. $.each(temp.upload, function (id, file) {
  276. if (!$('.autodoc_upload_status_' + id).length) {
  277. var obj = $(".autodoc_template_progress").children().clone();
  278. $(obj).addClass('autodoc_upload_status_' + id);
  279. $('.autodoc_upload_status').append(obj);
  280. stop = 0;
  281. }
  282. var obj = $('.autodoc_upload_status_' + id).children();
  283. if (file.error != null) {
  284. $(obj).html(file.obj.name + ': ' + file.error);
  285. $(obj).css('width', '100%');
  286. $(obj).addClass('bg-danger');
  287. $(obj).removeClass("progress-bar-animated");
  288. $(obj).removeClass("progress-bar-striped");
  289. }
  290. else if (file.progress == 100) {
  291. $(obj).html(file.obj.name);
  292. $(obj).css('width', '100%');
  293. $(obj).addClass('bg-success');
  294. $(obj).removeClass('progress-bar-animated');
  295. $(obj).removeClass("progress-bar-striped");
  296. }
  297. else if (file.progress < 50) {
  298. $(obj).html(file.obj.name + ': ' + 'reading file');
  299. $(obj).css('width', file.progress + '%');
  300. stop = 0;
  301. }
  302. else {
  303. $(obj).html(file.obj.name + ': ' + 'sending file');
  304. $(obj).css('width', file.progress + '%');
  305. stop = 0;
  306. }
  307. });
  308. if (stop) {
  309. conf.run.upload_status = 0;
  310. }
  311. else {
  312. setTimeout(function () { upload_status(); }, 100);
  313. }
  314. }