# Tracks specific coffeescript TracksApp = dialog_data: { project: { title: "Go to project" placeholder: "Type (part of) project name" autocomplete_link: "<%= Rails.application.routes.url_helpers.projects_path(format: :autocomplete) %>" target_link: "<%= Rails.application.routes.url_helpers.project_path "" %>" target_id: "id" # json/datum field containing id to use } context: { title: "Go to context" placeholder: "Type (part of) context name" autocomplete_link: "<%= Rails.application.routes.url_helpers.contexts_path(format: :autocomplete) %>" target_link: "<%= Rails.application.routes.url_helpers.context_path ""%>" target_id: "id" # json/datum field containing id to use } tag: { title: "Go to tagged actions" placeholder: "Type (part of) tag name" autocomplete_link: "<%= Rails.application.routes.url_helpers.tags_autocomplete_path %>" target_link: "<%= Rails.application.routes.url_helpers.tag_path "" %>" # json/datum field containing id to use. For tag it is the tag name, not the id field target_id: "value" } } refresh_page: -> location.reload(true) goto_page: (page) -> window.location.href = page go_home_page: -> TracksApp.goto_page "<%= Rails.application.routes.url_helpers.root_path %>" go_contexts_page: -> TracksApp.goto_page "<%= Rails.application.routes.url_helpers.contexts_path %>" go_projects_page: -> TracksApp.goto_page "<%= Rails.application.routes.url_helpers.projects_path %>" go_starred_page: -> TracksApp.goto_page "<%= Rails.application.routes.url_helpers.tag_path("starred") %>" go_menu: -> $('div#tracks-goto-dialog').modal() add_todo: -> $('div#tracks-add-action-dialog').modal() go_project: -> TracksApp.show_item_dialog(TracksApp.dialog_data.project) go_context: -> TracksApp.show_item_dialog(TracksApp.dialog_data.context) go_tag: -> TracksApp.show_item_dialog(TracksApp.dialog_data.tag) show_item_dialog: (settings) -> dialog = $('div#tracks-go-item-dialog') # clear input field and set attributes dialog.find("input#tracks-goto-item").typeahead("val", "") dialog.find("input#tracks-goto-item").attr("placeholder", settings.placeholder) dialog.find("input#tracks-goto-item").attr("data-link", settings.autocomplete_link) dialog.find("form").attr("data-link", settings.target_link) dialog.find("form").attr("data-id", settings.target_id) # set title of dialog dialog.find("h3#myModalLabel").html(settings.title) # set focus to input field when dialog is shown dialog.on 'shown', -> # twitter-typeahead adds span around search field with display:inline-block. This causes # the search field to ignore the width set in tracks.css $("span.twitter-typeahead").css("display", "block") $("input#tracks-goto-item").focus() # show the dialog dialog.modal() createSubmenu: (todo, itemToAddBefore) -> template_clone = $("div.todo-sub-menu-template").clone() itemToAddBefore.before(template_clone) todo_menu = todo.find("div.todo-sub-menu-template") todo_menu.removeClass("todo-sub-menu-template") todo_menu.addClass("todo-sub-menu") info = todo.find('a[rel="tracks-popover"]') info.attr("data-content", todo.attr("data-info")) info.popover() todo_menu.removeClass("hide") appendTodoSubMenu: (todo) -> if todo.find("div.todo-sub-menu").length is 0 notes_row = todo.find(".todo-notes").parent() submenu = TracksApp.createSubmenu(todo, notes_row) else todo.find("div.todo-sub-menu").removeClass("hide") selectTodo: (new_todo) -> selected_item = $("div.todo-item.selected-item") # do nothing if the new_todo is already selected unless new_todo.attr("id") == selected_item.attr("id") selected_item.find("div.todo-sub-menu").addClass("hide") selected_item.find("span.todo-item-detail").addClass("hide") TracksApp.noteOfTodo(selected_item).addClass("hide") selected_item.removeClass("selected-item") selected_item.find("div.tracks-todo-badges").addClass("hidden-phone") TracksApp.appendTodoSubMenu(new_todo) new_todo.find("span.todo-item-detail").removeClass("hide") new_todo.addClass("selected-item") new_todo.find("div.tracks-todo-badges").removeClass("hidden-phone") selectPrevNext: (go_next) -> current = prev = next = null stop = false $("div.todo-item").each -> if stop next = $(this) return false prev = current current = $(this) if $(this).hasClass("selected-item") stop = true if go_next TracksApp.selectTodo(prev) if prev? return prev else TracksApp.selectTodo(next) if next? return next selectPrev: -> unless TracksApp.selectPrevNext(true)? TracksApp.selectTodo($("div.todo-item").last()) selectNext: -> unless TracksApp.selectPrevNext(false)? TracksApp.selectTodo($("div.todo-item").first()) noteOfTodo: (todo) -> notes_id = todo.find("i.icon-book").attr("data-note-id") $("div#" + notes_id ) toggleNoteOfTodo: (todo) -> TracksApp.noteOfTodo(todo).toggleClass("hide") toggleNoteOfSelectedTodo: -> selected_item = $("div.todo-item.selected-item") TracksApp.toggleNoteOfTodo(selected_item) toggleDetailsPopover: -> if $("body").hasClass("projects") selected_item = $("div.project-badges") else selected_item = $("div.todo-item.selected-item") selected_item.find("a.tracks-popover-toggle").popover("toggle") group_view_by: (state) -> $.cookie('group_view_by', state) group_view_by_context: -> TracksApp.group_view_by('context') TracksApp.refresh_page() group_view_by_project: -> TracksApp.group_view_by('project') TracksApp.refresh_page() # Make TracksApp globally accessible. From http://stackoverflow.com/questions/4214731/coffeescript-global-variables root = exports ? this root.TracksApp = TracksApp $ -> $("a#menu-keyboard-shotcuts").click -> $('div#tracks-shortcuts-dialog').modal() $("a.button-add-todo").click -> TracksApp.add_todo() $("a.button-home").click -> TracksApp.go_home() $("a.button-goto").click -> TracksApp.go_menu() $("span.todo-description-icons i.icon-book").click -> TracksApp.toggleNoteOfTodo( $(this).parent().parent().parent().parent().parent() ) $("span.todo-item-description-container").click -> TracksApp.selectTodo( $(this).parent().parent().parent() ) autocompleteDataset = new Dataset limit: 7 remote: url: "/wrong-url-from-tracks-js.autocomplete" replace: (url, query) -> # replace url with data-link attribute $("input#tracks-goto-item").attr("data-link")+"?query="+query autocompleteDataset.initialize() $('input.ajax-typeahead').typeahead minLength: 2 hint: true autoselect: true sections: highlight: true source: autocompleteDataset $('input.ajax-typeahead').on "typeahead:selected", (ev, datum) -> form = $("div#tracks-go-item-dialog form") base_url = form.attr("data-link") base_id = datum[form.attr("data-id")] form.attr("action", base_url + base_id) form.submit() $('a[rel="tracks-popover"]').popover() $('div.project-description button.close').on "click", -> $(this).parent().addClass("hide") $("a.tracks-badge-description").removeClass("hide") $("a.tracks-badge-description").on "click", -> $("div.project-description").removeClass("hide") $(this).addClass("hide")