UNPKG

3.86 kBMarkdownView Raw
1A rule target Knockout Widget for CCNQ(4)
2-----------------------------------------
3
4A 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
6This module adds a `rule-target` component in Knockout.
7
8Usage
9-----
10
11```javascript
12require('ccnq-ko-rule-target')(knockout);
13```
14
15```html
16<rule-target bind-data="params: data: ..., gateways: ..., carriers: ..."></rule-target>
17```
18
19Parameters:
20- data: the `gwlist` item
21- gateways: a list of valid gateways
22- carriers: a list of valid carriers
23
24 module.exports = (ko) ->
25 class RuleTarget
26 constructor: ({data,$root}) ->
27 {gateways,carriers} = $root
28 assert data?, 'data is required'
29 assert gateways?, 'gateways is required'
30 assert carriers?, 'carriers is required'
31
32Data
33----
34
35A `gwlist` item typically contains one of:
36- `source_registrant` -- if the rule should route through the caller's registrant;
37- `gwid` -- if the call is to be routed out through a given gateway;
38- `carrierid` -- if the call is to be routed out through a carrier (a set of gateways with similar costs).
39
40 @source_registrant = ko.observable data.source_registrant
41 @gwid = ko.observable data.gwid
42 @carrierid = ko.observable data.carrierid
43
44 @gatewayValid = ko.pureComputed => (not @gwid()?) or @gwid() in gateways
45 @carrierValid = ko.pureComputed => (not @carrierid()?) or @carrierid() in carriers
46
47 # Initial values
48 chosen = if @source_registrant() is true
49 'registrant'
50 else if @carrierid()?
51 'carrier'
52 else if @gwid()?
53 'gateway'
54 @chosen = ko.observable chosen
55 @carrier_chosen = ko.pureComputed => @chosen() is 'carrier'
56 @gateway_chosen = ko.pureComputed => @chosen() is 'gateway'
57 @visible = ko.pureComputed => @chosen() isnt 'none'
58
59FIXME: We need a way to let the component's user know whether the data is valid or not!
60
61 # Behaviors
62 return
63
64 html = ->
65 name = "rule-target-#{Math.random()}"
66 {a,ul,li,label,input,text} = teacup
67 ul '.target', bind: visible: 'visible', ->
68 li '.choice', ->
69 label ->
70 input
71 type:'radio'
72 name:name
73 value:'registrant'
74 required:true
75 bind:
76 checked: 'chosen'
77 text 'Use Registrant'
78 li '.choice', ->
79 label ->
80 input
81 type:'radio'
82 name:name
83 value:'carrier'
84 bind:
85 checked: 'chosen'
86 required:true
87 text 'Use Carrier '
88 input
89 list:'carrier'
90 name: 'carrierid'
91 bind:
92 value: 'carrierid'
93 enable: 'carrier_chosen'
94 required:true
95 li '.choice', ->
96 label ->
97 input
98 type:'radio'
99 name:name
100 value:'gateway'
101 bind:
102 checked: 'chosen'
103 required:true
104 text 'Use Gateway '
105 input
106 list:'gateway'
107 name: 'gwid'
108 bind:
109 value: 'gwid'
110 enable: 'gateway_chosen'
111 required: true
112
113 ko.components.register 'rule-target',
114 viewModel: RuleTarget
115 template: teacup.render html
116
117 RuleTarget
118
119 teacup = require 'teacup'
120 teacup.use (require 'teacup-databind')()
121 assert = require 'assert'