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 7.8KB

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