7 * @class Roo.bootstrap.DocumentViewer
8 * @extends Roo.bootstrap.Component
9 * Bootstrap DocumentViewer class
10 * @cfg {Number} thumbWidth default 300
11 * @cfg {Number} thumbHeight default 300
14 * Create a new DocumentViewer
15 * @param {Object} config The config object
18 Roo.bootstrap.DocumentViewer = function(config){
19 Roo.bootstrap.DocumentViewer.superclass.constructor.call(this, config);
24 * Fire after initEvent
25 * @param {Roo.bootstrap.DocumentViewer} this
32 Roo.extend(Roo.bootstrap.DocumentViewer, Roo.bootstrap.Component, {
37 getAutoCreate : function()
41 cls : 'roo-document-viewer',
45 cls : 'roo-document-viewer-body',
49 cls : 'roo-document-viewer-preview'
53 cls : 'roo-document-viewer-thumb',
54 style : 'width: ' + this.thumbWidth + 'px; height: ' + this.thumbHeight + 'px;'
60 cls : 'roo-document-viewer-footer',
63 cls : 'btn-group btn-group-justified roo-document-viewer-btn-group',
71 cls : 'btn btn-default roo-document-viewer-delete',
72 html : '<i class="fa fa-trash"></i>'
85 initEvents : function()
88 this.bodyEl = this.el.select('.roo-document-viewer-body', true).first();
89 this.bodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
91 this.previewEl = this.el.select('.roo-document-viewer-preview', true).first();
92 this.previewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
94 this.thumbEl = this.el.select('.roo-document-viewer-thumb', true).first();
95 this.thumbEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
97 this.footerEl = this.el.select('.roo-document-viewer-footer', true).first();
98 this.footerEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
102 this.fireEvent('initial', this);
109 window.addEventListener("resize", function() { _this.resize(); } );
112 this.bodyEl.on('click', this.onClick, this);
114 this.pictureBtn.on('click', this.beforeSelectFile, this);
116 this.rotateLeft.on('click', this.onRotateLeft, this);
118 this.rotateRight.on('click', this.onRotateRight, this);
128 this.dragable = false;
129 this.pinching = false;
132 this.cropData = false;
138 this.setThumbBoxPosition();
139 this.setCanvasPosition();
142 beforeSelectFile : function(e)
146 this.fireEvent('beforeselectfile', this);
149 loadCanvas : function(src)
151 if(this.fireEvent('beforeloadcanvas', this, src) != false){
155 this.imageEl = document.createElement('img');
159 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
161 this.imageEl.src = src;
165 onLoadCanvas : function()
167 if(this.bodyHasOnClickEvent){
168 this.bodyEl.un('click', this.beforeSelectFile, this);
169 this.bodyHasOnClickEvent = false;
172 this.notifyEl.hide();
174 this.footerEl.show();
176 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
177 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
179 this.setThumbBoxPosition();
180 this.baseRotateLevel();
181 this.baseScaleLevel();
187 setCanvasPosition : function()
189 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
190 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
192 this.previewEl.setLeft(pw);
193 this.previewEl.setTop(ph);
196 onMouseDown : function(e)
200 this.dragable = true;
201 this.pinching = false;
203 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
204 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
208 onMouseMove : function(e)
216 var minX = Math.ceil(this.thumbEl.getLeft(true));
217 var minY = Math.ceil(this.thumbEl.getTop(true));
219 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
220 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
222 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
223 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
228 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
229 var bgY = Math.ceil(y + this.previewEl.getTop(true));
231 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
232 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
234 this.previewEl.setLeft(bgX);
235 this.previewEl.setTop(bgY);
237 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
238 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
241 onMouseUp : function(e)
245 this.dragable = false;
248 onMouseWheel : function(e)
252 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
254 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel());
255 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel());
258 e.getWheelDelta() == -1 &&
261 (this.rotate == 0 || this.rotate == 180) && (width < this.thumbEl.getWidth() || height < this.thumbEl.getHeight())
265 (this.rotate == 90 || this.rotate == 270) && (height < this.thumbEl.getWidth() || width < this.thumbEl.getHeight())
269 this.scale = (e.getWheelDelta() == 1) ? (this.scale - 1) : (this.scale + 1);
276 onRotateLeft : function(e)
282 (this.rotate == 0 || this.rotate == 180)
284 (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())
288 (this.rotate == 90 || this.rotate == 270)
290 (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())
297 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
303 onRotateRight : function(e)
309 (this.rotate == 0 || this.rotate == 180)
311 (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())
315 (this.rotate == 90 || this.rotate == 270)
317 (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())
324 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
332 this.previewEl.dom.innerHTML = '';
334 var canvasEl = document.createElement("canvas");
336 var contextEl = canvasEl.getContext("2d");
338 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
339 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
340 var center = this.imageEl.OriginWidth / 2;
342 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
343 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
344 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
345 center = this.imageEl.OriginHeight / 2;
348 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
350 contextEl.translate(center, center);
351 contextEl.rotate(this.rotate * Math.PI / 180);
353 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
355 this.canvasEl = document.createElement("canvas");
357 this.contextEl = this.canvasEl.getContext("2d");
359 switch (this.rotate) {
362 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
363 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
365 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
370 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
371 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
373 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
374 this.contextEl.drawImage(canvasEl, Math.abs(this.canvasEl.width - this.canvasEl.height), 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
378 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
383 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
384 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
386 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
387 this.contextEl.drawImage(canvasEl, 0, Math.abs(this.canvasEl.width - this.canvasEl.height), this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
391 this.contextEl.drawImage(canvasEl, Math.abs(this.canvasEl.width - this.canvasEl.height), 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
396 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
397 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
399 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
400 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
404 this.contextEl.drawImage(canvasEl, 0, Math.abs(this.canvasEl.width - this.canvasEl.height), this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
411 this.previewEl.appendChild(this.canvasEl);
413 this.setCanvasPosition();
418 var canvas = document.createElement("canvas");
420 var context = canvas.getContext("2d");
422 canvas.width = this.minWidth;
423 canvas.height = this.minHeight;
425 var cropWidth = this.thumbEl.getWidth();
426 var cropHeight = this.thumbEl.getHeight();
428 var x = this.thumbEl.getLeft(true) - this.previewEl.getLeft(true);
429 var y = this.thumbEl.getTop(true) - this.previewEl.getTop(true);
431 if(this.canvasEl.width - cropWidth < x){
432 x = this.canvasEl.width - cropWidth;
435 if(this.canvasEl.height - cropHeight < y){
436 y = this.canvasEl.height - cropHeight;
442 context.drawImage(this.canvasEl, x, y, cropWidth, cropHeight, 0, 0, canvas.width, canvas.height);
444 this.cropData = canvas.toDataURL(this.cropType);
446 this.fireEvent('crop', this, this.cropData);
450 setThumbBoxSize : function()
453 var width = Math.ceil(this.minWidth * height / this.minHeight);
455 if(this.minWidth > this.minHeight){
457 height = Math.ceil(this.minHeight * width / this.minWidth);
460 this.thumbEl.setStyle({
461 width : width + 'px',
462 height : height + 'px'
469 setThumbBoxPosition : function()
471 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
472 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
474 this.thumbEl.setLeft(x);
475 this.thumbEl.setTop(y);
479 baseRotateLevel : function()
484 typeof(this.exif) != 'undefined' &&
485 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
486 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
488 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
491 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
495 baseScaleLevel : function()
499 if(this.baseRotate == 6 || this.baseRotate == 8){
501 width = this.thumbEl.getHeight();
502 this.baseScale = height / this.imageEl.OriginHeight;
504 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
505 height = this.thumbEl.getWidth();
506 this.baseScale = height / this.imageEl.OriginHeight;
509 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
510 height = this.thumbEl.getWidth();
511 this.baseScale = height / this.imageEl.OriginHeight;
513 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
514 width = this.thumbEl.getHeight();
515 this.baseScale = width / this.imageEl.OriginWidth;
522 width = this.thumbEl.getWidth();
523 this.baseScale = width / this.imageEl.OriginWidth;
525 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
526 height = this.thumbEl.getHeight();
527 this.baseScale = height / this.imageEl.OriginHeight;
531 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
533 height = this.thumbEl.getHeight();
534 this.baseScale = height / this.imageEl.OriginHeight;
536 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
537 width = this.thumbEl.getWidth();
538 this.baseScale = width / this.imageEl.OriginWidth;
546 getScaleLevel : function()
548 return this.baseScale * Math.pow(1.1, this.scale);
551 onTouchStart : function(e)
555 var touches = e.browserEvent.touches;
561 if(touches.length == 1){
566 if(touches.length != 2){
572 for(var i = 0, finger; finger = touches[i]; i++){
573 coords.push(finger.pageX, finger.pageY);
576 var x = Math.pow(coords[0] - coords[2], 2);
577 var y = Math.pow(coords[1] - coords[3], 2);
579 this.startDistance = Math.sqrt(x + y);
581 this.startScale = this.scale;
583 this.pinching = true;
584 this.dragable = false;
588 onTouchMove : function(e)
592 if(!this.pinching && !this.dragable){
596 var touches = e.browserEvent.touches;
609 for(var i = 0, finger; finger = touches[i]; i++){
610 coords.push(finger.pageX, finger.pageY);
613 var x = Math.pow(coords[0] - coords[2], 2);
614 var y = Math.pow(coords[1] - coords[3], 2);
616 this.endDistance = Math.sqrt(x + y);
618 var scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
620 var width = Math.ceil(this.imageEl.OriginWidth * this.baseScale * Math.pow(1.1, scale));
621 var height = Math.ceil(this.imageEl.OriginHeight * this.baseScale * Math.pow(1.1, scale));
624 this.endDistance / this.startDistance < 1 &&
627 (this.rotate == 0 || this.rotate == 180) && (width < this.thumbEl.getWidth() || height < this.thumbEl.getHeight())
631 (this.rotate == 90 || this.rotate == 270) && (height < this.thumbEl.getWidth() || width < this.thumbEl.getHeight())
644 onTouchEnd : function(e)
648 this.pinching = false;
649 this.dragable = false;
653 prepare : function(input)
658 if(typeof(input) === 'string'){
659 this.loadCanvas(input);
663 if(!input.files || !input.files[0] || !this.urlAPI){
667 this.file = input.files[0];
668 this.cropType = this.file.type;
672 if(this.fireEvent('prepare', this, this.file) != false){
674 var reader = new FileReader();
676 reader.onload = function (e) {
677 if (e.target.error) {
678 Roo.log(e.target.error);
682 var buffer = e.target.result,
683 dataView = new DataView(buffer),
685 maxOffset = dataView.byteLength - 4,
689 if (dataView.getUint16(0) === 0xffd8) {
690 while (offset < maxOffset) {
691 markerBytes = dataView.getUint16(offset);
693 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
694 markerLength = dataView.getUint16(offset + 2) + 2;
695 if (offset + markerLength > dataView.byteLength) {
696 Roo.log('Invalid meta data: Invalid segment size.');
700 if(markerBytes == 0xffe1){
708 offset += markerLength;
718 var url = _this.urlAPI.createObjectURL(_this.file);
720 _this.loadCanvas(url);
725 reader.readAsArrayBuffer(this.file);
731 parseExifData : function(dataView, offset, length)
733 var tiffOffset = offset + 10,
737 if (dataView.getUint32(offset + 4) !== 0x45786966) {
738 // No Exif data, might be XMP data instead
742 // Check for the ASCII code for "Exif" (0x45786966):
743 if (dataView.getUint32(offset + 4) !== 0x45786966) {
744 // No Exif data, might be XMP data instead
747 if (tiffOffset + 8 > dataView.byteLength) {
748 Roo.log('Invalid Exif data: Invalid segment size.');
751 // Check for the two null bytes:
752 if (dataView.getUint16(offset + 8) !== 0x0000) {
753 Roo.log('Invalid Exif data: Missing byte alignment offset.');
756 // Check the byte alignment:
757 switch (dataView.getUint16(tiffOffset)) {
762 littleEndian = false;
765 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
768 // Check for the TIFF tag marker (0x002A):
769 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
770 Roo.log('Invalid Exif data: Missing TIFF marker.');
773 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
774 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
779 tiffOffset + dirOffset,
784 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
789 if (dirOffset + 6 > dataView.byteLength) {
790 Roo.log('Invalid Exif data: Invalid directory offset.');
793 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
794 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
795 if (dirEndOffset + 4 > dataView.byteLength) {
796 Roo.log('Invalid Exif data: Invalid directory size.');
799 for (i = 0; i < tagsNumber; i += 1) {
803 dirOffset + 2 + 12 * i, // tag offset
807 // Return the offset to the next directory:
808 return dataView.getUint32(dirEndOffset, littleEndian);
811 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
813 var tag = dataView.getUint16(offset, littleEndian);
815 this.exif[tag] = this.getExifValue(
819 dataView.getUint16(offset + 2, littleEndian), // tag type
820 dataView.getUint32(offset + 4, littleEndian), // tag length
825 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
827 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
836 Roo.log('Invalid Exif data: Invalid tag type.');
840 tagSize = tagType.size * length;
841 // Determine if the value is contained in the dataOffset bytes,
842 // or if the value at the dataOffset is a pointer to the actual data:
843 dataOffset = tagSize > 4 ?
844 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
845 if (dataOffset + tagSize > dataView.byteLength) {
846 Roo.log('Invalid Exif data: Invalid data offset.');
850 return tagType.getValue(dataView, dataOffset, littleEndian);
853 for (i = 0; i < length; i += 1) {
854 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
859 // Concatenate the chars:
860 for (i = 0; i < values.length; i += 1) {
862 // Ignore the terminating NULL byte(s):
863 if (c === '\u0000') {
875 Roo.apply(Roo.bootstrap.UploadCropbox, {
877 'Orientation': 0x0112
883 3: 180, //'bottom-right',
886 6: 90, //'right-top',
887 // 7: 'right-bottom',
888 8: 270 //'left-bottom'
892 // byte, 8-bit unsigned int:
894 getValue: function (dataView, dataOffset) {
895 return dataView.getUint8(dataOffset);
899 // ascii, 8-bit byte:
901 getValue: function (dataView, dataOffset) {
902 return String.fromCharCode(dataView.getUint8(dataOffset));
907 // short, 16 bit int:
909 getValue: function (dataView, dataOffset, littleEndian) {
910 return dataView.getUint16(dataOffset, littleEndian);
916 getValue: function (dataView, dataOffset, littleEndian) {
917 return dataView.getUint32(dataOffset, littleEndian);
921 // rational = two long values, first is numerator, second is denominator:
923 getValue: function (dataView, dataOffset, littleEndian) {
924 return dataView.getUint32(dataOffset, littleEndian) /
925 dataView.getUint32(dataOffset + 4, littleEndian);
929 // slong, 32 bit signed int:
931 getValue: function (dataView, dataOffset, littleEndian) {
932 return dataView.getInt32(dataOffset, littleEndian);
936 // srational, two slongs, first is numerator, second is denominator:
938 getValue: function (dataView, dataOffset, littleEndian) {
939 return dataView.getInt32(dataOffset, littleEndian) /
940 dataView.getInt32(dataOffset + 4, littleEndian);