PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/admin/scripts/post-format.js

https://gitlab.com/Magi1053/Extra
JavaScript | 250 lines | 201 code | 49 blank | 0 comment | 23 complexity | 9d9282e6495ced5d3aaf5a48fcc89c5d MD5 | raw file
  1. (function ($) {
  2. $(document).ready(function () {
  3. var $post_format_meta_boxes = $('.post-format-options').closest('.postbox'),
  4. $map = $('.et-post-format-map'),
  5. et_media_frame,
  6. $media_add_media = $('.et_media_add_media'),
  7. $video_post_format_box = $('#video-post-format'),
  8. $video_urls_container = $('.video_urls_container'),
  9. $video_url = $('.video_url'),
  10. video_url_count = $video_url.length,
  11. $add_video_url = $('.add_video_url');
  12. $media_add_media.on('click', function (e) {
  13. var $this = $(this);
  14. e.preventDefault();
  15. et_media_frame = wp.media.frames.et_media_frame = wp.media({
  16. title: $this.data('title'),
  17. button: {
  18. text: $this.data('title'),
  19. },
  20. states: [
  21. new wp.media.controller.Library({
  22. title: $this.data('title'),
  23. filterable: 'uploaded',
  24. multiple: 'add',
  25. editable: false,
  26. library: wp.media.query({
  27. type: $this.data('media-type')
  28. })
  29. })
  30. ]
  31. });
  32. et_media_frame.open();
  33. et_media_frame.on('select', function (e) {
  34. var selection = et_media_frame.state().get('selection');
  35. selection.each(function (selection_item) {
  36. if (selection_item.has('url')) {
  37. $($this.data('url-field')).val(selection_item.get('url'));
  38. }
  39. });
  40. });
  41. });
  42. $post_format_meta_boxes.each(function () {
  43. var this_id = $(this).attr('id');
  44. $('#' + this_id + '-hide').closest('label').hide();
  45. });
  46. $('input[name=et_post_format]').change(function () {
  47. var current_post_format = $(this).val(),
  48. post_format_field_init = $('#post-formats-select').data('init');
  49. $post_format_meta_boxes.each(function () {
  50. var $this = $(this),
  51. this_post_format = $this.find(".post-format-options").val();
  52. if (this_post_format === current_post_format) {
  53. if (!$this.is(':visible')) {
  54. $this.slideDown('fast');
  55. if (post_format_field_init) {
  56. $('body').animate({
  57. scrollTop: $this.offset().top - $this.find('.hndle').outerHeight() - 10
  58. },
  59. 200,
  60. 'swing',
  61. function () {
  62. $this.effect("highlight");
  63. });
  64. }
  65. $('#post-formats-select').data('init', 1);
  66. }
  67. } else {
  68. $this.hide();
  69. }
  70. });
  71. });
  72. $video_post_format_box.on('click', '.delete_video_url', function () {
  73. var $parent = $(this).parents('.video_url');
  74. $parent.slideUp('fast', function () {
  75. $parent.remove();
  76. var $video_urls = $video_post_format_box.find('.video_url');
  77. if ($video_urls.length <= 1) {
  78. $video_url.find('.delete_video_url').hide();
  79. } else {
  80. $video_url.find('.delete_video_url').show();
  81. }
  82. });
  83. });
  84. $add_video_url.on('click', function (e) {
  85. e.preventDefault();
  86. var video_url_number = video_url_count,
  87. $new_video_url = $video_url.last().clone(true, true),
  88. url_field_id = '_video_format_url_' + video_url_number;
  89. $new_video_url.find('input').attr('id', url_field_id).val('');
  90. $new_video_url.find('button').attr('data-url-field', '#' + url_field_id);
  91. $new_video_url.appendTo($video_urls_container);
  92. video_url_count = parseInt(video_url_number) + 1;
  93. $video_url.find('.delete_video_url').show();
  94. });
  95. $('input[name=et_post_format]:checked').trigger('change');
  96. function setup_post_format_map() {
  97. var map,
  98. marker,
  99. $address = $('#map_format_address'),
  100. $address_lat = $('#map_format_lat'),
  101. $address_lng = $('#map_format_lng'),
  102. $find_address = $('#map_format_find'),
  103. $zoom_level = $('#map_format_zoom'),
  104. geocoder = new google.maps.Geocoder(),
  105. default_zoom_level = 17;
  106. if ('' === $zoom_level.val()) {
  107. $zoom_level.val(default_zoom_level);
  108. }
  109. var geocode_address = function () {
  110. var address = $address.val();
  111. if (address.length <= 0) {
  112. $address_lat.val('');
  113. $address_lng.val('');
  114. return;
  115. }
  116. geocoder.geocode({
  117. 'address': address
  118. }, function (results, status) {
  119. if (status === google.maps.GeocoderStatus.OK) {
  120. var result = results[0];
  121. $address.val(result.formatted_address);
  122. $address_lat.val(result.geometry.location.lat());
  123. $address_lng.val(result.geometry.location.lng());
  124. update_map(result.geometry.location);
  125. } else {
  126. alert('Geocode was not successful for the following reason: ' + status);
  127. }
  128. });
  129. };
  130. var update_map = function (LatLng) {
  131. marker.setPosition(LatLng);
  132. map.setCenter(LatLng);
  133. };
  134. var update_zoom = function () {
  135. map.setZoom(parseInt($zoom_level.val()));
  136. };
  137. $address.on('blur', geocode_address).on('keydown', function (e) {
  138. if (13 === e.keyCode) {
  139. geocode_address();
  140. e.preventDefault();
  141. }
  142. });
  143. $find_address.on('click', function (e) {
  144. e.preventDefault();
  145. });
  146. setTimeout(function () {
  147. map = new google.maps.Map($map[0], {
  148. zoom: parseInt($zoom_level.val()),
  149. mapTypeId: google.maps.MapTypeId.ROADMAP
  150. });
  151. marker = new google.maps.Marker({
  152. map: map,
  153. draggable: true
  154. });
  155. google.maps.event.addListener(marker, 'dragend', function () {
  156. var drag_position = marker.getPosition();
  157. $address_lat.val(drag_position.lat());
  158. $address_lng.val(drag_position.lng());
  159. update_map(drag_position);
  160. latlng = new google.maps.LatLng(drag_position.lat(), drag_position.lng());
  161. geocoder.geocode({
  162. 'latLng': latlng
  163. }, function (results, status) {
  164. if (status === google.maps.GeocoderStatus.OK) {
  165. if (results[0]) {
  166. $address.val(results[0].formatted_address);
  167. } else {
  168. alert('No results found');
  169. }
  170. } else {
  171. alert('Geocoder failed due to: ' + status);
  172. }
  173. });
  174. });
  175. google.maps.event.addListener(map, 'zoom_changed', function () {
  176. var zoom_level = map.getZoom();
  177. $zoom_level.val(zoom_level);
  178. });
  179. if ('' !== $address_lat.val() && '' !== $address_lng.val()) {
  180. update_map(new google.maps.LatLng($address_lat.val(), $address_lng.val()));
  181. }
  182. if ('' !== $zoom_level.val()) {
  183. update_zoom();
  184. }
  185. }, 200);
  186. }
  187. if ($map.length) {
  188. $('input[name=et_post_format]').on('change', function () {
  189. if ('map' === $(this).val()) {
  190. setTimeout(function () {
  191. setup_post_format_map();
  192. }, 1500);
  193. }
  194. });
  195. if (!$map.parent().is(":visible")) {
  196. return;
  197. }
  198. setup_post_format_map();
  199. }
  200. }); // end $( document ).ready()
  201. })(jQuery);