This repository was archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakpoints_class.module
265 lines (240 loc) · 9.55 KB
/
breakpoints_class.module
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/**
* @file
* Breakpoints Class module.
*/
define('BREAKPOINTS_CLASS_CACHE_ID', 'breakpoints_class_cache');
define('BREAKPOINTS_CLASS_SETTINGS_PREFIX', 'breakpoints_class_prefix');
define('BREAKPOINTS_CLASS_SETTINGS_DEBUG', 'breakpoints_class_debug');
define('BREAKPOINTS_CLASS_SETTINGS_THEMES', 'breakpoints_class_themes');
// Enqure.js
define('BREAKPOINTS_CLASS_ENQUIRE_DOWNLOAD_URL', 'https://github.com/WickyNilliams/enquire.js');
define('BREAKPOINTS_CLASS_ENQUIRE_DOWNLOAD_FOLDER', '/sites/*/libraries/enquire.js/');
// matchMedia.js
define('BREAKPOINTS_CLASS_MATCH_MEDIA_DOWNLOAD_URL', 'https://github.com/paulirish/matchMedia.js');
define('BREAKPOINTS_CLASS_MATCH_MEDIA_DOWNLOAD_FOLDER', '/sites/*/libraries/matchMedia.js/');
/**
* Implements hook_menu().
*/
function breakpoints_class_menu() {
$items = array();
//@todo: implement own hook_permission
$items['admin/config/media/breakpoints/class-settings'] = array(
'title' => 'Classes',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array('breakpoints_class_config_form'),
'access arguments' => array('administer site configuration'),
'weight' => 30,
);
return $items;
}
/**
* Form for module configuration.
* @param $form
* @param $form_state
* @return mixed
*/
function breakpoints_class_config_form($form, &$form_state) {
$form = array();
$form[BREAKPOINTS_CLASS_SETTINGS_PREFIX] = array(
'#type' => 'textfield',
'#title' => t('Classes prefix'),
'#description' => t('This value will be used for prefix to body classes for resolving conflicts, for example, with !browserclass module.', array('!browserclass' => l('browserclass', 'https://www.drupal.org/project/browserclass'))),
'#default_value' => variable_get(BREAKPOINTS_CLASS_SETTINGS_PREFIX, ''),
);
// Get all themes list for selector.
$themes_list = list_themes();
$themes_options = array();
foreach ($themes_list as $theme_machine_name => $theme) {
$themes_options[$theme_machine_name] = $theme->info['name'];
}
$form[BREAKPOINTS_CLASS_SETTINGS_THEMES] = array(
'#type' => 'checkboxes',
'#title' => t('Themes'),
'#options' => $themes_options,
'#description' => t('Apply class adding to selected themes.'),
'#default_value' => variable_get(BREAKPOINTS_CLASS_SETTINGS_THEMES, array(variable_get('theme_default'))),
);
$form[BREAKPOINTS_CLASS_SETTINGS_DEBUG] = array(
'#type' => 'checkbox',
'#title' => t('Debug mode'),
'#description' => t('If Debug mode is ON you can see in JS console all logging for jumping between breakpoints and warn when classes adding is restricted by module\'s Themes settings.'),
'#default_value' => variable_get(BREAKPOINTS_CLASS_SETTINGS_DEBUG, FALSE),
);
return system_settings_form($form);
}
/**
* Configuration form validation.
*/
function breakpoints_class_config_form_validate($form, &$form_state) {
// Class can't start with number or dash value.
$first_char = $form_state['input']['breakpoints_class_prefix'][0];
if (is_numeric($first_char) || $first_char == '-') {
form_set_error('breakpoints_class_prefix', t('HTML class can\'t start with this character. That\'s the rules. Sorry. Use regular english letter instead.'));
}
// Remove all special characters and spaces which are not allowed for classes.
$form_state['values']['breakpoints_class_prefix'] = preg_replace('/[^A-Za-z0-9\-]/', '', $form_state['input']['breakpoints_class_prefix']);
}
/**
* Implements hook_page_alter().
*
* Add all needed breakpoints information to Drupal.settings.breakpoints_class
* javascript object.
*
* @see breakpoints_breakpoint_load_all()
*/
function breakpoints_class_page_alter(&$page) {
// Allow to add class only on needed themes.
global $theme;
$allowed_themes = variable_get(BREAKPOINTS_CLASS_SETTINGS_THEMES, array(variable_get('theme_default')));
$is_currently_needed = $allowed_themes[$theme] ? TRUE : FALSE;
// Cache breakpoints information.
$cached = cache_get(BREAKPOINTS_CLASS_CACHE_ID, 'cache');
if (!$cached || empty($cached->data)) {
$breakpoints = breakpoints_breakpoint_load_all();
$breakpoints_js_config = array();
foreach ($breakpoints as $breakpoint) {
$breakpoints_js_config[$breakpoint->name] = $breakpoint->breakpoint;
}
cache_set(BREAKPOINTS_CLASS_CACHE_ID, $breakpoints_js_config);
}
else {
$breakpoints_js_config = $cached->data;
}
// If Libraries needs to be loaded.
if($is_currently_needed) {
// Load all needed libraries on needed themes.
libraries_load('enquire.js');
libraries_load('matchMedia.js');
}
// Add configuration to Drupal.settings.breakpoints_class javascript object for all themes.
drupal_add_js(
array(
'breakpoints_class' => array(
'debug' => variable_get(BREAKPOINTS_CLASS_SETTINGS_DEBUG, FALSE),
'prefix' => variable_get(BREAKPOINTS_CLASS_SETTINGS_PREFIX, ''),
'breakpoints' => $breakpoints_js_config,
'isNeeded' => $is_currently_needed,
)
),
'setting'
);
}
/**
* Implements hook_libraries_info().
*/
function breakpoints_class_libraries_info() {
// enquire.js
$libraries['enquire.js'] = array(
'name' => 'Enquire JS',
'vendor url' => BREAKPOINTS_CLASS_ENQUIRE_DOWNLOAD_URL,
'download url' => BREAKPOINTS_CLASS_ENQUIRE_DOWNLOAD_URL,
'version arguments' => array(
'file' => 'dist/enquire.js',
'pattern' => '/enquire.js v([0-9a-zA-Z\.-]+)/',
),
'files' => array(
'js' => array(
'dist/enquire.js' => array(
'every_page' => TRUE,
'group' => JS_LIBRARY,
'preprocess' => 0,
'scope' => 'header',
'type' => 'file',
'weight' => -9999,
)
),
),
'variants' => array(
'minified' => array(
'files' => array(
'js' => array(
'dist/enquire.min.js'
),
),
),
),
);
// Matchmedia
$libraries['matchMedia.js'] = array(
'name' => 'matchMedia.js',
'vendor url' => BREAKPOINTS_CLASS_MATCH_MEDIA_DOWNLOAD_URL,
'download url' => BREAKPOINTS_CLASS_MATCH_MEDIA_DOWNLOAD_URL,
'version arguments' => array(
'file' => 'bower.json',
'pattern' => '/version": "([0-9a-zA-Z\.-]+)/',
),
'files' => array(
'js' => array(
'matchMedia.js' => array(
'every_page' => TRUE,
'group' => JS_LIBRARY,
'preprocess' => 0,
'scope' => 'header',
'type' => 'file',
'weight' => -9999,
),
)
),
'variants' => array(
'addListener' => array(
'files' => array(
'js' => array(
'matchMedia.js' => array(
'every_page' => TRUE,
'group' => JS_LIBRARY,
'preprocess' => 0,
'scope' => 'header',
'type' => 'file',
'weight' => -9999,
),
'matchMedia.addListener.js' => array(
'every_page' => TRUE,
'group' => JS_LIBRARY,
'preprocess' => 0,
'scope' => 'header',
'type' => 'file',
'weight' => -9998,
),
)
),
),
),
);
return $libraries;
}
/**
* Implements hook_requirements().
*/
function breakpoints_class_requirements($phase) {
$requirements = array();
// Collect all needed libraries.
$libraries = array();
$libraries['enquire.js'] = libraries_detect('enquire.js');
$libraries['matchMedia.js'] = libraries_detect('matchMedia.js');
switch ($phase) {
case 'runtime':
$requirements['breakpoints_class_enquirejs_library'] = array(
'title' => t('Enquire.js library'),
'value' => $libraries['enquire.js']['installed'] ? t('enquire.js library installed.') : $libraries['enquire.js']['error message'] . ' ' . t('It is required for Breakpoints Classes module. Please download library into @folder folder from !download_link', array(
'!download_link' => l(BREAKPOINTS_CLASS_ENQUIRE_DOWNLOAD_URL, BREAKPOINTS_CLASS_ENQUIRE_DOWNLOAD_URL),
'@folder' => BREAKPOINTS_CLASS_ENQUIRE_DOWNLOAD_FOLDER
)),
'severity' => $libraries['enquire.js']['installed'] ? REQUIREMENT_OK : REQUIREMENT_ERROR,
'weight' => 100,
);
// matchMedia.js
$requirements['breakpoints_class_match_media_library'] = array(
'title' => t('matchMedia.js library'),
'value' => $libraries['matchMedia.js']['installed'] ? t('matchMedia.js library installed.') : $libraries['matchMedia.js']['error message'] . ' ' . t('For better support of Breakpoints Classes module in IE please download library into @folder folder from !download_link', array(
'!download_link' => l(BREAKPOINTS_CLASS_MATCH_MEDIA_DOWNLOAD_URL, BREAKPOINTS_CLASS_MATCH_MEDIA_DOWNLOAD_URL),
'@folder' => BREAKPOINTS_CLASS_MATCH_MEDIA_DOWNLOAD_FOLDER
)),
'severity' => $libraries['matchMedia.js']['installed'] ? REQUIREMENT_OK : REQUIREMENT_WARNING,
'weight' => 100,
);
break;
}
return $requirements;
}