summaryrefslogtreecommitdiff
path: root/smarttv-client/Javascript/Timers.js
blob: 742f5f05bb5842e18e32a84497e6030b727f6c0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
var Timers = {
	timerList : [],
	scrollDur : 100,
	scrollFlip : 100,
	btnMode : 0
	// 0 nothing
	// 1: delete
	// 2: 
};

Timers.init = function() {
	/*
	div with scrolling property
	defualt: left ad right half
	Line has "RecOngoing" "Channel" "Start" "Stop" "Title"
	*/

	this.btnSelected = 0;
	this.scrolling = false;
	
	var elem = document.getElementById('timerScreen-anchor');
	elem.setAttribute('onkeydown', 'Timers.onInput();');

	$("#timerScreen").hide();	
};

Timers.show = function() {
	Main.log("Timers.show");
	$("#timerScreen").show();
	this.btnSelected = 0;

	Timers.focus();
	Timers.readTimers();
//	Timers.debug();
};

Timers.focus = function () {
	Main.log("Timers.focus");

	$("#timerScreen-anchor").focus();
};

Timers.debug = function () {
	for (var i = 0; i < 20; i++) {
		Timers.timerList.push( {title: "Timer One two three VZGTTTTTTGFDDD GHGZG"+i, isSingleEvent: true, printday:"", weekdays:0,
			day: 0, start:2015, stop:2013, starttime: 0, stoptime: 0, channelid: "", channelname : "Hello Channel", flags: 0,
			index: i, isrec: false, eventid: 0 });

	}
	Timers.createMenu();	
};

Timers.hide = function() {

	$("#timerScreen").hide();	
	Main.changeState(Main.eMAIN);
	Main.enableKeys();

	Timers.timerList = [];
	$("#timerTable").remove();

};

Timers.insertColon = function (t) {
// insert the collon before the last two digits
	var len = t.length;

	var o = t;
	if (len >= 2) {
		o = t.substr(0, (len-2)) + ":" + t.substr(len-2);
		switch (o.length) {
			case 3:
				o = "00" +o;
				break;
			case 4:
				o = "0" + o;
				break;
		};
	}
	return o;
};

Timers.readTimers = function () {
	var url = Config.serverUrl + "/timers.xml";
	$.ajax({
		url: url,
		type : "GET",
		success : function(data, status, XHR ) {
			Main.log ("Timers: Got response");
			$(data).find("timer").each(function () {
				var title = $(this).find('file').text();
				var isSingleEvent = ($(this).find('issingleevent').text() == "true") ? true : false;
				
				var printday = $(this).find('printday').text();
				var weekdays = parseInt($(this).find('weekdays').text());
				var day = parseInt($(this).find('day').text());
				var start = $(this).find('start').text();
				var stop = $(this).find('stop').text();
				var starttime = parseInt($(this).find('starttime').text());
				var stoptime = parseInt($(this).find('stoptime').text());
				var channelid = $(this).find('channelid').text();
				var channelname = $(this).find('channelname').text();				
				var flags = parseInt($(this).find('flags').text());
				var index = parseInt($(this).find('index').text());
				var isrec = ($(this).find('isrec').text() == "true") ? true: false;
				var eventid = parseInt($(this).find('eventid').text());
				
				Timers.timerList.push( {title: title, isSingleEvent: isSingleEvent, printday:printday, weekdays:weekdays,
					day: day, start: Timers.insertColon(start), stop: Timers.insertColon(stop), starttime: starttime, stoptime: stoptime, channelid: channelid, channelname : channelname,
					flags: flags, index: index, isrec: isrec, eventid: eventid });
			});
			
			Timers.createMenu();
		},
		error : function (jqXHR, status, error) {
			Main.logToServer ("Timers: Not found!!!");
		}
		});			
};

