module ExtjsHelper PARTIES_PATH = "/admin/parties/\\d+".freeze PANEL_NOTIFICATION_CLASS_NAME = 'xlsuite-tabpanel-inside-notifications'.freeze def get_half_height_size_of_tabpanel %Q`(xl.centerPanel.getInnerHeight()-parent.xl.centerPanel.getBottomToolbar().getSize().height)/2` end def to_extjs_date_field_value(input_time) time = Time.now time = input_time if !input_time.blank? && input_time.kind_of?(Time) time.strftime("%m/%d/%Y") end def refresh_grid_datastore_of(model_name) %Q` xl.runningGrids.each(function(pair){ var grid = pair.value; var dataStore = grid.getStore(); if (dataStore.proxy.conn.url.match(new RegExp('#{model_name.pluralize}\\.json$', "i"))) { dataStore.reload(); } }) ` end def close_tab_panel(tab_panel_id) "xl.closeTabPanel('#{tab_panel_id}');" end def send_default_get_ajax_request(source) %Q` new Ajax.Request('#{source}', {asynchronous:true, evalScripts:true, method:'get'}); ` end def render_inside_panel_notifications_container(class_name=PANEL_NOTIFICATION_CLASS_NAME) %Q`
` end def update_inside_panel_notifications(id=nil) id = create_id_from_params unless id %Q` var notificationElement = $$('##{id} .#{PANEL_NOTIFICATION_CLASS_NAME}').first(); if (notificationElement) { notificationElement.innerHTML = #{show_flash_messages.to_json}; } ` end # This helper method cannot be used in RHTML templates because the block is not captured def create_tab_using_ajax_response(title, prepend="", &block) mapped_id = create_id_from_params result = %Q` try { function #{mapped_id}(){ var mappedId = "#{mapped_id}"; if (xl.runningTabs.get(mappedId)) { xl.runningTabs.get(mappedId).show(); } else { var other_links_container = ""; var links_container = ""; var close_other_tabs_link = "Close other tabs"; var newPanel = new Ext.Panel({ id: mappedId, cls: 'tab-panel-wrapper', region: "center", title: "#{title}", titlebar: true, tbar: new Ext.Toolbar({ style: 'background-color: ' + xl.kToolbarBrownCSSColor + ';', items: [ other_links_container + links_container + close_other_tabs_link ] }), layout: 'fit', autoWidth: true, }); newPanel.on('beforedestroy', function() { newPanel.items.clear(); xl.runningTabs.unset(mappedId); }); xl.tabPanel.add(newPanel).show(); xl.runningTabs.set(mappedId, newPanel); ` result << block.call if block result << %Q` xl.viewport.render(); xl.resizeTabPanel(); newPanel.syncSize(); newPanel.on("show", function(){ xl.viewport.render(); xl.resizeTabPanel(); }); // Execute the post-render callback if present if (typeof _afterRenderCallback == 'function') { _afterRenderCallback(); } #{prepend} } } // end #{mapped_id}() #{mapped_id}(); } catch (error) { xl.log("Error Evaluating #{mapped_id}:"); xl.log(error); } ` result end def create_id_from_params array = [params[:controller], params[:action]] params_id = params[:id].blank? ? "nil" : params[:id] array << params_id array.join("_") end def link_to_function_in_iframe(iframe_id, *args) args[1] = "$('#{iframe_id}').contentWindow." + args[1] link_to_function(*args) end def open_last_incoming_request_tab return if last_incoming_request.blank? javascript_tag %Q! Ext.onReady(function(){ xl.createTab("#{last_incoming_request}"); }); ! end def render_using_extjs_layout javascript_tag %Q~ if (!parent.location.pathname.match(new RegExp("/admin$", "i"))) { window.location.href="#{blank_landing_url}"; } ~ end def update_notices_using_ajax_response(options={}) options.reverse_merge!({:on_root => false}) on_root = options[:on_root] ? "" : "parent." %Q~ #{on_root}$("status-bar-notifications").innerHTML = "#{render_plain_flash_messages}"; ~ end def get_default_grid_height(parent=true) if parent "parent.xl.centerPanel.getInnerHeight()-parent.xl.centerPanel.getBottomToolbar().getSize().height-15" else "xl.centerPanel.getInnerHeight()-xl.centerPanel.getBottomToolbar().getSize().height-15" end end def render_plain_flash_messages flashes = [] flashes << flash[:notice] flashes << flash[:message] flashes << flash[:warning] flashes.flatten.compact.map(&:strip).join(", ") end def update_notices javascript_tag %Q~ var iframe_object = parent.Ext.get('#{@_current_page_uri}'); if (!iframe_object) { iframe_object = parent.Ext.get('#{@_current_page_url}'); } if (iframe_object) { iframe_object.on("load", function(){ parent.$("status-bar-notifications").innerHTML = "#{e(render_plain_flash_messages)}" }); } ~ end def disable_iframe_scrolling javascript_tag %Q~ Ext.EventManager.onDocumentReady( function(){ var uri = "#{@_current_page_uri}"; var url = "#{@_current_page_url}"; var iframeId = parent.xl.generateIframeIdFromSource(uri); if (!iframeId) { iframeId = parent.xl.generateIframeIdFromSource(url); } var iframe_object = parent.$(uri); if (!iframe_object) { iframe_object = parent.$(url); } if (!iframe_object) { iframe_object = parent.$(iframeId); } if (iframe_object) { iframe_object.scrolling = "no"; } }); ~ end def initialize_east_console_title() params_clone = params.clone title = if @party "Related to " + @party.name.to_s else "Record Dashboard" end javascript_tag %Q! if(parent.xl) parent.xl.setEastTitle("#{title}", parent.$('current-displayed-iframe-source').value); ! end # TODO : need to clean this method especially the javascript code def initialize_center_header(default_title=nil) title = if default_title.blank? params_clone = params.clone temp = params_clone.delete(:controller).humanize + " | " + params_clone.delete(:action).humanize @title.blank? ? temp : @title else text end #link_to_this_page = link_to_function("Link to this page", "'#{@_current_page_url}'.execCommand('Copy')") #link_to_this_page = link_to_function("Link to this page", "window.clipboardData.setData('Text', '#{@_current_page_url}')") # This removes the http://mydomain.ext; the resulting string # matches the id of an iframe on the page and thusly the corresponding # titlebar spans for that. I don't think we can just do this.id # in the JS because I don't think this has any scope uri = @_current_page_uri url = @_current_page_url link_to_this_page = link_to("Link to this page", @_current_page_uri) out = [] javascript_tag %Q~ // This is the IFRAME's documentReady if(parent.xl) { Ext.EventManager.onDocumentReady( function(){ // Set up the variables for convenience var uri = '#{uri}'; var url = '#{url}'; var title = '#{title}'; var iframeId = parent.xl.generateIframeIdFromSource(uri); if (!iframeId) { iframeId = parent.xl.generateIframeIdFromSource(url); } // Ext.Panels are stored in parent.xl.runningTabs var panel_via_uri= parent.xl.runningTabs.get(uri); var panel_via_url = parent.xl.runningTabs.get(url); var panel_via_iframeId = parent.xl.runningTabs.get(iframeId); // Check to see which method is needed // and set iframeId accordingly if (panel_via_uri) { panel_via_uri.setTitle(title); iframeId = uri; } if (panel_via_url) { panel_via_url.setTitle(title); iframeId = url; } if (panel_via_iframeId){ panel_via_iframeId.setTitle(title); } // Set the fields if ( parent.$(iframeId + "-link") ){ parent.$(iframeId + "-link").update("#{e(link_to_this_page)}"); } if ( parent.$(iframeId + "-other-links") ){ parent.$(iframeId + "-other-links").update(""); } if ( parent.$(iframeId + '-refresh-iframe-link') ){ parent.$(iframeId + '-refresh-iframe-link').observe('click', function(e) { window.location.reload(true); }, false); } } ) } ~ end def change_centre_iframe_src(path, default_id=nil) if default_id "xl.createTab('#{path}', '#{default_id}')" else "xl.createTab('#{path}')" end end def create_tab_as_div(path) "xl.createTabAsDiv('#{path}')" end end