Advertisement
Guest User

ajax layered

a guest
Mar 26th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. /* Ajax-Layerd Nav Widgets
  2.  
  3. * Shopping Cart: Jigoshop
  4.  
  5. * File: Frontend JS
  6.  
  7. * License: GPL
  8.  
  9. * Copyright: SixtyOneDesigns
  10.  
  11. */
  12.  
  13.  
  14.  
  15. /* Globals
  16.  
  17. * Setup variables and id for areas that are
  18.  
  19. * going to be refreshed
  20.  
  21. */
  22.  
  23. var content = new Array; //Areas to be refreshed
  24.  
  25. content[0] = "products"; //Products id wrapper
  26.  
  27. content[1] = "pagination-wrapper";//Pagination id wrapper
  28.  
  29. var elements_to_remove = new Array; //Pop items in and out of this array so we know they've been refreshed
  30.  
  31. var DocReadyReload = false; //Set this to true if your getting sone javascript problems
  32.  
  33. var isWorking = false; //Flag to know if we're fetching a refresh
  34.  
  35. var http = getHTTPObject(); //Http object
  36.  
  37. var pagination
  38.  
  39.  
  40.  
  41. function checkPagination(){
  42.  
  43. if(jQuery('nav.pagination').length > 0){
  44.  
  45. pagination = ''
  46.  
  47. }
  48.  
  49. }
  50.  
  51. /* Event: document.ready
  52.  
  53. * Desc: Inititalize the page
  54.  
  55. * 1. Calls function to add Live Handlers to the widget areas, and product area fn= pageLoaderInit()
  56.  
  57. * 2. Build array of ids of widgets that are going to be refresed
  58.  
  59. */
  60.  
  61. jQuery(document).ready(function(){
  62.  
  63. pageLoaderInit();
  64.  
  65. jQuery('.widget_layered_nav').each(function(){
  66.  
  67. content.push(this.id);
  68.  
  69. });
  70.  
  71. return false;
  72.  
  73. });
  74.  
  75.  
  76.  
  77. /* Event: onpopstate
  78.  
  79. * Desc: Reload the page every time the browsers history changes
  80.  
  81. */
  82.  
  83. window.onpopstate = function(event) {
  84.  
  85. if (event.state != undefined) {
  86.  
  87. loadPage(document.location.toString(),1);
  88.  
  89. }
  90.  
  91. };
  92.  
  93. /* Function: pageLoaderInit
  94.  
  95. * Desc: Add live click handlers to anchors and checkboxes
  96.  
  97. * 1. On Click - load page, prevent broswer
  98.  
  99. * a. Calls fn = loadPage();
  100.  
  101. */
  102.  
  103. function pageLoaderInit(){
  104.  
  105. jQuery('.widget_layered_nav a, a.layerd_nav_clear, .widget_layered_nav input[type="checkbox"]').live('click', function(event){
  106.  
  107. this.blur();
  108.  
  109. var caption = this.title || this.name || "";
  110.  
  111. var group = this.rel || false;
  112.  
  113. loadPage(jQuery(this).data('link'));
  114.  
  115. event.preventDefault();
  116.  
  117. return false;
  118.  
  119. });
  120.  
  121. }
  122.  
  123. /* Function: getHTTPObject
  124.  
  125. * Returns: xmlhttprequest object
  126.  
  127. * Desc: Degrades to ActiveXObject to support older IE browsers
  128.  
  129. */
  130.  
  131. function getHTTPObject() {
  132.  
  133. var xmlhttp;
  134.  
  135. if (window.XMLHttpRequest) {
  136.  
  137. xmlhttp = new XMLHttpRequest();
  138.  
  139. }
  140.  
  141. else
  142.  
  143. {
  144.  
  145. if (window.ActiveXObject) {
  146.  
  147. xmlhttp = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  148.  
  149. }
  150.  
  151. }
  152.  
  153. return xmlhttp;
  154.  
  155. }
  156.  
  157. /* Function: loadPage
  158.  
  159. * Params:
  160.  
  161. * @url = url of target page,
  162.  
  163. * @push = whether to update browser history
  164.  
  165. * Desc: Reloads content areas
  166.  
  167. */
  168.  
  169. function loadPage(url, push){
  170.  
  171. //Make sure wer're not already doing something
  172.  
  173. if (!isWorking){
  174.  
  175. //get domain name...
  176.  
  177. nohttp = url.replace("http://","").replace("https://","");
  178.  
  179. firstsla = nohttp.indexOf("/");
  180.  
  181. pathpos = url.indexOf(nohttp);
  182.  
  183. path = url.substring(pathpos + firstsla);
  184.  
  185. //Only do a history state if clicked on the page.
  186.  
  187. if (push != 1) {
  188.  
  189. var stateObj = { foo: 1000 + Math.random()*1001 };
  190.  
  191. /*Only push history if not IE
  192.  
  193. * IE doesn't support
  194.  
  195. */
  196.  
  197. if(!jQuery.browser.msie){
  198.  
  199. history.pushState(stateObj, "ajax page loaded...", path);
  200.  
  201. }
  202.  
  203. }
  204.  
  205. /* Loop through each id in the content array()*/
  206.  
  207. jQuery.each(content, function(index, value){
  208.  
  209. /* Products container
  210.  
  211. * add an img / message to the products container to let user know it's being refreshed
  212.  
  213. */
  214.  
  215. if(value =="products"){
  216.  
  217. var max = 0;
  218.  
  219. max = jQuery('#products').outerHeight();
  220.  
  221. jQuery('#' + value + '').fadeOut("fast", function() {
  222.  
  223. jQuery('#' + value).html('<center style="min-height:'+max+'px;"><p>Loading...<br><img src="'+site.loading_img+'" alt="loading"></p></center>');
  224.  
  225. jQuery('#' + value).css({'height':max}).fadeIn("slow", function() {});
  226.  
  227. });
  228.  
  229. }
  230.  
  231. http.open('GET', url, true); //Get the new content
  232.  
  233. isWorking = true; //Set the isWorking flag to true so we don't bombard it with a bunch of requests at once
  234.  
  235. http.onreadystatechange = showPage; //Call showPage() function
  236.  
  237. http.send(null); //Don't send anything
  238.  
  239. })
  240.  
  241. }
  242.  
  243. return false;
  244.  
  245. }
  246.  
  247.  
  248.  
  249. /* Function: showPage()
  250.  
  251. * desc: replaces the contents of the target div with that of the new http request
  252.  
  253. */
  254.  
  255. function showPage(){
  256.  
  257. if (http.readyState == 4) { //Request has completed
  258.  
  259. if (http.status == 200) { //Request was good
  260.  
  261. isWorking = false; //No longer making the request
  262.  
  263. elements_to_remove=[];
  264.  
  265. elements_to_remove = content.slice();
  266.  
  267. /* Update content areas */
  268.  
  269. jQuery.each(content, function(index, value){
  270.  
  271. var details = http.responseText; //get the ajax response
  272.  
  273. //details = details.split('id="' + value + '"')[1]; //get the content for the target areas
  274.  
  275. //if (details != undefined){
  276.  
  277.  
  278.  
  279. if ( jQuery('#' + value, details).size() > 0 ) {
  280.  
  281.  
  282.  
  283. //details = details.substring(details.indexOf('>') + 1);
  284.  
  285.  
  286.  
  287. var depth = 1;
  288.  
  289. var output = '';
  290.  
  291.  
  292.  
  293. jQuery('#' + value).fadeOut("fast", function() {
  294.  
  295. jQuery('#' + value).html( jQuery('#' + value, details).html() );
  296.  
  297. jQuery('#' + value).fadeIn(1);
  298.  
  299. if (DocReadyReload == true) {
  300.  
  301. $(document).trigger("ready");
  302.  
  303. }
  304.  
  305. });
  306.  
  307.  
  308.  
  309. } else { //Empty the elements
  310.  
  311. jQuery.each(elements_to_remove, function(index,value){
  312.  
  313. jQuery('#'+value).empty();
  314.  
  315. });
  316.  
  317. }
  318.  
  319.  
  320.  
  321. });
  322.  
  323. /* Re-fire the pageLoaderInit() function. This adds the live click handlers to the newly
  324.  
  325. * readded elemets
  326.  
  327. */
  328.  
  329. pageLoaderInit();
  330.  
  331. return false
  332.  
  333. } else {
  334.  
  335. }
  336.  
  337. }
  338.  
  339. return false;
  340.  
  341. }
  342.  
  343. /* Function removeByValue
  344.  
  345. * params:
  346.  
  347. * @val = value of elment to pop out of array
  348.  
  349. * desc: Allows us to remove an element from a javascript array by value
  350.  
  351. */
  352.  
  353. Array.prototype.removeByValue = function(val) {
  354.  
  355. for(var i=0; i<this.length; i++) {
  356.  
  357. if(this[i] == val) {
  358.  
  359. this.splice(i, 1);
  360.  
  361. break;
  362.  
  363. }
  364.  
  365. }
  366.  
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement