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

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