## The contents of this file are subject to the Common Public Attribution
## License Version 1.0. (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License at
## http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
## License Version 1.1, but Sections 14 and 15 have been added to cover use of
## software over a computer network and provide for limited attribution for the
## Original Developer. In addition, Exhibit A has been modified to be
## consistent with Exhibit B.
##
## Software distributed under the License is distributed on an "AS IS" basis,
## WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
## the specific language governing rights and limitations under the License.
##
## The Original Code is reddit.
##
## The Original Developer is the Initial Developer.  The Initial Developer of
## the Original Code is reddit Inc.
##
## All portions of the code written by reddit are Copyright (c) 2006-2015
## reddit Inc. All Rights Reserved.
###############################################################################

<%!
   from pylons import g

   from r2.lib.filters import mako_websafe
   from r2.lib.template_helpers import get_domain
   from r2.lib.template_helpers import make_url_protocol_relative
   from r2.lib.pages.things import LinkButtons
   from r2.lib.pages import WrappedUser
   from r2.lib.strings import Score, strings
%>
 
<%inherit  file="printable.html"/>
<%namespace file="utils.html" import="plain_link, thing_timestamp, edited, nsfw_stamp" />
<%namespace file="printablebuttons.html" import="toggle_button" />
 
<%def name="numcol()">
  <span class="rank">
    ${thing.num_text}
  </span>
</%def>

<%def name="make_link(name, css_class, tabindex=0)">
  <a class="${css_class} may-blank ${ c.user_is_loggedin and 'loggedin' or ''}"
     href="${thing.href_url}"
     %if tabindex:
       tabindex="${tabindex}"
     %endif
     %if thing.nofollow:
       rel="nofollow"
     %endif
     %if c.cname:
       target="_top"
     %endif
     %if thing.mousedown_url:
       onmousedown="save_href($(this));this.href='${thing.mousedown_url}'"
     %endif
     %if thing.link_child and getattr(thing, "media_override", False):
       onclick="return expando_child(this)"
     %endif
     >
     ${caller.body()}
  </a>
</%def>

<%def name="bottom_buttons()">
  <ul class="flat-list buttons">
    %if thing.nsfw:
     <li class="rounded nsfw-stamp stamp">
      ${nsfw_stamp()}
     </li>
    %endif
    ${self.buttons()}
    ${self.admintagline()}
  </ul>
 </%def>

<%def name="flair()">
  %if c.user.pref_show_link_flair and thing.flair_text:
    <span class="linkflairlabel" title="${thing.flair_text}">${thing.flair_text}</span>
  %endif
</%def>

<%def name="entry()">
  <p class="title">
    %if c.site.link_flair_position == 'left':
      <%call expr="flair()" />
    %endif
    <%call expr="make_link('title', 'title', tabindex=1)">
      ${thing.title}
    </%call>
    %if c.site.link_flair_position == 'right':
      <%call expr="flair()" />
    %endif
    ${self.approval_checkmark()}
    &#32;

    ${self.domain()}

    %if c.user_is_admin:
        %for link_note in thing.link_notes:
           &#32;<span class="link-note">[${link_note}]</span>
        %endfor
    %endif
  </p>

  ##the expando button
  <% selftext_hide = thing.is_self and not thing.selftext %>
  %if thing.link_child and not thing.link_child.expand and not selftext_hide:
    <div class="expando-button collapsed
                ${thing.link_child.css_style}"
         onclick="expando_child(this)"></div>
  %endif

  <p class="tagline">
    ${self.tagline()}
  </p>

  <% 
     child_content = ""
     if thing.link_child and thing.link_child.load:
       child_content = unsafe(thing.link_child.content())
     expand = thing.link_child and thing.link_child.expand
  %>

  ## if we're not on a permalink page we'll render the buttons on top
  %if not expand:
    ${bottom_buttons()}
  %endif

%if not getattr(thing, "votable", True):
  <div class="unvotable-message">${strings.unvotable_message}</div>
