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-trash',
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';
100 this.trashBtn = this.el.select('.roo-upload-cropbox-picture', true).first();
101 this.pictureBtn.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
105 this.fireEvent('initial', this);
112 window.addEventListener("resize", function() { _this.resize(); } );
115 this.bodyEl.on('click', this.onClick, this);
117 this.pictureBtn.on('click', this.beforeSelectFile, this);
119 this.rotateLeft.on('click', this.onRotateLeft, this);
121 this.rotateRight.on('click', this.onRotateRight, this);
131 this.dragable = false;
132 this.pinching = false;
135 this.cropData = false;
141 this.setThumbBoxPosition();
142 this.setCanvasPosition();
145 beforeSelectFile : function(e)
149 this.fireEvent('beforeselectfile', this);
152 loadCanvas : function(src)
154 if(this.fireEvent('beforeloadcanvas', this, src) != false){
158 this.imageEl = document.createElement('img');
162 this.imageEl.addEventListener("load", function(){ _this.onLoadCanvas(); });
164 this.imageEl.src = src;
168 onLoadCanvas : function()
170 if(this.bodyHasOnClickEvent){
171 this.bodyEl.un('click', this.beforeSelectFile, this);
172 this.bodyHasOnClickEvent = false;
175 this.notifyEl.hide();
177 this.footerEl.show();
179 this.imageEl.OriginWidth = this.imageEl.naturalWidth || this.imageEl.width;
180 this.imageEl.OriginHeight = this.imageEl.naturalHeight || this.imageEl.height;
182 this.setThumbBoxPosition();
183 this.baseRotateLevel();
184 this.baseScaleLevel();
190 setCanvasPosition : function()
192 var pw = Math.ceil((this.bodyEl.getWidth() - this.canvasEl.width) / 2);
193 var ph = Math.ceil((this.bodyEl.getHeight() - this.canvasEl.height) / 2);
195 this.previewEl.setLeft(pw);
196 this.previewEl.setTop(ph);
199 onMouseDown : function(e)
203 this.dragable = true;
204 this.pinching = false;
206 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
207 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
211 onMouseMove : function(e)
219 var minX = Math.ceil(this.thumbEl.getLeft(true));
220 var minY = Math.ceil(this.thumbEl.getTop(true));
222 var maxX = Math.ceil(minX + this.thumbEl.getWidth() - this.canvasEl.width);
223 var maxY = Math.ceil(minY + this.thumbEl.getHeight() - this.canvasEl.height);
225 var x = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
226 var y = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
231 var bgX = Math.ceil(x + this.previewEl.getLeft(true));
232 var bgY = Math.ceil(y + this.previewEl.getTop(true));
234 bgX = (minX < bgX) ? minX : ((maxX > bgX) ? maxX : bgX);
235 bgY = (minY < bgY) ? minY : ((maxY > bgY) ? maxY : bgY);
237 this.previewEl.setLeft(bgX);
238 this.previewEl.setTop(bgY);
240 this.mouseX = Roo.isTouch ? e.browserEvent.touches[0].pageX : e.getPageX();
241 this.mouseY = Roo.isTouch ? e.browserEvent.touches[0].pageY : e.getPageY();
244 onMouseUp : function(e)
248 this.dragable = false;
251 onMouseWheel : function(e)
255 this.scale = (e.getWheelDelta() == 1) ? (this.scale + 1) : (this.scale - 1);
257 var width = Math.ceil(this.imageEl.OriginWidth * this.getScaleLevel());
258 var height = Math.ceil(this.imageEl.OriginHeight * this.getScaleLevel());
261 e.getWheelDelta() == -1 &&
264 (this.rotate == 0 || this.rotate == 180) && (width < this.thumbEl.getWidth() || height < this.thumbEl.getHeight())
268 (this.rotate == 90 || this.rotate == 270) && (height < this.thumbEl.getWidth() || width < this.thumbEl.getHeight())
272 this.scale = (e.getWheelDelta() == 1) ? (this.scale - 1) : (this.scale + 1);
279 onRotateLeft : function(e)
285 (this.rotate == 0 || this.rotate == 180)
287 (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())
291 (this.rotate == 90 || this.rotate == 270)
293 (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())
300 this.rotate = (this.rotate < 90) ? 270 : this.rotate - 90;
306 onRotateRight : function(e)
312 (this.rotate == 0 || this.rotate == 180)
314 (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())
318 (this.rotate == 90 || this.rotate == 270)
320 (this.canvasEl.height < this.thumbEl.getWidth() || this.canvasEl.width < this.thumbEl.getHeight())
327 this.rotate = (this.rotate > 180) ? 0 : this.rotate + 90;
335 this.previewEl.dom.innerHTML = '';
337 var canvasEl = document.createElement("canvas");
339 var contextEl = canvasEl.getContext("2d");
341 canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
342 canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
343 var center = this.imageEl.OriginWidth / 2;
345 if(this.imageEl.OriginWidth < this.imageEl.OriginHeight){
346 canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
347 canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
348 center = this.imageEl.OriginHeight / 2;
351 contextEl.scale(this.getScaleLevel(), this.getScaleLevel());
353 contextEl.translate(center, center);
354 contextEl.rotate(this.rotate * Math.PI / 180);
356 contextEl.drawImage(this.imageEl, 0, 0, this.imageEl.OriginWidth, this.imageEl.OriginHeight, center * -1, center * -1, this.imageEl.OriginWidth, this.imageEl.OriginHeight);
358 this.canvasEl = document.createElement("canvas");
360 this.contextEl = this.canvasEl.getContext("2d");
362 switch (this.rotate) {
365 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
366 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
368 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
373 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
374 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
376 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
377 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);
381 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
386 this.canvasEl.width = this.imageEl.OriginWidth * this.getScaleLevel();
387 this.canvasEl.height = this.imageEl.OriginHeight * this.getScaleLevel();
389 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
390 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);
394 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);
399 this.canvasEl.width = this.imageEl.OriginHeight * this.getScaleLevel();
400 this.canvasEl.height = this.imageEl.OriginWidth * this.getScaleLevel();
402 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
403 this.contextEl.drawImage(canvasEl, 0, 0, this.canvasEl.width, this.canvasEl.height, 0, 0, this.canvasEl.width, this.canvasEl.height);
407 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);
414 this.previewEl.appendChild(this.canvasEl);
416 this.setCanvasPosition();
421 var canvas = document.createElement("canvas");
423 var context = canvas.getContext("2d");
425 canvas.width = this.minWidth;
426 canvas.height = this.minHeight;
428 var cropWidth = this.thumbEl.getWidth();
429 var cropHeight = this.thumbEl.getHeight();
431 var x = this.thumbEl.getLeft(true) - this.previewEl.getLeft(true);
432 var y = this.thumbEl.getTop(true) - this.previewEl.getTop(true);
434 if(this.canvasEl.width - cropWidth < x){
435 x = this.canvasEl.width - cropWidth;
438 if(this.canvasEl.height - cropHeight < y){
439 y = this.canvasEl.height - cropHeight;
445 context.drawImage(this.canvasEl, x, y, cropWidth, cropHeight, 0, 0, canvas.width, canvas.height);
447 this.cropData = canvas.toDataURL(this.cropType);
449 this.fireEvent('crop', this, this.cropData);
453 setThumbBoxSize : function()
456 var width = Math.ceil(this.minWidth * height / this.minHeight);
458 if(this.minWidth > this.minHeight){
460 height = Math.ceil(this.minHeight * width / this.minWidth);
463 this.thumbEl.setStyle({
464 width : width + 'px',
465 height : height + 'px'
472 setThumbBoxPosition : function()
474 var x = Math.ceil((this.bodyEl.getWidth() - this.thumbEl.getWidth()) / 2 );
475 var y = Math.ceil((this.bodyEl.getHeight() - this.thumbEl.getHeight()) / 2);
477 this.thumbEl.setLeft(x);
478 this.thumbEl.setTop(y);
482 baseRotateLevel : function()
487 typeof(this.exif) != 'undefined' &&
488 typeof(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != 'undefined' &&
489 [1, 3, 6, 8].indexOf(this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']]) != -1
491 this.baseRotate = this.exif[Roo.bootstrap.UploadCropbox['tags']['Orientation']];
494 this.rotate = Roo.bootstrap.UploadCropbox['Orientation'][this.baseRotate];
498 baseScaleLevel : function()
502 if(this.baseRotate == 6 || this.baseRotate == 8){
504 width = this.thumbEl.getHeight();
505 this.baseScale = height / this.imageEl.OriginHeight;
507 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getWidth()){
508 height = this.thumbEl.getWidth();
509 this.baseScale = height / this.imageEl.OriginHeight;
512 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
513 height = this.thumbEl.getWidth();
514 this.baseScale = height / this.imageEl.OriginHeight;
516 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getHeight()){
517 width = this.thumbEl.getHeight();
518 this.baseScale = width / this.imageEl.OriginWidth;
525 width = this.thumbEl.getWidth();
526 this.baseScale = width / this.imageEl.OriginWidth;
528 if(this.imageEl.OriginHeight * this.baseScale < this.thumbEl.getHeight()){
529 height = this.thumbEl.getHeight();
530 this.baseScale = height / this.imageEl.OriginHeight;
534 if(this.imageEl.OriginWidth > this.imageEl.OriginHeight){
536 height = this.thumbEl.getHeight();
537 this.baseScale = height / this.imageEl.OriginHeight;
539 if(this.imageEl.OriginWidth * this.baseScale < this.thumbEl.getWidth()){
540 width = this.thumbEl.getWidth();
541 this.baseScale = width / this.imageEl.OriginWidth;
549 getScaleLevel : function()
551 return this.baseScale * Math.pow(1.1, this.scale);
554 onTouchStart : function(e)
558 var touches = e.browserEvent.touches;
564 if(touches.length == 1){
569 if(touches.length != 2){
575 for(var i = 0, finger; finger = touches[i]; i++){
576 coords.push(finger.pageX, finger.pageY);
579 var x = Math.pow(coords[0] - coords[2], 2);
580 var y = Math.pow(coords[1] - coords[3], 2);
582 this.startDistance = Math.sqrt(x + y);
584 this.startScale = this.scale;
586 this.pinching = true;
587 this.dragable = false;
591 onTouchMove : function(e)
595 if(!this.pinching && !this.dragable){
599 var touches = e.browserEvent.touches;
612 for(var i = 0, finger; finger = touches[i]; i++){
613 coords.push(finger.pageX, finger.pageY);
616 var x = Math.pow(coords[0] - coords[2], 2);
617 var y = Math.pow(coords[1] - coords[3], 2);
619 this.endDistance = Math.sqrt(x + y);
621 var scale = this.startScale + Math.floor(Math.log(this.endDistance / this.startDistance) / Math.log(1.1));
623 var width = Math.ceil(this.imageEl.OriginWidth * this.baseScale * Math.pow(1.1, scale));
624 var height = Math.ceil(this.imageEl.OriginHeight * this.baseScale * Math.pow(1.1, scale));
627 this.endDistance / this.startDistance < 1 &&
630 (this.rotate == 0 || this.rotate == 180) && (width < this.thumbEl.getWidth() || height < this.thumbEl.getHeight())
634 (this.rotate == 90 || this.rotate == 270) && (height < this.thumbEl.getWidth() || width < this.thumbEl.getHeight())
647 onTouchEnd : function(e)
651 this.pinching = false;
652 this.dragable = false;
656 prepare : function(input)
661 if(typeof(input) === 'string'){
662 this.loadCanvas(input);
666 if(!input.files || !input.files[0] || !this.urlAPI){
670 this.file = input.files[0];
671 this.cropType = this.file.type;
675 if(this.fireEvent('prepare', this, this.file) != false){
677 var reader = new FileReader();
679 reader.onload = function (e) {
680 if (e.target.error) {
681 Roo.log(e.target.error);
685 var buffer = e.target.result,
686 dataView = new DataView(buffer),
688 maxOffset = dataView.byteLength - 4,
692 if (dataView.getUint16(0) === 0xffd8) {
693 while (offset < maxOffset) {
694 markerBytes = dataView.getUint16(offset);
696 if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || markerBytes === 0xfffe) {
697 markerLength = dataView.getUint16(offset + 2) + 2;
698 if (offset + markerLength > dataView.byteLength) {
699 Roo.log('Invalid meta data: Invalid segment size.');
703 if(markerBytes == 0xffe1){
711 offset += markerLength;
721 var url = _this.urlAPI.createObjectURL(_this.file);
723 _this.loadCanvas(url);
728 reader.readAsArrayBuffer(this.file);
734 parseExifData : function(dataView, offset, length)
736 var tiffOffset = offset + 10,
740 if (dataView.getUint32(offset + 4) !== 0x45786966) {
741 // No Exif data, might be XMP data instead
745 // Check for the ASCII code for "Exif" (0x45786966):
746 if (dataView.getUint32(offset + 4) !== 0x45786966) {
747 // No Exif data, might be XMP data instead
750 if (tiffOffset + 8 > dataView.byteLength) {
751 Roo.log('Invalid Exif data: Invalid segment size.');
754 // Check for the two null bytes:
755 if (dataView.getUint16(offset + 8) !== 0x0000) {
756 Roo.log('Invalid Exif data: Missing byte alignment offset.');
759 // Check the byte alignment:
760 switch (dataView.getUint16(tiffOffset)) {
765 littleEndian = false;
768 Roo.log('Invalid Exif data: Invalid byte alignment marker.');
771 // Check for the TIFF tag marker (0x002A):
772 if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002A) {
773 Roo.log('Invalid Exif data: Missing TIFF marker.');
776 // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal:
777 dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
782 tiffOffset + dirOffset,
787 parseExifTags : function(dataView, tiffOffset, dirOffset, littleEndian)
792 if (dirOffset + 6 > dataView.byteLength) {
793 Roo.log('Invalid Exif data: Invalid directory offset.');
796 tagsNumber = dataView.getUint16(dirOffset, littleEndian);
797 dirEndOffset = dirOffset + 2 + 12 * tagsNumber;
798 if (dirEndOffset + 4 > dataView.byteLength) {
799 Roo.log('Invalid Exif data: Invalid directory size.');
802 for (i = 0; i < tagsNumber; i += 1) {
806 dirOffset + 2 + 12 * i, // tag offset
810 // Return the offset to the next directory:
811 return dataView.getUint32(dirEndOffset, littleEndian);
814 parseExifTag : function (dataView, tiffOffset, offset, littleEndian)
816 var tag = dataView.getUint16(offset, littleEndian);
818 this.exif[tag] = this.getExifValue(
822 dataView.getUint16(offset + 2, littleEndian), // tag type
823 dataView.getUint32(offset + 4, littleEndian), // tag length
828 getExifValue : function (dataView, tiffOffset, offset, type, length, littleEndian)
830 var tagType = Roo.bootstrap.UploadCropbox.exifTagTypes[type],
839 Roo.log('Invalid Exif data: Invalid tag type.');
843 tagSize = tagType.size * length;
844 // Determine if the value is contained in the dataOffset bytes,
845 // or if the value at the dataOffset is a pointer to the actual data:
846 dataOffset = tagSize > 4 ?
847 tiffOffset + dataView.getUint32(offset + 8, littleEndian) : (offset + 8);
848 if (dataOffset + tagSize > dataView.byteLength) {
849 Roo.log('Invalid Exif data: Invalid data offset.');
853 return tagType.getValue(dataView, dataOffset, littleEndian);
856 for (i = 0; i < length; i += 1) {
857 values[i] = tagType.getValue(dataView, dataOffset + i * tagType.size, littleEndian);
862 // Concatenate the chars:
863 for (i = 0; i < values.length; i += 1) {
865 // Ignore the terminating NULL byte(s):
866 if (c === '\u0000') {
878 Roo.apply(Roo.bootstrap.UploadCropbox, {
880 'Orientation': 0x0112
886 3: 180, //'bottom-right',
889 6: 90, //'right-top',
890 // 7: 'right-bottom',
891 8: 270 //'left-bottom'
895 // byte, 8-bit unsigned int:
897 getValue: function (dataView, dataOffset) {
898 return dataView.getUint8(dataOffset);
902 // ascii, 8-bit byte:
904 getValue: function (dataView, dataOffset) {
905 return String.fromCharCode(dataView.getUint8(dataOffset));
910 // short, 16 bit int:
912 getValue: function (dataView, dataOffset, littleEndian) {
913 return dataView.getUint16(dataOffset, littleEndian);
919 getValue: function (dataView, dataOffset, littleEndian) {
920 return dataView.getUint32(dataOffset, littleEndian);
924 // rational = two long values, first is numerator, second is denominator:
926 getValue: function (dataView, dataOffset, littleEndian) {
927 return dataView.getUint32(dataOffset, littleEndian) /
928 dataView.getUint32(dataOffset + 4, littleEndian);
932 // slong, 32 bit signed int:
934 getValue: function (dataView, dataOffset, littleEndian) {
935 return dataView.getInt32(dataOffset, littleEndian);
939 // srational, two slongs, first is numerator, second is denominator:
941 getValue: function (dataView, dataOffset, littleEndian) {
942 return dataView.getInt32(dataOffset, littleEndian) /
943 dataView.getInt32(dataOffset + 4, littleEndian);