Skip to content

Commit a3e7349

Browse files
authored
Merge pull request #18 from dmstr/feature/angular-options
Feature/angular options
2 parents 5ca252d + b9aaf19 commit a3e7349

File tree

1 file changed

+103
-74
lines changed

1 file changed

+103
-74
lines changed

src/widgets/FileManagerWidget.php

Lines changed: 103 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
use hrzg\filemanager\assets\AfmAsset;
1313
use yii\base\Widget;
14+
use yii\helpers\ArrayHelper;
15+
use yii\helpers\Json;
1416
use yii\helpers\Url;
1517
use yii\web\View;
1618

@@ -40,24 +42,22 @@ class FileManagerWidget extends Widget
4042
public $template = '<div data-ng-app="FileManagerApp"><div class="ng-cloak"><angular-filemanager></angular-filemanager></div></div>';
4143

4244
/**
43-
* @var string
45+
* @var boolean|null
4446
*/
45-
public $thumbnailUrlPrefix;
47+
public $enableThumbnails;
4648

4749
/**
48-
* @var string
50+
* @var boolean|null
4951
*/
50-
public $thumbnailUrlSuffix;
52+
public $enableIconPreviewView;
5153

5254
/**
53-
* @var boolean
55+
* array of angular fileManagerApp options
56+
*
57+
* @var null|array
5458
*/
55-
public $enableThumbnails;
59+
public $options;
5660

57-
/**
58-
* @var boolean
59-
*/
60-
public $enableIconPreviewView;
6161
/**
6262
* @inheritdoc
6363
*/
@@ -97,74 +97,103 @@ protected function setFileManagerConfig()
9797
$title = empty($this->title) ? getenv('AFM_TITLE') : $this->title;
9898
$lang = \Yii::$app->language;
9999

100-
// resolve options for JavaScript
101-
$enableThumbnails = $this->enableThumbnails ? 'true' : 'false';
102-
$enableIconPreviewView = $this->enableIconPreviewView ? 'true' : 'false';
100+
$this->options = is_array($this->options) ? $this->options : [];
101+
102+
$defaults = [
103+
'appName' => $title,
104+
'defaultLang' => $lang,
105+
// which functions should be activated in fileManagerApp?
106+
'searchForm' => true,
107+
'sidebar' => true,
108+
'breadcrumb' => true,
109+
'hidePermissions' => true,
110+
111+
// Handler Urls
112+
'listUrl' => $this->handlerUrl,
113+
'uploadUrl' => $this->handlerUrl,
114+
'renameUrl' => $this->handlerUrl,
115+
'copyUrl' => $this->handlerUrl,
116+
'moveUrl' => $this->handlerUrl,
117+
'removeUrl' => $this->handlerUrl,
118+
'getContentUrl' => $this->handlerUrl,
119+
'createFolderUrl' => $this->handlerUrl,
120+
'downloadFileUrl' => $this->handlerUrl,
121+
'downloadMultipleUrl' => $this->handlerUrl,
122+
'compressUrl' => $this->handlerUrl,
123+
'extractUrl' => $this->handlerUrl,
124+
'permissionsUrl' => $this->handlerUrl,
125+
126+
// Additional settings
127+
'multipleDownloadFileName' => 'filemanager.zip',
128+
'showSizeForDirectories' => false,
129+
'useBinarySizePrefixes' => false,
130+
'downloadFilesByAjax' => true,
131+
'previewImagesInModal' => true,
132+
'enablePermissionsRecursive' => false,
133+
'enableThumbnails' => false,
134+
'enableIconPreviewView' => false,
135+
136+
// File patterns
137+
'isEditableFilePattern' => '/\.(!)/i',
138+
'isImageFilePattern' => '/\.(jpe?g|gif|bmp|png|svg|tiff?)$/i',
139+
'isExtractableFilePattern' => '/\.(gz|tar|rar|g?zip)$/i',
140+
// define allowed actions for filemanager
141+
'allowedActions' => [
142+
'upload' => true,
143+
'rename' => false,
144+
'move' => true,
145+
'copy' => true,
146+
'edit' => true,
147+
'compress' => true,
148+
'compressChooseName' => true,
149+
'extract' => true,
150+
'download' => true,
151+
'downloadMultiple' => true,
152+
'downloadLink' => true,
153+
'preview' => true,
154+
'remove' => true,
155+
'createFolder' => true,
156+
'pickFiles' => false,
157+
'pickFolders' => false,
158+
'changePermissions' => $allowPermissions,
159+
]
160+
];
161+
162+
// these options are regEx and therefor can not be stored as strings in json
163+
// if preset, they will be handled later, see search/replace
164+
$specialOpts = [
165+
'isEditableFilePattern',
166+
'isImageFilePattern',
167+
'isExtractableFilePattern'
168+
];
169+
// store specialOpts for replacement
170+
$specialValues = [];
171+
172+
$config = ArrayHelper::merge($defaults, $this->options);
173+
174+
// now insert placeholders for $specialValues that can not be stored as strings, e.g. regexP
175+
foreach ($specialOpts as $opt) {
176+
if (!empty($config[$opt])) {
177+
$specialValues[$opt] = [
178+
'value' => $config[$opt],
179+
'placeholder' => '###' . strtoupper($opt) . '###',
180+
];
181+
$config[$opt] = $specialValues[$opt]['placeholder'];
182+
}
183+
}
184+
// generate config json
185+
$configJson = json_encode($config);
186+
// then replace placeholder strings in json config that can/should not be strings...
187+
foreach ($specialValues as $special) {
188+
$configJson = str_replace('"' . $special['placeholder']. '"', $special['value'], $configJson);
189+
}
103190

