FW_version["todoist.js"] = "$Id$";
if (typeof todoist_checkVar === 'undefined') {
var todoist_checkVar=1;
var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var csrfToken = req.getResponseHeader('X-FHEM-csrfToken');
var todoist_icon={};
var todoist_svgPrefix=' ';
todoist_icon.del=todoist_svgPrefix+'d="M0 84V56c0-13.3 10.7-24 24-24h112l9.4-18.7c4-8.2 12.3-13.3 21.4-13.3h114.3c9.1 0 17.4 5.1 21.5 13.3L312 32h112c13.3 0 24 10.7 24 24v28c0 6.6-5.4 12-12 12H12C5.4 96 0 90.6 0 84zm416 56v324c0 26.5-21.5 48-48 48H80c-26.5 0-48-21.5-48-48V140c0-6.6 5.4-12 12-12h360c6.6 0 12 5.4 12 12zm-272 68c0-8.8-7.2-16-16-16s-16 7.2-16 16v224c0 8.8 7.2 16 16 16s16-7.2 16-16V208zm96 0c0-8.8-7.2-16-16-16s-16 7.2-16 16v224c0 8.8 7.2 16 16 16s16-7.2 16-16V208zm96 0c0-8.8-7.2-16-16-16s-16 7.2-16 16v224c0 8.8 7.2 16 16 16s16-7.2 16-16V208z"/>';
todoist_icon.loading=' ';
function todoist_encodeParm(oldVal) {
var newVal;
newVal = oldVal.replace(/\$/g, '\\%24');
newVal = newVal.replace(/"/g, '%27');
newVal = newVal.replace(/#/g, '%23');
newVal = newVal.replace(/\+/g, '%2B');
newVal = newVal.replace(/&/g, '%26');
newVal = newVal.replace(/'/g, '%27');
newVal = newVal.replace(/=/g, '%3D');
newVal = newVal.replace(/\?/g, '%3F');
newVal = newVal.replace(/\|/g, '%7C');
newVal = newVal.replace(/\s/g, '%20');
return newVal;
};
function todoist_dialog(message,title) {
if (typeof title === 'undefined') title="Message";
$('
').appendTo('body').html('' + message + '
').dialog({
modal: true, title: 'Todoist '+title, zIndex: 10000, autoOpen: true,
width: 'auto', resizable: false,
buttons: {
OK: function () {
$(this).dialog("close");
}
},
close: function (event, ui) {
$(this).remove();
}
});
setTimeout(function(){
$('.ui-dialog').remove();
},10000);
};
function todoist_refreshTable(name,sortit) {
var i=1;
$('#todoistTable_' + name).find('tr.todoist_data').each(function() {
// order
var tid = $(this).attr("data-line-id");
$(this).removeClass("odd even");
if (i%2==0) $(this).addClass("even");
else $(this).addClass("odd");
if (typeof sortit != 'undefined') todoist_sendCommand('set ' + name + ' updateTask ID:'+ tid + ' order="' + i + '"');
i++;
});
if (i!=1) $('#todoistTable_' + name).find("tr.todoist_ph").hide();
if (i==1) $('#todoistTable_' + name).find("tr.todoist_ph").show();
refreshInput(name);
refreshInputs(name);
todoist_removeLoading(name);
}
function todoist_refreshTableWidth() {
$('.sortable').each(function() {
$(this).css('width','');
});
}
function todoist_reloadTable(name,val) {
var todoist_small = (screen.width < 480 || screen.height < 480);
$('#todoistTable_' + name).find('tr.todoist_data').remove();
$('#todoistTable_' + name).find('#newEntry_'+name).parent().parent().before(val);
todoist_refreshTable(name);
if (!todoist_small) $('#newEntry_' + name).focus();
}
function refreshInputs(name) {
$('#todoistTable_' + name).find('tr.todoist_data').find('td.todoist_input').find('input[type=text]').each(function() {
var w = $(this).prev('span').width()+5;
$(this).width(w);
});
}
function refreshInput(name) {
$('#newEntry_'+name).width(0);
var w = $('#newEntry_'+name).parent('td').width()-4;
$('#newEntry_'+name).width(w);
}
function todoist_sendCommand(cmd) {
var name = cmd.split(" ")[1];
todoist_addLoading(name);
var location = document.location.pathname;
if (location.substr(location.length -1, 1) == '/') {
location = location.substr(0, location.length -1);
}
var url = document.location.protocol + "//" + document.location.host + location;
FW_cmd(url + '?XHR=1&fwcsrf=' + csrfToken + '&cmd.' + name + '=' + cmd);
}
function todoist_addLoading(name) {
if ( $('.todoist_devType_' + name).find('.todoist_loadingDiv').length ) {
$('.todoist_devType_' + name).find('.todoist_loadingDiv').remove();
}
else {
$('.todoist_devType_' + name).append('' + todoist_icon.loading + '
');
setTimeout(function(){
todoist_removeLoading(name);
}, 10000);
}
}
function todoist_removeLoading(name) {
$('.todoist_devType_' + name).find('.todoist_loadingDiv').remove();
}
function todoist_ErrorDialog(name,text,title) {
todoist_dialog(text,title);
todoist_removeLoading(name);
}
function todoist_removeLine(name,id) {
var i=1;
$('#todoistTable_' + name).find('tr.todoist_data').each(function() {
var tid = $(this).attr("data-line-id");
if (tid==id) $(this).remove();
else {
$(this).removeClass("odd even");
if (i%2==0) $(this).addClass("even");
else $(this).addClass("odd");
i++;
}
});
if (i==1) $('#todoistTable_' + name).find("tr.todoist_ph").show();
todoist_refreshTable(name);
todoist_getSizes();
}
function todoist_addLine(name,id,title) {
var lastEl=$('#todoistTable_' + name).find('tr').last();
var prevEl=$(lastEl).prev('tr');
var cl="odd";
if (prevEl != 'undefined') {
cl = $(prevEl).attr('class');
if (cl=="odd") cl="even";
else cl="odd"
}
$(lastEl).before('\n' +
' \n'+
'
\n'+
' \n'+
' \n' +
' \n'+
' ' + title + ' \n'+
' '+
' \n' +
' \n' +
' \n'+
' x\n'+
' \n'+
' \n'+
' \n'
);
$('#todoistTable_' + name).find("tr.todoist_ph").hide();
todoist_getSizes();
todoist_refreshTable(name);
}
function resizable (el, factor) {
var int = Number(factor) || 7.7;
function resize() {el.style.width = ((el.value.length+1) * int) + 'px'}
var e = 'keyup,keypress,focus,blur,change'.split(',');
for (var i in e) el.addEventListener(e[i],resize,false);
resize();
}
function todoist_getSizes() {
var height = 0;
var width = 0;
$('.sortable .sortit').each(function() {
var tHeight = $(this).outerHeight();
if (tHeight > height) height = tHeight;
});
$('.sortable').css('max-height',height).css('height',height);
}
function todoist_addHeaders() {
$("
").appendTo($('.todoist_devType')).html(todoist_icon.ref);
$("
").appendTo($('.todoist_devType')).html(todoist_icon.del);
}
$(document).ready(function(){
todoist_getSizes();
todoist_addHeaders();
$('.todoist_name').each(function() {
var name = $(this).val();
todoist_refreshTable(name);
$('.todoist_devType_' + name).on('click','div.todoist_deleteAll',function(e) {
if (confirm(todoist_tt.clearconfirm)) {
todoist_sendCommand('set ' + name + ' clearList');
}
});
$('.todoist_devType_' + name).on('click','div.todoist_refresh',function(e) {
todoist_sendCommand('set ' + name + ' getTasks');
});
$('#todoistTable_' + name).on('mouseover','.sortit',function(e) {
$(this).find('div.todoist_move').addClass('todoist_sortit_handler');
});
$('#todoistTable_' + name).on('mouseout','.sortit',function(e) {
$(this).find('div.todoist_move').removeClass('todoist_sortit_handler');
});
$('#todoistTable_' + name).on('blur keypress','#newEntry_' + name,function(e) {
if (e.type!='keypress' || e.which==13) {
e.preventDefault();
var v=todoist_encodeParm($(this).val());
if (v!="") {
todoist_sendCommand('set '+ name +' addTask ' + v);
$(this).val("");
}
}
});
$('#todoistTable_' + name).on('click','input[type="checkbox"]',function(e) {
var val=$(this).attr('checked');
if (!val) {
var id=$(this).attr('data-id');
todoist_sendCommand('set ' + name + ' closeTask ID:'+ id);
}
});
$('#todoistTable_' + name).on('click','a.todoist_delete_'+name,function(e) {
if (confirm(todoist_tt.delconfirm)) {
var id=$(this).attr('data-id');
todoist_sendCommand('set ' + name + ' deleteTask ID:'+ id);
}
return false;
});
$('#todoistTable_' + name).on('click','span.todoist_task_text',function(e) {
var id = $(this).attr("data-id");
var val=$(this).html();
var width=$(this).width()+20;
$(this).hide();
$("input[data-id='" + id +"']").show().focus().val("").val(val);
});
$('#todoistTable_' + name).on('blur keypress','input.todoist_input_'+name,function(e) {
if (e.type!='keypress' || e.which==13) {
e.preventDefault();
var val = $(this).val();
var comp = $(this).prev().html();
var id = $(this).attr("data-id");
var val = $(this).val();
$(this).hide();
$("span.todoist_task_text[data-id='" + id +"']").show();
if (val != "" && comp != val) {
$("span.todoist_task_text[data-id='" + id +"']").html(val);
todoist_sendCommand('set ' + name + ' updateTask ID:'+ id + ' title="' + val + '"');
}
if (val == "" && e.which==13) {
if (confirm('Are you sure?')) {
$('#newEntry_' + name).focus();
todoist_sendCommand('set ' + name + ' deleteTask ID:'+ id);
}
}
todoist_refreshTable(name);
}
if (e.type=='keypress') {
resizable(this,7);
refreshInput(name);
}
});
});
var fixHelper = function(e, ui) {
ui.children().each(function() {
console.log(e);
$(this).width($(this).width());
});
return ui;
};
$( ".todoist_table table.sortable" ).sortable({
//axis: 'y',
revert: true,
items: "> tbody > tr.sortit",
handle: ".todoist_sortit_handler",
forceHelperSize: true,
placeholder: "sortable-placeholder",
connectWith: '.todoist_table table.sortable',
helper: fixHelper,
start: function( event, ui ) {
var width = ui.item.innerWidth();
var height = ui.item.innerHeight();
ui.placeholder.css("width",width).css("height",height);
},
stop: function (event,ui) {
var parent = ui.item.parent().parent();
var id = $(parent).attr('id');
var name = id.split(/_(.+)/)[1];
if (ui.item.attr('data-remove')==1) ui.item.remove();
todoist_refreshTable(name,1);
todoist_refreshTableWidth();
},
remove: function (event,ui) {
var id=ui.item.attr('data-line-id');
var tid = ui.item.attr('id');
var nameHT = tid.split("_");
var lastVal = nameHT.pop(); // Get last element
var nameH = nameHT.join("_");
todoist_sendCommand('set ' + nameH + ' deleteTask ID:'+ id);
},
over: function (event,ui) {
var width = ui.item.innerWidth();
var height = ui.item.innerHeight();
var hwidth = ui.placeholder.innerWidth();
if (width>hwidth) ui.placeholder.parent().parent().css("width",width).css("height",height);
if (width