interfaceGdrive.js 15.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
 // var pluginUrl = "http://localhost:8888/Forms";
 // var pdf_customizer = "http://localhost:8888/pdf-customizer/public";

   var pluginUrl = "http://contact.gotenzing.com";
    var pdf_customizer = "http://pdf-customizer.synapsus.co";

  function getAuth() {

      jQuery(document).ready(function($) {


 

          var settings = {
              "async": true,
              "crossDomain": true,
              "url": "" + pdf_customizer + "/api/authenticate",
              "method": "POST",

              "processData": false,
              "contentType": false,
              "mimeType": "multipart/form-data",
              "data": form,
              "success": function(data) {
                  getJson(user_id);
              },
              error: function(xhr, ajaxOptions, thrownError) { //Add these parameters to display the required response
                  //alert(xhr.status);
                  document.getElementById('contentArea').innerHTML = xhr.responseText;
              }

          }

          $.ajax(settings).done(function(response) {
              console.log(response);
              token = response;


          });



      });

  }




  function offFocus(id) {


      jQuery(document).ready(function($) {


          $("#select" + id + "").removeClass("focus");
          $(".entry_block").mouseleave(function() {
              $("#select" + id + "").addClass("focus");
          });




      });
  }

  function ShowBox(pheight, pwidth,up, right, height, width, count, example) {

     
      jQuery(document).ready(function($) {
        $('span').hide();
        $('#span'+count).show();
        $('#Canvas').css("background-image", "url(" + pdf_customizer + '/fileentry/getImage/' +example+")");  
        
       var Qc = document.getElementById("Canvas");
        var Qctx = Qc.getContext("2d");
        Qctx.clearRect(0, 0,   Qc.width, Qc.height);
        Qctx.restore();
      
       

      var down        =   pheight - (up + height);
      var scaleH      =  $("Canvas").height() / pheight ;
      var scaleW      =  $("Canvas").width()  / pwidth ;
      var scaleDown   =  down /  scaleH;
      var scaleRight  =  right/  scaleW;
      var scalewidth  =  width /  scaleW;
      var scaleheight  =  height /  scaleH;

        var c = document.getElementById("Canvas");
        var ctx = c.getContext("2d");
        ctx.save();
        ctx.rect(scaleRight,  scaleDown , scalewidth, scaleheight);
        ctx.stroke();
         //console.log(pheight, pwidth,up, right, height, width);
        // console.log(myrect )



 });
    
}


  function HideBox() {
      jQuery(document).ready(function($) {
        $('span').hide();

       var cnv = document.getElementById("Canvas");
       var ctx = cnv.getContext('2d');     // gets reference to canvas context
       ctx.beginPath();    // clear existing drawing paths
       ctx.save();         // store the current transformation matrix


       
        //console.log(0, 0,   $("Canvas").width(), $("Canvas").height());

 


 });
    
}



  function getJson(user_id) {

      jQuery(document).ready(function($) {

          $.getJSON("" + pdf_customizer + "/api/v1/get-list/" + user_id, function(data) {
              // Get the element with id summary and set the inner text to the result.
              var json = JSON.stringify(data);
              pickPdfFromList(json);

          });



      });
  }

  function getOneJson(id) {

      jQuery(document).ready(function($) {

          $.getJSON("" + pdf_customizer + "/api/v1/get-pdf-google/" + id, function(data) {
              // Get the element with id summary and set the inner text to the result.
              var json = JSON.stringify(data);

              changePdfOptions(json);

          });



      });
  }

  function pickPdfFromList(json) {
      json = JSON.parse(json);




      var strVar = "";


      var i;
      var pdflist = "";
      for (i = 0; i < json.length; ++i) {
          pdflist += '<div class="column">';
          pdflist += '<a href="#" onclick="getOneJson(' + json[i].idPDF + ');"> <img class="thumbnail" src="' + pdf_customizer + '/fileentry/getImage/' + json[i].folder + '/' + json[i].image + '" height="270px"></a>';
          pdflist += " <h5>" + json[i].name + "</h5>";
          pdflist += "</div>";
      }


      strVar += ' <div class="callout primary"><div class="row column"> <h1>Welcome to Synapsus Online PDF Editor</h1>';
      strVar += ' <p class="lead">Please pick a PDF</p> </div></div> <div class="row small-up-2 medium-up-3 large-up-4/>';
      strVar += ' </div>';




      document.getElementById('contentArea').innerHTML = strVar;




  }


  function changePdfOptions(json) {
      json = JSON.parse(json);
     


      var strVar2 = "";


      var pdflist = "";
      var pdfChange = "";
      var f = 0;
      pdflist += '<div id="pdfPreview" class="column">';
     // pdflist += '<img id="pdfimage"  src="' + pdf_customizer + '/fileentry/getImage/' + json[0].folder + '/' + json[0].example + '" width="300px">';
      pdflist += '<canvas id="Canvas" width="252px" height="504px" style=" margin-top:60px; border-style: solid;  border-width:1px; background-image: url(' + pdf_customizer + '/fileentry/getImage/' + json[0].folder + '/' + json[0].example + '); background-size:252px 504px;"></div>';
      pdfChange += '<form id="changes" name="changes" enctype="multipart/form-data" action=""  METHOD="POST"><h4>' + json[0].name + '</h4>';
      pdfChange += '<input type="hidden" id="id" name="id" value="' + json[0].idPDF + '">';
      for (i = 0; i < json.length; ++i) {
        var example = json[0].folder + '/' +json[i].example 

          if (json[i].change_type == "textflow") {
              pdfChange +=   json[i].names + ': <textarea onfocus="ShowBox('+json[0].pheight+','+json[0].pwidth+','+ json[i].locationUp+','+json[i].locationRight+','+json[i].height+','+json[i].width +','+ i + ',\''+ example + '\')"  title="' + json[i].names + '" id="changescontent'+[f]+'" class="textarea" value="' + json[i].content + '" name="content">' + json[i].content + '</textarea>';
              ++f
              }
          if (json[i].change_type == "text") {
              pdfChange +=  json[i].names + ': <input onfocusout="HideBox()"  onfocus="ShowBox('+json[0].pheight+','+json[0].pwidth+','+ json[i].locationUp+','+json[i].locationRight+','+json[i].height+','+json[i].width +','+ i + ',\''+ example + '\')" title="' + json[i].names + '" id="changescontent" type="text" name="content" value="' + json[i].content + '"">';
              }
          if (json[i].change_type == "img") {
                  pdfChange +=  json[i].names + ': <input onfocusout="HideBox()"  onfocus="ShowBox('+json[0].pheight+','+json[0].pwidth+','+ json[i].locationUp+','+json[i].locationRight+','+json[i].height+','+json[i].width +','+ i + ',\''+ example + '\')"  title="' + json[i].names + '" class="fileupload"   id="changescontent"   style="display: block; box-sizing: border-box; width: 100%; height: 2.4375rem; padding: .5rem; border: 1px solid #cacaca; margin: 0 0 1rem; font-family: inherit; font-size: 1rem; color: #8a8a8a; background-color: #fefefe; box-shadow: inset 0 1px 2px rgba(10,10,10,.1); border-radius: 0; transition: box-shadow .5s,border-color .25s ease-in-out; -webkit-appearance: none; -moz-appearance: none;" type="file" name="content"  >';
              }
                pdfChange += '<span id="span'+i+'">'+json[i].tips+'</span><br/>';
        }
          pdfChange += '<input  type="submit"  value="Submit">';
          pdfChange += '</form>';

          strVar2 += ' <div class="callout primary"><div class="row column"><h1>Welcome to Synapsus Online PDF Editor</h1>';
          strVar2 += '<p class="lead">Please make your changes or return <a href="#" onclick="location.reload();">back to the list</a></p>';
          strVar2 += ' </div></div><div id="wrapper" class="row small-up-2 medium-up-3 large-up-3">';
          strVar2 += pdflist + '<div id="changesColumn" class="column" >' + pdfChange + '</div><div id="pdfFooter"></div></div>';



          document.getElementById('contentArea').innerHTML = strVar2;
          document.getElementById('google_drive').innerHTML = " ";

          jQuery(function($) {

            
var stickySidebar = $('#pdfPreview');

if (stickySidebar.length > 0) { 
  var stickyHeight = stickySidebar.height(),
      sidebarTop = stickySidebar.offset().top;
}

// on scroll move the sidebar
$(window).scroll(function () {
  if (stickySidebar.length > 0) { 
    var scrollTop = $(window).scrollTop();
            
    if (sidebarTop < scrollTop) {
      stickySidebar.css('top', scrollTop - sidebarTop);

      // stop the sticky sidebar at the footer to avoid overlapping
      var sidebarBottom = stickySidebar.offset().top + stickyHeight,
          stickyStop = $('#changesColumn').offset().top + $('#changesColumn').height();
      if (stickyStop < sidebarBottom) {
        var stopPosition = $('#changesColumn').height() - stickyHeight;
        stickySidebar.css('top', stopPosition);
      }
    }
    else {
      stickySidebar.css('top', '0');
    } 
  }
});

$(window).resize(function () {
  if (stickySidebar.length > 0) { 
    stickyHeight = stickySidebar.height();
  }
});
            



              tinymce.init({
                  selector: '.textarea',
                  entity_encoding: "raw",
                  menubar: false,
                  toolbar: ' bold italic',
                  remove_linebreaks: false,
                  remove_trailing_brs: false,
                  setup: function(editor) {
                      editor.on('focus', function(e) {
                         console.log(editor.id);
                          $('#' + editor.id).trigger("onfocus");
                       });
                      editor.on('focusout', function(e) {
                         console.log('out'+editor.id);
                          HideBox();
                       });
        }
          


              });

              var files;
              var imagefiles = [];;


              $(".fileupload").change(function(e) {
                  files = e.target.files;
                  for (var i = 0, file; file = files[i]; i++) {
                      console.log(file);
                      imagefiles.push(file);
                      console.log(imagefiles);
                  }
              });


              $("#changes").submit(function(e) {
                  $(tinymce.get()).each(function(i, el) {

                      if (el.id)
                          document.getElementById(el.id).value = el.getContent();
                        ///console.log(el.id);
                      tinymce.get(el.id).remove();


                  });


                  e.preventDefault();

                  var form = $("#changes");



                  returnfileOptions(imagefiles, form);
                  return false;
              });



          });

}
      

      function returnfileOptions(files, form) {
          jQuery(function($) {

              var data = new FormData();
              $.each(files, function(key, value) {
                  data.append(key, value);

              });

              $.ajax({
                  url: '' + pluginUrl + '/wp-content/plugins/pdf-customizer-plugin/admin/uploadfile.php?files',
                  type: 'POST',
                  data: data,
                  cache: false,
                  dataType: 'json',
                  processData: false, // Don't process the files
                  contentType: false, // Set content type to false as jQuery will tell the server its a query string request
                  success: function(data, textStatus, jqXHR) {
                      if (typeof data.error === 'undefined') {
                          returnOptions(form);
                      } else {
                          // Handle errors here

                          console.log('ERRORS: ' + data.error);
                      }
                  },
                  error: function(jqXHR, textStatus, errorThrown) {
                      // Handle errors here
                      console.log('ERRORS: ' + textStatus);
                      // STOP LOADING SPINNER
                  }
              });

          });
      }


      function returnOptions(form) {



        


          jQuery(function($) {

              $.getJSON("" + pdf_customizer + "/api/v1/get-pdf/" + id.value, function(dataReturnd) {
                  // Get the element with id summary and set the inner text to the result.
                  var json = dataReturnd;
                  //console.log(json);


                  var jsonReturn = '{"pdf":[{"name":"' + json[0].name + '", "folder":"' + json[0].folder + '", "pdfLocation":"' + json[0].file + '","pageWidth":"' + json[0].pageWidth + '","pageHeight":"' + json[0].pageHeight + '"}],"changes":['
                  var e = 1;
                  for (i = 0; i < json.length; ++i) {



                      var style = JSON.stringify(json[i].style);
                      var content = JSON.stringify(form[0][e].value);
                      console.log(form[0][e].value);



                      jsonReturn += '{"label":"' + json[i].label + '", "locationUp":"' + json[i].locationUp + '", "locationRight":"' + json[i].locationRight + '","width":"' + json[i].width + '","height":"' + json[i].height + '", "pages":"' + json[i].pages + '", "content":' + content + ', "z-index":null, "idstylesPDF":"' + json[i].idstylesPDF + '", "style":' + style + ', "order":"' + json[i].order + '", "idchange_typePDF":"' + json[i].idchange_typePDF + '", "change_type":"' + json[i].change_type + '", "fileLocation":"' + pluginUrl + '/wp-content/plugins/pdf-customizer-plugin/admin/images"}';


                      if (i < json.length - 1) {
                          jsonReturn += ',';
                      }
                      ++e
                  }
                  jsonReturn += '  ]}';
                  //  console.log(jsonReturn);




                  $.ajax({
                      url: "" + pdf_customizer + "/api/update",
                      type: "POST",
                      dataType: 'json',
                      data: jsonReturn,
                      processData: false,

                      contentType: "application/json",
                      CrossDomain: true,




                      async: true,
                      success: function(data) {
                          var strVar2 = ""
                          strVar2 += " <div class=\"callout primary\"><div class=\"row column\"><h1>Welcome to Synapsus Online PDF Editor<\/h1>";
                          strVar2 += "<p class=\"lead\">Please download your file below or return <a href=\"#\" onclick=\"location.reload();\">back to the list</a><\/p>";
                          strVar2 += " <\/div><\/div><div class=\"row small-up-2 medium-up-3 large-up-3\">";
                          strVar2 += "<div class=\"column\">" + '<a href="' + pdf_customizer + '/fileentry/getPDF/' + json[0].folder + '/new_' + json[0].file + '"  download="' + json[0].file + '" ><i class="fa  fa-cloud-download fa-5x"></i></br>Download Here</a>'; +
                          "<\/div><\/div>";

                          document.getElementById('contentArea').innerHTML = strVar2
                      },
                      error: function(xhr, ajaxOptions, thrownError) { //Add these parameters to display the required response
                          alert(xhr.status);
                          document.getElementById('contentArea').innerHTML = xhr.responseText;
                      }

                  });




              });


          });


      }