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 | {gateways,carriers} = $root
|
48 | assert gateways?, 'gateways is required'
|
49 | assert carriers?, 'carriers is required'
|
50 |
|
51 | chosen = if value.source_registrant() is true
|
52 | 'registrant'
|
53 | else if value.carrierid()?
|
54 | 'carrier'
|
55 | else if value.gwid()?
|
56 | 'gateway'
|
57 | @chosen = ko.observable chosen
|
58 | @name = "rule-entry-chosen-#{Math.random()}"
|
59 |
|
60 | @gwid = value.gwid
|
61 | @carrierid = value.carrierid
|
62 |
|
63 | gateway_valid = (id) -> id? and id in gateways
|
64 | carrier_valid = (id) -> id? and id in carriers
|
65 |
|
66 | @valid = ko.computed =>
|
67 | switch @chosen()
|
68 | when 'registrant'
|
69 | @gwid null
|
70 | @carrierid null
|
71 | true
|
72 | when 'gateway'
|
73 | @carrierid null
|
74 | gateway_valid @gwid()
|
75 | when 'carrier'
|
76 | @gwid null
|
77 | carrier_valid @carrierid()
|
78 | else
|
79 | @gwid null
|
80 | @carrierid null
|
81 | false
|
82 |
|
83 | Flow the data back to the model.
|
84 |
|
85 | @chosen.subscribe =>
|
86 | value.source_registrant @chosen() is 'registrant'
|
87 | @valid.subscribe (is_valid) =>
|
88 | value._validated is_valid
|
89 |
|
90 | return
|
91 |
|
92 | HTML
|
93 | ----
|
94 |
|
95 | @html ({a,ul,li,label,input,text}) ->
|
96 | ul '.target', ->
|
97 | li '.choice', ->
|
98 | label ->
|
99 | input
|
100 | type:'radio'
|
101 | value:'registrant'
|
102 | required:true
|
103 | bind:
|
104 | checked: 'chosen'
|
105 | attr: '{name:name}'
|
106 | text 'Use Registrant'
|
107 | li '.choice', ->
|
108 | label ->
|
109 | input
|
110 | type:'radio'
|
111 | value:'carrier'
|
112 | bind:
|
113 | checked: 'chosen'
|
114 | attr: '{name:name}'
|
115 | required:true
|
116 | text 'Use Carrier '
|
117 | input
|
118 | list:'carrier'
|
119 | name: 'carrierid'
|
120 | bind:
|
121 | value: 'carrierid'
|
122 | enable: 'chosen() === "carrier"'
|
123 | required:true
|
124 | li '.choice', ->
|
125 | label ->
|
126 | input
|
127 | type:'radio'
|
128 | value:'gateway'
|
129 | bind:
|
130 | checked: 'chosen'
|
131 | attr: '{name:name}'
|
132 | required:true
|
133 | text 'Use Gateway '
|
134 | input
|
135 | list:'gateway'
|
136 | name: 'gwid'
|
137 | bind:
|
138 | value: 'gwid'
|
139 | enable: 'chosen() === "gateway"'
|
140 | required: true
|
141 |
|
142 | teacup = require 'teacup'
|
143 | teacup.use (require 'teacup-databind')()
|
144 | assert = require 'assert'
|