Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Buttons' visibility #28

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ that images, translations and such are being loaded from the `web` folder.
<body>
<div class='some-pdf-container'>
<pdfjs-viewer src="{{ pdf.src }}" scale="scale"
download="true" print="false" open="false"
buttons="buttons"
on-init="onInit()" on-page-load="onPageLoad(page)">
</pdfjs-viewer>
</div>
Expand All @@ -56,9 +56,29 @@ that images, translations and such are being loaded from the `web` folder.

The `scale` attribute can be used to obtain the current scale (zoom level) of the PDF. This is read only.

The directive takes the following optional attributes to modify the toolbar
The directive takes the `buttons` optional attribute to allow show and hide toolbar buttons.

download="false" print="false" open="false"
The following lists available buttons do show/hide
```
buttons: {
sidebarToggle : true,
viewFind : true,
previous : true,
next : true,
pageNumberLabel : true,
pageNumber : true,
numPages : true,
zoomOut : true,
zoomIn : true,
scaleSelectContainer : true,
presentationMode : true,
openFile : true,
print : true,
download : true,
viewBookmark : true,
secondaryToolbarToggle : true
}
```

Omitting these attributes will by default show the options in the toolbar.

Expand All @@ -74,6 +94,12 @@ angular.module('app').controller('AppCtrl', function($scope) {
$scope.pdf = {
src: 'example.pdf',
};

$scope.buttons = {
previous : false,
next : false,
print : true
};

$scope.$watch('scale', function() {
...
Expand Down Expand Up @@ -123,10 +149,32 @@ negative effect on the runtime performance.

pdfjsViewerConfigProvider.disableWorker();
pdfjsViewerConfigProvider.setVerbosity("infos"); // "errors", "warnings" or "infos"

pdfjsViewerConfigProvider.setButtonsVisibility({
openFile : true,
print : false,
download : false,
viewBookmark : true,
});
});

Note that a number of images used in the PDF.js viewer are loaded by the `viewer.css`. You can't configure these
through JavaScript. Instead you need to compile the `viewer.less` file as

lessc --global-var='pdfjsImagePath=/assets/pdf.js-viewer/images' viewer.less viewer.css

If you use *Grunt* you can compile `viewer.less` with `modifyVar` attribute:

less: {
options: {
modifyVars: {
pdfjsImagePath: '"../pdfviewer/images"'
},
files: {
"css/app.css": [
"pdf.js-viewer/viewer.less"
]
}
}
}

14 changes: 14 additions & 0 deletions demo/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
angular.module('app', ['pdfjsViewer']);

angular.module('app').config(function(pdfjsViewerConfigProvider) {
pdfjsViewerConfigProvider.setButtonsVisibility({
openFile : true,
print : true,
download : false,
viewBookmark : true
});
});

angular.module('app').controller('AppCtrl', function($scope) {
$scope.pdf = {
src: 'example.pdf'
};

$scope.buttons = {
print : false,
download: true
}
});
88 changes: 74 additions & 14 deletions dist/angular-pdfjs-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
cmapDir: null,
imageResourcesPath: null,
disableWorker: false,
verbosity: null
verbosity: null,
buttons: {}
};

this.setWorkerSrc = function(src) {
Expand All @@ -33,11 +34,17 @@
this.disableWorker = function(value) {
if (typeof value === 'undefined') value = true;
config.disableWorker = value;
}
};

this.setVerbosity = function(level) {
config.verbosity = level;
};

this.setButtonsVisibility = function(buttons) {
for (var key in buttons) {
config.buttons[key] = (buttons[key] === true);
}
};

this.$get = function() {
return config;
Expand All @@ -64,11 +71,11 @@
if (pdfjsViewerConfig.verbosity !== null) {
var level = pdfjsViewerConfig.verbosity;
if (typeof level === 'string') level = PDFJS.VERBOSITY_LEVELS[level];
PDFJS.verbosity = level;
PDFJS.verbosity = level;
}
}]);

