avatar_crop.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. {% extends "cranefly/usercp/layout.html" %}
  2. {% import "_forms.html" as form_theme with context %}
  3. {% import "cranefly/macros.html" as macros with context %}
  4. {% block title %}{{ macros.page_title(title=_('Crop Avatar')) }}{% endblock %}
  5. {% block action %}
  6. <div class="form-container usercp-avatar-crop">
  7. <form action="{% if after_upload %}{% url 'usercp_avatar_upload_crop' %}{% else %}{% url 'usercp_avatar_crop' %}{% endif %}" method="post">
  8. <div class="form-header">
  9. <div class="avatar-crop-preview">
  10. <img src="{{ MEDIA_URL }}{{ source }}" id="preview" alt="{% trans %}Avatar Preview{% endtrans %}" class="jcrop-preview" />
  11. </div>
  12. <h1>{% trans %}Crop Avatar{% endtrans %} <small>{% trans %}Change your Avatar{% endtrans %}</small></h1>
  13. <a href="{% url 'usercp_avatar' %}" class="btn btn-inverse pull-right">{% trans %}Cancel{% endtrans %}</a>
  14. <button name="save" type="submit" class="btn btn-danger pull-right">{% trans %}Crop Avatar{% endtrans %}</button>
  15. </div>
  16. {% if message %}
  17. <div class="messages-list">
  18. {{ macros.draw_message(message) }}
  19. </div>
  20. {% endif %}
  21. <input type="hidden" name="{{ csrf_id }}" value="{{ csrf_token }}">
  22. <input type="hidden" id="crop-b" name="crop_b" value="">
  23. <input type="hidden" id="crop-w" name="crop_w" value="">
  24. <input type="hidden" id="crop-x" name="crop_x" value="">
  25. <input type="hidden" id="crop-y" name="crop_y" value="">
  26. <div class="avatar-crop-target"><img src="{{ MEDIA_URL }}{{ source }}" id="target" alt="{% trans %}Uploaded Image{% endtrans %}"></div>
  27. </form>
  28. </div>
  29. {#
  30. <h2>{% trans %}Crop Avatar{% endtrans %} <small>{% trans %}Change your Avatar{% endtrans %}</small></h2>
  31. {% if message %}{{ macros.draw_message(message, 'alert-form') }}{% endif %}
  32. <form action="{% if after_upload %}{% url 'usercp_avatar_upload_crop' %}{% else %}{% url 'usercp_avatar_crop' %}{% endif %}" method="post">
  33. <input type="hidden" name="{{ csrf_id }}" value="{{ csrf_token }}">
  34. <input type="hidden" id="crop-b" name="crop_b" value="">
  35. <input type="hidden" id="crop-w" name="crop_w" value="">
  36. <input type="hidden" id="crop-x" name="crop_x" value="">
  37. <input type="hidden" id="crop-y" name="crop_y" value="">
  38. <div class="row">
  39. <div class="span6">
  40. <div class="avatar-crop-target"><img src="{{ MEDIA_URL }}{{ source }}" id="target" alt="{% trans %}Uploaded Image{% endtrans %}"></div>
  41. </div>
  42. <div class="span3">
  43. <div class="avatar-crop-preview">
  44. <img src="{{ MEDIA_URL }}{{ source }}" id="preview" alt="{% trans %}Avatar Preview{% endtrans %}" class="jcrop-preview" />
  45. </div>
  46. <div class="form-actions">
  47. <button name="save" type="submit" class="btn btn-primary">{% trans %}Crop Avatar{% endtrans %}</button>
  48. <a href="{% url 'usercp_avatar' %}" class="btn">{% trans %}Cancel{% endtrans %}</a>
  49. </div>
  50. </div>
  51. </div>
  52. </form>#}
  53. {% endblock %}
  54. {% block javascripts %}
  55. {{ super() }}
  56. <script src="{{ STATIC_URL }}cranefly/js/jquery.Jcrop.min.js"></script>
  57. <script type="text/javascript">
  58. $(function($){
  59. // Create variables (in this scope) to hold the API and image size
  60. var jcrop_api, boundx, boundy;
  61. var crop_b = $('#crop-b');
  62. var crop_w = $('#crop-w');
  63. var crop_x = $('#crop-x');
  64. var crop_y = $('#crop-y');
  65. var afx = $('.avatar-crop-preview').width() / {{ avatar_size }};
  66. $('#target').Jcrop({
  67. onChange: updatePreview,
  68. onSelect: updatePreview,
  69. aspectRatio: 1,
  70. minSize: [50, 50],
  71. setSelect: [ 0, 0, {{ avatar_size }}, {{ avatar_size }} ],
  72. },function(){
  73. // Use the API to get the real image size
  74. var bounds = this.getBounds();
  75. boundx = bounds[0];
  76. boundy = bounds[1];
  77. $('#crop-b').val(boundx);
  78. // Store the API in the jcrop_api variable
  79. jcrop_api = this;
  80. });
  81. function updatePreview(c)
  82. {
  83. if (parseInt(c.w) > 0)
  84. {
  85. var rx = {{ avatar_size }} / c.w;
  86. var ry = {{ avatar_size }} / c.h;
  87. $(crop_w).val(c.w);
  88. $(crop_x).val(c.x);
  89. $(crop_y).val(c.y);
  90. $('#preview').css({
  91. width: Math.round(rx * boundx * afx) + 'px',
  92. height: Math.round(ry * boundy * afx) + 'px',
  93. marginLeft: '-' + Math.round(rx * c.x * afx) + 'px',
  94. marginTop: '-' + Math.round(ry * c.y * afx) + 'px'
  95. });
  96. }
  97. };
  98. });
  99. </script>
  100. {% endblock %}