2 Copyright 2013 IBM Corp.
\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
8 http://www.apache.org/licenses/LICENSE-2.0
\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
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
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
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
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
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
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
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
75 <div class="form-row" id="node-once">
\r
76 <label> </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
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
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
90 padding-left: 110px;
\r
92 .inject-time-row select {
\r
98 .inject-time-times {
\r
101 .inject-time-row > .ui-spinner {
\r
104 border-color: rgb(204, 204, 204);
\r
106 #inject-time-time {
\r
110 .inject-time-count {
\r
111 width: 40px !important;
\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
123 <script type="text/javascript">
\r
124 RED.nodes.registerType('inject',{
\r
130 payload: {value:""},
\r
131 payloadType: {value:"date"},
\r
132 repeat: {value:""},
\r
133 crontab: {value:""},
\r
134 once: {value:false}
\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
143 else { return this.name||this.topic||"inject"; }
\r
145 labelStyle: function() {
\r
146 return this.name?"node_label_italic":"";
\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
158 $("#node-once").hide();
\r
159 $("#node-input-once").prop('checked', false);
\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
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
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
189 $(".inject-time-count").spinner({
\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
200 $.widget( "ui.injecttimespinner", $.ui.spinner, {
\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
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
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
227 $("#inject-time-time").injecttimespinner();
\r
229 var repeattype = "none";
\r
230 if (this.repeat != "" && this.repeat != 0) {
\r
231 repeattype = "interval";
\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
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
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
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
279 if (timeparts.length == 1) {
\r
281 var hours = timeparts[0].split("-");
\r
282 if (hours.length == 1) {
\r
283 if (hours[0] === "") {
\r
289 end = Number(hours[0])+1;
\r
293 end = (Number(hours[1])+1)%24;
\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
300 var endparts = timeparts[1].split("-");
\r
301 if (endparts.length == 1) {
\r
302 end = Number(endparts[0])+1;
\r
304 end = Number(endparts[1])+1;
\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
312 $("#inject-time-type-select option").filter(function() {return $(this).val() == "none";}).attr('selected',true);
\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
319 if (this.payloadType == null) {
\r
320 if (this.payload == "") {
\r
321 this.payloadType = "date";
\r
323 this.payloadType = "string";
\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
332 $("#node-input-row-payload").hide();
\r
335 $("#node-input-payloadType").val(this.payloadType);
\r
336 $("#node-input-payloadType").change();
\r
337 $("#inject-time-type-select").change();
\r
340 oneditsave: function() {
\r
343 var type = $("#inject-time-type-select option:selected").val();
\r
344 if (type == "none") {
\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
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
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
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
378 var startpart = "";
\r
380 if (startTime == 23) {
\r
383 startpart = startTime+"-23";
\r
385 if (endTime == 1) {
\r
388 endpart = "0-"+(endTime-1);
\r
390 timerange = startpart+","+endpart;
\r
393 if (count === "0") {
\r
394 crontab = count+" "+timerange+" * * "+days;
\r
397 crontab = "*/"+count+" "+timerange+" * * "+days;
\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
404 crontab = parts[1]+" "+parts[0]+" * * "+days;
\r
407 $("#node-input-repeat").val(repeat);
\r
408 $("#node-input-crontab").val(crontab);
\r
412 onclick: function() {
\r
413 var label = (this.name||this.payload).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");
\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
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
425 RED.notify("<strong>Error</strong>: unexpected error: ("+err.status+")"+err.response,"error");
\r
427 } else if (resp.status == 200) {
\r
428 RED.notify("Successfully injected: "+label,"success");
\r
430 RED.notify("<strong>Error</strong>: unexpected response: ("+resp.status+") "+resp.response,"error");
\r