var TracksForm = {
toggle: function(toggleLinkId, formContainerId, formId, hideLinkText,
hideLinkTitle, showLinkText, showLinkTitle) {
var form=$('#'+formContainerId)
form.toggle();
var toggleLink = $('#'+toggleLinkId);
if (!form.is(':visible')) {
toggleLink.text(showLinkText).attr('title', showLinkTitle);
}
else {
toggleLink.text(hideLinkText).attr('title', hideLinkTitle);
$('#'+formId+' input:text:first').focus();
}
toggleLink.parent().toggleClass('hide_form');
},
set_project_name: function (name) {
$('input#todo_project_name').val(name);
},
set_project_name_for_multi_add: function (name) {
$('#multi_todo_project_name').val(name);
},
set_context_name: function (name) {
$('input#todo_context_name').val(name);
},
set_context_name_for_multi_add: function (name) {
$('#multi_todo_context_name').val(name);
},
set_context_name_and_default_context_name: function (name) {
TracksForm.set_context_name(name);
$('input[name=default_context_name]').val(name);
},
set_project_name_and_default_project_name: function (name) {
TracksForm.set_project_name('');
$('#default_project_name_id').val(name);
$('#project_name').html(name);
},
set_tag_list: function (name) {
$('input#tag_list').val(name);
},
set_tag_list_for_multi_add: function (name) {
$('#multi_tag_list').val(name);
},
setup_behavior: function() {
/* toggle new todo form for single todo */
$('#toggle_action_new').click(function(){
if ($("#todo_multi_add").is(':visible')) { /* hide multi next action form first */
$('#todo_new_action').show();
$('#todo_multi_add').hide();
$('a#toggle_multi').text(i18n['shared.toggle_multi']);
}
TracksForm.toggle('toggle_action_new', 'todo_new_action', 'todo-form-new-action',
i18n['shared.hide_form'], i18n['shared.hide_action_form_title'],
i18n['shared.toggle_single'], i18n['shared.toggle_single_title']);
});
/* toggle new todo form for multi edit */
$('#toggle_multi').click(function(){
if ($("#todo_multi_add").is(':visible')) {
$('#todo_new_action').show();
$('#todo_multi_add').hide();
$('a#toggle_multi').text(i18n['shared.toggle_multi']);
}
else {
$('#todo_new_action').hide();
$('#todo_multi_add').show();
$('a#toggle_multi').text(i18n['shared.toggle_single']);
$('a#toggle_action_new').text(i18n['shared.hide_form']);
}
});
/* add behavior to clear the date both buttons for show_from and due */
$(".date_clear").live('click', function() {
$(this).prev().val('');
});
/* submit todo form after entering new todo */
$("button#todo_new_action_submit").live('click', function (ev) {
if (TodoItems.askIfNewContextProvided('', this))
submit_with_ajax_and_block_element('form#todo-form-new-action', $(this));
return false;
});
/* submit multi-todo form after entering multiple new todos */
$("button#todo_multi_new_action_submit").live('click', function (ev) {
if (TodoItems.askIfNewContextProvided('multi_', this))
submit_with_ajax_and_block_element('form#todo-form-multi-new-action', $(this));
return false;
});
}
}
var TracksPages = {
show_errors: function (html) {
$('div#error_status').html(html);
$('div#error_status').show();
},
show_edit_errors: function(html) {
$('div#edit_error_status').html(html);
$('div#edit_error_status').show();
},
show_errors_for_multi_add: function(html) {
$('div#multiple_error_status').html(html);
$('div#multiple_error_status').show();
},
hide_errors: function() {
$('div#error_status').hide();
$('div#edit_error_status').hide();
$('div#multiple_error_status').hide();
},
update_sidebar: function(html) {
$('#sidebar').html(html);
},
setup_nifty_corners: function() {
Nifty("div#recurring_new_container","normal");
Nifty("div#context_new_container","normal");
Nifty("div#feedlegend","normal");
Nifty("div#feedicons-project","normal");
Nifty("div#feedicons-context","normal");
Nifty("div#todo_new_action_container","normal");
Nifty("div#project_new_project_container","normal");
},
page_notify: function(type, message, fade_duration_in_sec) {
var flash = $('h4#flash');
flash.html("
"+message+"
");
flash = $('h4#flash');
flash.show();
flash.fadeOut(fade_duration_in_sec*1000);
},
set_page_badge: function(count) {
$('#badge_count').html(count);
},
setup_behavior: function () {
/* main menu */
$('ul.sf-menu').superfish({
delay: 250,
animation: {
opacity:'show',
height:'show'
},
autoArrows: false,
dropShadows: false,
speed: 'fast'
});
/* context menu */
$('ul.sf-item-menu').superfish({
delay: 100,
animation: {
opacity:'show',
height:'show'
},
autoArrows: false,
dropShadows: false,
speed: 'fast',
onBeforeShow: function() { /* highlight todo */
$(this.parent().parent().parent()).addClass("sf-item-selected");
},
onHide: function() { /* remove hightlight from todo */
$(this.parent().parent().parent()).removeClass("sf-item-selected");
}
});
/* for toggle notes link in mininav */
$("#toggle-notes-nav").click(function () {
$(".todo_notes").toggle();
});
/* fade flashes and alerts in automatically */
$(".alert").fadeOut(8000);
/* for edit project form and edit todo form
* TODO: refactor to separate calls from project and todo */
$('.edit-form a.negative').live('click', function(){
$(this).parents('.edit-form').fadeOut(200, function () {
$(this).parents('.list').find('.project').fadeIn(500);
$(this).parents('.container').find('.item-show').fadeIn(500);
})
});
}
}
var TodoItemsContainer = {
// public
ensureVisibleWithEffectAppear: function(elemId){
$('#'+elemId).fadeIn(500);
},
expandNextActionListing: function(itemsElem, skipAnimation) {
itemsElem = $(itemsElem);
if(skipAnimation == true) {
itemsElem.show();
}
else {
itemsElem.show('blind', 400);
}
TodoItems.showContainer(itemsElem.parentNode);
},
collapseNextActionListing: function(itemsElem, skipAnimation) {
itemsElem = $(itemsElem);
if(skipAnimation == true) {
itemsElem.hide();
}
else {
itemsElem.hide('blind', 400);
}
TodoItems.hideContainer(itemsElem.parentNode);
},
ensureContainerHeight: function(itemsElem) {
$(itemsElem).css({
height: '',
overflow: ''
});
},
expandNextActionListingByContext: function(itemsElemId, skipAnimation){
TodoItems.expandNextActionListing($('#'+itemsElemId).get(), skipAnimation);
},
setup_container_toggles: function(){
// bind handlers
$('.container_toggle').click(function(evt){
var toggle_target = $(this.parentNode.parentNode).find('.toggle_target');
if(toggle_target.is(':visible')){
// hide it
var imgSrc = $(this).find('img').attr('src');
$(this).find('img').attr('src', imgSrc.replace('collapse', 'expand'));
$.cookie(TodoItemsContainer.buildCookieName(this.parentNode.parentNode), true);
toggle_target.slideUp(500);
} else {
// show it
imgSrc = $(this).find('img').attr('src');
$(this).find('img').attr('src', imgSrc.replace('expand', 'collapse'));
$.cookie(TodoItemsContainer.buildCookieName(this.parentNode.parentNode), null);
toggle_target.slideDown(500);
}
return false;
});
// set to cookied state
$('.container.context').each(function(){
if($.cookie(TodoItemsContainer.buildCookieName(this))=="true"){
var imgSrc = $(this).find('.container_toggle img').attr('src');
if (imgSrc) {
$(this).find('.container_toggle img').attr('src', imgSrc.replace('collapse', 'expand'));
$(this).find('.toggle_target').hide();
}
}
});
},
// private
buildCookieName: function(containerElem) {
var tracks_login = $.cookie('tracks_login');
return 'tracks_'+tracks_login+'_context_' + containerElem.id + '_collapsed';
},
showContainer: function(containerElem) {
var imgSrc = $(containerElem).find('.container_toggle img').attr('src');
$(containerElem).find('.container_toggle img').attr('src', imgSrc.replace('expand', 'collapse'));
},
hideContainer: function (containerElem) {
var imgSrc = $(containerElem).find('.container_toggle img').attr('src');
$(containerElem).find('.container_toggle img').attr('src', imgSrc.replace('collapse', 'expand'));
}
}
var TodoItems = {
getContextsForAutocomplete: function (term, element_to_block) {
var allContexts = null;
var params = default_ajax_options_for_scripts('GET', relative_to_root('contexts.autocomplete'), element_to_block);
params.data = "term="+term;
params.dataType = "json";
params.async = false;
params.success = function(result){
allContexts = result;
}
$.ajax(params);
return allContexts;
},
askIfNewContextProvided: function(source, element_to_block) {
var givenContextName = $('#'+source+'todo_context_name').val();
if (givenContextName.length == 0) return true; // do nothing and depend on rails validation error
var contexts = TodoItems.getContextsForAutocomplete(givenContextName, element_to_block);
if (contexts) {
for (var i=0; i