# -*- encoding : utf-8 -*- module KirguduBase module GuiHelper # Helpers to Draw url_for and Routes def control_tag(property, data_source, additional_data, html_options = {}) if property.input_type == :chosen_select chosen_select_tag(property, data_source, additional_data, html_options) elsif property.input_type == :select select_tag(property, data_source, additional_data, html_options) elsif property.input_type == :autocomplete_with_id autocomplete_with_id_tag(property, data_source, html_options) elsif property.input_type == :autocomplete #autocomplete(property, html_options) elsif property.input_type == :switch_button switch_button_tag(property, data_source, html_options) elsif property.input_type == :date_picker #date_picker_tag(property, html_options) elsif property.input_type == :time_picker time_picker_tag(property, data_source, html_options) elsif property.input_type == :image_selector image_selector_tag(property, data_source, html_options) elsif property.input_type == :check_box check_box_tag(property, data_source, html_options) elsif property.input_type == :radio_button #radio_button(property, html_options) elsif property.input_type == :link link_tag(property, data_source, html_options) elsif property.input_type == :elastic_text_area elastic_textarea_tag(property, data_source, html_options) elsif property.input_type == :password elastic_textarea_tag(property, data_source, html_options) elsif property.input_type == :text_area text_area_tag(property, data_source, html_options) elsif property.input_type == :file_input #file_input(property, data_source, html_options) elsif property.input_type == :collection_check_boxes else text_field_tag(property, data_source, html_options) end end def text_field_tag(property, data_source, html_options = {}) html_options ||= {} ::KirguduBase::GuiHelper.html_options_prepare_commons(html_options, property, data_source) html_options[:id] = SecureRandom.uuid.underscore unless html_options[:id] html_options.merge!({ type: "text", :"control-type" => "text-field" }) content_tag("input", '', html_options) end def text_area_tag(property, data_source, html_options = {}) html_options ||= {} ::KirguduBase::GuiHelper.html_options_prepare_commons(html_options, property, data_source) html_options[:id] = SecureRandom.uuid.underscore unless html_options[:id] html_options[:"control-type"] = "text-area" control_value = html_options[:value] html_options[:value] = nil html_options[:"property-value"] = nil content_tag("textarea", control_value, html_options) end def password_field_tag(property, data_source, html_options = {}) html_options ||= {} ::KirguduBase::GuiHelper.html_options_prepare_commons(html_options, property, data_source) html_options.merge!({ type: "password", :"control-type" => "password-field" }) html_options[:id] = SecureRandom.uuid.underscore unless html_options[:id] content_tag("input", '', html_options) end def switcher_tag(label, data, *args) options = args.extract_options! options ||= {} options.symbolize_keys! value_off = options[:'value-off'] || "0" value_on = options[:'value-on'] || "1" off_input_options = {type: "radio", name: "#{label}", id: "#{label}-off", value: value_off, control_type: "switcher"} on_input_options = {type: "radio", name: "#{label}", id: "#{label}-on", value: value_on, control_type: "switcher"} if data == true on_input_options[:'checked'] = true else off_input_options[:'checked'] = true end toggle_options = {} toggle_options[:class] = "toggle" + (data == value_on ? ' on' : ' off') toggle_options[:class] = "toggle on" control_html = content_tag('div', class: "row-fluid") do content_tag('div', class: "pull-left") do content_tag('label', class: 'radio off') do tag('input', off_input_options) end + content_tag('label', class: 'radio on') do tag('input', on_input_options) end + content_tag('div', toggle_options) do content_tag('div', class: "yes") do options['label-on'] || 'ON' end + content_tag('div', class: "switch") do end + content_tag('div', class: "no") do options['label-off'] || 'OFF' end end end end control_html end def autocomplete_field_tag(property, data_source, html_options = {}) html_options ||= {} html_options_add_class(html_options, "error") if @object.errors[property.name].present? #ToDo: Fix it end def autocomplete_with_id_tag(property, data_source, html_options = {}) html_options ||= {} label_value = if data_source.is_a?(Hash) data_source[property.text_value_property] else data_source.send(property.text_value_property) end html_options.merge!(property.html_options) if property.html_options html_options.merge!( { :"value-property" => property.data_source.value_property, :"label-property" => property.data_source.label_property, url: property.data_source.url, label: label_value } ) ::KirguduBase::GuiHelper.html_options_add_class(html_options, "autocomplete-with-id") ::KirguduBase::GuiHelper.html_options_prepare_commons(html_options, property, data_source) html_options[:"control-type"] = "autocomplete-with-id" html_options[:type] = "hidden" #raise "HTML OPTIONS in autocomplete: #{html_options.to_json}" content_tag("input", nil, html_options) end def file_input_tag(property, data_source, html_options={}) html_options ||= {} ::KirguduBase::GuiHelper.html_options_prepare_commons(html_options, property, data_source) html_options[:type] = "file" html_options[:"control-type"] = "file-input" html_options[:id] = SecureRandom.uuid.underscore unless html_options[:id] content_tag("input", '', html_options) end def date_picker_tag(property, data_source, html_options={}) html_options ||= {} ::KirguduBase::GuiHelper.html_options_prepare_commons(html_options, property, data_source) ::KirguduBase::GuiHelper.html_options_add_class(html_options, ["date-picker"]) html_options[:"data-date-format"] = "dd-mm-yyyy" #ToDo: Fix for all date formats html_options[:type] = "text" html_options[:"control-type"] = "date-picker" html_options[:id] = SecureRandom.uuid.underscore unless html_options[:id] content_tag("input", '', html_options) end def chosen_select_tag(property, data_source, choices_data, html_options = {}) #options = {}.merge(params) #options[:id] = SecureRandom.uuid.underscore unless options[:id] # #classes = options[:class] ? options[:class].to_s.split(' ') : [] #classes.push ("chzn-select") unless classes.include?("chzn-select") #options[:class] = classes.join(' ') #options[:'control-type'] = "chosen-select" # #if options[:placeholder] # options[:"data-placeholder"] = options[:placeholder] # options[:placeholder] = nil #end # ######################## BUILDING ONCHANGE COMMANDS ##################### # #if options[:onchange] # options[:onchange] = options[:onchange].to_json #end # ##if options[:onchange] ## commands_set = [] ## if options[:onchange].is_a?(Hash) ## commands_set.push(options[:onchange]) ## elsif options[:onchange].is_a?(Array) ## options[:onchange].each do |command| ## if command.is_a?(Hash) ## commands_set.push(command) ## end ## end ## end ## ## commands_set.each do |cmd_options| ## cmd_options[:id] = "#{options[:id]}_onchange_action" ## cmd_options[:style] = "display: none;" ## result += content_tag("div", "", cmd_options) ## end ##end # # ############################## BUILDING CHOICES ######################### # #choices_html = "" #if choices && choices.is_a?(Array) # # if options[:parent] # if options[:parent_value].nil? # options[:disabled] = "disabled" # end # end # # choices_html = content_tag('option', '', value: '') # choices.each do |chs| # choice_id = chs[1] # choice_label = chs[0] # choice_attributes = Hash.new # choice_attributes[:value] = choice_id # if options[:value] # choice_attributes[:selected] = "selected" if choice_id.to_s == options[:value].to_s # end # # choices_html += content_tag('option', choice_label, choice_attributes) # end #else # options[:disabled] = "disabled" #end # # #content_tag('select', choices_html.html_safe, options) html_options ||= {} ::KirguduBase::GuiHelper.html_options_add_class(html_options, ["chzn-select"]) html_options[:'control-type'] = 'chosen-select' select_tag(property, data_source, choices_data, html_options) end def select_tag(property, data_source, choices_data, html_options = {}) html_options ||= {} control_value = ::KirguduBase::GuiHelper.get_value_for_control(property, data_source) html_options[:'control-type'] ||= 'select' if property.data_source.type == :jquery || property.data_source.type == :jquery_db html_options.merge!({ :"ds-method" => property.data_source.http_method, :"ds-url" => property.data_source.url, :"ds-value-property" => property.data_source.value_property, :"ds-label-property" => property.data_source.label_property, #ds_type: property.data_source.type == :jquery ? :jquery : :local :"ds-format" => property.data_source.data_format }) end choices_html = content_tag("option", property.i18n_data_as_string(:placeholder) || "", value: "") choices_data.each do |chs| choice_id = chs[1] choice_label = chs[0] choice_attributes = { value: choice_id } choice_attributes[:selected] = "selected" if control_value && choice_id.to_s == control_value.to_s choices_html += content_tag("option", choice_label, choice_attributes) end if choices_data ::KirguduBase::GuiHelper.html_options_prepare_commons(html_options, property, data_source) html_options[:id] = SecureRandom.uuid.underscore unless html_options[:id] content_tag("select", choices_html.html_safe, html_options) end def link_to_property(property, entry, html_options = {}) html_options ||= {} if property.target if property.target == :blank html_options[:target] = "_blank" elsif property.target == :hover_preview elsif property.target == :model_dialog end end html_options[:"control-type"] = "link" html_options[:"link-target"] = property.target if property.target link_value = entry.send(property.name) link_label = property.i18n_data_as_string(:placeholder) || link_value link_to(link_label, link_value, html_options) unless link_value.nil? && link_value.blank? end def left_menu_item_link(text, path, image_path, html_options = {}) raise "Stale method: 'left_menu_item_link'" is_active = "current" if current_page?(path) content_tag('li', class: is_active) do content_tag("a", href: path, target: options[:target]) do tag('img', src: image_path, width: "16", height: "16", valign: "middle") + text end end end def left_menu_item_divider raise "Stale method: 'left_menu_item_divider'" content_tag('li', "", class: "divider") end def left_menu_item_title(title) raise "Stale method: 'left_menu_item_title'" content_tag('li', class: "menuText") do "#{title}:" end end def left_menu_item_disabled(text, image_path) raise "Stale method: 'left_menu_item_disabled'" content_tag('li') do tag('img', src: image_path, width: "16", height: "16", valign: "middle") + text end end def link_to_entry(body, opts = {}, html_options = {}) raise "Stale method: 'link_to_entry'" link_to body, url_for(:params => build_query_string_params(request.params, opts)), html_options end def link_to_unless_current_entry(body, opts = {}, html_options = {}) raise "Stale method: 'link_to_unless_current_entry'" link_to_unless_current body, url_for(:params => build_query_string_params(request.params, opts)), html_options end def link_to_paged(*args, &block) raise "Stale method: 'link_to_paged'" if block_given? options = args.first || {} html_options = args.second link_to_paged(capture(&block), options, html_options) else name = args[0] options = args[1] || {} html_options = args[2] || {} html_options['link-type'] = 'paged' link_to_nav1(name, options, html_options) end end def link_to_show(*args, &block) raise "Stale method: 'link_to_show'" if block_given? options = args.first || {} html_options = args.second link_to_show(capture(&block), options, html_options) else name = args[0] options = args[1] || {} html_options = args[2] || {} html_options['link-type'] = 'show' link_to_nav1(name, options, html_options) end end def link_to_search(*args, &block) raise "Stale method: 'link_to_search'" if block_given? options = args.first || {} html_options = args.second link_to_search(capture(&block), options, html_options) else name = args[0] options = args[1] || {} html_options = args[2] || {} html_options['link-type'] = 'search' link_to_nav1(name, options, html_options) end end def link_to_edit(*args, &block) raise "Stale method: 'link_to_edit'" if block_given? options = args.first || {} html_options = args.second link_to_edit(capture(&block), options, html_options) else name = args[0] options = args[1] || {} html_options = args[2] || {} html_options['link-type'] = 'edit' link_to_nav1(name, options, html_options) end end def link_to_new(*args, &block) raise "Stale method: 'link_to_new'" if block_given? options = args.first || {} html_options = args.second link_to_new(capture(&block), options, html_options) else name = args[0] options = args[1] || {} html_options = args[2] || {} html_options['link-type'] = 'new' link_to_nav1(name, options, html_options) end end def link_to_delete(*args, &block) raise "Stale method: 'link_to_delete'" if block_given? options = args.first || {} html_options = args.second link_to_delete(capture(&block), options, html_options) else name = args[0] options = args[1] || {} html_options = args[2] || {} html_options['link-type'] = 'delete' html_options['http-method'] = 'delete' html_options['confirm-message'] = 'Are You sure You want to delete This Object?' link_to_nav1(name, options, html_options) end end def link_to_cart_add(*args, &block) raise "Stale method: 'link_to_cart_add'" if block_given? options = args.first || {} html_options = args.second link_to_cart_add(capture(&block), options, html_options) else name = args[0] options = args[1] || {} html_options = args[2] || {} html_options['link-type'] = 'cart-add' link_to_nav1(name, options, html_options) end end def link_to_cart_remove(*args, &block) raise "Stale method: 'link_to_cart_remove'" if block_given? options = args.first || {} html_options = args.second link_to_cart_remove(capture(&block), options, html_options) else name = args[0] options = args[1] || {} html_options = args[2] || {} html_options['link-type'] = 'cart-add' link_to_nav1(name, options, html_options) end end def link_to_mass(*args, &block) raise "Stale method: 'link_to_mass'" if block_given? options = args.first || {} html_options = args.second link_to_mass(capture(&block), options, html_options) else name = args[0] options = args[1] || {} html_options = args[2] || {} html_options['link-type'] = 'mass-action' link_to_nav1(name, options, html_options) end end def link_to_leftmenu(*args, &block) raise "Stale method: 'link_to_leftmenu'" if block_given? options = args.first || {} html_options = args.second link_to_leftmenu(capture(&block), options, html_options) else name = args[0] options = args[1] || {} html_options = args[2] || {} html_options['link-type'] = 'left-menu' link_to_nav1(name, options, html_options) end end def link_to_nav(label, url, html_options = {}) raise "Stale method: 'link_to_nav'" html_options ||= {} ::KirguduBase::GuiHelper.html_options_add_class(html_options, "navigate-link") link_to label, url, html_options end def self.html_options_add_class(html_options, classes_to_add) classes = html_options[:class] ? html_options[:class].split(' ') : [] if classes_to_add.is_a?(Array) classes_to_add.each do |item| classes << item.to_s unless classes.include?(item.to_s) end else classes << classes_to_add.to_s unless classes.include?(classes_to_add.to_s) end html_options[:class] = classes.join(" ") if classes.length > 0 end def self.html_options_set_ids(html_options, property, data_source) if data_source.is_a?(Hash) html_options.merge!( { id: property.name, name: property.name } ) elsif data_source.class.respond_to?(:for_form_params) html_options.merge!( { id: "#{data_source.class.for_form_params}_#{property.name}", name: "#{data_source.class.for_form_params}[#{property.name}]" } ) else for_form_params = data_source.class.to_s.underscore.gsub('/', '_') html_options.merge!( { id: "#{for_form_params}_#{property.name}", name: "#{for_form_params}[#{property.name}]" } ) end end def self.get_value_for_control(property, data_source) if data_source.is_a?(Hash) #raise "Data Source from HASH: #{data_source.to_json}" data_source[property.name] else data_source.send(property.name) end end def self.html_options_prepare_commons(html_options, property, data_source) control_value = ::KirguduBase::GuiHelper.get_value_for_control(property, data_source) html_options.merge!(property.html_options) if property.html_options html_options.merge!( value: control_value, :"property-name" => property.name, :"property-value" => control_value, :"parent-property" => (property.parent if property.parent), :"placeholder" => (property.i18n_data_as_string(:placeholder) if property.placeholder) ) # Adding to control attribute value: class = 'error' if error if data_source.respond_to?(:errors) && data_source.errors.is_a?(Hash) ::KirguduBase::GuiHelper.html_options_add_class(html_options, ["error"]) if data_source.errors[property.name].present? end ::KirguduBase::GuiHelper.html_options_set_ids(html_options, property, data_source) end end end