読者です 読者をやめる 読者になる 読者になる

woshidan's loose leaf

ぼんやり勉強しています。

coffee scriptっぽい気分に浸っていた

coffee scriptの練習のために書き直してたけど、 もう少し小さい単位でやればよかった...

ってか、DOMはオブジェクトの要素として渡すようにしないと 関数としてうまく切り分けにくいから、 何で書こうといろいろと苦しいのでは感あった...

coffee scriptっぽく見えるだろ、これ。動かないんだぜ......orz

do ->
  default_settings =
    opacity:     0.5
    iconSrc:     null
    caption:     "bottom"
    iconGravity: "left"
    tipID: -> Math.round Math.random() * 10000
    html: (tipID) ->
              """
                <div class="icon-tip tooltip data-tip-id="#{tipID}">
                  <span class="icon-top tooltip container">
                    <img class="icon-tip image left" />
                    <span class="icon-tip caption"></span>
                    <img class="icon-tip image right" />
                  </span>
                </div>
               """

  tip_settings = {}

  total = (numbers) ->
    sum = 0.0
    sum += num for num in numbers
    sum

  $("div.icon-tip.area").on "click", "a.icon-tip", (e) ->
    position = $(this).position()

    this.setAttribute "data-tip-id", default_settings.tipId() if $(this).data("tip-id")?

    { opacity, caption, iconSrc, tipID } = default_settings

    settings =
      opacity: if $target.data("opacity")?  then $target.data("opacity")  else opacity
      caption: if $target.data("caption")?  then $target.data("caption")  else caption
      iconSrc: if $target.data("icon-src")? then $target.data("icon-src") else iconSrc
      tipID:   if $target.data("tip-id")?   then $target.data("tip-id")   else tipID

    tipID = settings.tipID
    $tooltip = $("div[data-tip-id~=#{tipID}].icon-tip.tooltip")
    tip_settings[tipID] = tip_settings[tipID] or {}

    if $tooltip.length > 0 and $tooltip.css("opacity") > 0
      remove_tip()
      return false

    if $tooltip.length is 0
      # initializeIconTipElem = ($tooltip, tipID, settings) ->
      $("body").append default_settings.html(tipID)
      $tooltip = $("div[data-tip-id~=#{tipID}].icon-tip.tooltip")

      if settings["iconSrc"]?
        $tooltip.find("img.icon-tip.image").remove()
      else
        $tooltip.find("img.icon-tip.image.right").remove()
        $tooltip.find("img.icon-tip.image").attr "src", settings["iconSrc"]

      $tooltip.find("span.icon-tip.caption").html settings["caption"]


    current_tip_settings = tip_settings[tipID]
    # adjustTipSize(current_tip_settings, $tooltip)
    if current_tip_settings["width"] is null
      $tooltip.css "width", $tooltip.find("span.icon-tip.tooltip.container").outerWidth()
      current_tip_settings["width"] = $tooltip.css "width"
    else
      $tooltip.css "width", current_tip_settings["width"]

    if current_tip_settings["height"] is null
      $tooltip.css "height", $tooltip.find("span.icon-tip.tooltip.container").outerHeight() - 6
      current_tip_settings["height"] = $tooltip.css("height")
    else
      $tooltip.css "height", current_tip_settings["height"]

    # adjustTipPosition(current_tip_settings, $tooltip)
    if current_tip_settings["left"] is null
      $tooltip.css("left",
                   total([
                          position.left
                          $tooltip.find("span.icon-tip.tooltip.container").outerWidth() / 2
                          - $tooltip.find("img.icon-tip.image").outerWidth()
                        ])
                  )
      current_tip_settings["left"] = $tooltip.css("left")
    else
      $tooltip.css "width", current_tip_settings["width"]

    if current_tip_settings['top'] is null
      $icon_tip_area = $($('div.icon-tip.area')[0])
      $tooltip.css('top',
                   total([
                           - $icon_tip_area.position().top
                           - $icon_tip_area.innerHeight()
                           - parseInt($icon_tip_area.css('margin-top'))
                           + position.top - (14 * 2.5) - 6
                        ])
                  )
      current_tip_settings['top'] = $tooltip.css('top')
    else
      $tooltip.css 'top', current_tip_settings['top']

    # adjustTipAfterTriangle = (tipID, $tooltip) ->
    $tooltip_after = $("span[data-tip-id~=#{tipID}].icon-tip.tooltip.after")
    if $tooltip_after.length is 0
      $tooltip.append("<span class='icon-tip tooltip after' data-tip-id='#{tipID}'></span>")
      $tooltip_after = $("span[data-tip-id~=#{tipID}].icon-tip.tooltip.after")

    # adjustOpacity = ($tooltip, $tooltip_after, settings) ->
    $tooltip.css('opacity', settings["opacity"])
    $tooltip_after.css('opacity', 1)
    $tooltip.css('display', 'inline')

    current_tip_settings["click_flg"] = true
    removeTip = (event) ->
      if tip_settings[settings["tipId"]]["click_flg"]
        tip_settings[settings["tipId"]]["click_flg"] = false
        return false

      if $tooltip.css 'opacity' is 0
        $tooltip.css 'top', 0
        $tooltip.css 'left', 0
        $tooltip.css 'opacity', 0
        $tooltip_after.css 'opacity', 0
        $tooltip.css 'display', 'none'

        $(document).off 'click', remove_tip

      tip_settings[settings["tipId"]]["click_flg"] = true

    $(document).on 'click', remove_tip