191+
// finally create JS to init fileManagerApp
104192
$initFilemanagerJs = <<<JS
105193
angular.module('FileManagerApp').config(['fileManagerConfigProvider', function (config) {
106194
var defaults = config.\$get();
107-
var handler = '$this->handlerUrl';
108-
config.set({
109-
110-
// Application
111-
appName: '$title',
112-
defaultLang: '$lang',
113-
searchForm: true,
114-
sidebar: true,
115-
breadcrumb: true,
116-
hidePermissions: true,
117-
118-
// Allowed actions
119-
allowedActions: angular.extend(defaults.allowedActions, {
120-
remove: true,
121-
list: true,
122-
move: true,
123-
rename: true,
124-
copy: true,
125-
download: true,
126-
downloadMultiple: false,
127-
downloadLink: true,
128-
changePermissions: '$allowPermissions',
129-
compress: false,
130-
compressChooseName: false,
131-
extract: false,
132-
upload: true
133-
}),
134-
135-
// Handler
136-
listUrl: handler,
137-
uploadUrl: handler,
138-
renameUrl: handler,
139-
copyUrl: handler,
140-
moveUrl: handler,
141-
removeUrl: handler,
142-
getContentUrl: handler,
143-
createFolderUrl: handler,
144-
downloadFileUrl: handler,
145-
downloadMultipleUrl: handler,
146-
compressUrl: handler,
147-
extractUrl: handler,
148-
permissionsUrl: handler,
149-
150-
// Additional settings
151-
multipleDownloadFileName: 'filemanager.zip',
152-
showSizeForDirectories: false,
153-
useBinarySizePrefixes: false,
154-
downloadFilesByAjax: true,
155-
previewImagesInModal: true,
156-
enablePermissionsRecursive: false,
157-
thumbnailUrlPrefix: '{$this->thumbnailUrlPrefix}',
158-
thumbnailUrlSuffix: '{$this->thumbnailUrlSuffix}',
159-
enableThumbnails: {$enableThumbnails},
160-
enableIconPreviewView: {$enableIconPreviewView},
161-
162-
// File patterns
163-
isEditableFilePattern: /\.(!)/i,
164-
isImageFilePattern: /\.(jpe?g|gif|bmp|png|svg|tiff?)$/i,
165-
isExtractableFilePattern: /\.(gz|tar|rar|g?zip)$/i
166-
//tplPath: 'src/templates'
167-
});
195+
// we use merge here to get deep.copy, see: https://docs.angularjs.org/api/ng/function/angular.merge
196+
config.set(angular.merge(defaults, $configJson));
168197
}]);
169198
JS;
170199

0 commit comments

Comments
 (0)