This repository was archived by the owner on Feb 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrang.js.coffee
114 lines (96 loc) · 2.58 KB
/
rang.js.coffee
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
# dependency - Function.prototype.bind or underscore/lodash
class @Rang
@inject: (args...) ->
if args.length is 1
args = args[0].split ' '
if @$inject?.length > 0
for arg in args
@$inject.push arg
else
@$inject = args
@conf:
app: null
constructor: (args...) ->
if @constructor.$inject?
for key, index in @constructor.$inject
@[key] = args[index]
##
# Base is now used as mixin for shearing
# register between ctrl srv
# but not drt (directive aren't register)
# TODO find a way to factorize the 2 first line
#
class RangBase
@register: (app, name) ->
app = @conf.app unless app?
name ?= @name || @toString().match(/function\s*(.*?)\(/)?[1]
switch name.match(/[A-Z]*[^A-Z]+/g)[-1..][0]
when 'Ctrl'
app.controller name, @
when 'Srv'
app.service name, @
class @RangCtrl extends @Rang
@register: RangBase.register
constructor: (args...) ->
super args...
@s = @$scope
for key, fn of @constructor.prototype
continue unless typeof fn is 'function'
continue if key in ['constructor', 'initialize'] or key[0] is '_'
@s[key] = fn.bind?(@) || _.bind(fn, @)
@initialize?()
class @ScopeCtrl extends @RangCtrl
@inject '$scope'
##
# Service
#
#
class @RangSrv extends @Rang
@register: RangBase.register
constructor: (args...) ->
super args...
@initialize?()
class @RestSrv extends @RangSrv
@inject 'Restangular'
constructor: (args...) ->
super args...
@api = @Restangular.one 'api/v1'
##
# Directive
#
class @RangDrt
@register: ->
window.Bp.directive @toDrtName(), =>
@drt()
@toDrtName: ->
# TODO or @toString
name = @name.split('Drt')[0]
start = name[0].toLowerCase()
start + name.substr(1)
##
# ONDO Directive with conf and controller
#
class @RangCtrlDrt extends @Rang
@register: (app, name) ->
name ?= @name or @toString().match(/function\s*(.*?)\(/)?[1]
drtName = name.match(/[A-Z]*[^A-Z]+/g)
drtName = drtName[0...drtName.length-2].join ''
#console.log drtName
@conf.app.directive drtName, ->
restrict: "ACE"
remplace: true
templateUrl: "/rang_templates/#{drtName}"
scope: false
#controller: this
constructor: (args...) ->
super args...
@s = @$scope
for key, fn of @constructor.prototype
continue unless typeof fn is 'function'
continue if key in ['constructor', 'initialize'] or key[0] is '_'
@s[key] = fn.bind?(@) || _.bind(fn, @)
@initialize?()
class @ScopeCtrlDrt extends @RangCtrlDrt
@inject '$scope'
###
###