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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. console.log(ev.dataTransfer.files);
  162. if ( temp.documentid == null ) {
  163. $.post(conf.openapi + "/documents", {}, function(data) {
  164. temp.upload = [];
  165. temp.documentid = data.id;
  166. upload_status();
  167. handle_uploads(ev);
  168. });
  169. }
  170. else {
  171. handle_uploads(ev);
  172. }
  173. }
  174. function handle_uploads(ev) {
  175. $.each(ev.dataTransfer.files, function(id,file) {
  176. var fileid = temp.upload.length;
  177. temp.upload.push({
  178. name: file.name,
  179. type: file.type,
  180. size: file.size,
  181. progress: 0,
  182. error: null
  183. });
  184. if ( file.type != 'application/pdf' && file.type != 'image/png' && file.type != 'image/jpeg' ) {
  185. temp.upload[fileid]['progress']=1;
  186. temp.upload[fileid]['error']='invalid file type';
  187. }
  188. else {
  189. var reader = new FileReader();
  190. var data;
  191. reader.onloadend = function(e) {
  192. $.post(conf.openapi + "/documents/" + temp.documentid + "/data", e.result, function(data) {
  193. console.log(data);
  194. }, file.type)
  195. };
  196. reader.readAsArrayBuffer(data);
  197. }
  198. });
  199. return true;
  200. }
  201. function upload_status() {
  202. $.each(temp.upload, function(id, file) {
  203. var obj = $(".autodoc_template_progress").children();
  204. console.log(obj);
  205. });
  206. // setTimeout(upload_status, 1000);
  207. }