123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- /* AutoDoc Javascript */
-
- var conf = {
- default_hash: "home",
- openapi: "/v1",
- pageinit: {
- home: "page_load_home()"
- },
- home: {
- width: 4,
- curpage: 0,
- end: 0
- }
- };
-
- var temp = {
- upload: [ ],
- documentid: null
- };
-
- $(document).ready(function () {
-
- change_page();
- upload_zone_init();
-
- $(document).scroll(function () {
- page_home_scroll();
- });
- });
-
- $(window).bind("hashchange", function () {
- change_page();
- });
-
- function change_page() {
- var hash = document.location.hash.substr(1) || conf.default_hash;
-
- if (!$("#autodoc_tab_" + hash).length) {
- hash = conf.default_hash;
- }
-
- /* change menu highlights and content visibility */
- $("#autodoc_navbar")
- .find("a")
- .each(function (id, obj) {
- var curhash = $(obj)
- .attr("href")
- .substr(1);
- if (curhash == hash) {
- $(obj)
- .parent()
- .addClass("active");
- $("#autodoc_tab_" + curhash).removeClass("d-none");
- } else {
- $(obj)
- .parent()
- .removeClass("active");
- $("#autodoc_tab_" + curhash).addClass("d-none");
- }
- });
-
- if (conf.pageinit[hash]) {
- eval(conf.pageinit[hash]);
- }
- }
-
- function isvisible(obj) {
- var top_of_fileent = $(obj).offset().top;
- var bottom_of_fileent = $(obj).offset().top + $(obj).outerHeight();
- var bottom_of_screen = $(window).scrollTop() + $(window).innerHeight();
- var top_of_screen = $(window).scrollTop();
-
- if (bottom_of_screen > top_of_fileent && top_of_screen < bottom_of_fileent) {
- return true;
- }
- return false;
- }
-
- function page_load_home() {
- document_load();
- }
-
- function document_load() {
- if (conf.home.curpage == 0) {
- $("#autodoc_home_body").html("");
- }
-
- var tmpl = $("#autodoc_template_home");
- var row = $(tmpl)
- .find(".autodoc_template_home_row")
- .clone();
-
- $.getJSON(
- conf.openapi +
- "/documents?pageSize=" +
- conf.home.width +
- "&pageIndex=" +
- conf.home.curpage,
- null,
- function (data, textStatus, jqXHR) {
- // last document was loaded, stop scrolling
- if (data.length < conf.home.width) {
- conf.home.end = 1;
- }
- $.each(data, function (id, doc) {
- var col = $(tmpl)
- .find(".autodoc_template_home_col")
- .clone();
- $(col)
- .find(".autodoc_template_home_img")
- .attr(
- "src",
- conf.openapi +
- "/documents/" +
- doc.id +
- "/image?maxWidth=" +
- Math.floor(1000 / conf.home.width)
- );
-
- $(col)
- .find(".autodoc_template_home_owner")
- .html(doc.owner);
-
- $(col)
- .find(".autodoc_template_home_created")
- .html(doc.created);
-
- $(col)
- .find(".autodoc_template_home_name")
- .html(doc.name);
-
- $.each(doc.tags, function (id, tag) {
- $(col)
- .find(".autodoc_template_home_tag")
- .after(
- $(col)
- .find(".autodoc_template_home_tag")
- .clone()
- .html(tag)
- );
- });
- $(row).append(col);
- });
- $("#autodoc_home_body").append(row);
- // continue to load until out of browser.
- page_home_scroll();
- }
- );
- }
-
- function page_home_scroll() {
- // don't scroll if we've loaded the last document
- if (conf.home.end) {
- return;
- }
- if (isvisible($(".autodoc_template_home_col").last())) {
- console.log("scroll to page " + conf.home.curpage);
- conf.home.curpage++;
- document_load();
- }
- }
-
- function upload_zone_init() {
- $(window).on("dragover dragleave drop", function(e) { e.preventDefault() });
-
- $('.autodoc_upload_zone').on({
- dragenter: upload_zone_highlight,
- dragleave: upload_zone_normal,
- dragover: upload_zone_over,
- drop: upload_zone_drop
- });
- }
-
- function upload_zone_highlight(e) {
- $(e.target).addClass('autodoc_upload_zone_hover');
- }
-
- function upload_zone_over(e) {
- return true;
- }
-
- function upload_zone_normal(e) {
- $(e.target).removeClass('autodoc_upload_zone_hover');
- }
-
- function upload_zone_drop(e) {
- var ev = e.originalEvent;
- console.log(ev.dataTransfer.files);
-
- if ( temp.documentid == null ) {
- $.post(conf.openapi + "/documents", {}, function(data) {
- temp.upload = [];
- temp.documentid = data.id;
- upload_status();
- handle_uploads(ev);
- });
- }
- else {
- handle_uploads(ev);
- }
- }
-
- function handle_uploads(ev) {
-
- $.each(ev.dataTransfer.files, function(id,file) {
- var fileid = temp.upload.length;
- temp.upload.push({
- name: file.name,
- type: file.type,
- size: file.size,
- progress: 0,
- error: null
- });
-
- if ( file.type != 'application/pdf' && file.type != 'image/png' && file.type != 'image/jpeg' ) {
- temp.upload[fileid]['progress']=1;
- temp.upload[fileid]['error']='invalid file type';
- }
- else {
- var reader = new FileReader();
- var data;
- reader.onloadend = function(e) {
- $.post(conf.openapi + "/documents/" + temp.documentid + "/data", e.result, function(data) {
- console.log(data);
- }, file.type)
- };
- reader.readAsArrayBuffer(data);
-
- }
- });
- return true;
- }
-
- function upload_status() {
- $.each(temp.upload, function(id, file) {
- var obj = $(".autodoc_template_progress").children();
- console.log(obj);
- });
- // setTimeout(upload_status, 1000);
- }
|