An unfinished system to manage all your paper documentation in an easy way.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

autodoc.js 8.8KB

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