source: prototipo_portal_2018/prototipo/static/js/docsDraggable.js @ a9c1355

Last change on this file since a9c1355 was a9c1355, checked in by José Sulbarán <jsulbaran@…>, 7 years ago

Realizando la mezcla

  • Property mode set to 100644
File size: 5.0 KB
Line 
1
2
3var __PDF_DOC,
4  __CURRENT_PAGE,
5  __TOTAL_PAGES,
6  __PAGE_RENDERING_IN_PROGRESS = 0,
7  __CANVAS = $('#pdf-canvas2').get(0),
8  __CANVAS_CTX = __CANVAS.getContext('2d'),
9  __id = "",
10  __NUMBER = 0;
11
12 $("#pdf-canvas2").hide();
13function showPDF2(pdf_url) {
14  $("#pdf-loader2").show();
15
16  PDFJS.getDocument({ url: pdf_url }).then(function(pdf_doc) {
17    __PDF_DOC = pdf_doc;
18    __TOTAL_PAGES = __PDF_DOC.numPages;
19   
20    // Hide the pdf loader and show pdf container in HTML
21    $("#pdf-loader2").hide();
22    $("#pdf-contents2").show();
23    $("#pdf-total-pages2").text(__TOTAL_PAGES);
24
25    // Show the
26      showPage2(1);
27 
28  }).catch(function(error) {
29    // If error re-show the upload button
30    $("#pdf-loader2").hide();
31    $("#upload-button2").show();
32   
33    alert(error.message);
34  });;
35}
36
37function showPage2(page_no) {
38  __PAGE_RENDERING_IN_PROGRESS = 1;
39  __CURRENT_PAGE = page_no;
40
41  // Disable Prev & Next buttons while page is being loaded
42  $("#pdf-next2, #pdf-prev2").attr('disabled', 'disabled');
43
44  // While page is being rendered hide the canvas and show a loading message
45  $("#pdf-canvas2").hide();
46  $("#page-loader2").show();
47
48  // Update current page in HTML
49  $("#pdf-current-page2").text(page_no);
50  $("#pagepdf").val(page_no);
51 
52  // Fetch the page
53  __PDF_DOC.getPage(page_no).then(function(page) {
54    // As the canvas is of a fixed width we need to set the scale of the viewport accordingly
55
56
57    var scale_required = __CANVAS.width / page.getViewport(1).width;
58
59    // Get viewport of the page at required scale
60    var viewport = page.getViewport(scale_required);
61
62    // Set canvas height
63
64    __CANVAS.height = viewport.height;
65
66
67    var renderContext = {
68      canvasContext: __CANVAS_CTX,
69      viewport: viewport
70    };
71
72
73    // Render the page contents in the canvas
74    page.render(renderContext).then(function() {
75      __PAGE_RENDERING_IN_PROGRESS = 0;
76
77      // Re-enable Prev & Next buttons
78      $("#pdf-next2, #pdf-prev2").removeAttr('disabled');
79
80      // Show the canvas and hide the page loader
81      $("#page-loader2").hide();
82
83        __id = __CANVAS.toDataURL()
84       
85      var viewdimen = page.getViewport(1);
86      console.log( "Width: " + viewdimen.width + ", Height: " + viewdimen.height);     
87
88      $("#container1").attr("style", "background-image: url('"+__id+"');background-size: cover; background-repeat: no-repeat;  height:"+viewdimen.height+"px; width:"+viewdimen.width+"px");     
89      $("#container1").attr("style", "background-image: url('"+__id+"');background-size: cover; background-repeat: no-repeat;  height:"+(viewdimen.height)+"px; width:"+(viewdimen.width)+"px");
90     
91
92    });
93  });
94
95
96}
97
98// Upon click this should should trigger click on the #file-to-upload file input element
99// This is better than showing the not-good-looking file input element
100$("#upload-button2").on('click', function() {
101  //$("#img").removeAttr('src');
102  //$("#img").removeAttr('class');
103  //$(".cropper-container").remove();
104  $("#pdf-main-container2").hide(); 
105  $("#pdf-main-container").show(); 
106
107  $("#file-to-upload2").trigger('click');
108
109});
110
111// When user chooses a PDF file
112$("#file-to-upload2").on('change', function() {
113
114  // Validate whether PDF
115    if(['application/pdf'].indexOf($("#file-to-upload2").get(0).files[0].type) == -1) {
116        alert('Error : Not a PDF');
117        return;
118    }
119
120
121  // Send the object url of the pdf
122  showPDF2(URL.createObjectURL($("#file-to-upload2").get(0).files[0]));
123
124});
125
126// Previous page of the PDF
127$("#pdf-prev2").on('click', function() {
128  if(__CURRENT_PAGE != 1)
129    showPage2(--__CURRENT_PAGE);
130});
131
132// Next page of the PDF
133$("#pdf-next2").on('click', function() {
134   
135
136  if(__CURRENT_PAGE != __TOTAL_PAGES)
137   
138    showPage2(++__CURRENT_PAGE);
139
140});
141
142
143var transferred = false;
144$('.text').draggable({
145    connectToSortable: $('#container1'),
146    helper: 'clone',
147    start: function(event, ui)
148    {
149        $(this).hide();
150    },
151    stop: function(event, ui)
152    {
153
154        if(!transferred) {
155            $(this).show();
156        }
157        else{ 
158       
159            $(this).remove();
160            transferred = false;
161        }
162    }
163});
164
165
166function InicializarObjectoPositicion(){
167  var container = document.getElementById('container1'),
168    element = document.getElementsByClassName('text')[0];
169 
170  // options
171  var options = {
172    limit: container,
173    setCursor: true
174  };
175
176  // initialize drag
177  new Draggable(element, options); 
178
179}
180
181
182function NuevoObjectoInicial(){
183
184  var num = 0;
185  $( ".text" ).draggable({ 
186    containment: '#container1',
187    scroll: false,
188    stop: function(event, ui)
189      {
190       
191        num+= 1;
192        if (num == 1){
193          $("#firma_visible #Formato_Visible .text").hide();
194          $('#dataX').val(0);
195          $('#dataY').val(0);
196        }else{
197          $('#dataX').val(ui.position['left']);
198          $('#dataY').val(ui.position['top']);
199        }
200       
201      },
202   
203  });
204}
205
206$('#container1').sortable({
207    receive: function(event, ui)
208    { 
209       
210        transferred = true;
211        InicializarObjectoPositicion()
212        NuevoObjectoInicial();
213
214       
215    }
216});
Note: See TracBrowser for help on using the repository browser.