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