admin.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //= require Chart.bundle
  2. //= require chartkick
  3. //= require moment
  4. //= require EpicEditor
  5. //= require jquery-sortable/jquery-sortable
  6. //= require bootstrap-daterangepicker
  7. //= require jquery.fix.clone
  8. //= require admin/file_uploads
  9. //= require admin/s3_uploads.js
  10. //= require admin/congress_message_tabulation
  11. //= require admin/petitions
  12. //= require admin/epic_editor_helper
  13. //= require_tree ./admin/action_pages
  14. //= require s3_cors_fileupload
  15. //= require bootstrap-sass/bootstrap/tab
  16. //= require bootstrap-responsive-tabs
  17. //= require react
  18. //= require react_ujs
  19. //= require_tree ./admin/components
  20. $(document).on('ready', function() {
  21. var editor = initEpicEditor('action-page-description', 'description');
  22. var editor2 = $("#action-page-what-to-say").parents(".panel").is(".disabled") ?
  23. null : initEpicEditor('action-page-what-to-say', 'what-to-say');
  24. var editor3 = initEpicEditor('epic-petition-description', 'petition-description');
  25. var editor4 = initEpicEditor('epic-action-summary', 'action-summary');
  26. var editor5 = initEpicEditor('epic-email-text', 'email-text');
  27. var editor6 = initEpicEditor('epic-victory-message', 'victory-message');
  28. $("#action-page-description").data("editor", editor);
  29. $("#action-page-what-to-say").data("editor", editor2);
  30. $("#epic-petition-description-").data("editor", editor3);
  31. $("#epic-action-summary").data("editor", editor4);
  32. $("#epic-email-text").data("editor", editor5);
  33. $("#epic-victory-message").data("editor", editor);
  34. $("#date_control_container").each(function() {
  35. var date_start = $(this).data("date-start");
  36. var date_end = $(this).data("date-end");
  37. var action = $(".date_submit", this)[0];
  38. var base_url = window.location.pathname;
  39. action.href = base_url + "?date_start=" + date_start + "&date_end=" + date_end + window.location.hash;
  40. $("input#date_range", this).daterangepicker({
  41. format: "YYYY-MM-DD",
  42. startDate: date_start,
  43. endDate: date_end
  44. });
  45. $("input#date_range", this).on('apply.daterangepicker', function(ev, picker){
  46. date_start = picker.startDate.format("YYYY-MM-DD");
  47. date_end = picker.endDate.format("YYYY-MM-DD");
  48. action.href = base_url + "?date_start=" + date_start + "&date_end=" + date_end + window.location.hash;
  49. $('.daterangepicker').hide();
  50. });
  51. $("input#date_range", this).on('cancel.daterangepicker', function(ev, picker){
  52. $('.daterangepicker').hide();
  53. });
  54. });
  55. $(".edit-action .panel-heading").click(function(){
  56. setTimeout(function() { editor2 && editor2.reflow() }, 100);
  57. });
  58. // This is a hack to make the "edit petition description" Epiceditor display at the right height when the panel is openened.
  59. $("#action_page_enable_petition").change(function(){
  60. setTimeout(function(){editor3.reflow()}, 100);
  61. });
  62. $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  63. if(typeof editor5 != 'undefined'){
  64. editor5.reflow();
  65. editor6.reflow();
  66. }
  67. });
  68. $('.thumbnail').on('click', function (ev) {
  69. var image = $(ev.currentTarget);
  70. var md = image.attr('src');
  71. $('.markdown-image-code').text(md);
  72. return false;
  73. });
  74. if (window.location.hash.length) {
  75. $('a[data-tab="'+window.location.hash.slice(1)+'"]').tab('show');
  76. window.scrollTo(0, 0); // Otherwise page starts scrolled to initial tab location
  77. }
  78. $('.edit-action .nav-tabs a').on('show.bs.tab', function(e) {
  79. var anchor = $(e.target).data('tab');
  80. $('#anchor').val(anchor);
  81. history.pushState(null,null,'#' + anchor);
  82. });
  83. $('.action_pages-index .nav-tabs a').on('show.bs.tab', function(e) {
  84. var anchor = $(e.target).data('tab');
  85. $('#anchor').val(anchor);
  86. history.pushState(null,null,'#' + anchor);
  87. });
  88. $("#action-page-form.edit_action_page").on("submit.check_timestamp", function(e) {
  89. e.preventDefault();
  90. e.stopPropagation();
  91. var form = this;
  92. $.get(form.action + ".json", function(record) {
  93. if (new Date(record.updated_at) <= new Date(form.dataset.timestamp)
  94. || confirm("Are you sure? This page has been updated since you started editing.")) {
  95. $(form).off("submit.check_timestamp").submit();
  96. }
  97. });
  98. });
  99. var preview_button = $('#action-page-preview').click(function() {
  100. var form = $('#action-page-form').clone()
  101. .attr("id", null).css("display", "none")
  102. .attr('action', preview_button.attr('href'))
  103. .attr('target', '_blank');
  104. $("body").append(form);
  105. form.submit();
  106. return false;
  107. });
  108. // Bootstrap popovers and tooltips
  109. $('.action_pages-index i.has-tooltip').tooltip();
  110. var popovers = "#protip2, #protip3, .photo-specs-popover, .photo-popover";
  111. $(popovers).popover();
  112. $(popovers).on('shown.bs.popover', function(){
  113. $(".popover").click(function(e){
  114. e.stopPropagation();
  115. });
  116. $("body").click(function(){
  117. $(popovers).popover('hide');
  118. $("body").off('click');
  119. });
  120. });
  121. // Prevent scrolling to the top of the page when clicking on protip popovers.
  122. $( 'a[href="#"]' ).click( function(e) {
  123. e.preventDefault();
  124. });
  125. // Bootstrap responsive tabs: https://github.com/openam/bootstrap-responsive-tabs
  126. fakewaffle.responsiveTabs(['xs']);
  127. $("#action .panel").on("show.bs.collapse", function(e) {
  128. $(".caret .icon", this).addClass("ion-arrow-up-b").removeClass("ion-arrow-down-b");
  129. });
  130. $("#action .panel").on("hide.bs.collapse", function(e) {
  131. $(".caret .icon", this).addClass("ion-arrow-down-b").removeClass("ion-arrow-up-b");
  132. });
  133. $("#action_page_partner_ids").select2({
  134. placeholder: "Start typing to search..."
  135. });
  136. });
  137. // Make chart.js redraw charts when we resize the browser
  138. Chart.defaults.global.responsive = true;