forked from pbastowski/angular2-now
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular2-now.js
710 lines (570 loc) · 28.3 KB
/
angular2-now.js
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
var angular2now = function () {
'use strict';
var angular2now = {
SetModule: SetModule,
Component: Component,
Directive: Component,
ScopeShared: ScopeShared,
ScopeNew: ScopeNew,
View: View,
Inject: Inject,
Controller: Controller,
Service: Service,
Filter: Filter,
Injectable: Service,
bootstrap: bootstrap,
State: State,
options: options,
Options: Options,
MeteorMethod: MeteorMethod
};
var isCordova = typeof cordova !== 'undefined';
var inj = angular.injector(['ng']);
var $q = inj.get('$q');
var currentModule;
var currentNameSpace;
var controllerAs;
var ng2nOptions = {
currentModule: function() { return currentModule; }
};
var angularModule = angular.module;
// Monkey patch angular.module
angular.module = SetModule;
function SetModule () {
/**
* Name-spacing applies to provider names, not modules. Each module
* has to have a unique name of it's own.
*
* A namespace may be specified like this:
* SetModule('ftdesiree:helpers')
* The namespace, once set, will remain in force until removed.
* Remove the namespace like this:
* angular.module(':helpers')
**/
currentModule = arguments[0].split(':');
if (currentModule.length === 1) {
// No namespace, just the module name
currentModule = currentModule[0];
} else {
// Split off the name-space and module name
currentNameSpace = currentModule[0];
currentModule = currentModule[1];
// Reassign arguments[0] without the namespace
arguments[0] = currentModule;
}
return angularModule.apply(angular, arguments);
}
// Create a new name from the concatenation of
// the currentNameSpace and the name argument
function nameSpace(name) {
var nsName = name;
if (currentNameSpace) {
//nsName = camelCase(currentModule) + '.' + name;
nsName = currentNameSpace + '_' + name;
}
return nsName;
}
// Cancels out the automatic creation of isolate scope for the directive,
// because Angular 1.x allows only one isolate scope directive per element.
// This is useful when actually creating directives, which add behaviour
// to an existing element, as opposed to components which are stand alone
// bits of html and behaviour.
// The other way to do this is to pass "scope: undefined" to @Component.
function ScopeShared (target) {
target.scope = undefined;
return target
}
// Requests a new scope to be created when the directive is created.
// The other way to do this is to pass "scope: true" to @Component.
function ScopeNew (target) {
target.scope = true;
return target
}
//function Directive(options) {
//
// // A string passed is assumed to be the attribute name of the directive.
// if (typeof options === 'string')
// options = { selector: options };
//
// // Directives have shared scope by default (scope:undefined).
// // Optionally they can have a new scope created (scope: true).
// // If you require an isolate scope for your directive then
// // pass "scope: { ... }" in options.
// if (options && !options.hasOwnProperty('scope'))
// angular.merge(options, { scope: undefined });
//
// return Component(options);
//}
function Component(options) {
options = options || {};
// Allow shorthand notation of just passing the selector name as a string
if (typeof options === 'string')
options = { selector: options };
return function (target) {
// service injections, which could also have been specified by using @Inject
if (options.injectables && options.injectables instanceof Array)
target = Inject(options.injectables)(target);
// Selector name may be prefixed with a '.', in which case "restrict: 'C'" will be used
options.selector = camelCase(options.selector || '') + '';
if (options.selector[0] === '.') {
var isClass = true;
options.selector = options.selector.slice(1);
}
// Save the unCamelCased selector name, so that bootstrap() can use it
target.selector = unCamelCase(options.selector);
// The template can be passed in from the @View decorator
options.template = target.template || undefined;
options.templateUrl = target.templateUrl || undefined;
// Build the require array.
// Our controller needs the same injections as the component's controller,
// but with the "@*" injections renamed to "$scope". The link function will pass
// the "@*" injections directly to the component controller.
var requiredControllers = [options.selector];
target.$inject = target.$inject || [];
target.$inject = target.$inject.map(function(dep) {
if ( /^@[^]{0,2}/.test(dep[0]) ) {
requiredControllers.push('?' + dep.slice(1));
dep = 'delete-me'
}
return dep;
});
// Remove all the 'delete-me' entries
target.$inject = target.$inject.filter(function(v) { return v !== 'delete-me'; });
// Remember the original $inject, as it will be needed in the link function.
// In the link function we will receive any requested component controllers
// which we will then inject into the arguments that we will pass to the
// actual constructor of our component.
target.$injectDefer = target.$inject || [];
// Create the angular directive
var ddo = {
restrict: options.restrict || (options.template + options.templateUrl) ? 'EA' : isClass ? 'C' : 'A',
controllerAs: options.controllerAs || controllerAs || target.controllerAs || options.selector,
scope: target.hasOwnProperty('scope') ? target.scope : options.hasOwnProperty('scope') ? options.scope : options['bind'] || {},
bindToController: target.bindToController || true,
template: options.template,
templateUrl: options.templateUrl,
controller: target,
replace: options.replace || false,
transclude: /ng-transclude/i.test(options.template) || target.transclude,
require: options.require || target.require || requiredControllers,
link: options.link || target.link || link
};
try {
angular.module(currentModule)
.directive(options.selector, function () {
return ddo;
});
} catch (er) {
throw new Error('Does module "' + currentModule + '" exist? You may need to use angular.module("youModuleName").');
}
return target;
function link(scope, el, attr, controllers) {
// Create a service with the same name as the selector
// That holds a reference to our component
//angular.module(currentModule).value(camelCase(target.selector), controllers[0]);
// Alternate syntax for the injection of other component's controllers
if (controllers[0].$dependson) {
controllers[0].$dependson.apply(controllers[0], controllers.slice(1));
}
}
};
}
// Does a provider with a specific name exist in the current module
function serviceExists(serviceName) {
return !!getService(serviceName);
}
function getService(serviceName, moduleName) {
if (!moduleName)
moduleName = currentModule;
return angular.module(moduleName)
._invokeQueue
.filter(function (v, i) {
return v[0] === '$provide' && v[2][0] === serviceName
})[0];
}
function camelCase(s) {
return s.replace(/-(.)/g, function (a, b) {
return b.toUpperCase();
});
}
function unCamelCase(c) {
var s = c.replace(/([A-Z])/g, '-$1')
.replace(/--/g, '-')
.toLowerCase();
if (s[0] === '-') s = s.slice(1);
return s;
}
// 2015-09-01 Replaced the whole Inject function with a new more flexible version.
// Thanks to Steven Weingärtner for his code, which works with both Classes and Methods,
// as well as preserving injectables from a parent class (when extending a parent class).
// New features:
// - Dependencies can be passed in as arguments, not requiring the array wrapper. The
// original syntax with the array wrapper is still supported.
// - Methods of a class can now be Injected also
// - Child classes will inherit the parent class's injectables, which will be appended
// to the end of the child's dependencies
function Inject() {
var deps;
if (arguments[0] instanceof Array)
deps = arguments[0];
else
deps = Array.prototype.slice.call(arguments);
if (deps.length === 0) {
throw new Error('@Inject: No dependencies passed in');
}
return function (target, name, descriptor) {
var injectable = target;
if (descriptor)
injectable = descriptor.value;
if (!injectable)
throw new TypeError('@Inject can only be used with classes or class methods.')
var existingInjects = injectable.$inject;
injectable.$inject = [];
angular.forEach(deps, function (dep) {
// Namespace any injectables without an existing nameSpace prefix and also
// not already prefixed with '$', '@' or '@^'.
if (dep[0] !== '$' && dep[0] !== '@' && dep.indexOf('_') === -1)
dep = nameSpace(dep);
if (injectable.$inject.indexOf(dep) === -1) {
injectable.$inject.push(dep);
}
});
if (existingInjects) {
injectable.$inject = injectable.$inject.concat(existingInjects);
}
return descriptor || target;
};
}
function View(options) {
options = options || {};
// Allow shorthand notation of just passing the templateUrl as a string
if (typeof options === 'string')
options = { templateUrl: options };
if (!options.template) options.template = undefined;
return function (target) {
target.template = options.template;
target.templateUrl = options.templateUrl;
// When a templateUrl is specified in options, then transclude can also be specified
target.transclude = options.transclude;
// directives is an array of child directive controllers (Classes)
target.directives = options.directives;
// Check for the new <content> tag and add ng-transclude to it, if not there.
if (target.template)
target.template = transcludeContent(target.template);
return target;
};
// If template contains the new <content> tag then add ng-transclude to it.
// This will be picked up in @Component, where ddo.transclude will be set to true.
function transcludeContent(template) {
var s = (template || '').match(/\<content[ >]([^\>]+)/i);
if (s) {
if (s[1].toLowerCase().indexOf('ng-transclude') === -1)
template = template.replace(/\<content/i, '<content ng-transclude');
}
return template;
}
}
function Controller(options) {
options = options || {};
// Allow shorthand notation of just passing the name as a string
if (typeof options === 'string')
options = { name: options };
return function (target) {
angular.module(currentModule)
.controller(nameSpace(options.name), target);
return target;
};
}
function Service(options) {
options = options || {};
// Allow shorthand notation of just passing the name as a string
if (typeof options === 'string')
options = { name: options };
return function (target) {
angular.module(currentModule)
.service(nameSpace(options.name), target);
//.factory(options.name, function () { return new target })
return target;
};
}
function Filter(options) {
options = options || {};
// Allow shorthand notation of just passing the name as a string
if (typeof options === 'string')
options = { name: options };
return function (target) {
filterFunc.$inject = target.$inject;
angular.module(currentModule)
.filter(nameSpace(options.name), filterFunc);
function filterFunc() {
var args = Array.prototype.slice.call(arguments);
var f = new (Function.prototype.bind.apply(target, [null].concat(args)));
return f;
}
return target;
};
}
/**
* Bootstraps the Angular 1.x app.
*
* @param ?target undefined | string | class
* undefined: bootstraps on document and the current angular module
* string: will use document.querySelector to find the element by this string
* class: bootstraps on the component defined on this class, looks for selector
*
* @param ?config angular.bootstrap() config object, see AngularJS doco
*/
function bootstrap(target, config) {
if (!target || (target && !target.selector && typeof target === 'function')) {
target = {selector: currentModule};
var bootOnDocument = true;
}
// Allow string shortcut for target.selector. Can be the name of any HTML tag.
if (typeof target === 'string') {
target = {selector: target};
}
// Mark this class as a bootstrap component. This allows @State
// to handle it correctly.
target.bootstrap = true;
var bootModule = target.selector || currentModule;
if (bootModule !== currentModule)
angular.module(bootModule);
if (!config)
config = {strictDi: false};
if (!Meteor) var Meteor = {};
if (isCordova)
angular.element(document).on("deviceready", onReady);
else
angular.element(document).ready(onReady);
function onReady() {
// Find the component's element
if (!bootOnDocument)
var el = document.querySelector(target.selector);
// Or use document, if user passed no arguments
else
var el = document.body;
angular.bootstrap(el, [bootModule], config);
}
}
/**
* State can be used to annotate either a Component or a class and assign
* a ui-router state to it.
*
* @param options literal object
* name: name of the state
* url: url associated with this state
* template: template
* templateUrl: templateUrl
* templateProvider: templateProvider
* defaultRoute: truthy = .otherwise(url)
* string = .otherwise(defaultRoute)
* resolve: Literal object, see ui-router resolve
* abstract: true/false
* params: Literal object, see ui-router doco
* controller: A controller is automatically assigned, but if you need
* finer control then you can assign your own controller
* controllerAs: Specify ControllerAs for cases when there is no
* @Component used
*
* If a class is annotated then it is assumed to be the controller and
* the state name will be used as the name of the injectable service
* that will hold any resolves requested.
*
* When a component is annotated and resolves requested, then the component's
* selector name is used as the name of the injectable service that holds
* their values.
*/
function State(options) {
if (!options || !(options instanceof Object) || options.name === undefined)
throw new Error('@State: Valid options are: name, url, defaultRoute, template, templateUrl, templateProvider, resolve, abstract, data.');
return function (target) {
var deps;
var resolvedServiceName = nameSpace(camelCase(target.selector || (options.name+'').replace('.', '-')));
// Indicates if there is anything to resolve
var doResolve;
// Values to resolve can either be supplied in options.resolve or as a static method on the
// component's class
var resolves = options.resolve || target.resolve;
// Is there a resolve block?
if (resolves && resolves instanceof Object && (deps = Object.keys(resolves)).length)
doResolve = true;
// Create an injectable value service to share the resolved values with the controller
// The service bears the same name as the component's camelCased selector name.
if (doResolve) {
if (!serviceExists(resolvedServiceName)) {
angular.module(currentModule).value(resolvedServiceName, {});
}
}
// Configure the state
angular.module(currentModule)
.config(['$urlRouterProvider', '$stateProvider', '$locationProvider',
function ($urlRouterProvider, $stateProvider, $locationProvider) {
// Activate this state, if options.defaultRoute = true.
// If you don't want this then don't set options.defaultRoute to true
// and, instead, use $state.go inside the constructor to active a state.
// You can also pass a string to defaultRoute, which will become the default route.
if (options.defaultRoute)
$urlRouterProvider.otherwise((typeof options.defaultRoute === 'string') ? options.defaultRoute : options.url);
// Optionally configure html5Mode
if (! (typeof options.html5Mode === 'undefined'))
$locationProvider.html5Mode(options.html5Mode);
// The user can supply a controller through a parameter in options
// or the class itself can be used as the controller if no component is annotated.
var userController = options.controller || (!target.selector ? target : undefined);
// Also, de-namespace the resolve injectables for ui-router to inject correctly
if (userController && userController.$inject && userController.$inject.length && deps && deps.length) {
deps.forEach(function(dep) {
var i = userController.$inject.indexOf(currentNameSpace+'_'+dep);
if (i!==-1)
userController.$inject[i] = dep;
});
}
// This is the state definition object
var sdo = {
url: options.url,
// Default values for URL parameters can be configured here.
// ALso, parameters that do not appear in the URL can be configured here.
params: options.params,
// The State applied to a bootstrap component can be abstract,
// if you don't want that state to be able to activate.
abstract: options.abstract,
templateUrl: options.templateUrl,
// This is the "inline" template, as opposed to the templateUrl.
// 1) If either options.templateUrl or options.templateProvider is specified then
// template will be set to undefined.
// 2) If options.template is provided then it will be used.
// 3) Otherwise, if this is a component, but not the bootstrap(**) component,
// then we use it's selector to create the inline template "<selector></selector>".
// 4) Otherwise, we provide the following default template "<div ui-view></div>".
//(**) The bootstrap component will be rendered by Angular directly and must not
// be rendered again by ui-router, or you will literally see it twice.
// todo: allow the user to specify their own div/span instead of forcing "div(ui-view)"
template: options.templateUrl || options.templateProvider ? undefined : options.template || ((target.template || target.templateUrl) && !target.bootstrap && target.selector ? target.selector.replace(/^(.*)$/, '<$1></$1>') : '<div ui-view=""></div>'),
// The option for dynamically setting a template based on local values
// or injectable services
templateProvider: options.templateProvider,
// Do we need to resolve stuff? If so, then we also provide a controller to catch the resolved data.
resolve: resolves,
// A user supplied controller OR
// An internally created proxy controller, if resolves were requested for a Component.
controller: userController || (doResolve ? controller : undefined),
// Optionally controllerAs can be specifically set for those situations,
// when we use @State on a class and there is no @Component defined.
controllerAs: target.controllerAs || options.controllerAs || (ng2nOptions.hasOwnProperty('controllerAs') && !target.hasOwnProperty('selector') ? ng2nOptions.controllerAs : undefined),
// onEnter and onExit events
onEnter: options.onEnter,
onExit: options.onExit,
// Custom data
data: options.data
};
// Create the state
$stateProvider.state(options.name, sdo);
// When our automatic controller is used, we inject the resolved values into it,
// along with the injectable service that will be used to publish them.
// If the user supplied a controller than we do not inject anything
if (doResolve) {
deps.unshift(resolvedServiceName);
controller.$inject = deps;
}
// Populate the published service with the resolved values
function controller() {
var args = Array.prototype.slice.call(arguments);
// This is the service that we "unshifted" earlier
var localScope = args[0];
args = args.slice(1);
// Now we copy the resolved values to the service.
// This service can be injected into a component's constructor, for example.
deps.slice(1).forEach(function (v, i) {
localScope[v] = args[i];
});
}
}]);
return target;
};
}
// Allow configuration of some angular2-now default settings
// controllerAs: if provided, will user this string instead of component name, for example "vm"
function options(options) {
if (!options) return ng2nOptions;
if (typeof options.controllerAs !== 'undefined') {
controllerAs = options.controllerAs;
}
// Optional spinner object can be registered. It must expose show() and hide() methods.
// The spinner will be activated before any I/O operations and deactivated once they complete.
ng2nOptions.spinner = options.spinner || {show: angular.noop, hide: angular.noop};
// events expose beforeCall() and afterCall().
// beforeCall() will be called before any I/O operations.
// afterCall() will be called after any I/O operations have completed.
ng2nOptions.events = options.events || {beforeCall: angular.noop, afterCall: angular.noop};
// The noConflict option allows us to control whether or not angular2-now
// monkey-patches angular.module.
// true = don't monkey patch.
// false = (default for versions < 0.4.0) DO monkey patch angular.module
// for backwards compatibility
if (typeof options.noConflict !== 'undefined') {
if (options.noConflict)
angular.module = angularModule;
else
angular.module = SetModule;
}
}
function Options(options) {
return function(target) {
angular.merge(ng2nOptions, options);
return target;
}
}
// The name of the Meteor.method is the same as the name of class method.
function MeteorMethod(_options) {
var options = angular.merge({}, _options, ng2nOptions);
var spinner = options.spinner || {show: angular.noop, hide: angular.noop};
var events = options.events || {beforeCall: angular.noop, afterCall: angular.noop};
return function (target, name, descriptor) {
// Create a method that calls the back-end
descriptor.value = function () {
var argv = Array.prototype.slice.call(arguments);
var deferred = $q.defer();
if (typeof spinner === 'string') {
if (angular.injector(['ng', currentModule]).has(options.spinner)) {
spinner = angular.injector(['ng', currentModule]).get(options.spinner);
options.spinner = spinner;
} else
throw new Error('Spinner "' + spinner + '" does not exist.');
}
argv.unshift(name);
argv.push(resolver);
if (spinner) spinner.show();
events.beforeCall(); // Call optional events.beforeCall()
// todo: should call Meteor after resolution of promise returned by beforeCall()
Meteor.call.apply(this, argv);
deferred.promise.finally(function() {
spinner.hide();
options.events.afterCall(); // Call optional events.afterCall()
});
return deferred.promise;
function resolver(err, data) {
if (err)
deferred.reject(err);
else
deferred.resolve(data);
}
};
return descriptor;
}
}
return angular2now;
}();
// Node.js style
if (typeof module !== 'undefined' && module.exports) {
module.exports = angular2now;
}
// AMD / RequireJS
else if (typeof define !== 'undefined' && define.amd) {
define('angular2now', [], function () {
return angular2now;
});
}
// included directly
else {
this.angular2now = angular2now;
}