-
Notifications
You must be signed in to change notification settings - Fork 0
/
restful_angular_example.module
277 lines (253 loc) · 8.6 KB
/
restful_angular_example.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
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* @file
* Example module for the RESTful AngularJs module.
*
* Demonstrate the use of AngularJs forms instead of Form API, along with
* RESTful endpoint and the entity validator module.
*/
/**
* Implements hook_menu().
*/
function restful_angular_example_menu() {
$items['restful-example/%'] = array(
'title' => 'AngularJs example',
'access callback' => 'node_access',
'access arguments' => array('create', 'article'),
'page callback' => 'restful_angular_example_page',
'page arguments' => array(1),
'delivery callback' => 'restful_angular_example_delivery',
);
return $items;
}
/**
* Custom delivery callback.
*
* Used to override html.tpl.php from the module.
*/
function restful_angular_example_delivery($page_callback_result) {
global $language;
// Pass variables to the template.
$vars = array(
'language' => $language,
'head_title' => drupal_get_title(),
'favicon' => '',
'styles' => drupal_get_css(),
'scripts' => drupal_get_js(),
'messages' => drupal_get_messages(),
'content' => $page_callback_result,
);
echo theme('restful_angular_example_html', $vars);
drupal_page_footer();
}
/**
* Implements hook_theme().
*/
function restful_angular_example_theme() {
$theme['restful_angular_example_angular'] = array(
'template' => 'restful-angular',
'path' => drupal_get_path('module', 'restful_angular_example') . '/templates',
'variables' => array(
'url' => NULL,
'controller' => NULL,
),
);
$theme['restful_angular_example_html'] = array(
'template' => 'restful-angular-html',
'path' => drupal_get_path('module', 'restful_angular_example') . '/templates',
'variables' => array(
'language' => NULL,
'head_title' => NULL,
'favicon' => NULL,
'styles' => NULL,
'scripts' => NULL,
'messages' => NULL,
'content' => NULL,
),
);
return $theme;
}
/**
* Implements hook_ctools_plugin_directory().
*/
function restful_angular_example_ctools_plugin_directory($module, $plugin) {
if ($module == 'restful') {
return 'plugins/' . $plugin;
}
}
/**
* Implements hook_library().
*/
function restful_angular_example_library() {
$bower_path = drupal_get_path('module', 'restful_angular_example') . '/components/restful-app/bower_components';
// AngularJS library.
$libraries['angular'] = array(
'title' => t('AngularJS'),
'version' => '1.2.27',
'website' => 'https://github.com/angular/angular.js',
'js' => array(
$bower_path . '/angular/angular.js' => array(),
),
);
// JSON pretty print library.
$libraries['json-pretty-print'] = array(
'title' => t('JSON pretty print'),
'version' => '0.1.1',
'website' => 'https://github.com/darul75/ng-prettyjson',
'js' => array(
$bower_path . '/ng-prettyjson/dist/ng-prettyjson.min.js' => array(),
),
'css' => array(
$bower_path . '/ng-prettyjson/dist/ng-prettyjson.min.css' => array(),
),
'dependencies' => array(
array('restful_angular_example', 'angular'),
),
);
// Select2 library.
$libraries['select2'] = array(
'title' => t('Select 2'),
'version' => '0.0.5',
'website' => 'https://github.com/angular-ui/ui-select2',
'js' => array(
$bower_path . '/select2/select2.js' => array(),
$bower_path . '/angular-ui-select2/src/select2.js' => array(),
),
'css' => array(
$bower_path . '/select2/select2.css' => array(),
),
'dependencies' => array(
array('restful_angular_example', 'angular'),
),
);
// File upload library.
$libraries['file-upload'] = array(
'title' => t('File upload'),
'version' => '2.0.0',
'website' => 'https://github.com/danialfarid/angular-file-upload',
'js' => array(
$bower_path . '/danialfarid-angular-file-upload/dist/angular-file-upload-shim.min.js' => array(),
$bower_path . '/danialfarid-angular-file-upload/dist/angular-file-upload.min.js' => array(),
),
'dependencies' => array(
array('restful_angular_example', 'angular'),
),
);
// ng-admin library.
$libraries['ng-admin'] = array(
'title' => t('ng-admin'),
'version' => '0.3.3',
'website' => 'https://github.com/marmelab/ng-admin',
'js' => array(
$bower_path . '/ng-admin/build/ng-admin.min.js' => array(),
),
'css' => array(
$bower_path . '/ng-admin/build/ng-admin.min.css' => array(),
),
'dependencies' => array(
array('restful_angular_example', 'angular'),
),
);
// Load our custom app.
$app_path = drupal_get_path('module', 'restful_angular_example') . '/components/restful-app/dist';
// Custom libraries.
$libraries['restful-angular-form'] = array(
'title' => t('RESTful Angular Form'),
'version' => '1.0.0',
'website' => 'https://github.com/Gizra/restful/blob/7.x-1.x/modules/restful_angular_example/README.md',
'js' => array(
$app_path . '/restful-app.js' => array(),
),
'css' => array(
$app_path . '/css/restful-app.css' => array(),
),
'dependencies' => array(
array('restful_angular_example', 'json-pretty-print'),
array('restful_angular_example', 'select2'),
array('restful_angular_example', 'file-upload'),
),
);
$libraries['restful-angular-admin'] = array(
'title' => t('RESTful Angular Admin'),
'version' => '1.0.0',
'website' => 'https://github.com/Gizra/restful/blob/7.x-1.x/modules/restful_angular_example/README.md',
'js' => array(
$app_path . '/restful-app.js' => array(),
),
'css' => array(
$app_path . '/css/restful-app.css' => array(),
),
'dependencies' => array(
array('restful_angular_example', 'ng-admin'),
),
);
return $libraries;
}
/**
* Page callback; Load the AngularJs form.
*/
function restful_angular_example_page($example) {
$controller_name = preg_replace('/[-_]/', ' ', $example) . ' Ctrl';
$controller_name = ucwords($controller_name);
$controller_name = preg_replace('/ /', '', $controller_name);
if ($example == 'form') {
// Pass info via Drupal.settings.
$settings['restfulExample'] = array(
'basePath' => url('', array('absolute' => TRUE)),
'csrfToken' => drupal_get_token(\Drupal\restful\Plugin\authentication\Authentication::TOKEN_VALUE),
'data' => array(
'article' => array(
'label' => 'no',
),
),
);
drupal_add_js($settings, 'setting');
// This is a big piece of hack, since Drupal.settings is not available when
// adding Angular. This is because of the JS_LIBRARY group against the hard
// coded group in core to add the settings. The alternative is add a global
// variable in the window object that contains the information we want to
// pass to the angular module constructor.
drupal_add_js("window.restfulExampleModules = ['angularFileUpload', 'ngPrettyJson', 'ui.select2']", array(
'type' => 'inline',
// Make sure it is available when constructing the angular module.
'scope' => 'header',
'group' => JS_LIBRARY - 10,
));
// Theme function simply declares the angular app, and ng-includes the app's
// view.
$app_path = drupal_get_path('module', 'restful_angular_example') . '/components/restful-app/dist';
$url = url($app_path . '/views/form.html', array('absolute' => TRUE));
// Add the library.
drupal_add_library('restful_angular_example', 'restful-angular-form');
}
if ($example == 'admin') {
// Pass info via Drupal.settings.
$settings['restfulExample'] = array(
'apiPath' => url(variable_get('restful_hook_menu_base_path', 'api'), array('absolute' => TRUE)),
'apiVersion' => 'v1.5',
'csrfToken' => drupal_get_token(\Drupal\restful\Plugin\authentication\Authentication::TOKEN_VALUE),
'data' => array(
'article' => array(
'label' => 'no',
),
),
);
drupal_add_js($settings, 'setting');
drupal_add_js("window.restfulExampleModules = ['ng-admin', 'restangular']", array(
'type' => 'inline',
// Make sure it is available when constructing the angular module.
'scope' => 'header',
'group' => JS_LIBRARY - 10,
));
// Theme function simply declares the angular app, and ng-includes the app's
// view.
$app_path = drupal_get_path('module', 'restful_angular_example') . '/components/restful-app/dist';
$url = url($app_path . '/views/admin.html', array('absolute' => TRUE));
// Add the library.
drupal_add_library('restful_angular_example', 'restful-angular-admin');
}
if (empty($url)) {
return t('Example not found.');
}
return theme('restful_angular_example_angular', array('url' => $url, 'controller' => $controller_name));
}