Guest User

Untitled

a guest
Jan 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.91 KB | None | 0 0
  1. // PLUGIN
  2.  
  3. // Form add fields
  4.  
  5. function add_fields(link, association, content) {
  6. var new_id = new Date().getTime();
  7. var regexp = new RegExp("new_" + association, "g");
  8. $(link).parent().before(content.replace(regexp, new_id));
  9. }
  10.  
  11. // Clear default input text
  12.  
  13. $('input.default_tex_switch').each(function() {
  14. var default_value = this.value;
  15. $(this).css('color', '#666'); // this could be in the style sheet instead
  16. $(this).focus(function() {
  17. if(this.value == default_value) {
  18. this.value = '';
  19. $(this).css('color', '#333');
  20. }
  21. });
  22. $(this).blur(function() {
  23. if(this.value === '') {
  24. $(this).css('color', '#666');
  25. this.value = default_value;
  26. }
  27. });
  28. });
  29.  
  30. jQuery.fn.confirmRequired = function() {
  31. this.live('click', function (){
  32. return confirm(I18n.t('warnings.areyousure'));
  33. });
  34. };
  35.  
  36. jQuery.fn.submitOnce = function() {
  37. $(this).live('submit',function(e){
  38.  
  39. // If this form has a .valid() function attached, run it and ensure that
  40. // it returns true. (Both this and validation are bound to submit event,
  41. // so we can't be sure that validation ran first.)
  42. if ($(this).valid && typeof($(this).valid) == 'function') {
  43. // console.log('validation attached');
  44. if ($(this).valid()) {
  45. // console.log('valid!');
  46. } else {
  47. // console.log('not valid!');
  48. return false;
  49. }
  50. } else {
  51. // console.log('no validation attached');
  52. }
  53.  
  54. if ($(this).data('submitted')) {
  55. // console.log('already submitted!');
  56. // Prevent submission
  57. return false;
  58. } else {
  59. $(this).data('submitted', true);
  60. // console.log('not submitted yet!');
  61.  
  62. // NOTE: We were binding to the submit event and making it use AJAX
  63. // instead. If you don't need to do that, you can discard the if/else
  64. // below and just return true.
  65. // If you do, this will ensure that things run in the correct order -
  66. // ensure it's not a dupe submission, then submit it with ajax.
  67. // Your ajaxifying function should be something like:
  68. // $('form.ajax').bind('firstsubmit', function(){ // do stuff });
  69.  
  70. // For any forms that should be submitted by ajax
  71. if ($(this).hasClass('ajax')){
  72. // Custom event needed because prevent_double_submission is already
  73. // bound to the submit event. We need it to execute before any ajax
  74. // submission starts, but if they both bind to the submit event, we
  75. // can't guarantee which will execute first. So instead, we bind
  76. // prevent_double_submission to submit and any ajax submitter to the
  77. // firstsubmit event, which we trigger here
  78. $(this).trigger('firstsubmit');
  79. // Prevent NORMAL submit - ajax will handle this
  80. return false;
  81.  
  82. // Non-ajax forms submit as usual
  83. } else {
  84. return true;
  85. }
  86. }
  87. });
  88. // Keep chainability
  89. return $(this);
  90. };
  91.  
  92.  
  93.  
  94. // Require all fields to be filled in
  95. jQuery.fn.requireContent = function() {
  96. $(this).each(function() {
  97. var form = $(this);
  98. if (form.find('input[value=""], textarea:empty').length > 0) {
  99. $('button[type=submit], input[type=submit]', form).attr('disabled','disabled');
  100. }
  101. $("input[type=text], input[type=password], textarea").bind("change keyup blur focus", function() {
  102. // Check all fields in the form and disable the submit buttons if not all filled
  103. var emptyfields = 0;
  104. form.find('input[type=text], textarea').each(function() {
  105. if ($(this).val() === "") {
  106. $('button[type=submit], input[type=submit]', form).attr('disabled','disabled');
  107. emptyfields++;
  108. }
  109. });
  110. if (emptyfields === 0) {
  111. $('button[type=submit], input[type=submit]', form).removeAttr('disabled');
  112. }
  113. });
  114. });
  115. };
  116.  
  117. // Select All/ Select None
  118. jQuery.fn.selectAll = function() {
  119. var form = $(this).parents('form');
  120. this.live('click', function (){
  121. form.find('input[type=checkbox][disabled!=true]').attr('checked', true);
  122. });
  123. };
  124.  
  125. jQuery.fn.selectNone = function() {
  126. var form = $(this).parents('form');
  127. this.live('click', function (){
  128. form.find('input[type=checkbox][disabled!=true]').attr('checked', false);
  129. });
  130. };
  131.  
  132. jQuery.fn.hideNewAddressForm = function() {
  133. var fields = this.children("div.field");
  134. fields.hide();
  135. this.find('h2').replaceWith("<h2 id='reset'>ou <a href='#order_new_address_form'>introduisez une nouvelle adresse</a></h2>");
  136. this.find('h2').find('a').click(function() {
  137. fields.show();
  138. });
  139. };
  140. jQuery.fn.imageRollover = function() {
  141. $(this).live('mouseover', function() {
  142. var images = $(this).find("img");
  143. images.eq(0).hide();
  144. images.eq(1).show();
  145. }).live('mouseout', function () {
  146. var images = $(this).find("img");
  147. images.eq(1).hide();
  148. images.eq(0).show();
  149. });
  150. };
  151. jQuery.fn.imageRolloverWithFade = function() {
  152. $(this).hover(function(){
  153. $(this).find("img:last").fadeOut("fast");
  154. }, function(){
  155. $(this).find("img:last").show();
  156. });
  157. };
  158.  
  159.  
  160. //plugin
  161. jQuery.fn.topLink = function(settings) {
  162. settings = jQuery.extend({
  163. min: 1,
  164. fadeSpeed: 200,
  165. ieOffset: 50
  166. }, settings);
  167. return this.each(function() {
  168. //listen for scroll
  169. var el = $(this);
  170. el.css('display','none'); //in case the user forgot
  171. $(window).scroll(function() {
  172. //stupid IE hack
  173. if(!jQuery.support.hrefNormalized) {
  174. el.css({
  175. 'position': 'absolute',
  176. 'top': $(window).scrollTop() + $(window).height() - settings.ieOffset
  177. });
  178. }
  179. if($(window).scrollTop() >= settings.min)
  180. {
  181. el.fadeIn(settings.fadeSpeed);
  182. }
  183. else
  184. {
  185. el.fadeOut(settings.fadeSpeed);
  186. }
  187. });
  188. });
  189. };
  190.  
  191. // AJAX
  192.  
  193.  
  194. jQuery.ajaxSetup({
  195. 'beforeSend': function(xhr) {
  196. //xhr.setRequestHeader("Accept", "text/javascript")
  197. xhr.setRequestHeader("Accept", "text/javascript, text/html, application/xml, text/xml, */*");
  198. }
  199. });
  200.  
  201.  
  202. jQuery(document).ajaxSend(function(event, request, settings) {
  203. if (typeof(AUTH_TOKEN) == "undefined") return;
  204. if (settings.type == 'GET') return;
  205. // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  206. settings.data = settings.data || "";
  207. settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
  208. });
  209.  
  210. jQuery.fn.submitWithAjax = function() {
  211. this.live('submit', function() {
  212. $.post(this.action, $(this).serialize(), null, "script");
  213. return false;
  214. });
  215. return this;
  216. };
  217.  
  218. jQuery.fn.ajaxLink = function() {
  219. this.click(function() {
  220. $.ajax({
  221. url: this.href,
  222. dataType: "script"
  223. });
  224. return false;
  225. });
  226. };
  227.  
  228.  
  229. jQuery.fn.read_more_modal = function() {
  230. if (this.length >= 1) {
  231. var description = $(this).html();
  232. var string_lenght = 300;
  233. var description_stripped = description.replace(/(<.*?>)/ig,"").substring(0, string_lenght);
  234.  
  235. if (description.length > string_lenght) {
  236. $(this).html(description_stripped).append("... <a href='#' class='read_more'>Lire plus</a><div class='hidden_description'></div>");
  237. $('.hidden_description').html(description);
  238.  
  239. $('.read_more').click(function() {
  240. $('.hidden_description').modal({
  241. opacity:80,
  242. autoResize: true,
  243. overlayCss: { backgroundColor:"#666" },
  244. overlayClose:true
  245. });
  246. });
  247. }
  248. };
  249. };
  250.  
  251. // FILTERS
  252.  
  253. $('.product_filter').live('change', function() {
  254. window.location.hash = '';
  255. $(this).closest('form').submit();
  256. });
  257.  
  258. $('.filter_select li label').click(function() {
  259. window.location.hash = '';
  260. $(this).siblings('input').click();
  261. $(this).closest('form').submit();
  262. });
  263.  
  264. $('#product_filter form input[type="submit"]:first').hide();
  265.  
  266. $('#brand_filter_form input[type="submit"]:first').hide();
  267.  
  268.  
  269. // TOOLTIP
  270.  
  271. $('.tooltip').tipsy({gravity: $.fn.tipsy.autoNS, live: true});
  272.  
  273. $('.form_guide').tipsy({trigger: 'focus', gravity: 'w', live: true});
  274.  
  275. $('label.sizes').tipsy({delayOut: 1600,html: true, live: true});
  276.  
  277.  
  278. // PRODUCT SHOW
  279.  
  280. $('#size_select li input[type="radio"]').click(function() {
  281. $('#favorite_size_select li input[value="'+ $(this).val() +'"]').attr('checked','checked');
  282. });
  283.  
  284. $('#button_add_to_favorites').button({
  285. icons: {
  286. primary: 'ui-icon-heart'
  287. },
  288. text: true,
  289. disabled: false
  290.  
  291. });
  292.  
  293. $('button.add_to_cart').button({
  294. icons: {
  295. primary: 'ui-icon-cart'
  296. },
  297. text: true,
  298. disabled: false
  299. });
  300.  
  301. // DOM READY STUF
  302.  
  303. $(document).ready(function(){
  304.  
  305. $('#top_link').topLink({
  306. min: 400,
  307. fadeSpeed: 500
  308. });
  309. //smoothscroll
  310. $('#top_link').click(function(e) {
  311. e.preventDefault();
  312. $.scrollTo(0,300);
  313. });
  314.  
  315. // BACK BUTTONS
  316. var zoom_options = {
  317. zoomType: 'standard',
  318. lens:true,
  319. preloadImages: false,
  320. alwaysOn:false,
  321. showEffect: 'fadein',
  322. zoomWidth: 308,
  323. zoomHeight: 568,
  324. preloadText: 'Chargement',
  325. position:'right'
  326. //...MORE OPTIONS
  327. };
  328. var quick_zoom_options = {
  329. zoomType: 'standard',
  330. lens:true,
  331. preloadImages: true,
  332. alwaysOn:false,
  333. zoomWidth: 410,
  334. zoomHeight: 390,
  335. showEffect: 'fadein',
  336. fadeinSpeed: 'slow',
  337. xOffset: 15,
  338. preloadText: 'chargement du zoom',
  339. position:'right'
  340. //...MORE OPTIONS
  341. };
  342. $(".MagicZoom").jqzoom(zoom_options);
  343. $('.QuickMagicZoom').jqzoom(quick_zoom_options);
  344.  
  345. $('.product_gallery_zoom_link').live('click', function(event) {
  346. var product_id = $(this).attr("id").match(/[\d]+$/);
  347. $.ajax({
  348. url: '/products/'+product_id+'/quick_show',
  349. type: 'get',
  350. dataType: 'text',
  351. cache: true,
  352. success: function(data) {
  353. $.modal(data, {
  354. opacity:80,
  355. minHeight:421,
  356. autoResize: true,
  357. overlayCss: { backgroundColor:"#666" },
  358. overlayClose:true });
  359. $('#size_select').sizeSelect();
  360. $(".no_size_tooltip").tipsy({gravity: "w"});
  361.  
  362. $('.QuickMagicZoom').jqzoom(quick_zoom_options);
  363. $('button.add_to_cart').button({
  364. icons: {
  365. primary: 'ui-icon-cart'
  366. },
  367. text: true,
  368. disabled: false
  369. });
  370. $('#button_add_to_favorites').button({
  371. icons: {
  372. primary: 'ui-icon-heart'
  373. },
  374. text: true,
  375. disabled: false
  376. });
  377. $('#size_select').bind('click', function() {
  378. $(".no_size_tooltip").attr('original-title', '');
  379. $('#size_select li input[type="radio"]').click(function() {
  380. $('#favorite_size_select li input[value="'+ $(this).val() +'"]').attr('checked','checked');
  381. });
  382. });
  383. }
  384. });
  385. return false;
  386. });
  387.  
  388. $('.size_grid_link').live('click', function(event) {
  389. $.ajax({
  390. url: '/help/sizes',
  391. type: 'get',
  392. dataType: 'text',
  393. cache: true,
  394. success: function(data) {
  395. $.modal(data, {
  396. opacity:80,
  397. minHeight:421,
  398. autoResize: true,
  399. overlayCss: { backgroundColor:"#666" },
  400. overlayClose:true });
  401. }
  402. });
  403. return false;
  404. });
  405.  
  406.  
  407. $('.back_button.product_page a').click(function(){ _gaq.push(['_trackEvent', 'Buttons', 'Click - Back Button - Product show']); });
  408. $('.back_button.gallery_page a').click(function(){ _gaq.push(['_trackEvent', 'Buttons', 'Click - Back Button - Gallery']); });
  409.  
  410. $('form.remoteForm').submitWithAjax();
  411. $('a.remote').ajaxLink();
  412. $('.needs_confirmation').confirmRequired();
  413. $('form.submitOnce').submitOnce();
  414. $('form.requireContent').requireContent();
  415. $('a.selectAll').selectAll();
  416. $('a.selectNone').selectNone();
  417. $('#order_new_address_form').hideNewAddressForm();
  418. $('.product .image').imageRollover();
  419. $('#brands_slideshow .items .brand').imageRolloverWithFade();
  420. $('#favorite_size_select').hide();
  421. $(".short").read_more_modal();
  422.  
  423. $('.size_chart').click(function() {
  424. // Act on the event
  425. });
  426.  
  427. // UI STYLES
  428.  
  429. $(".uniform select, input:checkbox, input.radio:radio").uniform();
  430.  
  431. // Brand submenu
  432. $("#left_menu .brands").click(function(){
  433. $("#brands_submenu").slideToggle("fast");
  434. $(this).toggleClass('close');
  435. $(this).parent().toggleClass('current');
  436. return false;
  437. });
  438. $('#brands_submenu .brand').hover(function(){
  439. $(this).siblings().stop().animate({ opacity: 0.5 }, "fast");
  440. }, function(){
  441. $(this).siblings().stop().animate({ opacity: 0.8 }, "slow");
  442. });
  443.  
  444. $('.submenu').hover(function() { $(this).parent('li').toggleClass('hovered'); });
  445.  
  446. // External links
  447. $("a[rel='external']").click(function(){
  448. window.open($(this).attr("href"));
  449. return false;
  450. });
  451.  
  452. jQuery('#product_page div.pagination a').live('click', function(e) {
  453. $.bbq.pushState({'page': $.deparam.querystring(e.target.href).page });
  454. return false;
  455. });
  456.  
  457. $(window).bind("hashchange.page", function(e) {
  458. if ($.deparam.fragment().page !== undefined) {
  459. $("<div id='ajax_overlay' style='display: none;'><img src='/images/icons/ajax-loader.gif' /></div>").insertAfter('#product_filter');
  460. $('#gallery').fadeOut(1500);
  461. $.scrollTo(0, 1800);
  462. $("#ajax_overlay").fadeIn(1400, function() {
  463. $.ajax({
  464. url: $.param.querystring(document.location.href, {'page': $.bbq.getState("page")}),
  465. dataType: 'text',
  466. cache: false,
  467. success: function(data) {
  468. $("#products_page_gallery").html(data);
  469. $("#ajax_overlay").fadeOut(800);
  470. $('#gallery').fadeIn('fast');
  471. $("select#cart_item_inventory_item_id").uniform();
  472.  
  473. per_page = $.deparam.querystring(e.target.href).per_page === undefined ? 20 : $.deparam.querystring(e.target.href).per_page;
  474. from = (($.bbq.getState("page") - 1) * per_page + 1);
  475. to = ($.bbq.getState("page") * per_page);
  476. $("#total_pages").html("<b>"+from+"&nbsp;-&nbsp;"+to+"</b>");
  477.  
  478. }
  479. });
  480. });
  481. }
  482. });
  483.  
  484. if($.deparam.fragment().page) {
  485. $(window).trigger("hashchange.page");
  486. } else {
  487. $.scrollTo(0, 0);
  488. }
  489.  
  490. $("#top_menu a.login").click(function(){
  491. $('#login').modal({
  492. opacity:80,
  493. minHeight:421,
  494. autoResize: true,
  495. overlayCss: { backgroundColor:"#666" },
  496. overlayClose:true
  497. });
  498. return false;
  499. });
  500.  
  501.  
  502. });
  503.  
  504. // Hide campaign banner and set cookie
  505.  
  506. if ($('.close_campaign').length !== 0) {
  507.  
  508. var campaign_id = "campaign_" + $(".campaign").attr('id');
  509.  
  510. var campaign_closed = $.cookie(campaign_id);
  511.  
  512. $('.close_campaign').toggle(function(){
  513. $(".campaign_banner").slideUp();
  514. $(this).parent().find('h3').addClass('toggled');
  515. $('.close_campaign').text('Montrer');
  516. $.cookie(campaign_id, "closed");
  517. },function(){
  518. $(".campaign_banner").slideDown();
  519. $('.close_campaign').text('Fermer');
  520. $(this).parent().find('h3').removeClass('toggled');
  521. $.cookie(campaign_id, null);
  522. });
  523.  
  524. if (campaign_closed == 'closed') {
  525. $(".campaign_banner").hide();
  526. $('.close_campaign').text('Montrer');
  527. $('.close_campaign').parent().find('h3').addClass('toggled');
  528. }
  529.  
  530.  
  531. }
Add Comment
Please, Sign In to add comment