我想重构这个插件但是写法非常奇怪,根本就无法理解它的代码逻辑,无从下手
不知道怎么看这个代码。
// ----------------------------------------------------------------------------
// Pagination Plugin - A jQuery Plugin to paginate content
// v 1.0 Beta
// Dual licensed under the MIT and GPL licenses.
// ----------------------------------------------------------------------------
// Copyright (C) 2010 Rohit Singh Sengar
// http://rohitsengar.cueblocks.net/
// ----------------------------------------------------------------------------
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ----------------------------------------------------------------------------//------------ initializing all the values needed in paginator. -----------------//--- Variables for internal use ----var pageElement = Array();var paginatorId = '';var currentPage = 1; // current page, default 1var allItems = 0; // no. of repeating items in the container where paginator is appliedvar lastPage = 1; // last page, default 1//--- Attributes that can be changed according to use ---var startPage = 1; // start pagevar itemsPerPage = 6; // no. of items you want to show on one pagevar firstPageSymbol = '<<'; // to indicate First Pagevar previousPageSymbol = '<'; // to indicate Previous Pagevar nextPageSymbol = '>'; // to indicate Next Pagevar lastPageSymbol = '>>'; // to indicate Last Pagevar separator = ' | '; // To separate paginator's itemsvar paginatorPosition = 'bottom'; // where you want the paginator to be. Accepted values are 'top','bottom','both'var paginatorStyle = 1; // To define which style of paginator you need.
// 1 - for << | < | 1 | 2 | 3 | > | >>
// 2 - for << | < | 1/8 | > | >>
// 3 - for < | 1 | 2 | 3 | >
// 4 - for < | >var enablePageOfOption = false; // it shows on which are you currently, i.e. Page 3 of 6 Page(s), if turned truevar enableGoToPage = false; // shows a drop down of all pages for go/jump to any page user want to go, if turned true. Useful incase there are large no. of pagesvar textGoToPage = 'Go to'; // text for above option. You can change it to 'Jump to Page' or anything you like. The above option needs to turned on for this.var enableSelectNoItems = false; // if you want to change items per page on the fly.var textSelectNoItems = 'Items Per Page'; // text for above option. You can change it to 'Change No. of tag/page' or anything you like. The above option needs to turned on for this.var paginatorValues = Array(5, 10, 15, 20, 25, 30); // list of values for above option (enableSelectNoItems).var anchorLink = 'javascript:void(0);'; // if you want to change href of the paginator anchor text (links for page) to '#' or to something else. As # is append on the address bar upon clicking I used javascript:void(); which is clean.
//-----------functions starts----------------------------------------------------
eval(function(p, a, c, k, e, r) { e = function(c) { return (c < a ? '' : e(parseInt(c / a))) + ((c = c % a) > 35 ? String.fromCharCode(c + 29) : c.toString(36)) }; if (!''.replace(/^/, String)) { while (c--) r[e(c)] = k[c] || e(c); k = [function(e) { return r[e] } ]; e = function() { return '\\w+' }; c = 1 }; while (c--) if (k[c]) p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]); return p } ('1c.1d.1e({1f:y(){h=N;S(1g){z\'1h\':{h.T(\'<n 7="u"></n>\');A}z\'1i\':{h.O(\'<n 7="u"></n>\');A}z\'1j\':{h.T(\'<n 7="u"></n>\');h.O(\'<n 7="u"></n>\');A}U:{h.O(\'<n 7="u"></n>\')}}P()},1k:y(){$(\'.u\').1l();h.C().Q()}});y P(){j(k<1)k=5;B=h.C().V;j(B%k==0)o=J(B/k);v o=J(B/k)+1;j((K<1)||(K>o))K=1;r(K,1)}y r(a,b){j(a<0){j(a==-1)a=w-1;v a=w+1}w=a;G=(w-1)*k;j(!b){h.1m("W",y(){R();h.C().X();h.C().Y(G,k+G).Q();h.1n("W")})}v{R();h.C().X();h.C().Y(G,k+G).Q()}}y 1o(a){k=a;j(B%k==0)o=J(B/k);v o=J(B/k)+1;r(1)}y R(){$(".u").Z("");t a=\'\';t b=\'\';t c=\'\';t d=\'\';t e=\' 8 \'+w+\' 10 \'+o+\' 8(s) \';t f=\' \'+1p+\' <L 11="r(N.D);" >\';t g=\' \'+1q+\' <L 11="k=1r(N.D);P();" >\';12(t i=0;i<E.V;i++){j(k==E[i])g+=\'<x D="\'+E[i]+\'" M="M">\'+E[i]+\'</x>\';v g+=\'<x D="\'+E[i]+\'">\'+E[i]+\'</x>\'}g+=\'</L>\';j(w==1){6=\'<a l="\'+m+\'" 7="p" 9="13 8">\'+14+\'</a>\'+q;a=b=6;6=\'<a l="\'+m+\'" 7="p" 9="15 8">\'+16+\'</a>\'+q;a+=6;b+=6;c+=6;d+=6}v{6=\'<a l="\'+m+\'" 7="H" I="r(1);" 9="13 8">\'+14+\'</a>\'+q;a=b=6;6=\'<a l="\'+m+\'" 7="H" I="r(-1);" 9="15 8">\'+16+\'</a>\'+q;a+=6;b+=6;c+=6;d+=6}12(t i=1;i<=o;i++){j(i==w){a+=\'<a l="\'+m+\'" 7="p" 9="8 \'+i+\'">\'+i+\'</a>\'+q;b+=\'<a l="\'+m+\'" 7="p" 9="8 \'+i+\'">\'+i+\'/\'+o+\'</a>\'+q;c+=\'<a l="\'+m+\'" 7="p" 9="8 \'+i+\'">\'+i+\'</a>\'+q;f+=\'<x D="\'+i+\'" M="M">\'+i+\'</x>\'}v{6=\'<a l="\'+m+\'" 7="H" I="r(\'+i+\');" 9="8 \'+i+\'">\'+i+\'</a>\'+q;a+=6;c+=6;f+=\'<x D="\'+i+\'">\'+i+\'</x>\'}}f+=\'</L>\';j(w==o){6=\'<a l="\'+m+\'" 7="p" 9="17 8">\'+18+\'</a>\';a+=6;b+=6;c+=6;d+=6;6=q+\'<a l="\'+m+\'" 7="p" 9="19 8">\'+1a+\'</a>\';a+=6;b+=6}v{6=\'<a l="\'+m+\'" 7="H" I="r(-2);" 9="17 8">\'+18+\'</a>\';a+=6;b+=6;c+=6;d+=6;6=q+\'<a l="\'+m+\'" 7="H" I="r(\'+o+\');" 9="19 8">\'+1a+\'</a>\';a+=6;b+=6}S(1s){z 1:6=a;A;z 2:6=b;A;z 3:6=c;A;z 4:6=d;A;U:6=a}j(1t)6+=\'<F 7="p" 9="8 1u">\'+e+\'</F>\';j(1v)6+=\'<F 7="p" 9="1b 8">\'+f+\'</F>\';j(1w)6+=\'<F 7="p" 9="1b 1x. 10 1y 1z 1A">\'+g+\'</F>\';$(".u").Z(6)}', 62, 99, '||||||style|class|Page|title||||||||paginatorId||if|itemsPerPage|href|anchorLink|div|lastPage|inactive|separator|appendContent||var|paginator|else|currentPage|option|function|case|break|allItems|children|value|paginatorValues|span|till|active|onclick|parseInt|startPage|select|selected|this|after|initPaginator|show|createPaginator|switch|before|default|length|medium|hide|slice|html|of|onchange|for|First|firstPageSymbol|Previous|previousPageSymbol|Next|nextPageSymbol|Last|lastPageSymbol|Select|jQuery|fn|extend|pagination|paginatorPosition|top|bottom|both|depagination|remove|fadeOut|fadeIn|changePaginator|textGoToPage|textSelectNoItems|Number|paginatorStyle|enablePageOfOption|Information|enableGoToPage|enableSelectNoItems|no|items|per|page'.split('|'), 0, {}))

