Advertisement
Guest User

Untitled

a guest
May 25th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. (function() {
  2.  
  3. var ELS = {
  4. DesignImg: '#js-design-img',
  5. Spot: '.js-design-spot',
  6. SpotForm: '.js-spot-form',
  7. ExistingSpotForm: '.js-existing-spot-form',
  8. NewSpot: '.js-new-design-spot',
  9. NewSpotForm: '.js-add-new-spot',
  10. NewSpotXPos: '#design-x-pos',
  11. NewSpotYPos: '#design-y-pos',
  12. NewSpotCommentContainer: '.js-new-spot-comment-container',
  13. DesignContainer: '.js-design-container',
  14. // SpotFormContainer: '.spot-form-container',
  15. MaintainFocus: '.js-maintain-focus',
  16. CommentTextarea: '.js-design-comment-text',
  17. MainApp: '.js-main-app',
  18.  
  19. UpdateTitle: '#new_design_title',
  20. UpdateSubtitle: '#new_design_subtitle'
  21. };
  22.  
  23. var VAR = {
  24. Around: 10
  25. };
  26.  
  27.  
  28. /*
  29. * function name says it all
  30. * @dependencies: jQuery, Velocity and Waypoints
  31. */
  32. function showIcon(iconClass) {
  33.  
  34. if(!iconClass) {
  35. console.log('you must define an icon class');
  36. return;
  37. }
  38.  
  39. var waypoint = new Waypoint({
  40. element: document.getElementsByClassName(iconClass),
  41. handler: function(direction) {
  42. $(this.element).velocity({
  43. opacity: 1,
  44. scaleX: "1.1",
  45. scaleY: "1.1"
  46. },{
  47. duration: 200
  48. }).velocity({
  49. scaleX: "1",
  50. scaleY: "1"
  51. }, {
  52. duration: 300
  53. });
  54. this.destroy();
  55. },
  56. offset: '60%'
  57. });
  58. }
  59.  
  60. /*
  61. * Some animation on homepage
  62. */
  63. function homeStuff() {
  64. var $homepage = $('#js-home-page');
  65.  
  66. if( !$homepage.length) {
  67. return;
  68. }
  69.  
  70. showIcon('js-upload-home');
  71. showIcon('js-share-home');
  72. showIcon('js-feedback-home');
  73. }
  74.  
  75. /*
  76. * Show the comment box, may be the new comment box or a existing one
  77. * @e: click event
  78. * @offset: the current element (design img)
  79. * @_this: the context element
  80. */
  81. function showNewSpotBox(e, offset) {
  82. var xPos = e.pageX;
  83. var yPos = e.pageY;
  84. var relativeX = Math.round(e.pageX - offset.left);
  85. var relativeY = Math.round(e.pageY - offset.top);
  86.  
  87. $(ELS.NewSpotForm).closest(ELS.NewSpotCommentContainer).find(ELS.NewSpot).css({
  88. 'top': yPos - 10, // this 3 is a :hover hackish
  89. 'left': xPos - 10 // this 2 is a :hover hackish
  90. });
  91.  
  92. $(ELS.NewSpotForm).css({
  93. 'top': yPos - 3, // this 3 is a :hover hackish
  94. 'left': xPos - 2 // this 2 is a :hover hackish
  95. }).css('display', 'block');
  96.  
  97. $(ELS.NewSpotForm).find(ELS.MaintainFocus).trigger('focus').css('display','');
  98.  
  99. $(ELS.NewSpotForm).find(ELS.NewSpotXPos).val(relativeX);
  100. $(ELS.NewSpotForm).find(ELS.NewSpotYPos).val(relativeY);
  101. }
  102.  
  103. // Animate update title and subtitle with ajax
  104. // author: rffaguiar
  105. // since: 1.0
  106. // date: 25/03/2015
  107. // @dependencies: jquery, velocity, sweetalert
  108. function animateServerResponse(formClass) {
  109.  
  110. $(formClass).on('ajax:beforeSend', function() {
  111. var $updateIcon = $(this).find('.js-form-update-icon');
  112. $updateIcon.addClass('fa-refresh fa-spin');
  113. $updateIcon.velocity({
  114. opacity: 1
  115. });
  116. });
  117.  
  118. $(formClass).on('ajax:success', function() {
  119.  
  120. var $updateIcon = $(this).find('.js-form-update-icon');
  121. $updateIcon.velocity({
  122. opacity: 0
  123. },{
  124. complete: function(){
  125. $updateIcon.removeClass('fa-refresh fa-spin');
  126. $updateIcon.addClass('fa-check');
  127. $updateIcon.velocity({ opacity: 1 }, {
  128. complete: setTimeout(function(){
  129. $updateIcon.velocity({ opacity: 0 }, {
  130. complete: function() {
  131. $updateIcon.removeClass('fa-check');
  132. }
  133. });
  134. }, 2500)
  135. });
  136. }
  137. });
  138.  
  139. });
  140.  
  141. $(formClass).on('ajax:error', function() {
  142.  
  143. var $updateIcon = $(this).find('.js-form-update-icon');
  144. $updateIcon.velocity({
  145. opacity: 0
  146. },{
  147. complete: function(){
  148. $updateIcon.removeClass('fa-refresh fa-spin');
  149. $updateIcon.addClass('fa-times');
  150. $updateIcon.velocity({ opacity: 1 }, {
  151. complete: setTimeout(function(){
  152. $updateIcon.velocity({ opacity: 0 }, {
  153. complete: function() {
  154. $updateIcon.removeClass('fa-times');
  155. swal({
  156. title: "Ooops..!",
  157. text: "Infelizmente não foi possível atualizar este campo. Tente novamente mais tarde ou entre em contato conosco.",
  158. type: "error",
  159. confirmButtonText: "Ok!"
  160. });
  161.  
  162. }
  163. });
  164. }, 2500)
  165. });
  166. }
  167. });
  168. });
  169. }
  170.  
  171. // Allow update title and subtitle with ajax
  172. // author: rffaguiar
  173. // since: 1.0
  174. // date: 20/03/2015
  175. function updateTitleSubtitle() {
  176. $(ELS.UpdateTitle).on('blur', function() {
  177. $(this).closest('form').submit();
  178. });
  179.  
  180. $(ELS.UpdateSubtitle).on('blur', function() {
  181. $(this).closest('form').submit();
  182. });
  183.  
  184. animateServerResponse('.js-form-update-title');
  185. animateServerResponse('.js-form-update-subtitle');
  186. }
  187.  
  188. function maintainTextareaFocus() {
  189. $(ELS.MainApp).on('focusin', ELS.MaintainFocus, function() {
  190. $parent = $(this).closest(ELS.SpotForm);
  191. $parent.css('display', 'block');
  192. });
  193.  
  194. $(ELS.MainApp).on('focusout', ELS.MaintainFocus, function() {
  195. $parent = $(this).closest(ELS.SpotForm);
  196. $parent.css('display', '');
  197. });
  198. }
  199.  
  200. // Show/Hide the new Spot when img is clicked and we need to show the new spot form
  201. function littleHackish() {
  202. $(ELS.NewSpotForm).hover(function(){
  203. $(this).closest(ELS.NewSpotCommentContainer).find(ELS.NewSpot).css('display', 'block');
  204. }, function() {
  205. $(this).closest(ELS.NewSpotCommentContainer).find(ELS.NewSpot).css('display', 'none');
  206. });
  207.  
  208. }
  209.  
  210. function commentDesign() {
  211. maintainTextareaFocus();
  212. littleHackish();
  213.  
  214. $(ELS.DesignContainer).on('click', ELS.DesignImg, function(e) {
  215. console.log('imagem foi clicada');
  216. var _this = this;
  217. var offset = $(this).offset();
  218. showNewSpotBox(e, offset);
  219. });
  220.  
  221. // clear comment textarea when ajax is a success
  222. $(ELS.ExistingSpotForm).on('ajax:success', function(e, data, status, xhr) {
  223. $('textarea', ELS.ExistingSpotForm).val('');
  224. // $(ELS.FormNewDesignComment).append(xhr.responseText);
  225. }).on('ajax:error', function(e, xhr, status, error) {
  226. $(ELS.FormNewDesignComment).append('<p>Erro de papai :(</p>');
  227. console.log(error)
  228. console.log(status)
  229. console.log(xhr)
  230. });
  231.  
  232. // clear comment textarea when ajax is a success
  233. $(ELS.NewSpotForm).on('ajax:success', function(e, data, status, xhr){
  234. $(ELS.CommentTextarea, ELS.NewSpotForm).val('');
  235. // $(ELS.FormNewDesignComment).append(xhr.responseText);
  236. }).on('ajax:error', function(e, xhr, status, error) {
  237. $(ELS.FormNewDesignComment).append('<p>Erro de papai :(</p>');
  238. console.log(error)
  239. console.log(status)
  240. console.log(xhr)
  241. });
  242.  
  243. updateTitleSubtitle();
  244. }
  245.  
  246. function selectText(element) {
  247. var doc = document
  248. , text = doc.getElementById(element)
  249. , range, selection
  250. ;
  251. if (doc.body.createTextRange) {
  252. range = document.body.createTextRange();
  253. range.moveToElementText(text);
  254. range.select();
  255. } else if (window.getSelection) {
  256. selection = window.getSelection();
  257. range = document.createRange();
  258. range.selectNodeContents(text);
  259. selection.removeAllRanges();
  260. selection.addRange(range);
  261. }
  262. }
  263.  
  264. function copyToClipboard() {
  265. var $designPage = $('#df-design-page');
  266.  
  267. if( !$designPage.length) {
  268. return;
  269. }
  270.  
  271. $('#df-modal-link').on('click', function() {
  272. selectText('df-modal-link');
  273. });
  274.  
  275. var client = new ZeroClipboard( document.getElementById('df-copy-to-clipboard-link') );
  276.  
  277. client.on('ready', function(readyEvent) {
  278. // ZeroClipboard SWF is ready
  279. client.on('aftercopy', function(event) {
  280. // this == client
  281. // event.target == the element that was clicked
  282. // event.target.style.display = 'none';
  283. // alert('Copied text to clipboard: ' + event.data['text/plain']);
  284. setTimeout(function(){
  285. $('#js-df-modal-link').foundation('reveal', 'close')
  286. }, 1000);
  287.  
  288. });
  289. });
  290. }
  291.  
  292. function notifications() {
  293. var $notifications = $('#df-notifications');
  294.  
  295. if (!$notifications.length) {
  296. return false;
  297. }
  298.  
  299. var text = '';
  300. var $deviseNotice = $('#js-df-devise-notice');
  301. var $deviseAlert = $('#js-df-devise-alert');
  302.  
  303. if ($deviseNotice.length) {
  304. text = $deviseNotice.text();
  305. }
  306.  
  307. if ($deviseAlert.length) {
  308. text = $deviseAlert.text();
  309. }
  310.  
  311. toastr.options = {
  312. 'closeButton': true,
  313. 'timeout': 10000,
  314. 'extendedTimeOut': 10000,
  315. 'positionClass': 'toast-bottom-left'
  316. };
  317.  
  318. toastr.info(text);
  319.  
  320. }
  321.  
  322. function init() {
  323. $(document).ready(function(){
  324. commentDesign();
  325. homeStuff();
  326. copyToClipboard();
  327. notifications();
  328. });
  329. }
  330.  
  331. init();
  332. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement