cirandas.net

ref: master

plugins/responsive/lib/ext/application_helper.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
require_dependency 'application_helper'

module ApplicationHelper

  protected

  module ResponsiveMethods
    FORM_CONTROL_CLASS = 'form-control'

    def button(type, label, url, html_options = {})
      return super unless theme_responsive?

      option = html_options.delete(:option) || 'default'
      size = html_options.delete(:size) || 'xs'
      the_class = "with-text btn btn-#{size} btn-#{option} icon-#{type}"
      the_class << ' ' << html_options[:class] if html_options.has_key?(:class)

      #button_without_text type, label, url, html_options.merge(class: the_class)
      the_title = html_options[:title] || label
      if html_options[:disabled]
        content_tag(:a, content_tag(:span, label), html_options.merge(class: the_class, title: the_title))
      else
        link_to(content_tag(:span, label), url, html_options.merge(class: the_class, title: the_title))
      end
    end

    def button_without_text(type, label, url, html_options = {})
      return super unless theme_responsive?

      option = html_options.delete(:option) || 'default'
      size = html_options.delete(:size) || 'xs'
      the_class = "btn btn-#{size} btn-#{option} icon-#{type}"
      if html_options.has_key?(:class)
        the_class << ' ' << html_options[:class]
      end
      the_title = html_options[:title] || label
      if html_options[:disabled]
        content_tag(:a, '', html_options.merge(class: the_class, title: the_title))
      else
        link_to('', url, html_options.merge(class: the_class, title: the_title))
      end
    end

    def button_to_function(type, label, js_code, html_options = {}, &block)
      return super unless theme_responsive?

      option = html_options.delete(:option) || 'default'
      size = html_options.delete(:size) || 'xs'
      html_options[:class] = "btn btn-#{size} btn-#{option} with-text #{html_options[:class]}"
      html_options[:class] << " icon-#{type}"
      link_to_function(label, js_code, html_options, &block)
    end

    def button_to_function_without_text(type, label, js_code, html_options = {}, &block)
      return super unless theme_responsive?
      html_options[:title] ||= label
      option = html_options.delete(:option) || 'default'
      size = html_options.delete(:size) || 'xs'
      html_options[:class] = "" unless html_options[:class]
      html_options[:class] << " btn btn-#{size} btn-#{option} icon-#{type}"
      link_to_function('', js_code, html_options, &block)
    end

    def button_to_remote(type, label, options, html_options = {})
      return super unless theme_responsive?
      option = html_options.delete(:option) || 'default'
      size = html_options.delete(:size) || 'xs'
      html_options[:class] = "btn btn-#{size} btn-#{option} with-text" unless html_options[:class]
      html_options[:class] << " icon-#{type}"
      link_to_remote(label, options, html_options)
    end

    def button_to_remote_without_text(type, label, options, html_options = {})
      return super unless theme_responsive?

      html_options[:title] ||= label
      option = html_options.delete(:option) || 'default'
      size = html_options.delete(:size) || 'xs'
      html_options[:class] = "" unless html_options[:class]
      html_options[:class] << " btn btn-#{size} btn-#{option} icon-#{type}"
      link_to_remote('', options, html_options.merge(title: label))
    end

    def icon(icon_name, html_options = {})
      return super unless theme_responsive?

      option = html_options.delete(:option) || 'default'
      size = html_options.delete(:size) || 'xs'
      the_class = "btn btn-#{size} btn-#{option} #{icon_name}"
      if html_options.has_key?(:class)
        the_class << ' ' << html_options[:class]
      end
      content_tag(:div, '', html_options.merge(class: the_class))
    end

    def icon_button(type, text, url, html_options = {})
      return super unless theme_responsive?

      option = html_options.delete(:option) || 'default'
      size = html_options.delete(:size) || 'xs'
      the_class = "btn btn-#{size} btn-#{option} icon-button icon-#{type}"
      if html_options.has_key?(:class)
        the_class << ' ' << html_options[:class]
      end

      link_to(content_tag(:span, text), url, html_options.merge(class: the_class, title: text))
    end

    def button_bar options = {}, &block
      return super unless theme_responsive?

      options[:class] ||= ''
      options[:class] << 'button-bar'

      content_tag :div, options do
        [
          capture(&block).to_s,
          tag('br', style: 'clear: left;'),
        ].safe_join
      end
    end

    def expirable_button(content, action, text, url, html_options = {})
      return super unless theme_responsive?

      option = html_options.delete(:option) || 'default'
      size = html_options.delete(:size) || 'xs'
      html_options[:class] = ["btn btn-#{size} btn-#{option} with-text icon-#{action.to_s}", html_options[:class]].compact.join(' ')
      expirable_content_reference content, action, text, url, html_options
    end

    def search_contents_menu
      return super unless theme_responsive?

      host  = environment.default_hostname
      links = [
        [_('All contents'),             {host: host, controller: :search, action: :contents, category_path: ''}],
        [s_('contents|More recent'),    {host: host, controller: :search, action: :contents, filter: 'more_recent'}],
        [s_('contents|More viewed'),    {host: host, controller: :search, action: :contents, filter: 'more_popular'}],
        [s_('contents|Most commented'), {host: host, controller: :search, action: :contents, filter: 'more_comments'}],
      ]
      if logged_in?
        links.push [_('New content'), '', modal_options({href: url_for({controller: 'cms', action: 'new', profile: current_user.login, cms: true})})]
      end

      content_tag :li, class: 'dropdown' do
        [
          link_to('#', class: 'dropdown-toggle icon-menu-articles', title: _('Contents'), data: {toggle: 'dropdown', hover: 'dropdown'}) do
            content_tag :span, _('Contents')
          end,
          content_tag(:ul, class: 'dropdown-menu', role: 'menu') do
            links.map do |(name, url)|
              content_tag :li do
                link_to name, url
              end
            end.safe_join
          end,
        ].safe_join
      end
    end

    def search_people_menu
      return super unless theme_responsive?

      host  = environment.default_hostname
      links = [
        [_('All people'),           {host: host, controller: :search, action: :people, category_path: ''}],
        [s_('people|More recent'),  {host: host, controller: :search, action: :people, filter: 'more_recent'}],
        [s_('people|More active'),  {host: host, controller: :search, action: :people, filter: 'more_active'}],
        [s_('people|More popular'), {host: host, controller: :search, action: :people, filter: 'more_popular'}],
      ]
      if logged_in?
        links.push [_('My friends'),     {profile: current_user.login, controller: 'friends'}]
        links.push [_('Invite friends'), {profile: current_user.login, controller: 'invite', action: 'friends'}]
      end

      content_tag :li, class: 'dropdown' do
        [
          link_to('#', class: "dropdown-toggle icon-menu-people", title: _('People'), data: {toggle: 'dropdown', hover: 'dropdown'}) do
            content_tag :span, _('People')
          end,
          content_tag(:ul, class: 'dropdown-menu', role: 'menu') do
            links.map do |params|
              content_tag :li do
                link_to *params
              end
            end.safe_join
          end
        ].safe_join
      end
    end

    def search_communities_menu
      return super unless theme_responsive?

      host  = environment.default_hostname
      links = [
        [_('All communities'),           {host: host, controller: :search, action: :communities, category_path: ''}],
        [s_('communities|More recent'),  {host: host, controller: :search, action: :communities, filter: 'more_recent'}],
        [s_('communities|More active'),  {host: host, controller: :search, action: :communities, filter: 'more_active'}],
        [s_('communities|More popular'), {host: host, controller: :search, action: :communities, filter: 'more_popular'}],
      ]
      if logged_in?
        links.push [_('My communities'), {profile: current_user.login, controller: 'memberships'}]
        links.push [_('New community'),  {profile: current_user.login, controller: 'memberships', action: 'new_community'}]
      end

      content_tag :li, class: 'dropdown' do
        [
          link_to('#', class: 'dropdown-toggle icon-menu-community', title: _('Communities'), data: {toggle: 'dropdown', hover: 'dropdown'}) do
            content_tag :span, _('Communities')
          end,
          content_tag(:ul, class: 'dropdown-menu', role: 'menu') do
            links.map do |params|
              content_tag :li do
                link_to *params
              end
            end.safe_join
          end
        ].safe_join
      end
    end

    def manage_link(list, kind, title)
      return super unless theme_responsive?

      if list.present?
        link_to_all = nil
        if list.count > 5
          list = list.first(5)
          link_to_all = link_to(content_tag(:strong, _('See all')), controller: 'memberships', profile: user.identifier)
        end
        link = list.map do |element|
          link_to(content_tag(:strong, element.short_name(25)), element.admin_url, class: "icon-menu-"+element.class.identification.underscore, title: _('Manage %s') % element.short_name)
        end
        if link_to_all
          link << link_to_all
        end

        [
          content_tag(:li, nil, class: 'divider'),
          content_tag(:li, title, class: 'dropdown-header'),
          link.map do |l|
            content_tag :li, l
          end.safe_join
        ].safe_join
      end
    end

    def popover_menu title,menu_title,links,html_options={}
      return super unless theme_responsive?

      menu_content = ""
      first = true
      links.each do |link|
        if link[:link].present?
          menu_content += link[:link]
        else
          link.each do |link_label , html_options|
            unless html_options[:style].present? and html_options[:style] =~ /display *: *none/
              menu_content << '<br/>' unless first
              first = false
              menu_content << content_tag(:a, link_label,html_options)
            end
          end
        end
      end

      option = html_options.delete(:option) || 'default'
      size   = html_options.delete(:size) || 'xs'
      button_tag '',
        class: "btn btn-#{size} btn-#{option} btn-popover-menu icon-parent-folder",
        data:  {
          html:      'true',
          toggle:    'popover',
          placement: 'top',
          trigger:   'focus',
          content:   menu_content,
          title:     menu_title,
        }
    end


    def tag(name, options = nil, open = false, escape = true)
      return super unless theme_responsive?

      # Call the original tag method to do the real work
      field_html = super

      field_html =~ /\A[^>]* type=['"]([^'"]*)['"]/
      field_html =~ /\A<(\w*)/ unless $1
      field_type = $1

      if %w(text email number password select textarea url).include? field_type
        field_html =~ /\A[^>]* class=['"]([^'"]*)['"]/
        if $1
          field_html = field_html.sub $1, $1+" #{FORM_CONTROL_CLASS}"
        else
          field_html =~ /\A(<\w*)/
          field_html = field_html.sub $1, $1+" class='#{FORM_CONTROL_CLASS}' "
        end
      end

      field_html
    end

    def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
      return super unless theme_responsive?

      # Call the original tag method to do the real work
      field_html = super

      field_html =~ /\A[^>]* type=['"]([^'"]*)['"]/
      field_html =~ /\A<(\w*)/ unless $1
      field_type = $1

      if %w(text email number password select textarea url).include? field_type
        field_html =~ /\A[^>]* class=['"]([^'"]*)['"]/
        if $1
          field_html = field_html.sub $1, $1+" #{FORM_CONTROL_CLASS}"
        else
          field_html =~ /\A(<\w*)/
          field_html = field_html.sub $1, $1+" class='#{FORM_CONTROL_CLASS}' "
        end
      end

      field_html
    end

    def labelled_form_for(name, options = {}, &proc)
      return super unless theme_responsive?

      if options[:horizontal]
        options[:html] = {} unless options[:html]
        options[:html][:class] = "" unless options[:html][:class]
        options[:html][:class] << " form-horizontal"
      end
      form_for(name, { builder: NoosferoFormBuilder }.merge(options), &proc)
    end

  # TODO: Make optional fields compliant to horizontal form
  #  def optional_field profile, name, field_html = nil, only_required = false, &block
  #  end

  end

  include ResponsiveChecks
  prepend ResponsiveMethods

  # TODO: apply theme_responsive? condition
  class NoosferoFormBuilder

    def self.output_field text, field_html, field_id = nil, options={}
      # try to guess an id if none given
      if field_id.nil?
        field_html =~ /\A[^>]* id=['"]([^'"]*)['"]/
        field_id = $1
      end
      field_html =~ /\A[^>]* type=['"]([^'"]*)['"]/
      field_html =~ /\A<(\w*)/ unless $1
      field_type = $1

      if %w(text email number password select textarea url).include? field_type
        field_html =~ /\A[^>]* class=['"]([^'"]*)['"]/
        if $1
          field_html = field_html.sub $1, $1+" #{ApplicationHelper::ResponsiveMethods::FORM_CONTROL_CLASS}"
        else
          field_html =~ /\A(<\w*)/
          if $1
            field_html = field_html.sub $1, $1+" class='#{ApplicationHelper::ResponsiveMethods::FORM_CONTROL_CLASS}' "
          end
        end
      end

      if options[:horizontal]
        label_html = content_tag :label, gettext(text), class: 'control-label col-sm-3 col-md-2 col-lg-2', for: field_id
        content    = [
          label_html,
          content_tag(:div, field_html.html_safe, class: 'col-sm-9 col-md-6 col-lg-6'),
        ].safe_join
        content_tag :div, content, class: 'form-group'
      else
        label_html = content_tag :label, gettext(text), class: 'control-label', for: field_id
        content    = [label_html, field_html.html_safe].safe_join
        content_tag :div, content, class: 'form-group'
      end
    end
  end

end