-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinify.module
More file actions
169 lines (147 loc) · 6.56 KB
/
Minify.module
File metadata and controls
169 lines (147 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
class Minify extends WireData implements Module, ConfigurableModule {
/**
* Basic information about module
*/
public static function getModuleInfo() {
return array(
'title' => 'Minify',
'summary' => 'Compresses and combines javascript and CSS files for use in templates using the Minify library. Please see the license file included for licensing info.',
'href' => 'http://processwire.com/talk/topic/1537-minify/',
'version' => 103,
'permanent' => false,
'autoload' => false,
'singular' => true,
);
}
/**
* Default configuration for module
*
* This is based on Ryan's examples [used also in @apeisa's AdminBar, for instance]
*/
static public function getDefaultData() {
return array(
'scripts_path' => wire('config')->urls->templates . "scripts/",
'styles_path' => wire('config')->urls->templates . "styles/",
);
}
/**
* Populate the default config data
*
*/
public function __construct() {
foreach(self::getDefaultData() as $key => $value) {
$this->$key = $value;
}
}
/**
* Initialize the module and setup hooks
*/
public function init() {
// Not required
}
static public function getModuleConfigInputfields(array $data) {
$templatepath = trim(wire('config')->urls->templates, '/'); // We need to trim off the start and end slashes for the Minify URL
$rootpath = wire('config')->paths->root;
$fields = new InputfieldWrapper();
$modules = Wire::getFuel('modules');
$default_data = self::getDefaultData();
$data = array_merge($default_data, $data);
$scripts_path_exists = $data['scripts_path'] && is_dir($rootpath.$data['scripts_path']);
$styles_path_exists = $data['styles_path'] && is_dir($rootpath.$data['styles_path']);
// Path configuration
$fieldset = $modules->get("InputfieldFieldset");
$fieldset->label = __("Directory settings");
if ($scripts_path_exists || $styles_path_exists) $fieldset->collapsed = true;
$field = $modules->get("InputfieldText");
$field->name = "scripts_path";
$field->label = __("Scripts directory");
$field->value = $data['scripts_path'];
$field->notes = sprintf(__("Starting from the root of your ProcessWire installation. Example: %s."), $default_data['scripts_path']);
$field->description = __("In order to minify script files, you need to specify path for your site scripts here.");
if ($scripts_path_exists) $field->collapsed = true;
$fieldset->add($field);
$field = $modules->get("InputfieldText");
$field->name = "styles_path";
$field->label = ("Styles directory");
$field->value = $data['styles_path'];
$field->notes = sprintf(__("Starting from the root of your ProcessWire installation. Example: %s."), $default_data['styles_path']);
$field->description = __("In order to minify style files, you need to specify path for your site styles here.");
if ($styles_path_exists) $field->collapsed = true;
$fieldset->add($field);
$fields->add($fieldset);
// Scripts
if ($scripts_path_exists) {
$scripts = array();
$iterator = new RecursiveDirectoryIterator($rootpath.$data['scripts_path']);
foreach (new RecursiveIteratorIterator($iterator) as $filename => $cur) {
$filename = str_replace('\\', '/', $filename);
if (strrpos($filename, '.js') == strlen($filename)-3) {
$scripts[] = str_replace($rootpath.$data['scripts_path'], '', $filename);
}
}
$field = $modules->get("InputfieldAsmSelect");
$field->attr('name', 'scripts');
foreach($scripts as $script) {
$field->addOption($script);
}
$field->label = __("Scripts available for selection");
$scripts = implode(',', $scripts);
$field->description = __("Select the .js files from your scripts folder that you wish to minify.");
$str = "var scriptlist = '';
$('#' + $(this).parent().attr('id') + ' .asmListItemLabel').each(function(index) {
var separator = '';
if (scriptlist != '') {
separator = ',';
}
scriptlist = scriptlist + separator + $(this).html();
});
var minString = '<strong style=\"font-weight: bold;\">" . __("Copy and paste this before your </head> tag:") . "</strong><br /><br /><input type=\"text\" style=\"width: 100%;\" value=\"<script type="text/javascript" src="<?php echo \$config->urls->siteModules; ?>Minify/min/b=" . trim($data['scripts_path'], '/') . "&f=' + scriptlist + '"></script>\" /><br /><br />'
+ '" . htmlentities(__("Note: Ensure that if you are adding an jQuery-related scripts that you add jQuery to the top of the list before it is called by any other script.")) . "';
var minStringContainer = $(this).parent().attr('id') + 'Text';
if ($('#' + minStringContainer).length == 0) {
$(this).after('<div id=\"' + minStringContainer + '\">' + minString + '</div>');
} else {
$('#' + minStringContainer).html(minString);
}";
$field->attr('onchange', $str);
$fields->append($field);
}
// Styles
if ($styles_path_exists) {
$styles = array();
$iterator = new RecursiveDirectoryIterator($rootpath.$data['styles_path']);
foreach (new RecursiveIteratorIterator($iterator) as $filename => $cur) {
$filename = str_replace('\\', '/', $filename);
if (strrpos($filename, '.css') == strlen($filename)-4) {
$styles[] = str_replace($rootpath.$data['styles_path'], '', $filename);
}
}
$field = $modules->get("InputfieldAsmSelect");
$field->attr('name', 'styles');
foreach($styles as $style) {
$field->addOption($style);
}
$field->label = __("Stylesheets available for selection");
$field->description = __("Select the .css files from your styles folder that you wish to minify.");
$str = "var stylelist = '';
$('#' + $(this).parent().attr('id') + ' .asmListItemLabel').each(function(index) {
var separator = '';
if (stylelist != '') {
separator = ',';
}
stylelist = stylelist + separator + $(this).html();
});
var minString = '<strong style=\"font-weight: bold;\">" . __("Copy and paste this before your </head> tag:") . "</strong><br /><br /><input type=\"text\" style=\"width: 100%;\" value=\"<link href="<?php echo \$config->urls->siteModules; ?>Minify/min/b=" . trim($data['styles_path'], '/') . "&f=' + stylelist + '" rel="stylesheet" type="text/css" />\" />';
var minStringContainer = $(this).parent().attr('id') + 'Text';
if ($('#' + minStringContainer).length == 0) {
$(this).after('<div id=\"' + minStringContainer + '\">' + minString + '</div>');
} else {
$('#' + minStringContainer).html(minString);
}";
$field->attr('onchange', $str);
$fields->append($field);
}
return $fields;
}
}