module.directive('pdfjsViewer', ['$interval', function ($interval) {
module.directive('pdfjsViewer', ['$interval', 'pdfjsViewerConfig', function ($interval, pdfjsViewerConfig) {
return {
template: '<pdfjs-wrapper>\n' +
' <div id="outerContainer">\n' +
Expand Down Expand Up @@ -445,6 +452,7 @@
onInit: '&',
onPageLoad: '&',
scale: '=?',
buttons: '=?'
},
link: function ($scope, $element, $attrs) {
$element.children().wrap('<div class="pdfjs" style="width: 100%; height: 100%;"></div>');
Expand Down Expand Up @@ -513,21 +521,32 @@
}, function () {
if (!$attrs.src) return;

if ($attrs.open === 'false') {
document.getElementById('openFile').setAttribute('hidden', 'true');
document.getElementById('secondaryOpenFile').setAttribute('hidden', 'true');
// Restored for backward compatibility
if ($attrs.open){
if(!$scope.buttons){
$scope.buttons = {};
}
$scope.buttons.openFile = ($attrs.open === 'true');
}

if ($attrs.download === 'false') {
document.getElementById('download').setAttribute('hidden', 'true');
document.getElementById('secondaryDownload').setAttribute('hidden', 'true');

// Restored for backward compatibility
if ($attrs.download) {
if(!$scope.buttons){
$scope.buttons = {};
}
$scope.buttons.download = ($attrs.download === 'true');
}

if ($attrs.print === 'false') {
document.getElementById('print').setAttribute('hidden', 'true');
document.getElementById('secondaryPrint').setAttribute('hidden', 'true');
// Restored for backward compatibility
if ($attrs.print) {
if(!$scope.buttons){
$scope.buttons = {};
}
$scope.buttons.print = ($attrs.print === 'true');
}

setButtonsVisibility(pdfjsViewerConfig.buttons, $scope.buttons);

if ($attrs.width) {
document.getElementById('outerContainer').style.width = $attrs.width;
}
Expand All @@ -542,5 +561,46 @@
};
}]);

function setButtonsVisibility(defaultButtons, customButtons) {

// Merge default provider's buttons and custom directive's buttons
var allButtons = angular.merge({}, defaultButtons, customButtons);
var key;
var keyValue;
var button;
var buttonSecond;

for (key in allButtons) {
keyValue = null;
if (allButtons && allButtons.hasOwnProperty(key)) {
keyValue = allButtons[key];
}
else {
// Default shows
keyValue = true;
}

button = document.getElementById(key);
if (button !== null) {
if (keyValue === false) {
button.setAttribute('hidden', 'true');
}
else {
button.removeAttribute('hidden');
}
}

buttonSecond = document.getElementById('secondary' + (key.charAt(0).toUpperCase() + key.slice(1)));
if (buttonSecond !== null) {
if (keyValue === false) {
buttonSecond.setAttribute('hidden', 'true');
}
else {
button.removeAttribute('hidden');
}
}
}
}

//
}();
88 changes: 74 additions & 14 deletions src/angular-pdfjs-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
cmapDir: null,
imageResourcesPath: null,
disableWorker: false,
verbosity: null
verbosity: null,
buttons: {}
};

this.setWorkerSrc = function(src) {
Expand All @@ -33,11 +34,17 @@
this.disableWorker = function(value) {
if (typeof value === 'undefined') value = true;
config.disableWorker = value;
}
};

this.setVerbosity = function(level) {
config.verbosity = level;
};

this.setButtonsVisibility = function(buttons) {
for (var key in buttons) {
config.buttons[key] = (buttons[key] === true);
}
};

this.$get = function() {
return config;
Expand All @@ -64,18 +71,19 @@
if (pdfjsViewerConfig.verbosity !== null) {
var level = pdfjsViewerConfig.verbosity;
if (typeof level === 'string') level = PDFJS.VERBOSITY_LEVELS[level];
PDFJS.verbosity = pdfjsViewerConfig.verbosity;
PDFJS.verbosity = level;
}
}]);

module.directive('pdfjsViewer', ['$interval', function ($interval) {
module.directive('pdfjsViewer', ['$interval', 'pdfjsViewerConfig', function ($interval, pdfjsViewerConfig) {
return {
templateUrl: file.folder + '../../pdf.js-viewer/viewer.html',
restrict: 'E',
scope: {
onInit: '&',
onPageLoad: '&',
scale: '=?',
buttons: '=?'
},
link: function ($scope, $element, $attrs) {
$element.children().wrap('<div class="pdfjs" style="width: 100%; height: 100%;"></div>');
Expand Down Expand Up @@ -144,21 +152,32 @@
}, function () {
if (!$attrs.src) return;

if ($attrs.open === 'false') {
document.getElementById('openFile').setAttribute('hidden', 'true');
document.getElementById('secondaryOpenFile').setAttribute('hidden', 'true');
// Restored for backward compatibility
if ($attrs.open){
if(!$scope.buttons){
$scope.buttons = {};
}
$scope.buttons.openFile = ($attrs.open === 'true');
}

if ($attrs.download === 'false') {
document.getElementById('download').setAttribute('hidden', 'true');
document.getElementById('secondaryDownload').setAttribute('hidden', 'true');

// Restored for backward compatibility
if ($attrs.download) {
if(!$scope.buttons){
$scope.buttons = {};
}
$scope.buttons.download = ($attrs.download === 'true');
}

if ($attrs.print === 'false') {
document.getElementById('print').setAttribute('hidden', 'true');
document.getElementById('secondaryPrint').setAttribute('hidden', 'true');
// Restored for backward compatibility
if ($attrs.print) {
if(!$scope.buttons){
$scope.buttons = {};
}
$scope.buttons.print = ($attrs.print === 'true');
}

setButtonsVisibility(pdfjsViewerConfig.buttons, $scope.buttons);

if ($attrs.width) {
document.getElementById('outerContainer').style.width = $attrs.width;
}
Expand All @@ -173,6 +192,47 @@
};
}]);

function setButtonsVisibility(defaultButtons, customButtons) {

// Merge default provider's buttons and custom directive's buttons
var allButtons = angular.merge({}, defaultButtons, customButtons);
var key;
var keyValue;
var button;
var buttonSecond;

for (key in allButtons) {
keyValue = null;
if (allButtons && allButtons.hasOwnProperty(key)) {
keyValue = allButtons[key];
}
else {
// Default shows
keyValue = true;
}

button = document.getElementById(key);
if (button !== null) {
if (keyValue === false) {
button.setAttribute('hidden', 'true');
}
else {
button.removeAttribute('hidden');
}
}

buttonSecond = document.getElementById('secondary' + (key.charAt(0).toUpperCase() + key.slice(1)));
if (buttonSecond !== null) {
if (keyValue === false) {
buttonSecond.setAttribute('hidden', 'true');
}
else {
button.removeAttribute('hidden');
}
}
}
}

// === get current script file ===
var file = {};
file.scripts = document.querySelectorAll('script[src]');
Expand Down