gallery_field_editor.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. (function ( $ ) {
  2. var full_image_container = null;
  3. var links = null;
  4. var edit_enabled = false;
  5. var upload_button;
  6. var delete_button;
  7. var delete_images_span;
  8. var editor_main = null;
  9. var el;
  10. var deletingImagesCLick = function(){
  11. $(this).toggleClass('deleting');
  12. return false;
  13. }
  14. var sortingImagesClick = function(){
  15. return false;
  16. }
  17. var cancelDeleting = function(){
  18. editor_main.slideDown();
  19. delete_images_span.slideUp();
  20. edit_enabled = false;
  21. links.removeClass('deleting');
  22. links.unbind('click',deletingImagesCLick);
  23. el.removeClass('edit_list');
  24. }
  25. var init_gallery_editor = function(){
  26. editor_main = el.find('.editor_main');
  27. var upload_form = el.find('form');
  28. var upload_field = el.find("form input.upload_input");
  29. upload_button = el.find('.upload-btn');
  30. delete_images_span = el.find('.delete_images');
  31. delete_button = el.find(".delete-btn");
  32. var delete_cancel_button = el.find(".delete-cancel-btn");
  33. var delete_confirm_button = el.find(".delete-confirm-btn");
  34. var sort_button = el.find('.sort-btn');
  35. var sort_span = el.find('.sort_images');
  36. //var images = images
  37. upload_field.on('change', function(){
  38. upload_form.submit();
  39. editor_main.slideUp();
  40. el.find('h2.loading').slideDown();
  41. });
  42. upload_button.click(function(){
  43. upload_field.trigger('click');
  44. });
  45. delete_button.click(function(){
  46. editor_main.slideUp();
  47. delete_images_span.slideDown();
  48. full_image_container.empty();
  49. links.blur();
  50. edit_enabled = true;
  51. links.on('click',deletingImagesCLick);
  52. el.addClass('edit_list');
  53. });
  54. delete_cancel_button.click(function(){
  55. cancelDeleting();
  56. });
  57. delete_confirm_button.click(function(){
  58. var delete_ids = [];
  59. links.filter('.deleting').each(function(){
  60. delete_ids.push($(this).data('image-id'));
  61. });
  62. if(delete_ids.length === 0){
  63. alert($(this).data('empty-text'));
  64. } else {
  65. if(confirm("" + delete_ids.length + $(this).data('confirm-text')))
  66. {
  67. links.filter('.deleting').each(function(){
  68. $(this).parent().remove();
  69. });
  70. $.post("/gallery_field_image/delete",{
  71. entity_id: el.data('entity-id'),
  72. field: el.data('entity-field'),
  73. images: delete_ids.join(","),
  74. success: function(){
  75. cancelDeleting();
  76. el.find('.editor').slideUp();
  77. el.find('.editor_toggler').slideDown();
  78. }
  79. }
  80. );
  81. }
  82. }
  83. });
  84. sort_button.click(function(){
  85. editor_main.slideUp();
  86. sort_span.slideDown();
  87. full_image_container.empty();
  88. links.blur();
  89. edit_enabled = true;
  90. links.on('click',sortingImagesClick);
  91. el.addClass('edit_list');
  92. el.find('.images .dragger').sortable().on('sortupdate', function(){
  93. var image_ids = [];
  94. el.find('.images .dragger').children().each(function(index){
  95. image_ids.push($(this).find('a').data('image-id'));
  96. });
  97. $.post("/gallery_field_image/save_sort",{
  98. entity_id: el.data('entity-id'),
  99. field: el.data('entity-field'),
  100. images: image_ids.join(",")
  101. }
  102. );
  103. });
  104. });
  105. el.find('.sort-success-btn').click(function(){
  106. el.find('.images .dragger').sortable('destroy');
  107. editor_main.slideDown();
  108. sort_span.slideUp();
  109. edit_enabled = false;
  110. links.unbind('click',sortingImagesClick);
  111. el.removeClass('edit_list');
  112. el.find('.editor').slideUp();
  113. el.find('.editor_toggler').slideDown();
  114. });
  115. };
  116. $.fn.gallery_field = function() {
  117. if(this.length === 0) return;
  118. links = this.find(".images a");
  119. full_image_container = this.find('.image_full');
  120. el = this;
  121. links.focus(function(){
  122. if(edit_enabled){
  123. this.blur();
  124. return;
  125. }
  126. var image = $('<img/>');
  127. var thumb_href = $(this).find('img').attr('src');
  128. var full_href = this.href;
  129. image.attr('src', thumb_href);
  130. image.css({'opacity':0.5});
  131. full_image_container.html(image);
  132. image.attr('src', full_href);
  133. image.load(function(){
  134. image.css({'opacity':1});
  135. });
  136. });
  137. links.click(function(){
  138. if(edit_enabled) return true;
  139. if(false === $(this).is(":focus")){
  140. this.focus();
  141. }
  142. return false;
  143. });
  144. links.keydown(function(e) {
  145. if(edit_enabled) return;
  146. var link = $(this);
  147. if(e.keyCode == 37) { // left
  148. var prev = link.parent().prev().find('a');
  149. if(prev.length){
  150. prev.focus();
  151. }
  152. }
  153. else if(e.keyCode == 39) { // right
  154. var next = link.parent().next().find('a');
  155. if(next){
  156. next.focus();
  157. }
  158. }
  159. });
  160. if(this.hasClass('gallery-field-editor'))
  161. {
  162. init_gallery_editor(this);
  163. }
  164. this.find('.editor_toggler').click(function(){
  165. el.find('.editor').slideDown();
  166. $(this).slideUp();
  167. return false;
  168. });
  169. this.find('.edit-cancel-btn').click(function(){
  170. if(edit_enabled){
  171. cancelDeleting();
  172. }
  173. el.find('.editor').slideUp();
  174. el.find('.editor_toggler').slideDown();
  175. });
  176. };
  177. }( jQuery ));
  178. jQuery(function(){
  179. $('.gallery-field-images-list').each(function(){
  180. $(this).gallery_field();
  181. });
  182. });