1/** 2 * jQuery Yii ListView plugin file. 3 * 4 * @author Qiang Xue <[email protected]> 5 * @link http://www.yiiframework.com/ 6 * @copyright Copyright © 2008-2010 Yii Software LLC 7 * @license http://www.yiiframework.com/license/ 8 */ 9 10;(function($) { 11 /** 12 * yiiListView set function. 13 * @param options map settings for the list view. Availablel options are as follows: 14 * - ajaxUpdate: array, IDs of the containers whose content may be updated by ajax response 15 * - ajaxVar: string, the name of the GET variable indicating the ID of the element triggering the AJAX request 16 * - pagerClass: string, the CSS class for the pager container 17 * - sorterClass: string, the CSS class for the sorter container 18 * - updateSelector: string, the selector for choosing which elements can trigger ajax requests 19 * - beforeAjaxUpdate: function, the function to be called before ajax request is sent 20 * - afterAjaxUpdate: function, the function to be called after ajax response is received 21 */ 22 $.fn.yiiListView = function(options) { 23 return this.each(function(){ 24 var settings = $.extend({}, $.fn.yiiListView.defaults, options || {}), 25 $this = $(this), 26 id = $this.attr('id'); 27 28 if(settings.updateSelector == undefined) { 29 settings.updateSelector = '#'+id+' .'+settings.pagerClass.replace(/\s+/g,'.')+' a, #'+id+' .'+settings.sorterClass.replace(/\s+/g,'.')+' a'; 30 } 31 $.fn.yiiListView.settings[id] = settings; 32 33 if(settings.ajaxUpdate.length > 0) { 34 $(document).on('click.yiiListView', settings.updateSelector,function(){ 35 if(settings.enableHistory && window.History.enabled) { 36 var url = $(this).attr('href').split('?'), 37 params = $.deparam.querystring('?'+url[1]); 38 39 delete params[settings.ajaxVar]; 40 window.History.pushState(null, document.title, decodeURIComponent($.param.querystring(url[0], params))); 41 } else { 42 $.fn.yiiListView.update(id, {url: $(this).attr('href')}); 43 } 44 return false; 45 }); 46 47 if(settings.enableHistory && window.History.enabled) { 48 $(window).bind('statechange', function() { // Note: We are using statechange instead of popstate 49 var State = window.History.getState(); // Note: We are using History.getState() instead of event.state 50 $.fn.yiiListView.update(id, {url: State.url}); 51 }); 52 } 53 } 54 }); 55 }; 56 57 $.fn.yiiListView.defaults = { 58 ajaxUpdate: [], 59 ajaxVar: 'ajax', 60 pagerClass: 'pager', 61 loadingClass: 'loading', 62 sorterClass: 'sorter' 63 // updateSelector: '#id .pager a, '#id .sort a', 64 // beforeAjaxUpdate: function(id) {}, 65 // afterAjaxUpdate: function(id, data) {}, 66 // url: 'ajax request URL' 67 }; 68 69 $.fn.yiiListView.settings = {}; 70 71 /** 72 * Returns the key value for the specified row 73 * @param id string the ID of the list view container 74 * @param index integer the zero-based index of the data item 75 * @return string the key value 76 */ 77 $.fn.yiiListView.getKey = function(id, index) { 78 return $('#'+id+' > div.keys > span:eq('+index+')').text(); 79 }; 80 81 /** 82 * Returns the URL that generates the list view content. 83 * @param id string the ID of the list view container 84 * @return string the URL that generates the list view content. 85 */ 86 $.fn.yiiListView.getUrl = function(id) { 87 var settings = $.fn.yiiListView.settings[id]; 88 return settings.url || $('#'+id+' > div.keys').attr('title'); 89 }; 90 91 /** 92 * Performs an AJAX-based update of the list view contents. 93 * @param id string the ID of the list view container 94 * @param options map the AJAX request options (see jQuery.ajax API manual). By default, 95 * the URL to be requested is the one that generates the current content of the list view. 96 */ 97 $.fn.yiiListView.update = function(id, options) { 98 var customError, 99 settings = $.fn.yiiListView.settings[id]; 100 101 if (options && options.error !== undefined) { 102 customError = options.error; 103 delete options.error; 104 } 105 106 $('#'+id).addClass(settings.loadingClass); 107 options = $.extend({ 108 type: 'GET', 109 url: $.fn.yiiListView.getUrl(id), 110 success: function(data,status) { 111 $.each(settings.ajaxUpdate, function(i,v) { 112 var id='#'+v; 113 $(id).replaceWith($(id,'<div>'+data+'</div>')); 114 }); 115 if(settings.afterAjaxUpdate != undefined) 116 settings.afterAjaxUpdate(id, data); 117 $('#'+id).removeClass(settings.loadingClass); 118 }, 119 error: function(XHR, textStatus, errorThrown) { 120 var ret, err; 121 $('#'+id).removeClass(settings.loadingClass); 122 if (XHR.readyState === 0 || XHR.status === 0) { 123 return; 124 } 125 if (customError !== undefined) { 126 ret = customError(XHR); 127 if (ret !== undefined && !ret) { 128 return; 129 } 130 } 131 switch (textStatus) { 132 case 'timeout': 133 err = 'The request timed out!'; 134 break; 135 case 'parsererror': 136 err = 'Parser error!'; 137 break; 138 case 'error': 139 if (XHR.status && !/^\s*$/.test(XHR.status)) { 140 err = 'Error ' + XHR.status; 141 } else { 142 err = 'Error'; 143 } 144 if (XHR.responseText && !/^\s*$/.test(XHR.responseText)) { 145 err = err + ': ' + XHR.responseText; 146 } 147 break; 148 } 149 150 if (settings.ajaxUpdateError !== undefined) { 151 settings.ajaxUpdateError(XHR, textStatus, errorThrown, err); 152 } else if (err) { 153 alert(err); 154 } 155 } 156 }, options || {}); 157 158 if(options.data!=undefined && options.type=='GET') { 159 options.url = $.param.querystring(options.url, options.data); 160 options.data = {}; 161 } 162 options.url = $.param.querystring(options.url, settings.ajaxVar+'='+id); 163 164 if(settings.beforeAjaxUpdate != undefined) 165 settings.beforeAjaxUpdate(id); 166 $.ajax(options); 167 }; 168 169})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.