diff --git a/static/scripts/trackster.js b/static/scripts/trackster.js index 336a068b92c..80addfdfc1b 100644 --- a/static/scripts/trackster.js +++ b/static/scripts/trackster.js @@ -677,6 +677,7 @@ var Drawable = function(name, view, container, prefs, drag_handle_class) { this.prefs = this.config.values; this.drag_handle_class = drag_handle_class; this.is_overview = false; + this.action_icons = {}; // FIXME: this should be a saved setting this.content_visible = true; @@ -688,8 +689,10 @@ var Drawable = function(name, view, container, prefs, drag_handle_class) { if (this.header_div) { this.container_div.append(this.header_div); - this.icons_div = this.build_icons_div().hide(); - this.header_div.append(this.icons_div); + this.icons_div = $("
").css("float", "left").hide().appendTo(this.header_div); + this.build_action_icons(this.action_icons_def); + this.header_div.append( $("") ); + // Suppress double clicks in header so that they do not impact viz. this.header_div.dblclick( function(e) { e.stopPropagation(); } ); @@ -706,6 +709,64 @@ var Drawable = function(name, view, container, prefs, drag_handle_class) { } }; +Drawable.prototype.action_icons_def = [ + // Hide/show drawable content. + { + name: "toggle_icon", + title: "Hide/show content", + css_class: "toggle", + on_click_fn: function(drawable) { + if ( drawable.content_visible ) { + drawable.action_icons.toggle_icon.addClass("toggle-expand").removeClass("toggle"); + drawable.hide_contents(); + drawable.content_visible = false; + } else { + drawable.action_icons.toggle_icon.addClass("toggle").removeClass("toggle-expand"); + drawable.content_visible = true; + drawable.show_contents(); + } + } + }, + // Edit settings. + { + name: "settings_icon", + title: "Edit settings", + css_class: "settings-icon", + on_click_fn: function(drawable) { + var cancel_fn = function() { hide_modal(); $(window).unbind("keypress.check_enter_esc"); }, + ok_fn = function() { + drawable.config.update_from_form( $(".dialog-box") ); + hide_modal(); + $(window).unbind("keypress.check_enter_esc"); + }, + check_enter_esc = function(e) { + if ((e.keyCode || e.which) === 27) { // Escape key + cancel_fn(); + } else if ((e.keyCode || e.which) === 13) { // Enter key + ok_fn(); + } + }; + + $(window).bind("keypress.check_enter_esc", check_enter_esc); + show_modal("Configure", drawable.config.build_form(), { + "Cancel": cancel_fn, + "OK": ok_fn + }); + } + }, + // Remove. + { + name: "remove_icon", + title: "Remove", + css_class: "remove-icon", + on_click_fn: function(drawable) { + // Tipsy for remove icon must be deleted when drawable is deleted. + $(".tipsy").remove(); + drawable.remove(); + } + } +]; + extend(Drawable.prototype, { init: function() {}, request_draw: function() {}, @@ -749,9 +810,27 @@ extend(Drawable.prototype, { */ build_header_div: function() {}, /** - * Build drawable's icons div. + * Add an action icon to this object. Appends icon unless prepend flag is specified. */ - build_icons_div: function() {}, + add_action_icon: function(name, title, css_class, on_click_fn, prepend) { + var drawable = this; + this.action_icons[name] = $("").attr("href", "javascript:void(0);").attr("title", title) + .addClass("icon-button").addClass(css_class).tipsy( {gravity: 's'} ) + .click( function() { on_click_fn(drawable); } ) + .appendTo(this.icons_div); + }, + /** + * Build drawable's icons div from object's icons_dict. + */ + build_action_icons: function(action_icons_def) { + // Create icons. + var icon_dict; + for (var i = 0; i < action_icons_def.length; i++) { + icon_dict = action_icons_def[i]; + this.add_action_icon(icon_dict.name, icon_dict.title, icon_dict.css_class, + icon_dict.on_click_fn, icon_dict.prepend); + } + }, /** * Update icons. */ @@ -880,65 +959,6 @@ extend(DrawableGroup.prototype, Drawable.prototype, DrawableCollection.prototype this.name_div = $("").addClass("track-name").text(this.name).appendTo(header_div); return header_div; }, - build_icons_div: function() { - var icons_div = $("").css("float", "left"); - - this.toggle_icon = $("").attr("href", "javascript:void(0);").attr("title", "Hide/show group content") - .addClass("icon-button toggle").tipsy( {gravity: 's'} ) - .appendTo(icons_div); - this.settings_icon = $("").attr("href", "javascript:void(0);").attr("title", "Edit settings") - .addClass("icon-button settings-icon").tipsy( {gravity: 's'} ) - .appendTo(icons_div); - this.remove_icon = $("").attr("href", "javascript:void(0);").attr("title", "Remove") - .addClass("icon-button remove-icon").tipsy( {gravity: 's'} ) - .appendTo(icons_div); - var group = this; - - // Toggle icon hides or shows the group content. - this.toggle_icon.click( function() { - if ( group.content_visible ) { - group.toggle_icon.addClass("toggle-expand").removeClass("toggle"); - group.hide_contents(); - group.content_visible = false; - } else { - group.toggle_icon.addClass("toggle").removeClass("toggle-expand"); - group.content_visible = true; - group.show_contents(); - } - }); - - // Clicking on settings icon opens group config. - this.settings_icon.click( function() { - var cancel_fn = function() { hide_modal(); $(window).unbind("keypress.check_enter_esc"); }, - ok_fn = function() { - group.config.update_from_form( $(".dialog-box") ); - hide_modal(); - $(window).unbind("keypress.check_enter_esc"); - }, - check_enter_esc = function(e) { - if ((e.keyCode || e.which) === 27) { // Escape key - cancel_fn(); - } else if ((e.keyCode || e.which) === 13) { // Enter key - ok_fn(); - } - }; - - $(window).bind("keypress.check_enter_esc", check_enter_esc); - show_modal("Configure Group", group.config.build_form(), { - "Cancel": cancel_fn, - "OK": ok_fn - }); - }); - - // Clicking on remove icon removes group. - this.remove_icon.click( function() { - // Tipsy for remove icon must be deleted when group is deleted. - $(".tipsy").remove(); - group.remove(); - }); - - return icons_div; - }, hide_contents : function () { this.content_div.hide(); }, @@ -2493,7 +2513,64 @@ var Track = function(name, view, container, prefs, data_url, data_query_wait) { this.content_div = $("