auto_image_handling.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Image Node Auto-Format with Auto Image Grouping.
  2. // Original version by Steve McKenzie.
  3. // Altered by Stella Power for jQuery version.
  4. (function ($) {
  5. function parse_url(url, param) {
  6. param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  7. url = url.replace(/&/, "&");
  8. var regexS = "[\\?&]"+param+"=([^&#]*)";
  9. var regex = new RegExp(regexS);
  10. var results = regex.exec(url);
  11. if (results === null) {
  12. return "";
  13. }
  14. else {
  15. return results[1];
  16. }
  17. }
  18. function lightbox2_init_triggers(classes, rel_type, custom_class) {
  19. if (classes == '' || rel_type == 0) {
  20. return;
  21. }
  22. var settings = Drupal.settings.lightbox2;
  23. var link_target = "";
  24. if (settings.node_link_target !== 0) {
  25. link_target = 'target="'+ settings.node_link_target +'"';
  26. }
  27. $("a:has("+classes+")").each(function(i) {
  28. if ((!settings.disable_for_gallery_lists && !settings.disable_for_acidfree_gallery_lists) || (!$(this).parents("td.giAlbumCell").attr("class") && !$(this).parents(".galleries").length && !$(this).parents(".acidfree-folder").length && !$(this).parents(".acidfree-list").length) || ($(this).parents(".galleries").length && !settings.disable_for_gallery_lists) || (($(this).parents(".acidfree-folder").length || $(this).parents(".acidfree-list").length) && !settings.disable_for_acidfree_gallery_lists)) {
  29. var child = $(this).find(classes);
  30. // Ensure the child has a class attribute we can work with.
  31. if ($(child).attr("class")) {
  32. // Set the alt text.
  33. var alt = $(child).attr("alt");
  34. if (!alt) {
  35. alt = "";
  36. }
  37. // Set the image node link text.
  38. var link_text = settings.node_link_text;
  39. var download_link_text = settings.download_link_text;
  40. var rewrite = 1;
  41. // Set the rel attribute.
  42. var rel = "lightbox";
  43. var lightframe = false;
  44. if (rel_type == "lightframe_ungrouped") {
  45. rel = "lightframe[]";
  46. lightframe = true;
  47. }
  48. else if (rel_type == "lightframe") {
  49. lightframe = true;
  50. }
  51. else if (rel_type == "lightbox_ungrouped") {
  52. rel = "lightbox[]";
  53. }
  54. if (rel_type != "lightbox_ungrouped" && rel_type != "lightframe_ungrouped") {
  55. rel = rel_type + "[" + $(child).attr("class") + "]";
  56. }
  57. // Set the basic href attribute - need to ensure there's no language
  58. // string (e.g. /en) prepended to the URL.
  59. var id = null;
  60. var href = $(child).attr("src");
  61. var download = null;
  62. var orig_href = $(this).attr("href");
  63. var pattern = new RegExp(settings.file_path);
  64. if (orig_href.match(pattern)) {
  65. var lang_pattern = new RegExp(Drupal.settings.basePath + "\\w\\w\\/");
  66. orig_href = orig_href.replace(lang_pattern, Drupal.settings.basePath);
  67. }
  68. var frame_href = orig_href;
  69. // Handle flickr images.
  70. if ($(child).attr("class").match("flickr-photo-img") ||
  71. $(child).attr("class").match("flickr-photoset-img")) {
  72. href = $(child).attr("src").replace("_s.", ".").replace("_t.", ".").replace("_m.", ".").replace("_b.", ".");
  73. if (rel_type != "lightbox_ungrouped" && rel_type != "lightframe_ungrouped") {
  74. rel = rel_type + "[flickr]";
  75. if ($(child).parents("div.block-flickr").attr("class")) {
  76. id = $(child).parents("div.block-flickr").attr("id");
  77. rel = rel_type + "["+ id +"]";
  78. }
  79. }
  80. download = href;
  81. }
  82. // Handle "image-img_assist_custom" images.
  83. else if ($(child).filter("img[class*=img_assist_custom]").size()) {
  84. // Image assist uses "+" signs for spaces which doesn't work for
  85. // normal links.
  86. if (settings.display_image_size != "original") {
  87. orig_href = orig_href.replace(/\+/, " ");
  88. href = $(child).attr("src").replace(new RegExp("\\.img_assist_custom-[0-9]+x[0-9]+"), ((settings.display_image_size === "")?settings.display_image_size:"."+ settings.display_image_size));
  89. if (rel_type != "lightbox_ungrouped" && rel_type != "lightframe_ungrouped") {
  90. rel = rel_type + "[node_images]";
  91. }
  92. if (lightframe) {
  93. frame_href = orig_href + "/lightbox2";
  94. }
  95. }
  96. else {
  97. rewrite = 0;
  98. }
  99. }
  100. // Handle "inline" images.
  101. else if ($(child).attr("class").match("inline")) {
  102. href = orig_href;
  103. }
  104. // Handle gallery2 block images.
  105. else if ($(child).attr("class").match("ImageFrame_image") || $(child).attr("class").match("ImageFrame_none")) {
  106. var thumb_id = parse_url(href, "g2_itemId");
  107. var new_id = parse_url(orig_href, "g2_itemId");
  108. if (new_id && thumb_id) {
  109. var g2pattern = new RegExp("g2_itemId="+thumb_id);
  110. var replacement = "g2_itemId="+ new_id;
  111. href = href.replace(g2pattern, replacement);
  112. }
  113. rel = rel_type + "[gallery2]";
  114. if ($(child).parents("div.block-gallery").attr("class")) {
  115. id = $(child).parents("div.block-gallery").attr("id");
  116. rel = rel_type + "["+ id +"]";
  117. }
  118. download = href;
  119. }
  120. // Set the href attribute.
  121. else if (settings.image_node_sizes != '()' && !custom_class) {
  122. if (settings.display_image_size != "original") {
  123. href = $(child).attr("src").replace(new RegExp(settings.image_node_sizes), ((settings.display_image_size === "")?settings.display_image_size:"."+ settings.display_image_size)).replace(/(image\/view\/\d+)(\/[\w\-]*)/, ((settings.display_image_size === "")?"$1/_original":"$1/"+ settings.display_image_size));
  124. if (rel_type != "lightbox_ungrouped" && rel_type != "lightframe_ungrouped") {
  125. rel = rel_type + "[node_images]";
  126. if ($(child).parents("div.block-multiblock,div.block-image").attr("class")) {
  127. id = $(child).parents("div.block-multiblock,div.block-image").attr("id");
  128. rel = rel_type + "["+ id +"]";
  129. }
  130. }
  131. download = $(child).attr("src").replace(new RegExp(settings.image_node_sizes), "").replace(/(image\/view\/\d+)(\/[\w\-]*)/, "$1/_original");
  132. if (lightframe) {
  133. frame_href = orig_href + "/lightbox2";
  134. }
  135. }
  136. else {
  137. rewrite = 0;
  138. }
  139. }
  140. // Modify the image url.
  141. var img_title = $(child).attr("title");
  142. if (!img_title) {
  143. img_title = $(this).attr("title");
  144. if (!img_title) {
  145. img_title = $(child).attr("alt");
  146. }
  147. $(child).attr({title: img_title});
  148. }
  149. if (lightframe) {
  150. href = frame_href;
  151. }
  152. if (rewrite) {
  153. if (!custom_class) {
  154. var title_link = "";
  155. if (link_text.length) {
  156. title_link = "<br /><br /><a href=\"" + orig_href + "\" id=\"lightbox2-node-link-text\" "+ link_target +" >"+ link_text + "</a>";
  157. }
  158. if (download_link_text.length && download) {
  159. title_link = title_link + " - <a href=\"" + download + "\" id=\"lightbox2-download-link-text\" target=\"_blank\">" + download_link_text + "</a>";
  160. }
  161. rel = rel + "[" + img_title + title_link + "]";
  162. $(this).attr({
  163. rel: rel,
  164. href: href
  165. });
  166. }
  167. else {
  168. if (rel_type != "lightbox_ungrouped" && rel_type != "lightframe_ungrouped") {
  169. rel = rel_type + "[" + $(child).attr("class") + "]";
  170. if ($(child).parents("div.block-image").attr("class")) {
  171. id = $(child).parents("div.block-image").attr("id");
  172. rel = rel_type + "["+ id +"]";
  173. }
  174. }
  175. rel = rel + "[" + img_title + "]";
  176. $(this).attr({
  177. rel: rel,
  178. href: orig_href
  179. });
  180. }
  181. }
  182. }
  183. }
  184. });
  185. }
  186. function lightbox2_image_nodes() {
  187. var settings = Drupal.settings.lightbox2;
  188. // Don't do it on the image assist popup selection screen.
  189. var img_assist = document.getElementById("img_assist_thumbs");
  190. if (!img_assist) {
  191. // Select the enabled image types.
  192. lightbox2_init_triggers(settings.trigger_lightbox_classes, "lightbox_ungrouped");
  193. lightbox2_init_triggers(settings.custom_trigger_classes, settings.custom_class_handler, true);
  194. lightbox2_init_triggers(settings.trigger_lightbox_group_classes, "lightbox");
  195. lightbox2_init_triggers(settings.trigger_slideshow_classes, "lightshow");
  196. lightbox2_init_triggers(settings.trigger_lightframe_classes, "lightframe_ungrouped");
  197. lightbox2_init_triggers(settings.trigger_lightframe_group_classes, "lightframe");
  198. }
  199. }
  200. Drupal.behaviors.initAutoLightbox = {
  201. attach: function(context, settings) {
  202. lightbox2_image_nodes();
  203. }
  204. };
  205. //End jQuery block
  206. }(jQuery));