解决方案 »

  1.   

    把eval替换成document.write就可以看到它的源码jQuery.fn.extend({pagination:function(){paginatorId=this;switch(paginatorPosition){case'top':{paginatorId.before('
    ');break}case'bottom':{paginatorId.after('
    ');break}case'both':{paginatorId.before('
    ');paginatorId.after('
    ');break}default:{paginatorId.after('
    ')}}initPaginator()},depagination:function(){$('.paginator').remove();paginatorId.children().show()}});function initPaginator(){if(itemsPerPage<1)itemsPerPage=5;allItems=paginatorId.children().length;if(allItems%itemsPerPage==0)lastPage=parseInt(allItems/itemsPerPage);else lastPage=parseInt(allItems/itemsPerPage)+1;if((startPage<1)||(startPage>lastPage))startPage=1;appendContent(startPage,1)}function appendContent(a,b){if(a<0){if(a==-1)a=currentPage-1;else a=currentPage+1}currentPage=a;till=(currentPage-1)*itemsPerPage;if(!b){paginatorId.fadeOut("medium",function(){createPaginator();paginatorId.children().hide();paginatorId.children().slice(till,itemsPerPage+till).show();paginatorId.fadeIn("medium")})}else{createPaginator();paginatorId.children().hide();paginatorId.children().slice(till,itemsPerPage+till).show()}}function changePaginator(a){itemsPerPage=a;if(allItems%itemsPerPage==0)lastPage=parseInt(allItems/itemsPerPage);else lastPage=parseInt(allItems/itemsPerPage)+1;appendContent(1)}function createPaginator(){$(".paginator").html("");var a='';var b='';var c='';var d='';var e=' Page '+currentPage+' of '+lastPage+' Page(s) ';var f=' '+textGoToPage+' ';var g=' '+textSelectNoItems+'';for(var i=0;i'+paginatorValues[i]+'';else g+=''+paginatorValues[i]+''}g+='';if(currentPage==1){style=''+firstPageSymbol+''+separator;a=b=style;style=''+previousPageSymbol+''+separator;a+=style;b+=style;c+=style;d+=style}else{style=''+firstPageSymbol+''+separator;a=b=style;style=''+previousPageSymbol+''+separator;a+=style;b+=style;c+=style;d+=style}for(var i=1;i<=lastPage;i++){if(i==currentPage){a+=''+i+''+separator;b+=''+i+'/'+lastPage+''+separator;c+=''+i+''+separator;f+=''+i+''}else{style=''+i+''+separator;a+=style;c+=style;f+=''+i+''}}f+='';if(currentPage==lastPage){style=''+nextPageSymbol+'';a+=style;b+=style;c+=style;d+=style;style=separator+''+lastPageSymbol+'';a+=style;b+=style}else{style=''+nextPageSymbol+'';a+=style;b+=style;c+=style;d+=style;style=separator+''+lastPageSymbol+'';a+=style;b+=style}switch(paginatorStyle){case 1:style=a;break;case 2:style=b;break;case 3:style=c;break;case 4:style=d;break;default:style=a}if(enablePageOfOption)style+=''+e+'';if(enableGoToPage)style+=''+f+'';if(enableSelectNoItems)style+=''+g+'';$(".paginator").html(style)}