[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / core / 20-inject.html
1 <!--\r
2   Copyright 2013 IBM Corp.\r
3 \r
4   Licensed under the Apache License, Version 2.0 (the "License");\r
5   you may not use this file except in compliance with the License.\r
6   You may obtain a copy of the License at\r
7 \r
8   http://www.apache.org/licenses/LICENSE-2.0\r
9 \r
10   Unless required by applicable law or agreed to in writing, software\r
11   distributed under the License is distributed on an "AS IS" BASIS,\r
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13   See the License for the specific language governing permissions and\r
14   limitations under the License.\r
15 -->\r
16 \r
17 <script type="text/x-red" data-template-name="inject">\r
18     <div class="form-row node-input-payload">\r
19         <label for="node-input-payloadType"><i class="fa fa-envelope"></i> Payload</label>\r
20         <select id="node-input-payloadType" style="width:125px !important">\r
21           <option value="date">timestamp</option>\r
22           <option value="none">blank</option>\r
23           <option value="string">string</option>\r
24         </select>\r
25     </div>\r
26 \r
27     <div class="form-row" id="node-input-row-payload">\r
28         <label for="node-input-payload"></label>\r
29         <input type="text" id="node-input-payload" placeholder="payload">\r
30     </div>\r
31 \r
32     <div class="form-row">\r
33         <label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>\r
34         <input type="text" id="node-input-topic" placeholder="topic">\r
35     </div>\r
36 \r
37     <div class="form-row">\r
38         <label for=""><i class="fa fa-repeat"></i> Repeat</label>\r
39         <select id="inject-time-type-select"  style="width: 288px"><option value="none">none</option><option value="interval">interval</option><option value="interval-time">interval between times</option><option value="time">at a specific time</option></select>\r
40         <input type="hidden" id="node-input-repeat" placeholder="payload">\r
41         <input type="hidden" id="node-input-crontab" placeholder="payload">\r
42     </div>\r
43 \r
44     <div class="form-row inject-time-row hidden" id="inject-time-row-interval">\r
45         every <input id="inject-time-interval-count" class="inject-time-count" value="1"></input>\r
46               <select style="width: 100px" id="inject-time-interval-units"><option value="s">seconds</option><option value="m">minutes</option><option value="h">hours</option></select><br/>\r
47         <!-- on <select disabled id="inject-time-interval-days" class="inject-time-days"></select> -->\r
48     </div>\r
49 \r
50     <div class="form-row inject-time-row hidden" id="inject-time-row-interval-time">\r
51         at every <select style="width: 90px" id="inject-time-interval-time-units" class="inject-time-int-count" value="1">\r
52             <option value="1">1</option>\r
53             <option value="2">2</option>\r
54             <option value="3">3</option>\r
55             <option value="4">4</option>\r
56             <option value="5">5</option>\r
57             <option value="6">6</option>\r
58             <option value="10">10</option>\r
59             <option value="12">12</option>\r
60             <option value="15">15</option>\r
61             <option value="20">20</option>\r
62             <option value="30">30</option>\r
63             <option value="0">60</option>\r
64         </select> minutes<br/>\r
65         between <select id="inject-time-interval-time-start" class="inject-time-times"></select>\r
66         and <select id="inject-time-interval-time-end" class="inject-time-times"></select><br/>\r
67         on <select id="inject-time-interval-time-days" class="inject-time-days"></select>\r
68     </div>\r
69 \r
70     <div class="form-row inject-time-row hidden" id="inject-time-row-time">\r
71         at <input id="inject-time-time" value="12:00"></input><br/>\r
72         on <select id="inject-time-time-days" class="inject-time-days"></select>\r
73     </div>\r
74 \r
75     <div class="form-row" id="node-once">\r
76         <label>&nbsp;</label>\r
77         <input type="checkbox" id="node-input-once" style="display: inline-block; width: auto; vertical-align: top;">\r
78         <label for="node-input-once" style="width: 70%;">Fire once at start ?</label>\r
79     </div>\r
80 \r
81     <div class="form-row">\r
82         <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>\r
83         <input type="text" id="node-input-name" placeholder="name">\r
84     </div>\r
85 \r
86     <div class="form-tips"><b>Note:</b> "interval between times" and "at a specific time" will use cron.<br/>See info box for details.</div>\r
87 </script>\r
88 <style>\r
89     .inject-time-row {\r
90         padding-left: 110px;\r
91     }\r
92     .inject-time-row select {\r
93         margin: 3px 0;\r
94     }\r
95     .inject-time-days {\r
96         width: 262px;\r
97     }\r
98     .inject-time-times {\r
99         width: 90px;\r
100     }\r
101     .inject-time-row > .ui-spinner {\r
102         height: 28px;\r
103         margin: 3px 0;\r
104         border-color: rgb(204, 204, 204);\r
105     }\r
106     #inject-time-time {\r
107         margin-top: 3px;\r
108         width: 75px;\r
109     }\r
110     .inject-time-count {\r
111         width: 40px !important;\r
112     }\r
113 </style>\r
114 <script type="text/x-red" data-help-name="inject">\r
115     <p>Pressing the button on the left side of the node allows a message on a topic to be injected into the flow. This is mainly for test purposes.</p>\r
116     <p>If no payload is specified the payload is set to the current time in millisecs since 1970. This allows subsequent functions to perform time based actions.</p>\r
117     <p>The repeat function does what it says on the tin and continuously sends the payload every x seconds.</p>\r
118     <p>The Fire once at start option actually waits 50mS before firing to give other nodes a chance to instantiate properly.</p>\r
119     <p><b>Note: </b>"Interval between times" and "at a specific time" will use cron. This means that 20 minutes will be at the next hour, 20 minutes past and 40 minutes past - not in 20 minutes time.\r
120     If you want every 20 minutes from now - use the basic "interval" option.</p>\r
121 </script>\r
122 \r
123 <script type="text/javascript">\r
124     RED.nodes.registerType('inject',{\r
125         category: 'input',\r
126         color:"#a6bbcf",\r
127         defaults: {\r
128             name: {value:""},\r
129             topic: {value:""},\r
130             payload: {value:""},\r
131             payloadType: {value:"date"},\r
132             repeat: {value:""},\r
133             crontab: {value:""},\r
134             once: {value:false}\r
135         },\r
136         inputs:0,\r
137         outputs:1,\r
138         icon: "inject.png",\r
139         label: function() {\r
140             if (this.payloadType === "string") {\r
141                 return this.name||this.topic||this.payload||"inject";\r
142             }\r
143             else { return this.name||this.topic||"inject"; }\r
144         },\r
145         labelStyle: function() {\r
146             return this.name?"node_label_italic":"";\r
147         },\r
148         oneditprepare: function() {\r
149             $("#inject-time-type-select").change(function() {\r
150                 $("#node-input-crontab").val('');\r
151                 var id = $("#inject-time-type-select option:selected").val();\r
152                 $(".inject-time-row").hide();\r
153                 $("#inject-time-row-"+id).show();\r
154                 if ((id == "none") || (id == "interval")) {\r
155                     $("#node-once").show();\r
156                 }\r
157                 else {\r
158                     $("#node-once").hide();\r
159                     $("#node-input-once").prop('checked', false);\r
160                 }\r
161             });\r
162 \r
163             var days = [\r
164                 {v:"*",t:"every day"},\r
165                 {v:"1-5",t:"Mondays to Fridays"},\r
166                 {v:"0,6",t:"Saturdays and Sundays"},\r
167                 {v:"1",t:"Mondays"},\r
168                 {v:"2",t:"Tuesdays"},\r
169                 {v:"3",t:"Wednesdays"},\r
170                 {v:"4",t:"Thursdays"},\r
171                 {v:"5",t:"Fridays"},\r
172                 {v:"6",t:"Saturdays"},\r
173                 {v:"0",t:"Sundays"}\r
174             ];\r
175 \r
176             $(".inject-time-days").each(function() {\r
177                 for (var d in days) {\r
178                     $(this).append($("<option></option>").val(days[d].v).text(days[d].t));\r
179                 }\r
180             });\r
181 \r
182             $(".inject-time-times").each(function() {\r
183                 for (var i=0;i<24;i++) {\r
184                     var l = (i<10?"0":"")+i+":00";\r
185                     $(this).append($("<option></option>").val(i).text(l));\r
186                 }\r
187             });\r
188 \r
189             $(".inject-time-count").spinner({\r
190                 //max:60,\r
191                 min:1\r
192             });\r
193 \r
194             $("#inject-time-interval-units").change(function() {\r
195                 var units = $("#inject-time-interval-units option:selected").val();\r
196                 //$("#inject-time-interval-days").prop("disabled",(units == "s")?"disabled":false);\r
197                 //$(".inject-time-count").spinner("option","max",(units == "h")?24:60);\r
198             });\r
199 \r
200             $.widget( "ui.injecttimespinner", $.ui.spinner, {\r
201                 options: {\r
202                     // seconds\r
203                     step: 60 * 1000,\r
204                     // hours\r
205                     page: 60\r
206                 },\r
207                 _parse: function( value ) {\r
208                     if ( typeof value === "string" ) {\r
209                         // already a timestamp\r
210                         if ( Number( value ) == value ) {\r
211                             return Number( value );\r
212                         }\r
213                         var p = value.split(":");\r
214                         var offset = new Date().getTimezoneOffset();\r
215                         return (((Number(p[0])+1)*60)+Number(p[1])+offset)*60*1000;\r
216                     }\r
217                     return value;\r
218                 },\r
219                 _format: function( value ) {\r
220                     var d = new Date(value);\r
221                     var h = d.getHours();\r
222                     var m = d.getMinutes();\r
223                     return ((h < 10)?"0":"")+h+":"+((m < 10)?"0":"")+m;\r
224                 }\r
225             });\r
226 \r
227             $("#inject-time-time").injecttimespinner();\r
228 \r
229             var repeattype = "none";\r
230             if (this.repeat != "" && this.repeat != 0) {\r
231                 repeattype = "interval";\r
232                 var r = "s";\r
233                 var c = this.repeat;\r
234                 if (this.repeat % 60 === 0) { r = "m"; c = c/60; }\r
235                 if (this.repeat % 1440 === 0) { r = "h"; c = c/24; }\r
236                 $("#inject-time-interval-count").val(c);\r
237                 $("#inject-time-interval-units").val(r);\r
238                 //$("#inject-time-interval-units option").filter(function() {return $(this).val() == "s";}).attr('selected',true);\r
239                 $("#inject-time-interval-days").prop("disabled","disabled");\r
240             } else if (this.crontab) {\r
241                 var cronparts = this.crontab.split(" ");\r
242                 var days = cronparts[4];\r
243                 if (!isNaN(cronparts[0]) && !isNaN(cronparts[1])) {\r
244                     repeattype = "time";\r
245                     // Fixed time\r
246                     var time = cronparts[1]+":"+cronparts[0];\r
247                     $("#inject-time-time").val(time);\r
248                     $("#inject-time-type-select option").filter(function() {return $(this).val() == "s";}).attr('selected',true);\r
249                     $("#inject-time-time-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);\r
250                 }\r
251                 //else if (cronparts[0] == "0") {\r
252                 //    // interval - hours\r
253                 //    var hours = cronparts[1].slice(2);\r
254                 //    repeattype = "interval";\r
255                 //    $("#inject-time-interval-days").prop("disabled",false);\r
256                 //    $("#inject-time-interval-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);\r
257                 //    $("#inject-time-interval-count").val(hours)\r
258                 //    $("#inject-time-interval-units option").filter(function() {return $(this).val() == "h";}).attr('selected',true);\r
259                 //} else if (cronparts[1] == "*") {\r
260                 //    // interval - minutes\r
261                 //    var minutes = cronparts[0].slice(2);\r
262                 //    repeattype = "interval";\r
263                 //    $("#inject-time-interval-days").prop("disabled",false);\r
264                 //    $("#inject-time-interval-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);\r
265                 //    $("#inject-time-interval-count").val(minutes)\r
266                 //    $("#inject-time-interval-units option").filter(function() {return $(this).val() == "m";}).attr('selected',true);\r
267                 //}\r
268                 else {\r
269                     repeattype = "interval-time";\r
270                     // interval - time period\r
271                     var minutes = cronparts[0].slice(2);\r
272                     if (minutes === "") { minutes = "0"; }\r
273                     $("#inject-time-interval-time-units").val(minutes);\r
274                     $("#inject-time-interval-time-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);\r
275                     var time = cronparts[1];\r
276                     var timeparts = time.split(",");\r
277                     var start;\r
278                     var end;\r
279                     if (timeparts.length == 1) {\r
280                         // 0 or 0-10\r
281                         var hours = timeparts[0].split("-");\r
282                         if (hours.length == 1) {\r
283                             if (hours[0] === "") {\r
284                                 start = "0";\r
285                                 end = "0";\r
286                             }\r
287                             else {\r
288                                 start = hours[0];\r
289                                 end = Number(hours[0])+1;\r
290                             }\r
291                         } else {\r
292                             start = hours[0];\r
293                             end = (Number(hours[1])+1)%24;\r
294                         }\r
295                     } else {\r
296                         // 23,0 or 17-23,0-10 or 23,0-2 or 17-23,0\r
297                         var startparts = timeparts[0].split("-");\r
298                         start = startparts[0];\r
299 \r
300                         var endparts = timeparts[1].split("-");\r
301                         if (endparts.length == 1) {\r
302                             end = Number(endparts[0])+1;\r
303                         } else {\r
304                             end = Number(endparts[1])+1;\r
305                         }\r
306                     }\r
307                     $("#inject-time-interval-time-start option").filter(function() {return $(this).val() == start;}).attr('selected',true);\r
308                     $("#inject-time-interval-time-end option").filter(function() {return $(this).val() == end;}).attr('selected',true);\r
309 \r
310                 }\r
311             } else {\r
312                 $("#inject-time-type-select option").filter(function() {return $(this).val() == "none";}).attr('selected',true);\r
313             }\r
314 \r
315             $(".inject-time-row").hide();\r
316             $("#inject-time-type-select option").filter(function() {return $(this).val() == repeattype;}).attr('selected',true);\r
317             $("#inject-time-row-"+repeattype).show();\r
318 \r
319             if (this.payloadType == null) {\r
320                 if (this.payload == "") {\r
321                     this.payloadType = "date";\r
322                 } else {\r
323                     this.payloadType = "string";\r
324                 }\r
325             }\r
326 \r
327             $("#node-input-payloadType").change(function() {\r
328                 var id = $("#node-input-payloadType option:selected").val();\r
329                 if (id == "string") {\r
330                     $("#node-input-row-payload").show();\r
331                 } else {\r
332                     $("#node-input-row-payload").hide();\r
333                 }\r
334             });\r
335             $("#node-input-payloadType").val(this.payloadType);\r
336             $("#node-input-payloadType").change();\r
337             $("#inject-time-type-select").change();\r
338 \r
339         },\r
340         oneditsave: function() {\r
341             var repeat = "";\r
342             var crontab = "";\r
343             var type = $("#inject-time-type-select option:selected").val();\r
344             if (type == "none") {\r
345                 // nothing\r
346             } else if (type == "interval") {\r
347                 var count = $("#inject-time-interval-count").val();\r
348                 var units = $("#inject-time-interval-units option:selected").val();\r
349                 var days = $("#inject-time-interval-days option:selected").val();\r
350                 if (units == "s") {\r
351                     repeat = count;\r
352                 } else {\r
353                     if (units == "m") {\r
354                         //crontab = "*/"+count+" * * * "+days;\r
355                         repeat = count * 60;\r
356                     } else if (units == "h") {\r
357                         //crontab = "0 */"+count+" * * "+days;\r
358                         repeat = count * 60 * 24;\r
359                     }\r
360                 }\r
361             } else if (type == "interval-time") {\r
362                 var count = $("#inject-time-interval-time-units").val();\r
363                 var startTime = Number($("#inject-time-interval-time-start option:selected").val());\r
364                 var endTime = Number($("#inject-time-interval-time-end option:selected").val());\r
365                 var days = $("#inject-time-interval-time-days option:selected").val();\r
366                 var timerange = "*";\r
367                 if (startTime == endTime) {\r
368                     //TODO: invalid\r
369                     repeat = "";\r
370                     crontab = "";\r
371                 } else if (endTime == 0) {\r
372                     timerange = startTime+"-23";\r
373                 } else if (startTime+1 < endTime) {\r
374                     timerange = startTime+"-"+(endTime-1);\r
375                 } else if (startTime+1 == endTime) {\r
376                     timerange = startTime;\r
377                 } else {\r
378                     var startpart = "";\r
379                     var endpart = "";\r
380                     if (startTime == 23) {\r
381                         startpart = "23";\r
382                     } else {\r
383                         startpart = startTime+"-23";\r
384                     }\r
385                     if (endTime == 1) {\r
386                         endpart = "0";\r
387                     } else {\r
388                         endpart = "0-"+(endTime-1);\r
389                     }\r
390                     timerange = startpart+","+endpart;\r
391                 }\r
392                 repeat = "";\r
393                 if (count === "0") {\r
394                     crontab = count+" "+timerange+" * * "+days;\r
395                 }\r
396                 else {\r
397                     crontab = "*/"+count+" "+timerange+" * * "+days;\r
398                 }\r
399             } else if (type == "time") {\r
400                 var time = $("#inject-time-time").val();\r
401                 var days = $("#inject-time-time-days option:selected").val();\r
402                 var parts = time.split(":");\r
403                 repeat = "";\r
404                 crontab = parts[1]+" "+parts[0]+" * * "+days;\r
405             }\r
406 \r
407             $("#node-input-repeat").val(repeat);\r
408             $("#node-input-crontab").val(crontab);\r
409 \r
410         },\r
411         button: {\r
412             onclick: function() {\r
413                 var label = (this.name||this.payload).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");\r
414                 if (this.payloadType === "date") { label = "timestamp"; }\r
415                 if (this.payloadType === "none") { label = "blank"; }\r
416                 d3.xhr("inject/"+this.id).post(function(err,resp) {\r
417                         if (err) {\r
418                             if (err.status == 404) {\r
419                                 RED.notify("<strong>Error</strong>: inject node not deployed","error");\r
420                             } else if (err.status == 500) {\r
421                                 RED.notify("<strong>Error</strong>: inject failed, see log for details.","error");\r
422                             } else if (err.status == 0) {\r
423                                 RED.notify("<strong>Error</strong>: no response from server","error");\r
424                             } else {\r
425                                 RED.notify("<strong>Error</strong>: unexpected error: ("+err.status+")"+err.response,"error");\r
426                             }\r
427                         } else if (resp.status == 200) {\r
428                             RED.notify("Successfully injected: "+label,"success");\r
429                         } else {\r
430                             RED.notify("<strong>Error</strong>: unexpected response: ("+resp.status+") "+resp.response,"error");\r
431                         }\r
432                 });\r
433             }\r
434         }\r
435     });\r
436 \r
437 </script>\r