1 | A rule target Knockout Widget for CCNQ(4)
|
2 | -----------------------------------------
|
3 |
|
4 | A rule target is an item in the `gwlist` field of a `rule` entry in a CCNQ4 `ruleset` database. Such a database is used by the [`tough-rate`](https://github.com/shimaore/tough-rate) LCR engine, especially its `routes-registrant`, `routes-carrierid`, `routes-gwid` middleware modules.
|
5 |
|
6 | This module adds a `rule-target` component in Knockout.
|
7 |
|
8 | Usage
|
9 | -----
|
10 |
|
11 | ```coffeescript
|
12 | {RuleTarget,rule_target} = (require 'ccnq-ko-rule-target') knockout
|
13 | ```
|
14 |
|
15 | Parameters:
|
16 | - value: the `gwlist` item
|
17 | - $root.gateways: a list of valid gateways
|
18 | - $root.carriers: a list of valid carriers
|
19 |
|
20 | module.exports = (require 'ccnq-ko') 'rule-target', (ko) ->
|
21 |
|
22 | class RuleTarget
|
23 | constructor: (data) ->
|
24 | assert data?, 'data is required'
|
25 |
|
26 | Data
|
27 | ----
|
28 |
|
29 | A `gwlist` item typically contains one of:
|
30 | - `source_registrant` -- if the rule should route through the caller's registrant;
|
31 | - `gwid` -- if the call is to be routed out through a given gateway;
|
32 | - `carrierid` -- if the call is to be routed out through a carrier (a set of gateways with similar costs).
|
33 |
|
34 | @source_registrant = ko.observable data.source_registrant
|
35 | @gwid = ko.observable data.gwid
|
36 | @carrierid = ko.observable data.carrierid
|
37 | @_validated = ko.observable false
|
38 | return
|
39 |
|
40 | View Model
|
41 | ----------
|
42 |
|
43 | We expect `params="value:$data,$root:$root"`. This means `value` is a `RuleTarget` object.
|
44 |
|
45 | @view ({value,$root}) ->
|
46 | # assert value instanceof RuleTarget, 'value should be an instance of RuleTarget'
|
47 | assert value?, 'value is required'
|
48 | {gateways,carriers} = $root
|
49 | assert gateways?, 'gateways is required'
|
50 | assert carriers?, 'carriers is required'
|
51 |
|
52 | chosen = if value.source_registrant() is true
|
53 | 'registrant'
|
54 | else if value.carrierid()?
|
55 | 'carrier'
|
56 | else if value.gwid()?
|
57 | 'gateway'
|
58 | @chosen = ko.observable chosen
|
59 | @name = "rule-entry-chosen-#{Math.random()}"
|
60 |
|
61 | @gwid = value.gwid
|
62 | @carrierid = value.carrierid
|
63 |
|
64 | gateway_valid = (id) -> id? and id in gateways
|
65 | carrier_valid = (id) -> id? and id in carriers
|
66 |
|
67 | @valid = ko.computed =>
|
68 | switch @chosen()
|
69 | when 'registrant'
|
70 | @gwid null
|
71 | @carrierid null
|
72 | true
|
73 | when 'gateway'
|
74 | @carrierid null
|
75 | gateway_valid @gwid()
|
76 | when 'carrier'
|
77 | @gwid null
|
78 | carrier_valid @carrierid()
|
79 | else
|
80 | @gwid null
|
81 | @carrierid null
|
82 | false
|
83 |
|
84 | Flow the data back to the model.
|
85 |
|
86 | @chosen.subscribe =>
|
87 | value.source_registrant @chosen() is 'registrant'
|
88 | @valid.subscribe (is_valid) =>
|
89 | value._validated is_valid
|
90 |
|
91 | return
|
92 |
|
93 | HTML
|
94 | ----
|
95 |
|
96 | @html ({a,ul,li,label,input,text}) ->
|
97 | ul '.target', ->
|
98 | li '.choice', ->
|
99 | label ->
|
100 | input
|
101 | type:'radio'
|
102 | value:'registrant'
|
103 | required:true
|
104 | bind:
|
105 | checked: 'chosen'
|
106 | attr: '{name:name}'
|
107 | text 'Use Registrant'
|
108 | li '.choice', ->
|
109 | label ->
|
110 | input
|
111 | type:'radio'
|
112 | value:'carrier'
|
113 | bind:
|
114 | checked: 'chosen'
|
115 | attr: '{name:name}'
|
116 | required:true
|
117 | text 'Use Carrier '
|
118 | input
|
119 | list:'carrier'
|
120 | name: 'carrierid'
|
121 | bind:
|
122 | value: 'carrierid'
|
123 | enable: 'chosen() === "carrier"'
|
124 | required:true
|
125 | li '.choice', ->
|
126 | label ->
|
127 | input
|
128 | type:'radio'
|
129 | value:'gateway'
|
130 | bind:
|
131 | checked: 'chosen'
|
132 | attr: '{name:name}'
|
133 | required:true
|
134 | text 'Use Gateway '
|
135 | input
|
136 | list:'gateway'
|
137 | name: 'gwid'
|
138 | bind:
|
139 | value: 'gwid'
|
140 | enable: 'chosen() === "gateway"'
|
141 | required: true
|
142 |
|
143 | teacup = require 'teacup'
|
144 | teacup.use (require 'teacup-databind')()
|
145 | assert = require 'assert'
|