Skip to content

Commit

Permalink
Refactor the CoffeeScript code
Browse files Browse the repository at this point in the history
  • Loading branch information
YOSHIDA Hiroki committed Jun 27, 2015
1 parent cdf963d commit 20496ce
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 70 deletions.
123 changes: 56 additions & 67 deletions backend/app/assets/javascripts/comable/admin/pages.coffee
@@ -1,80 +1,69 @@
$(document).ready( ->
# 公開/非公開の制御
disp_published_at_by_published()
class @Page
constructor: ->
$(document).ready(@ready)

# 日付を空にされたら非公開にする
swicth_unpublished_by_page_published_at()
ready: =>
@radio_published = $('#page_published_at_published')
@radio_unpublished = $('#page_published_at_unpublished')
@published_at = $('#page_published_at')

# ページタイトルの制御
set_page_title()
@initialize_visibility()
@add_event_to_set_visibility()
@add_event_to_set_page_title()
@add_event_to_set_meta_description()

# メタディスクリプションの制御
set_meta_description()
)
# 公開/非公開の制御
initialize_visibility: ->
if @radio_published.is(':checked')
@published()
if @radio_unpublished.is(':checked')
@unpublished()

# 公開/非公開の制御
disp_published_at_by_published = ->
# 初期状態
if $('#published_true').prop('checked') is true
published_action()
if $('#published_false').prop('checked') is true
unpublished_action()
# 公開の際の制御
published: =>
@published_at.show()
@published_at.val(moment().format('YYYY-MM-DD HH:mm')) unless @published_at.val()

# 公開がチェックされた時
$('#published_true').click( ->
published_action()
)
# 非公開の際の制御
unpublished: =>
@published_at.hide()
@published_at.val('')

# 非公開がチェックされた時
$('#published_false').click( ->
unpublished_action()
)
add_event_to_set_visibility: ->
@radio_published.click(@published)
@radio_unpublished.click(@unpublished)

# 公開の際の制御
published_action = ->
$('#page_published_at').show()
if $('#page_published_at').val() == ''
$('#page_published_at').val(moment().format("YYYY-MM-DD HH:mm"))
# 日付を空にされたら非公開にする
@published_at.blur( =>
@radio_unpublished.click() unless @published_at.val()
)

# 非公開の際の制御
unpublished_action = ->
$('#page_published_at').hide()
$('#page_published_at').val('')
# ページタイトルの制御
add_event_to_set_page_title: ->
$title = $('#page_title')
$page_title = $('#page_page_title')

# 日付を空にされたら非公開にする
swicth_unpublished_by_page_published_at = ->
$('#page_published_at').blur( ->
if $(this).val() == ''
$('#published_false').click()
$(this).hide()
$(this).val('')
)
# タイトルを入力した時にページタイトルが空だったらタイトルの値を入れる
$title.blur( ->
$page_title.val($(this).val()) unless $page_title.val()
)

# ページタイトルの制御
set_page_title = ->
# タイトルを入力した時にページタイトルが空だったらタイトルの値を入れる
$('#page_title').blur( ->
if $('#page_page_title').val() == ''
$('#page_page_title').val($(this).val())
)
# ページタイトルが空にされたらタイトルの値を入れる
$page_title.blur( ->
$(this).val($title.val()) unless $(this).val()
)

# ページタイトルが空にされたらタイトルの値を入れる
$('#page_page_title').blur( ->
if $(this).val() == ''
$(this).val($('#page_title').val())
)
# メタディスクリプションの制御
add_event_to_set_meta_description: ->
$content = $('#page_content')
$meta_description = $('#page_meta_description')

# メタディスクリプションの制御
set_meta_description = ->
# 内容を入力した時にページタイトルが空だったら内容の値を入れる
$('#page_content').blur( ->
if $('#page_meta_description').val() == ''
$('#page_meta_description').val($($(this).val()).text())
)
# 内容を入力した時にメタディスクリプションが空だったら内容の値を入れる
$content.blur( ->
$meta_description.val($($(this).val()).text()) unless $meta_description.val()
)

# メタディスクリプションが空にされたら内容の値を入れる
$('#page_meta_description').blur( ->
if $(this).val() == ''
$html = $($('#page_content').val())
$(this).val($html.text())
)
# メタディスクリプションが空にされたら内容の値を入れる
$meta_description.blur( ->
$(this).val($($content.val()).text()) unless $(this).val()
)
9 changes: 6 additions & 3 deletions backend/app/views/comable/admin/pages/_form.slim
@@ -1,3 +1,6 @@
javascript:
new Page
= error_messages_for @page

- url = @page.new_record? ? comable.admin_pages_path : comable.admin_page_path(@page)
Expand Down Expand Up @@ -60,10 +63,10 @@
.form-group
.radio-inline
label
= radio_button_tag :published, true, @page.published_at.present?
= f.radio_button :published_at, :published, checked: @page.published_at.present?
= Comable.t('admin.nav.pages.published')
.radio-inline
label
= radio_button_tag :published, false, @page.published_at.blank?
= f.radio_button :published_at, :unpublished, checked: @page.published_at.blank?
= Comable.t('admin.nav.pages.unpublished')
= f.text_field :published_at, value: @page.published_at.try(:strftime,'%Y-%m-%d %H:%M'), class: 'datetimepicker'
= f.text_field :published_at, value: @page.published_at.try(:strftime, '%Y-%m-%d %H:%M'), class: 'datetimepicker'

0 comments on commit 20496ce

Please sign in to comment.