{"size": 6195, "ext": "js", "max_stars_repo_path": "src/bitech/ui5/pdf/Viewer.js", "max_stars_repo_name": "Bitech-AG/ui5.pdf", "max_stars_repo_stars_event_min_datetime": "2019-10-28T18:53:58.000Z", "max_stars_repo_stars_event_max_datetime": "2019-10-28T18:53:58.000Z", "max_issues_repo_path": "src/bitech/ui5/pdf/Viewer.js", "max_issues_repo_name": "Bitech-AG/ui5.pdf", "max_issues_repo_issues_event_min_datetime": "2019-10-17T18:45:51.000Z", "max_issues_repo_issues_event_max_datetime": "2021-03-09T21:53:19.000Z", "max_forks_repo_path": "src/bitech/ui5/pdf/Viewer.js", "max_forks_repo_name": "Bitech-AG/ui5.pdf", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 19, "max_forks_count": null, "avg_line_length": 34.226519337, "max_line_length": 106, "alphanum_fraction": 0.5179983858}
sap.ui.define([
    "sap/ui/core/XMLComposite",
    "sap/ui/model/resource/ResourceModel"
], function (Composite, ResourceModel) {
    return Composite.extend("bitech.ui5.pdf.Viewer", {
        metadata: {
            properties: {
                height: "sap.ui.core.CSSSize",
                minPagesForSmallNavigation: {
                    type: "int",
                    defaultValue: 2
                },
                minPagesForBigNavigation: {
                    type: "int",
                    defaultValue: 3
                },
                scale: "string",
                src: "string",
                noDataText: {
                    type: "string",
                    defaultValue: "noDataText"
                },
                title: "string",
                width: "sap.ui.core.CSSSize"
            }
        },
        fragment: "bitech.ui5.pdf.Viewer",

        init: function () {
            var oDocument = this.byId("Document"),
                oI18n = new ResourceModel({
                    bundleName: "bitech.ui5.pdf.messagebundle"
                });
            
            this.setModel(oI18n, "bitech.ui5.pdf.i18n");

            oDocument.attachEvent("documentLoaded", null, this._onDocumentLoaded.bind(this));
            oDocument.attachEvent("drawn", null, this._onDocumentDrawn.bind(this));
        },

        _toFirstPage: function(oEvent) {
            var oDocument = this.byId("Document");

            oDocument.setPage(1);
        },

        _toLastPage: function(oEvent) {
            var oDocument = this.byId("Document");

            oDocument.setPage(oDocument.getPageCount());
        },

        _toNextPage: function(oEvent) {
            var oDocument = this.byId("Document");

            oDocument.setPage(oDocument.getPage() + 1);
        },

        _onDocumentDrawn: function(oEvent) {
            var oDocument = oEvent.getSource(),
                oZoomInput = this.byId("zoomInput"),
                oZoomIn = this.byId("zoomIn"),
                oZoomOut = this.byId("zoomOut");

            oZoomInput.setValue((oDocument.getScale() * 100).toFixed(2));
            oZoomInput.setVisible(true);
            oZoomIn.setVisible(true);
            oZoomOut.setVisible(true);
        },

        _onDocumentLoaded: function (oEvent) {
            var oDocument = oEvent.getSource();

            this.getResourceBundle().then(function (oBundle) {
                var oPageCountLabel = this.byId("pageCountLabel"),
                    oPageInput = this.byId("pageInput"),
                    oFirstButton = this.byId("firstButton"),
                    oPrevButton = this.byId("prevButton"),
                    oNextButton = this.byId("nextButton"),
                    oLastButton = this.byId("lastButton");

                oFirstButton.setVisible(this.getMinPagesForBigNavigation() <= oDocument.getPageCount());
                oPrevButton.setVisible(this.getMinPagesForSmallNavigation() <= oDocument.getPageCount());

                if (oDocument.getPageCount() > 1) {
                    oPageCountLabel.setText(oBundle.getText("pageCountText", [oDocument.getPageCount()]));

                }
                oPageInput.setVisible(oDocument.getPageCount() > 1);
                oPageInput.setValue(1);
                oPageCountLabel.setVisible(oDocument.getPageCount() > 1);
                
                oNextButton.setVisible(this.getMinPagesForSmallNavigation() <= oDocument.getPageCount());
                oLastButton.setVisible(this.getMinPagesForBigNavigation() <= oDocument.getPageCount());

                this._onPageSelected({
                    getSource: function () {
                        return oDocument;
                    }
                });
            }.bind(this));
        },

        _onPageSelected: function(oEvent) {
            var oDocument = oEvent.getSource(),
                oPageInput = this.byId("pageInput"),
                oFirstButton = this.byId("firstButton"),
                oPrevButton = this.byId("prevButton"),
                oNextButton = this.byId("nextButton"),
                oLastButton = this.byId("lastButton");

            oFirstButton.setEnabled(oDocument.getPage() > 1);
            oPrevButton.setEnabled(oDocument.getPage() > 1);
            oPageInput.setValue(oDocument.getPage());
            oNextButton.setEnabled(oDocument.getPage() < oDocument.getPageCount());
            oLastButton.setEnabled(oDocument.getPage() < oDocument.getPageCount());
        },

        _toPrevPage: function(oEvent) {
            var oDocument = this.byId("Document");

            oDocument.setPage(oDocument.getPage() - 1);
        },

        _uploadFile: function (oEvent) {
            var oReader = new FileReader(),
                oFiles = oEvent.getParameter("files");

            if (!oFiles || !oFiles.length) {
                return;
            }
            this.setBusy(true);

            oReader.onload = function (oLoadEvent) {
                var oDocument = this.byId("Document");
    
                oDocument.setScale();
                this.setSrc(oLoadEvent.target.result);
            }.bind(this);

            this.setTitle(oFiles[0].name);

            oReader.readAsDataURL(oFiles[0]);
        },

        _onChangePageNumber: function (oEvent) {
            var iValue = +oEvent.getParameter("value"),
                oDocument = this.byId("Document");

            oDocument.setPage(iValue);
        },

        _onChangeZoom: function(oEvent) {
            var oZoomInput = oEvent.getSource(),
                nValue = oZoomInput.getValue(),
                oDocument = this.byId("Document");

            nValue = nValue ? nValue / 100 : nValue;

            oDocument.setScale(nValue);

        },

        _zoom: function(nStep) {
            var oDocument = this.byId("Document"),
                nValue = oDocument.getScale();
            
            nValue = nValue + nStep < 0 ? 0 : nValue + nStep;

            oDocument.setScale(nValue);
        },

        _zoomIn: function() {
            this._zoom(0.25);
        },

        _zoomOut: function() {
            this._zoom(-0.25);
        }
    });
}, true);