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

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