%endif

  <div class="expando" ${"style='display: none'" if not expand else ""}>
    %if expand:
      ${child_content}
    %else:
      <span class="error">loading...</span>
    %endif
  </div>

  ##if we are on a permalink page, we'll render the buttons below
  %if expand:
    ${bottom_buttons()}
  %endif

 ##populate the expando cache if we have something
  %if not expand and child_content:
    <script type="text/javascript">
      var cache = expando_cache();
      cache["${thing._fullname}_cache"] = "${websafe(child_content)}";
    </script>
  %endif
</%def>

<%def name="thing_css_class(what)" buffered="True">
${parent.thing_css_class(what)} ${"over18" if thing.over_18 else ""} ${thing.visited and 'visited' or ''}
%if c.user.pref_show_link_flair and (thing.flair_text or thing.flair_css_class):
  <% css = thing.flair_css_class or '' %>
  linkflair ${' '.join('linkflair-' + c for c in css.split())}
%endif
</%def>

<%def name="subreddit()" buffered="True">
  ${plain_link('/r/' + thing.subreddit.name, thing.subreddit_path, sr_path = False,
               cname = False, _class = "subreddit hover may-blank")}
</%def>

<%def name="midcol(display=True, cls = '')">
    <div class="midcol ${cls}"
       ${not display and "style='display:none'" or ''}>
    ${self.arrow(thing, 1, thing.likes)}
    %if thing.pref_compress:
      <div class="score-placeholder"></div>
    %elif thing.hide_score:
      <div class="score likes">&bull;</div> 
      <div class="score unvoted">&bull;</div> 
      <div class="score dislikes">&bull;</div> 
    %else:
      ${self.score(thing, thing.likes, tag='div')}
    %endif
    ${self.arrow(thing, 0, thing.likes == False)}
  </div>
 ${self.thumbnail()}
</%def>


<%def name="domain()">
  <span class="domain">
    (<a href="${thing.domain_path}">${thing.domain_str}</a>)
    %if c.user_is_admin:
      &#32;
      <a class="adminbox" href="/admin/domain/${thing.domain}">d</a>
    %endif
  </span>
</%def>

<%def name="tagline()">
  <%
    taglinetext = mako_websafe(thing.taglinetext).replace(" ", "&#32;")
  %>
  ${unsafe(taglinetext % dict(reddit=self.subreddit(),
                              score=capture(self.score, thing, thing.likes, tag='span'),
                              when=capture(thing_timestamp, thing, thing.timesince, live=True, include_tense=True),
                              author=WrappedUser(thing.author, thing.attribs, thing).render(),
                              lastedited=capture(edited, thing, thing.lastedited)
                              ))}
  ${self.gildings()}
  %if thing.stickied:
    &#32;-&#32;<span class="stickied-tagline" title="selected by this subreddit's moderators">stickied post</span>
  %endif

  %if getattr(thing, 'savedcategory', None) is not None:
    ${plain_link(_('category: %s') % thing.savedcategory,
                 '/user/%s/saved/%s' % (c.user.name, thing.savedcategory),
                 _class='save-category' + ('' if thing.savedcategory else ' hidden')
                )}
  %endif
</%def>

<%def name="child()">
</%def>

<%def name="buttons(comments=True, delete=True, report=True, additional='')">
  ${LinkButtons(thing, comments = comments, delete = delete,
                report = report,
               )}
</%def>

<%def name="thumbnail()">
  %if thing.thumbnail:
  <%call expr="make_link('thumbnail', 'thumbnail ' + (thing.thumbnail if thing.thumbnail_sprited else ''))">
    % if not thing.thumbnail_sprited:
        <%
            if hasattr(thing, 'thumbnail_size'):
                scaling_factor = 1
                if thing.thumbnail_size[0] > g.thumbnail_size[0]:
                  # hidpi scaling, calculate in case hidpi changes definition in the future and
                  # we have multiple sets of image dimensions. Currently should always be 1 or 2.
                  # Width is always the maximum allowed, so we don't need to check height.
                  scaling_factor = thing.thumbnail_size[0] // g.thumbnail_size[0]

                size_str = "width='%d' height='%d'" % (thing.thumbnail_size[0] // scaling_factor, thing.thumbnail_size[1] // scaling_factor)
            else:
                size_str = ""
        %>
        <img src="${make_url_protocol_relative(thing.thumbnail)}" ${size_str} alt="">
    % endif
  </%call>
  %endif
</%def>

