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; }); $('input[class=predecessor_add_button]').live('click', function(){ var text = $('input[name=predecessor_list]').val(); if (text.length > 0) { $('ul#predecessor_ul').show(); if (text.length > 35) { text = text.substring(0,35)+"..."; } var html = $('ul#predecessor_ul').html(); var new_li = "
  • "+ text + "
  • "; $('ul#predecessor_ul').html(html + new_li); $('input[name=predecessor]').val(''); $('input[name=predecessor]').focus(); } }); } } 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 35) { // cut off string todo_spec = todo_spec.substring(0,35)+"..."; } // show the new dep in list var html = $('ul#predecessor_ul').html(); var new_li = "
  • "+ todo_spec + "
  • "; $('ul#predecessor_ul').html(html + new_li); $('li#pred_'+todo_id).slideDown(500); $('input[name=predecessor_input]').val(''); $('input[name=predecessor_input]').focus(); return false; } }); $('input[class=pred_remove_button]').live('click', function() { predecessor_list = $('input[name=predecessor_list]'); id_list = split( predecessor_list.val() ); $("li#pred_"+this.id).slideUp(500).remove(); // remove from ul // remove from array new_list = new Array(); while (id_list.length > 0) { elem = id_list.pop(); if (elem != this.id && elem != '' && elem != ' ') { new_list.push ( elem ); } } // update id list predecessor_list.val( new_list.join(", ") ); return false; // prevent submit }) /* have to bind on keypress because of limitations of live() */ $('input[name=project_name]').live('keypress', function(){ $(this).bind('blur', project_defaults); }); $('input[name=context_name]').live('keypress', function(){ $(this).attr('edited', 'true'); }); $('input[name=tag_list]').live('keypress', function(){ $(this).attr('edited', 'true'); }); /* Drag & Drop for successor/predecessor */ function drop_todo(evt, ui) { var dragged_todo = ui.draggable[0].id.split('_')[2]; var dropped_todo = this.id.split('_')[2]; ui.draggable.remove(); $('.drop_target').hide(); // IE8 doesn't call stop() in this situation $(this).block({ message: null }); $.post(relative_to_root('todos/add_predecessor'), { successor: dragged_todo, predecessor: dropped_todo }, null, 'script'); } function drag_todo(){ $('.drop_target').show(); $(this).parents(".container").find(".context_target").hide(); } $('.item-show').draggable({ handle: '.grip', revert: 'invalid', start: drag_todo, stop: function() { $('.drop_target').hide(); } }); $('.item-show').droppable({ drop: drop_todo, tolerance: 'pointer', hoverClass: 'hover' }); /* Drag & drop for changing contexts */ function drop_todo_on_context(evt, ui) { var target = $(this); var dragged_todo = ui.draggable[0].id.split('_')[2]; var context_id = this.id.split('_')[1]; ui.draggable.remove(); target.block({ message: null }); setTimeout(function() { target.show() }, 0); $.post(relative_to_root('todos/change_context'), { "todo[id]": dragged_todo, "todo[context_id]": context_id }, function(){ target.unblock(); target.hide(); }, 'script'); } $('.context_target').droppable({ drop: drop_todo_on_context, tolerance: 'pointer', hoverClass: 'hover' }); /* Reset auto updater */ field_touched = false; /* shrink the notes on the project pages. This is not live(), so this needs * to be run after ajax adding of a new note */ $('.note_wrapper').truncate({ max_length: 90, more: '', less: '' }); } $(document).ready(function() { TracksPages.setup_nifty_corners(); TodoItemsContainer.setup_container_toggles(); /* enable page specific behavior */ $([ 'IntegrationsPage', 'NotesPage', 'ProjectListPage', 'ContextListPage', 'FeedsPage', 'RecurringTodosPage', 'TodoItems', 'TracksPages', 'TracksForm', 'SearchPage', 'UsersPage' ]).each(function() { eval(this+'.setup_behavior();'); }); /* Gets called from all AJAX callbacks, too */ enable_rich_interaction(); });