forked from squareplanetdesign/cpanel_todo_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutline.txt
executable file
·678 lines (516 loc) · 18.9 KB
/
outline.txt
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
Outline:
Start with an idea.
Build it leveraging as much cPanel and third-party infrastructure as possible.
* these were the same principals we used to develop the cjt2 framework.
What will not be show in detail:
* how the backend is implemented. Its in the repo, but is just a prototype.
Survey:
* Who here has created a plugin for cPanel?
* Who here has tried to build their plugin with AngularJS and cjt 2.0?
Focus:
* Building a modern JavaScript application as part of a cPanel plugin.
* Third Party Components:
* AngularJS 1.4.x
* Boostrap 3 & FontAwsome
* requirejs
* jquery
* cPanel Components/Resources
* cPanel Style Guide: https://styleguide.cpanel.net
* cjt 2.0 - library of reusable JavaScript and Angular components.
* UAPI/CGI
What are each of these components?
* AngularJS - mvvc - https://angularjs.org/
* Its an opinionated way to build frontend applications.
* You do it the way the developers intended or you will face some problems.
* Lots of example code, tutorials and free tools and libraries built on top of angular.
What are each of these components?
* Bootstrap 3 - http://getbootstrap.com/
* A library of design layout components that make writting mobile friendly apps easier
* Its complicated once you get past the basics.
* To get your apps to look like cPanels, use our style guide that is based on bootstrap.
https://styleguide.cpanel.net
* Things we use in cpanel from bootstrap: buttons, wells, alerts, grid, forms, glyph icons.
* We also use FontAwsome for additional glyph icons.
What are each of these components?
* requirejs - http://requirejs.org/
* Its a javascript to for loading application dependencies.
* You write small single purpose modules
* The tool will get them all loaded for you.
* requirejs comes with a specialized code optimizer that will help you build optimized resources for you site. (r.js)
What are each of these components?
* jquery - https://jquery.com/
* A very helpful set of DOM minipulators and AJAX tools.
* You usually don't need this with angularjs, but its available for some more advanced scenarios.
What are each of these components?
* cjt 2.0 - Custom JavaScript and Angular components developed by cPanel
* Available to us and to you.
* Substantial documentation in the JavaScript files.
* /usr/local/cpanel/share/libraries/cjt2/src
* We distribute both a production build and debug versions of cjt 2.0 with the product.
What are each of these components?
* UAPI - cpanels preferred API model for developing applications for cPanel and Webmail
A note about where to find stuff in cPanel folders.
/usr/local/cpanel - base directory for all cpanel code.
/usr/local/cpanel/Cpanel - perl modules related to the product. Any custom modules you write will be somewhere here. Plus you can use anything in here to build your app.
/usr/local/cpanel/base/frontend/paper_lantern - all front end resource will be under this folder for cPanel.
/usr/local/cpanel/base/frontend/paper_lantern/libraries - third party libraries and cjt2 live here.
/usr/local/cpanel/base/webmail/paper_lantern - all front end resource will be under this folder for Webmail.
/usr/local/cpanel/base/webmail/paper_lantern/libraries - third party libraries and cjt2 live here.
/usr/local/cpanel/whostmgr/docroot - all front end resource will be under this folder for WHM.
/usr/local/cpanel/whostmgr/docroot/templates - all cjt2 frontend app resource will be under this folder for WHM.
/usr/local/cpanel/whostmgr/docroot/libraries - third party libraries and cjt2 live here.
Where your plugin should put your files:
* Plugins are just code (perl/php/python for your backend) + html templates, css, javascript, and images.
* Few requirements about where you put your files on disk within the cPanel directories.
* Some folders are important:
/usr/local/cpanel/Cpanel/API - Your modules implementing UAPI calls must go here.
* Other not so much. Lets put our cPanel application here:
/usr/local/cpanel/base/frontend/paper_lantern/thirdparty/todo
A quick note about how we organize our apps:
* Within a cjt 2.0 application folder, we create the following folders:
todo/directives - angular directives (js)
todo/filters - angular filters (js)
todo/views - view partials (html/tt2) and controllers (js)
todo/models - sometimes not present if not need.
todo/index.html.tt - the main application template
todo/index.css - the css file for the application, provides any custom styling not provided by bootstrap or the paper_lantern style system.
todo/index.js - startup code for you application
todo/index.dist.js - production boot-strapper, will attempt to load minified/combined files.
todo/index.devel.js - debugging boot-strapper, will load all non-minified JavaScript modules as individual files.
Lets start writing our first angular application for cpanel.
Start with index.html.tt
* Its the template for our applications main body.
* It uses the master template just like our cPanel applications so it fits right into the product.
* Its mostly boiler plate.
>> Here is an example
[%
SET CPANEL.CPVAR.dprefix = "../";
WRAPPER '_assets/master.html.tt'
app_key = 'plugin_cpanel_todo' # defines the apps name in the dynamicui.conf
# for the plugin.
include_legacy_stylesheets = 0 # we want only the newest stuff so no
include_legacy_scripts = 0 # we want only the newest stuff so no
include_cjt = 0 # we want only the newest stuff so no
include_lang = 0 # we want only the newest stuff so no
use_master_bootstrap = 0 # our application will take charge of
# initializing the master application in
# addition to our app.
page_stylesheets = [
'todo/index.css' # styles for our page
];
%]
<div class="body-content">
<div>
MADE IT
</div>
<div ng-controller="applicationController">
<div id="viewContent"
class="ng-cloak"
ng-view
ng-cloak></div>
</div>
</div>
>> git checkout
frontend/paper_lantern/plugins/cpanel/todo/index.html
Next lets get the bootstrappers and main application javascript file in place:
Create the following:
todo/index.js
todo/index.dist.js
todo/index.devel.js
>> Here is an example of each
* Running your app in production mode:
Production:
https://<server>:2082/<session>/frontend/paper_lantern/thirdparty/todo/index.html
* expects the presence of minified and combined files.
* Running your app in debug mode:
Development:
https://<server>:2082/<session>/frontend/paper_lantern/thirdparty/todo/index.html?debug=1
* Its slower to load since it loads all modules individually.
* Easier for development since you don't have to build each time.
Wiring the bootstrap code into your template
* PROCESS '_assets/cjt2_include.tmpl';
Create the index.dist.js:
// Loads the application with the pre-built combined files
require( ["frameworksBuild", "locale!cjtBuild", "app/index.cmb"], function() {
require(
[
"master/master",
"app/index"
],
function(MASTER, APP) {
MASTER();
APP();
}
);
});
Create the index.js:
require(
[
// Application Modules
"cjt/views/applicationController",
], function() {
var appContent = angular.element("#content");
if(appContent[0] !== null){
// apply the app after requirejs loads everything
angular.bootstrap(appContent[0], ["App"]);
}
}
);
>> Lets try it
Next add our first view
* todo/views/todosView.ptt
* todo/views/todosController.js
Wiring your view into the application
* Add the require line:
app/views/todoController
app - maps to your applications current directory
require([
// Application Modules
"cjt/views/applicationController",
"app/views/todosController"
], function() {...});
User some cjt2 components: (Step 4)
* You can also bring in cjt2 components
cjt/directive/alertDirective
cjt/services/alertService
cjt - maps to the paper_lantern/libraries/cjt2 or paper_lantern/libraries/cjt2-dist folders.
Lets get it listing something: (Step 5)
[
{
id: 1,
subject: 'wash the car',
created: 12345678,
updated: 12345678,
doned: null,
description: 'remember you have a coupon',
status: 1,
},
{
id: 2,
subject: 'take out the trash',
created: 12345678,
updated: 12345678,
doned: null,
description: 'do not forget the upstairs bath room again',
status: 1,
},
{
id: 3,
subject: 'record the starwars marathon on tv',
created: 12345633,
updated: 12345639,
doned: 12345641,
description: 'What channel?',
status: 2,
}
]
Wiring hard-coded data in the app:
* Services are for managing data
* They are stateless usually.
>> app/services/todoAPI.js
return {
list: function() {
return data;
}
}
Now lets get the view working with the hardcoded data:
* load app/services/todoAPI into the controller
* call the api method
* store in $scope so the view can access them.
$scope.todos = todoAPI.list();
And finally wire in the UI
<div class="container">
<div class="row">
<div class="col-xs-11">
Description
</div>
<div class="col-xs-1">
Done
</div>
</div>
<div class="row" ng-repeat"todo in todos">
<div class="col-xs-11">
{{todo.subject}}
</div>
<div class="col-xs-1">
<input type="checkbox"
ng-model="todo.status"
ng-true-value="2"
ng-false-value="1"
ng-checked="todo.status === 2">
</div>
<div class="col-xs-12">
{{todo.description}}
</div>
</div>
</div>
And run it:
>> ???
Now wire in the service to the actual backend:
service:
cjt/api,
cjt/api/uapi,
cjt/api/uapiRequest,
// Add a factory that handles the APIs
app.factory("todoAPI", [
"$q",
"APIService",
function(
$q,
APIService
) {
// Set up the service's constructor and parent
var TodoService = function() {};
TodoService.prototype = new APIService();
// Extend the prototype with any class-specific functionality
angular.extend(TodoService.prototype, {
list : function() {
var apiCall = new APIREQUEST.Class();
apiCall.initialize("Todo", "list_todos");
return this.deferred(apiCall).promise;
}
});
return new TodoService();
}
]);
controller:
todoAPI.list().then(function(resp) {
$scope.todos = resp.data;
}).catch(function(error) {
// Do something with the error
});
Run it again.
>> ???
Add a todo:
Router:
$routeProvider.when("/todo/new", {
controller: "todosController",
templateUrl: CJT.buildFullPath("plugins/cpanel/todo/views/addTodoView.ptt")
});
todosView.ptt
<button ng-click="open_add_view()">New Item</button>
Controller:
$scope.open_add_view = function() {
$scope.newTodo = {
subject: "",
description: ""
};
$scope.loadView("todo/new");
};
$scope.add = function(todo) {
todoAPI.mark(todo.id).then(function(resp) {
$scope.todos.push(resp.data);
});
};
addTodoView.ptt
<div>
<div>
<label>Subject:</label>
</div>
<div>
<input type="text" ng-model="newtodo.subject">
</div>
<div>
<label>Description:</label>
</div>
<div>
<textarea ng-model="newtodo.description" rows="3" cols="50"></textarea>
</div>
<div>
<button ng-click="add(newtodo)">Save</button>
</div>
</div>
Service:
add: function(todo) {
var request = new APIREQUEST.Class();
request.initialize("Todo", "add_todo");
request.addArgument("subject", todo.subject);
request.addArgument("description", todo.description);
return this.deferred(request).promise;
}
Mark a todo as done
mark: function(todo) {
var request = new APIREQUEST.Class();
request.initialize("Todo", "mark_todo");
request.addArgument("id", todo.id);
request.addArgument("status", todo.status);
return this.deferred(request).promise;
}
controller:
$scope.mark = function(todo) {
todoAPI.mark(todo).then(function(resp) {
todo.status = resp.data.status;
todo.doned = resp.data.doned;
todo.changed = resp.data.changed;
}).catch(function(error) {
alertService.add({
type: "danger", id: "markFailed",
message: "Failed: " + error
});
});
};
template:
<input type="checkbox"
ng-model="todo.status"
ng-true-value="2"
ng-false-value="1"
ng-checked="todo.status === 2"
ng-change="mark(todo)">
Filter todos
Add the filter controls:
<div class="col-xs-12 col-sm-6 col-md-5">
<input type="text"
ng-model="subjectFilter"
placeholder="Search">
</div>
<div class="col-xs-12 col-sm-6 col-md-5">
<label>
<input type="checkbox"
ng-model="hideDone"
ng-checked="todo.status === 2" >
<span>Hide Done</span>
</label>
</div>
Add the filter:
<div class="row" ng-repeat"todo in todos | filter: by_subject_and_status track by todo.id ">
// Hide done items initially
$scope.hideDone = true;
var pattern, patternSubject;
$scope.by_subject_and_status = function(todo) {
if (!pattern || patternSubject !== $scope.subjectFilter) {
pattern = new RegExp($scope.subjectFilter);
}
if ($scope.hideDone) {
return pattern.test(todo.subject) && todo.status !== 2;
} else {
return pattern.test(todo.subject);
}
};
Edit a todo.
Service:
update: function(todo) {
var request = new APIREQUEST.Class();
request.initialize("Todo", "update_todo");
request.addArgument("id", todo.id);
request.addArgument("subject", todo.subject);
request.addArgument("description", todo.description);
return this.deferred(request).promise;
}
Controller:
$scope.edit = function(todo) {
if (todo.edit) {
todoAPI.update(todo).then(function(resp) {
todo.doned = resp.data.doned;
todo.changed = resp.data.changed;
todo.edit = false;
});
} else {
todo.edit = true;
}
};
View:
<strong ng-hide="todo.edit">{{todo.subject}}</strong>
<input ng-show="todo.edit" ng-model="todo.subject"/>
<strong ng-hide="todo.edit">{{todo.description}}</strong>
<input ng-show="todo.edit" ng-model="todo.description"/>
<button ng-click="edit(todo)">
<span ng-hide="todo.edit" class="glyphicon glyphicon-pencil"
title="Edit"></span>
<span ng-hide="todo.edit" class="glyphicon glyphicon-floppy-disk"
title="Save"></span>
</button>
Delete a todo.
Service:
return {
remove: function(todo) {
var request = new APIREQUEST.Class();
request.initialize("Todo", "remove_todo");
request.addArgument("id", todo.id);
return this.deferred(request).promise;
}
}
Controller:
[
"angular",
"lodash",
"cjt/services/alertService",
"app/services/todoAPI"
],
function(angular, _) {
$scope.remove = function(todo) {
todoAPI.remove(todo).then(function() {
var index = _.findIndex( $scope.todos, function(item) {
return todo.id === item.id;
});
if (index !== -1) {
$scope.todos.splice(index, 1);
}
}).catch(function(error) {
alertService.add({
type: "danger", id: "removeFailed",
message: error
});
});
};
View:
<div class="col-xs-12">
<span ng-if="!todo.edit">{{todo.description}}</span>
<textarea ng-if="todo.edit" ng-model="todo.description" rows="3" cols="50"></textarea>
<button ng-click="remove(item)">Delete</button>
</div>
Advanced topics:
Prefetch data:
* use an api in your main template to fetch your data before the page loads
SET todos = Uapi.exec('Todo', 'list_todos', {
'api.sort_column' => 'created',
});
<script type="text/javascript">
/* Optimization 1: Prefetch */
PAGE.todos = [% todos.json() %];
</script>
var todos = [];
// Optimization 1: Prefetch data
if (PAGE.todos && PAGE.todos.status) {
todos = PAGE.todos.data;
}
app.value("todoData", {
todos: todos
});
Prefetch your view partials
* embed your view partials in your main template.
[%# Optimization 2: Preload view partials %]
<script type="text/ng-template" id="views/todosView.ptt">
[% PROCESS 'views/todosView.ptt' %]
</script>
<script type="text/ng-template" id="views/addTodoView.ptt">
[% PROCESS 'views/addTodoView.ptt' %]
</script>
* Update the routers to load the cached templates
$routeProvider.when("/todos/", {
controller: "todosController",
templateUrl: "views/todosView.ptt"
});
$routeProvider.when("/todo/new", {
controller: "todosController",
templateUrl: "views/addTodoView.ptt"
});
Optimizing your application:
* node/npm
* grunt/gulp
* r.js
Localizing your applications:
cjt2/utils/locale - LOCALE.maketext()
[% locale.maketext() %]
bin/build_locale_database
Error handling with Promises:
promise.then(function(resp) {
// what to do when it didn't fail.
}).catch(function(error) {
// what to do when it did fail.
});
Alert Service to provide feedback:
cjt/service/alertService
cjt/directives/alertList
Spinners to show a long running async call is running
cjt/directives/actionButton
cjt/directives/spinner