/** * Реализации history ajax loader * * @author Александр Хрищанович * @version 1.0 * @require jquery, jquery.historypage.js * @example $('#mainMenu a').historyAjax({ container: '#for_ajax', callbackBefore: function () { alert('start'); }, callbackComplete: function () { alert('stop') }, }); * */ (function ($) { $.fn.historyAjax = function (settings) { //$.fn.historyAjax._nowSection = ''; var _options = $.extend({}, $.fn.historyAjax.defaults, settings || {}); var _elements = this; var _fromHistory = true; /** * Проверка на то, что ссылки и формы должны обрабатываться ajax */ var skipLink = function (object) { if ($(object).is('.no_ajax')) { return true; } else { var regExp = { doc: new RegExp('.*\.doc', 'i'), xls: new RegExp('.*\.xls', 'i'), jpg: new RegExp('.*\.jpg', 'i'), gif: new RegExp('.*\.gif', 'i'), png: new RegExp('.*\.png', 'i') }; for (var item in regExp) { if(regExp[item].test($(object).attr('href'))) { return true; } } if ($(object).attr('target') == '_blank') { return true; } } } /** * Обработчик события на элементе */ var initHandler = function(object) { var _this = this; _this._fromHistory = true; if (!skipLink(object)) { if ($(object).is('form')) { if (!$(object).attr('onsubmit')) { $(object).submit(function () { _this._fromHistory = false; postForm(this); return false; }); } } else { $(object).click(function() { _this._fromHistory = false; hightlightCurrent(object); getContent(this.href); return false; }); } } } /** * Подсветка текущего пункта */ var hightlightCurrent = function (object) { $(_elements).removeClass('current'); $('.ajaxed').removeClass('ajaxed'); $(object) .addClass('current') .addClass('ajaxed'); } /** * Отправляем запрос */ var getContent = function (section, params, isPost) { if (!section) { section = _root; } _section = section.split(_root); section = _section.length > 1 ? _section[1] : section; if (params || isPost) { _loaderData(section, params, isPost); } else { if(!section) { section = '#'; } historyPage.GoTo(section, _options.historyPageKey); return false; } } var _loaderData = function(section, params, isPost) { var _this = this; var callback = function(data, status){ if (data == null) { $(_options.container).hide(); return; } else { pageTrack(_root + section); $(_options.container) .html(data); preloader.hide(); _options.callbackComplete(_this._fromHistory); /** * Назначаем обработчики на свеже-пришедшее */ _setEventsFormElement($(_options.container)); $('a, form', $(_options.container)).each(function () { initHandler(this); }); } $.fn.historyAjax._nowSection = section; _this._fromHistory = true; return; } /* if (!section) { callback(null); return; } */ _options.callbackBefore(_this._fromHistory); preloader.show(); if (!params) { params = []; } params.push({name: 'ajax', value: 1}); if(isPost) { $.post(_root + section, params, callback); } else { $.get(_root + section, params, callback); } $('.log').ajaxError(function() { preloader.hide(); document.location = _root + '404'; }); } /** * Функция для фикса бага с формами в IE6 * при перезагрузки флэшки-пустышки пропадает фокус из поля */ var _setEventsFormElement = function ( parent ) { if ( $.browser.msie && parseInt($.browser.version) == 6 ) { // назначаем обработчик для input type="text" $('input, textarea, select', parent) .focus(function ( ) { historyPage.StopDisplatch(); }) .blur(function ( ) { historyPage.StartDispatch(); }); } } $.fn.historyAjax._postForm = function (form) { var json = $(form).serializeArray(); var onlyGet = $(form).attr('method') == 'get' ? true : false; if ($(form).attr('action')) { var action = $(form).attr('action') ; } else { var action = $.fn.historyAjax._nowSection; } json.push({name: 'form_submit', 'value': $('input[name=form_submit]').val()}); getContent(action, json, onlyGet ? false : true); return false; } return this.each(function () { initHandler(this); historyPage.addCallBack(function (url) { _loaderData(url); }, _options.historyPageKey) }); }; $.fn.historyAjax.defaults = { container: '#for_ajax', historyPageKey: 'default', callbackBefore: function () {}, callbackComplete: function () {} }; })(jQuery); var postForm = function (_this) { $.fn.historyAjax._postForm(_this); return false; } var preloader = { show : function () { $('#overlay') .height($('body').height()) .show(); }, hide : function () { $('#overlay').hide(); } } var pageTrack = function (url) { if (typeof(_gaq) != 'undefined') { var pageTracker = _gat._getTracker(); pageTracker._trackPageview(url); } if (typeof(yaCounter3080809) != 'undefined') { yaCounter3080809.hit(url, document.title, _referer); } } $(function () { if (!_isDisableAjax()) { var split = document.location.toString().split('#') if (1 == split.length && document.location.toString() != _root) { var nav = document.location.toString().split(_root); document.location = _root + '#' + nav[1]; } } })