Timers.createMenu= function () {	
	var p_width = $("body").outerWidth();
	var p_height = $("body").outerHeight();

	var table = $("<div>", {id : "timerTable", style :"overflow-y: scroll;margin-top:3px;margin-bottom:3px; padding-right:3px; height:100%;"});
	$("#timerView").append(table);
	
	$("#timerScreen").show();
	var cur_date = 0;
	for (var i = 0; i < Timers.timerList.length; i++) {
		Main.log("Timers: " + Timers.timerList[i].title);
		
		if (Timers.timerList[i].isSingleEvent == false) {
			cur_date = 0; 
			table.append( $("<div>", { text : ((Timers.timerList[i].day != 0) ? (Timers.getPrintDate(Timers.timerList[i].day) + " - ") : "") +Timers.getWeekdays(Timers.timerList[i].weekdays), class : "style_menuItem style_headline", style : "text-align:left;overflow-x: hidden;white-space : nowrap;"}));			

		}else if (cur_date != Timers.timerList[i].day) {
			cur_date = Timers.timerList[i].day; 
			table.append( $("<div>", { text : Timers.getPrintDate(Timers.timerList[i].day) , class : "style_menuItem style_headline", style : "text-align:left;overflow-x: hidden;white-space : nowrap;"}));			
		}
		table.append(Timers.createEntry(i, $("#timerTable").width()));
	}

	this.btnSelected = 0;
	$("#tmr-0").removeClass('style_menuItem').addClass('style_menuItemSelected'); 
	

};

Timers.getPrintDate = function (day) {
	var d = new MyDate (day * 1000);
	return d.getDate() + "." + (d.getMonth() +1) + "." + d.getFullYear();	 
};

Timers.getWeekdays = function (wd) {
	var map = wd.toString(2);
	var res = "";

	if (map.length <= 7) {
		var suf = "0000000".slice(0, (7-map.length));
		map = suf + map;
	}
	else {
		// take the last 7 digits
		map = map.slice ((7-map.length));
	}

	res += ((map[6] == "1") ? "M" : "-");
	res += ((map[5] == "1") ? "T" : "-");
	res += ((map[4] == "1") ? "W" : "-");
	res += ((map[3] == "1") ? "T" : "-");
	res += ((map[2] == "1") ? "F" : "-");
	res += ((map[1] == "1") ? "S" : "-");
	res += ((map[0] == "1") ? "S" : "-");

	return res;	 
};

Timers.createEntry= function (i, w) {
	Main.log("width= " +w);
	
	var row = $("<div>", {id: "tmr-"+i, class : "style_menuItem", style : "text-align:left;overflow-x: hidden;white-space : nowrap;"}); //, style : "text-overflow: ellipsis;white-space : nowrap;" 

//	row.append($("<div>", {class : ((Timers.timerList[i].isrec ==true) ? "style_timerRec" : ""), style : "display: inline-block;"}));
	
	
	row.append($("<div>", {class : ((Timers.timerList[i].isrec ==true) ? "style_timerRec" : ((Timers.timerList[i].flags & 1) != 0) ? "style_timerAct" : "style_timerNone"), style : "display: inline-block; color : inherit;"})); 

	if (	Timers.timerList[i].isSingleEvent == true) {
	}
	else {
	}
	row.append($("<div>", {text : Timers.timerList[i].channelname, style : "padding-left:5px;width:12%; display: inline-block; color : inherit; ", class : "style_overflow"}));
	row.append($("<div>", {text : Timers.timerList[i].start, style : "padding-left:5px; width:9%; display: inline-block; color : inherit; ", class : "style_overflow"}));
	row.append($("<div>", {text : Timers.timerList[i].stop, style : "padding-left:5px; width:9%; display: inline-block; color : inherit; ", class : "style_overflow"}));	
	row.append($("<div>", {text : Timers.timerList[i].title, style : "padding-left:5px; width:68%;display: inline-block; color : inherit; ", class : "style_overflow"}));
	return row;
};

Timers.resetBtns = function () {
	this.btnSelected = 0;
	for (var i =0; i <= Timers.timerList.length; i++) {
		$("#tmr-" + i).removeClass('style_menuItemSelected').addClass('style_menuItem'); 
	}
	$("#tmr-0").removeClass('style_menuItem').addClass('style_menuItemSelected'); 
};

Timers.resetView = function () {
	Timers.timerList = [];
	$("#timerTable").remove();
	
	Timers.readTimers();
	

};

Timers.timerCallback = function () {
// delete the current timer
	Main.log("****** Delete Timer: " + Timers.timerList[this.btnSelected].title);
	
//	var obj = new execRestCmd(RestCmds.CMD_DelTimer, Timers.timerList[this.btnSelected].index);
	Server.delTimer(Timers.timerList[this.btnSelected].index);

};


Timers.selectBtnUp = function () {
	var btnname = "#tmr-"+this.btnSelected;
	Main.log( "Timers-BtnUp: Old: " +this.btnSelected + " btn= "+btnname);
	$(btnname).removeClass('style_menuItemSelected').addClass('style_menuItem'); 
	if (this.btnSelected == 0) {
		this.btnSelected = (Timers.timerList.length-1);
		$("#timerTable").animate ({scrollTop: $("#tmr-" + this.btnSelected).position().top}, this.scrollFlip);
		}
	else {
		this.btnSelected--;
		var pos = $("#tmr-" + this.btnSelected).position().top;
		Main.log("pos= " + pos + " ovlBodyHeight= " + $("#timerTable").height() + " scrollTop= " + $("#timerTable").scrollTop());
		if (pos < 0) {
			$("#timerTable").animate ({scrollTop: $("#timerTable").scrollTop() + pos}, this.scrollDur);
			Main.log("New scrollTop= " +$("#timerTable").scrollTop() +" new pos= " + $("#tmr-" + this.btnSelected).position().top);
		}
		
	}
	$("#tmr-" + this.btnSelected).removeClass('style_menuItem').addClass('style_menuItemSelected'); 
	Main.log("Timers-BtnUp: New: " +this.btnSelected);
};

Timers.selectBtnDown = function () {
	var btnname = "#tmr-"+this.btnSelected;
	$("#tmr-" + this.btnSelected).removeClass('style_menuItemSelected').addClass('style_menuItem'); 
	Main.log( "Timers-BtnDown: Old: " +this.btnSelected + " btn= "+btnname);
	if (this.btnSelected == (Timers.timerList.length-1)) {
		this.btnSelected = 0;
		$("#timerTable").animate ({scrollTop: 0}, this.scrollFlip);
	}
	else {
		this.btnSelected++;
		var pos = $("#tmr-" + this.btnSelected).position().top;
		var height = $("#tmr-" + this.btnSelected).outerHeight() +10;
		Main.log("pos= " + pos + " height= " + height + " ovlBodyHeight= " + $("#timerTable").height() + " scrollTop= " + $("#timerTable").scrollTop());
		if ((pos + height) > $("#timerTable").height()) {
			$("#timerTable").animate ({scrollTop: $("#timerTable").scrollTop() + (pos + height) - $("#timerTable").height()}, this.scrollDur); 
			Main.log("New scrollTop= " +$("#timerTable").scrollTop() +" new pos= " + $("#tmr-" + this.btnSelected).position().top);
		}
				
	}
	Main.log( "Timers-BtnDown: New: " +this.btnSelected );

	$("#tmr-" + this.btnSelected).removeClass('style_menuItem').addClass('style_menuItemSelected'); 	
};

Timers.onInput = function () {
    var keyCode = event.keyCode;
	Main.log(" Timers key= " + keyCode);
    switch(keyCode) {
		case tvKey.KEY_UP:
			Main.log("Timers-Select Up");
			this.selectBtnUp();
			break;
		case tvKey.KEY_DOWN:
			Main.log("Timers-Select Down");
			this.selectBtnDown();
		break;
		case tvKey.KEY_ENTER:
			Buttons.ynShow();

		break;
		case tvKey.KEY_YELLOW:

			Timers.timerCallback = function () {
			// delete the current timer
				Main.log("Timers.timerCallback: " + Timers.timerList[this.btnSelected].title);
				Server.delTimer(Timers.timerList[this.btnSelected].index);
			};

			Buttons.ynShow();

		break;
		case tvKey.KEY_RED:
			if ((Timers.timerList[this.btnSelected].flags & 1) != 0) {
				// Timer is currently active
				Buttons.ynHeadlineText ("Deactivate Timer ?");
				Timers.timerCallback = function () {
						Main.log("Timers.timerCallback Deactivate Timer: " + Timers.timerList[this.btnSelected].title);						
						Server.actTimer(Timers.timerList[this.btnSelected].index, false);
					};
				
			}
			else {
				// Timer is currently deactive
				Buttons.ynHeadlineText ("Activate Timer ?");
				Timers.timerCallback = function () {
					Main.log("Timers.timerCallback Activate Timer: " + Timers.timerList[this.btnSelected].title);						
					Server.actTimer(Timers.timerList[this.btnSelected].index,  true);
				};
			}

			Buttons.ynShow();
//			Timers.hide();

		break;
    	case tvKey.KEY_TOOLS:
    		Helpbar.showHelpbar();
    	break;
		
		case tvKey.KEY_RETURN:
		case tvKey.KEY_EXIT:
			Timers.hide();
			
			if (this.returnCallback  != null)
				this.returnCallback(); 	    	
			break;

	}
	try {
		widgetAPI.blockNavigation(event);
	}
	catch (e) {
	};
};