1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/mic-pages-jQun

В этом репозитории не указан файл с открытой лицензией (LICENSE). При использовании обратитесь к конкретному описанию проекта и его зависимостям в коде.
Клонировать/Скачать
jQun_2.0.0.js 99 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
china-liji Отправлено 08.04.2015 08:41 b2dd363
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376
/*
* 类库名称 :jQun
* 中文释义 :聚集在一起的千里马
* 文档状态 :2.0.0
* 本次修改 :
* 1. 针对Chrome 42版本,进行全面改版:去除自定义类名称,如:new Class(constructor, "name", SuperClass) 改为 new Class(constructor, SuperClass);
* 2. 优化类的代码,提高运行效率;
* 3. 其他逻辑均不变。
* 开发浏览器信息 :firefox 20.0+、chrome 26.0+、IE 10+、基于webkit的手机浏览器
* 官方网站 :http://www.jqunjs.com
*/
// jQun的定义
new function(window, defineProperty){
this.jQun = (function(create){
function jQun(theConstructor, _SuperClass){
/// <summary>
/// 生成一个继承指定父类的新生类。
/// </summary>
/// <param name="theConstructor" type="Function">新生类的构造函数。</param>
/// <param name="_SuperClass" type="jQun">需要继承的父类。</param>
var callee = arguments.callee;
with(
callee
){
if(
!isInstanceOf(this, callee)
){
return new HTMLElementList(arguments[0], arguments[1]);
}
theConstructor.toString = toString;
}
return theConstructor.prototype = create(
_SuperClass || this.getOwnClass(),
{
constructor : {
value : theConstructor,
writable : true,
configurable : true
}
}
);
}
return jQun;
}(
Object.create
));
defineProperty(window, "jQun", { value : this.jQun });
}(
window,
Object.defineProperty
);
// 基本方法和属性的定义
new function(jQun, Object, Array){
this.every = (function(){
return function(obj, fn, _this){
/// <summary>
/// 确定对象的所有成员是否满足指定的测试。
/// </summary>
/// <param name="obj" type="Object">需要测试成员的对象。</param>
/// <param name="fn" type="Function">用于测试对象成员的测试函数。</param>
/// <param name="_this" type="*">指定测试函数的 this 对象。</param>
if(
obj instanceof Array
){
return obj.every(fn, _this);
}
var isNumber = typeof obj === "number";
if(
typeof _this !== "undefined"
){
fn = fn.bind(_this);
}
if(
isNumber
){
obj = new Array(obj + 1).join(" ");
}
for(
var o in obj
){
if(
fn.apply(
_this,
isNumber ? [o - 0] : [obj[o], o, obj]
)
){
continue;
}
return false;
}
return true;
};
}());
this.forEach = (function(every){
return function(obj, fn, _this){
/// <summary>
/// 遍历对象的所有成员并对其执行指定操作函数。
/// </summary>
/// <param name="obj" type="Object">需要遍历的对象。</param>
/// <param name="fn" type="Function">指定操作的函数。</param>
/// <param name="_this" type="*">指定操作函数的 this 对象。</param>
every(
obj,
function(){
fn.apply(this, arguments);
return true;
},
_this
);
return obj;
};
}(
this.every
));
this.define = (function(forEach, defineProperty){
return function(obj, name, value, _descriptor){
/// <summary>
/// 将属性添加到对象或修改现有属性的特性。
/// </summary>
/// <param name="obj" type="Object">对其添加或修改属性的对象。</param>
/// <param name="name" type="String">需要添加或修改的属性名。</param>
/// <param name="value" type="*">需要添加或修改的属性值。</param>
/// <param name="_descriptor" type="Object">需要添加或修改的属性描述符。</param>
var desc = { configurable : true, writable : true };
forEach(
_descriptor,
function(value, name){
desc[name] = value;
}
);
if(
_descriptor && !!(_descriptor.gettable || _descriptor.settable)
){
desc.get = value.get;
desc.set = value.set;
delete desc["writable"];
}
else{
desc.value = value;
}
defineProperty(obj, name, desc);
return obj;
};
}(
this.forEach,
Object.defineProperty
));
this.defineProperties = (function(forEach, define){
return function(obj, props, _descriptor){
/// <summary>
/// 将一个或多个属性添加到对象,并/或修改现有属性的特性。
/// </summary>
/// <param name="obj" type="Object">对其添加或修改属性的对象。</param>
/// <param name="props" type="Object">包含一个或多个属性的键值对。</param>
/// <param name="_descriptor" type="Object">需要添加或修改的属性描述符。</param>
forEach(
props,
function(value, name){
define(obj, name, value, _descriptor);
}
);
return obj;
};
}(
this.forEach,
this.define
));
this.set = (function(forEach){
return function(obj, props){
/// <summary>
/// 添加或修改指定对象的属性。
/// </summary>
/// <param name="obj" type="Object">需要添加或修改属性的对象。</param>
/// <param name="props" type="Object">需要添加或修改的属性集合。</param>
forEach(
props,
function(val, name){
obj[name] = val;
}
);
return obj;
};
}(
this.forEach
));
this.except = (function(set, forEach){
return function(obj, props){
/// <summary>
/// 返回一个不包含所有指定属性名称的对象。
/// </summary>
/// <param name="obj" type="Object">需要排除属性的对象。</param>
/// <param name="props" type="Array">需要排除的属性名称数组。</param>
var result = set({}, obj);
forEach(
props,
function(name){
delete result[name];
}
);
return result;
}
}(
this.set,
this.forEach
));
this.isInstanceOf = (function(getPrototypeOf){
return function(obj, constructor){
/// <summary>
/// 判断对象是否为指定类构造函数的一级实例(即直接由该类实例化)。
/// </summary>
/// <param name="obj" type="Object">用于判断的实例对象。</param>
/// <param name="constructor" type="Function">指定的类的构造函数。</param>
return getPrototypeOf(obj) === constructor.prototype;
};
}(
Object.getPrototypeOf
));
this.isPropertyOf = (function(every, getOwnPropertyNames, getOwnPropertyDescriptor){
return function(obj, property){
/// <summary>
/// 检测对象自己是否具有指定属性或访问器。
/// </summary>
/// <param name="obj" type="Object">一个可能具有指定属性或访问器的对象。</param>
/// <param name="property" type="*">用于检测的属性或访问器。</param>
return !every(
getOwnPropertyNames(obj),
function(name){
return every(
this,
function(n){
return this[n] !== property;
},
getOwnPropertyDescriptor(obj, name)
);
},
["value", "get", "set"]
);
};
}(
this.every,
Object.getOwnPropertyNames,
Object.getOwnPropertyDescriptor
));
this.nesting = (function(forEach){
return function(obj, fn, _this){
/// <summary>
/// 将对象中的每个枚举元素进行再枚举并执行指定操作(双重嵌套的forEach)。
/// </summary>
/// <param name="obj" type="Object">需要嵌套枚举并执行指定操作的对象(一般为json)。</param>
/// <param name="fn" type="Function">指定的操作函数。</param>
/// <param name="_this" type="*">指定操作函数的 this 对象。</param>
forEach(
obj,
function(o){
forEach(o, fn, _this);
}
);
return obj;
};
}(
this.forEach
));
this.toArray = (function(slice){
return function(obj, _start, _end){
/// <summary>
/// 将类似数组的对象转化为数组。
/// </summary>
/// <param name="obj" type="Object">需要转化为数组的对象。</param>
/// <param name="_start" type="Number">进行截取,截取的起始索引。</param>
/// <param name="_end" type="Number">需要截取的末尾索引。</param>
return slice.call(obj, _start || 0, _end);
};
}(
Array.prototype.slice
));
this.merge = (function(nesting, toArray){
return function(obj, args){
/// <summary>
/// 深度合并对象中所有项,返回新的集合。
/// </summary>
/// <param name="obj" type="Object, Array">需要合并的项。</param>
/// <param name="args" type="Object, Array">其他需要合并的项列表。</param>
if(
obj instanceof Array
){
return obj
.concat
.apply(
obj,
toArray(arguments, 1)
);
}
var result = {}, another = arguments[1];
nesting(
arguments,
function(value, name){
result[name] = typeof value === "object" ? this(value) : value;
},
arguments.callee
);
return result;
};
}(
this.nesting,
this.toArray
));
this.toString = (function(){
return function(){
/// <summary>
/// 使函数在控制台里看起来像本地代码。
/// </summary>
var name = this.name;
if(
name
){
return "function " + name + "() { [native code] }";
}
return "function (){ [native code] }";
};
}());
this.prototype = (function(prototype, forEach, define, defineProperties, getPrototypeOf){
defineProperties(
prototype,
{
assign : function(props){
/// <summary>
/// 给该类的属性赋值。
/// </summary>
/// <param name="props" type="Object">包含一个或多个属性的键值对。</param>
/// <returns>this</returns>
forEach(
props,
function(val, name){
if(typeof val === "undefined")
return;
this[name] = val;
},
this
);
return this;
},
getOwnClass : function(){
/// <summary>
/// 获取自身类。
/// </summary>
return this.constructor.prototype;
},
getSuperClass : function(){
/// <summary>
/// 获取父类。
/// </summary>
return getPrototypeOf(this.getOwnClass());
},
isSubOf : function(SuperClass){
/// <summary>
/// 判断该类是否是指定类的子孙类。
/// </summary>
/// <param name="SuperClass" type="jQun, Function">指定的类,或指定类的构造函数。</param>
return this instanceof SuperClass.constructor;
},
override : function(props, _descriptor){
/// <summary>
/// 重写一个或多个属性的值。
/// </summary>
/// <param name="props" type="Object">包含一个或多个属性的键值对。</param>
/// <param name="_descriptor" type="Object">被添加或修改属性的描述符。</param>
this.props(props, _descriptor);
return this;
},
props : function(props, _descriptor){
/// <summary>
/// 将一个或多个属性添加到该类,并/或修改现有属性的特性。
/// </summary>
/// <param name="props" type="Object">包含一个或多个属性的键值对。</param>
/// <param name="_descriptor" type="Object">被添加或修改属性的描述符。</param>
defineProperties(this, props, _descriptor);
return this;
},
toString : function(){
/// <summary>
/// 对象字符串。
/// </summary>
return "[jQun " + this.constructor.name + "]";
}
}
);
/*
bug:Object.prototype.__proto__ 为 null,每个对象将自带__proto__属性,
而新版本的chrome等浏览器,如果对象不是继承Object对象,那么则无__proto__属性。
(2013.08.20)目前有些浏览器不支持,如:手机QQ浏览器,手机百度浏览器;
(2015.01.06)nodejs也不支持。
*/
try {
define(
prototype,
"__proto__",
Object.getOwnPropertyDescriptor(
Object.prototype,
"__proto__"
),
{ settable : true, gettable : true }
);
}
catch(e){}
return prototype;
}(
Object.create(
null,
{
constructor : {
value : jQun,
writable : true
}
}
),
this.forEach,
this.define,
this.defineProperties,
Object.getPrototypeOf
));
this.forEach(
this,
function(property, name, self){
if(
typeof property === "function"
){
property.toString = self.toString;
}
this[name] = property;
},
jQun
);
}(
jQun,
Object,
Array
);
// 面向对象的基础
new function(
jQun, Function,
getSuperClass, defineProperties, forEach, set, toString,
getDescriptor, freeze, console,
getArgumentNames, getArgumentsString
){
this.Class = (function(AnonymousClass, getConstructor){
function Class(_constructor, _SuperClass){
/// <summary>
/// 派生出一个类。
/// </summary>
/// <param name="_constructor" type="Function">源构造函数。</param>
/// <param name="_SuperClass" type="Class, Interface">需要继承的父类或接口。</param>
var constructor = _constructor ? _constructor : AnonymousClass;
return new jQun(
getConstructor(
constructor,
constructor.name || AnonymousClass.name,
getArgumentNames(constructor),
getArgumentNames(
_SuperClass ? _SuperClass.constructor.source : null
)
),
_SuperClass || this.getOwnClass()
);
};
Class = new jQun(Class);
Class.override({
getSuperClass : function(){
/// <summary>
/// 获取父类。
/// </summary>
var Super = getSuperClass.call(this);
return Super === Class ? null : Super;
},
toString : function(){
/// <summary>
/// 对象字符串。
/// </summary>
return "[Class " + this.constructor.name + "]";
}
});
return Class.constructor;
}(
// AnonymousClass
function AnonymousClass(){},
// getConstructor
function(source, name, argumentNames, parentArgumentNames){
return defineProperties(
new Function([
"return function " + name + " (){",
"var callee = arguments.callee, Super = this.getSuperClass.call(callee.prototype);",
"if(",
"Super",
"){",
"Super",
".constructor",
".call(",
"this",
getArgumentsString(
parentArgumentNames,
argumentNames
),
");",
"}",
"return callee.source.apply(this, arguments);",
"}",
].join(""))(),
{
argumentNames : argumentNames,
source : source
}
);
}
));
this.StaticClass = (function(AnonymousStaticClass, getConstructor){
function StaticClass(_constructor, _props, _descriptor){
/// <summary>
/// 派生出一个静态类。
/// </summary>
/// <param name="_constructor" type="Function">源构造函数。</param>
/// <param name="_props" type="Object">类的属性。</param>
/// <param name="_descriptor" type="Object">被添加属性的描述符。</param>
var CreatedStaticClass,
constructor = _constructor ? _constructor : AnonymousStaticClass;
CreatedStaticClass = new jQun(
getConstructor(
constructor.name || AnonymousStaticClass.name
),
this.getOwnClass()
);
if(
_props
){
CreatedStaticClass.props(_props, _descriptor);
}
constructor.call(CreatedStaticClass);
return CreatedStaticClass;
};
StaticClass = new jQun(StaticClass);
StaticClass.override({
getSuperClass : function(){
/// <summary>
/// 获取父类。
/// </summary>
return null;
},
toString : function(){
/// <summary>
/// 对象字符串。
/// </summary>
return "[StaticClass " + this.constructor.name + "]";
}
});
return StaticClass.constructor;
}(
// AnonymousStaticClass
function AnonymousStaticClass(){},
// getConstructor
function(name){
return getDescriptor(
new Function("return { get '" + name + "' (){} };")(),
name
).get;
}
));
this.Enum = (function(every, hasOwnProperty){
function Enum(data){
/// <summary>
/// 枚举。
/// </summary>
/// <param name="data" type="Array, Object">枚举数据数组或键值对。</param>
if(
data instanceof Array
){
forEach(
data,
function(val, i){
this[val] = i;
},
this
);
}
else {
set(this, data);
}
freeze(this);
};
Enum = new jQun(Enum);
Enum.props({
every : function(fn, _this){
/// <summary>
/// 确定枚举的所有成员是否满足指定的测试。
/// </summary>
/// <param name="fn" type="Function">用于测试对象成员的测试函数。</param>
/// <param name="_this" type="*">指定测试函数的 this 对象。</param>
return every(this, fn, _this);
},
forEach : function(fn, _this){
/// <summary>
/// 遍历枚举的所有成员并对其执行指定操作函数。
/// </summary>
/// <param name="fn" type="Function">指定操作的函数。</param>
/// <param name="_this" type="*">指定操作函数的 this 对象。</param>
return forEach(this, fn, _this);
},
getNameByValue : function(value, _shouldReturnCamelCasing){
/// <summary>
/// 根据枚举值获取枚举名称。
/// </summary>
/// <param name="value" type="*">指定的枚举值。</param>
/// <param name="value" type="*">是否应该返回驼峰式的值。</param>
var name;
this.every(function(v, n){
if(value === v){
name = n;
return false;
}
return true;
});
return _shouldReturnCamelCasing ? name.substring(0, 1).toLowerCase() + name.substring(1) : name;
},
has : function(propertyName){
/// <summary>
/// 判断是否有指定名称的枚举。
/// </summary>
/// <param name="propertyName" type="String">指定的名称。</param>
return hasOwnProperty.call(this, propertyName);
}
});
return Enum.constructor;
}(
jQun.every,
Object.prototype.hasOwnProperty
));
this.Interface = (function(Class, isInstanceOf, getConstructor, getName){
function Interface(propertyNames, _Super){
/// <summary>
/// 定义接口。
/// </summary>
/// <param name="propertyNames" type="Array">该接口所规定拥有的属性名称。</param>
/// <param name="_Super" type="Interface, Class">需要继承的父接口或父类。</param>
if(
!isInstanceOf(this, Interface.constructor)
){
return;
}
var CreatedInterface, Super = _Super || Interface;
CreatedInterface = new jQun(
getConstructor(
propertyNames,
getArgumentNames(Super.constructor.source)
),
Super
);
return freeze(CreatedInterface);
};
Interface = new jQun(Interface);
return Interface.constructor;
}(
this.Class,
jQun.isInstanceOf,
// getConstructor
function(propertyNames, argumentNames){
var names = "Interface <" + propertyNames.join(", ") + ">";
return defineProperties(
getDescriptor(
new Function([
"return {",
"get '" + names + "' (){",
(
propertyNames.length > 0 ?
[
"var name;",
"['" + propertyNames.join("', '") + "'].every(",
"function(propertyName){",
"if(",
"propertyName in this",
"){",
"return true;",
"}",
"name = propertyName;",
"return false;",
"},",
"this",
");",
"if(",
"typeof name === 'string'",
"){",
"throw '接口 ' + this.toString() + ' :必须定义属性 \"' + name + '\" !';",
"return;",
"}",
].join("") :
""
),
"this.getSuperClass",
".call(",
"arguments.callee.prototype",
")",
".constructor",
".apply(",
"this,",
"arguments",
");",
"}",
" };"
].join(""))(),
names
).get,
{
source : new Function([
"return function Interface(" + argumentNames.join(", ") + "){};"
].join(""))(),
argumentNames : argumentNames
}
);
},
// getName
function(propertyNames){
return "Interface [" + propertyNames.join(", ") + "]";
}
));
this.Namespace = (function(){
function Namespace(_members){
/// <summary>
/// 开辟一个命名空间,并可赋予初始成员。
/// </summary>
/// <param name="members" type="Object">成员键值对。</param>
if(
!_members
){
return;
}
this.members(_members);
};
Namespace = new jQun(Namespace);
Namespace.props({
members : function(members){
/// <summary>
/// 给该命名空间赋予成员。
/// </summary>
/// <param name="members" type="Object">成员键值对。</param>
return set(this, members);
}
});
return Namespace.constructor;
}());
defineProperties(jQun, this);
}(
jQun,
Function,
jQun.prototype.getSuperClass,
jQun.defineProperties,
jQun.forEach,
jQun.set,
jQun.toString,
Object.getOwnPropertyDescriptor,
Object.freeze,
console,
// getArgumentNames
function(ARG_LIST_REGX, ARG_REGX, emptyArray){
return function(source){
if(
!source
){
return emptyArray;
}
return source
.toString()
.match(
ARG_LIST_REGX
)[1]
.match(
ARG_REGX
) || emptyArray;
};
}(
// ARG_LIST_REGX
/function[^\(]*\(([^\)]*)/,
// ARG_REGX
/([\$\_a-zA-Z]+[\w\$]*)/g,
// emptyArray
Object.freeze([])
),
// getArgumentsString
function(parentArgumentNames, argumentNames){
if(
(parentArgumentNames.length || argumentNames.length) === 0
){
return "";
}
return parentArgumentNames
.map(
function(name){
var i = argumentNames.indexOf(name);
if(
i === -1
){
if(
name.indexOf("_") === 0
){
i = argumentNames.indexOf(name.substring(1));
}
}
return ", arguments[" + i + "]";
}
)
.join("");
}
);
new function(jQun, Class, StaticClass, Interface, Enum, defineProperties, forEach){
// 一些独立的基础类
(function(forEach){
this.OSs = (function(){
return new Enum(
["Unknow", "Windows", "Macintosh", "Linux", "Android", "IPhone", "IPad", "IPod", "WindowsPhone"]
);
}());
this.BrowserCores = (function(){
return new Enum(
["Unknow", "Blink", "AppleWebKit", "Gecko", "Trident"]
);
}());
this.BrowserAgents = (function(){
return new Enum(
["Unknow", "Chrome", "Firefox", "MSIE", "Safari", "Opera", "MicroMessenger", "MQQBrowser"]
);
}());
this.Browser = (function(OSs, BrowserCores, BrowserAgents, userAgent, parseInt){
function Browser(){
/// <summary>
/// 浏览器信息。
/// </summary>
var core, info = {};
[
/Mozilla\/[\d\.]+ \(([^; ]+).*(AppleWebKit)\/([\d\.]+).*(Chrome|MicroMessenger|MQQBrowser|Version)\/([\d\.]+)/,
/Mozilla\/[\d\.]+ \(([^; ]+).*rv\s*\:\s*([\d\.]+).*(Gecko)\/[\d\.]+ ([^\/\s]+)\/([\d\.]+)/,
/(MSIE) ([\d\.]+).*(Windows).*(Trident)\/([\d\.]+)/,
/Mozilla\/[\d\.]+ \(([^; ]+).*(Trident)\/([\d\.]+).*rv\s*\:\s*([\d\.]+)/
].every(function(regx, i){
var result = userAgent.match(regx);
if(
result === null
){
return true;
}
result.splice(0, 1);
this[i].forEach(
function(key, i){
var value = result[i];
info[key] = key in this ? this[key][value.substring(0, 1).toUpperCase() + value.substring(1)] : value;
},
{ agent : BrowserAgents, core : BrowserCores, os : OSs }
);
return false;
}, [
["os", "core", "coreVersion", "agent", "version"],
["os", "coreVersion", "core", "agent", "version"],
["agent", "version", "os", "core", "coreVersion"],
["os", "core", "coreVersion", "version"]
]);
core = info.core;
if(
core === BrowserCores.AppleWebKit
){
if(
userAgent.indexOf("Android") > -1
){
info.os = OSs.Android;
}
if(
info.agent === BrowserAgents.Chrome
){
if(
parseInt(info.version) > 27
){
info.core = BrowserCores.Blink;
}
}
else if(
!info.agent
){
info.agent = BrowserAgents.Safari;
}
}
else if(
core === BrowserCores.Trident
){
info.agent = BrowserAgents[info.isMobile ? "WindowsPhone" : "MSIE"];
}
info.name = BrowserAgents.getNameByValue(info.agent);
this.assign(info);
};
Browser = new StaticClass(Browser, {
// 浏览器代理商
agent : BrowserAgents.Unknow,
// 浏览器核心
core : BrowserCores.Unknow,
// 浏览器核心版本
coreVersion : "0",
// 浏览器名称
name : BrowserAgents.getNameByValue(BrowserAgents.Unknow),
// 操作系统
os : OSs.Unknow,
// 浏览器版本
version : "0"
});
Browser.props({
getMobileOSVersion : function(){
/// <summary>
/// 获取移动操作系统的版本,精确到浮点数。
/// </summary>
var version;
if(
this.isMobile
){
var result = userAgent.match(this.os === OSs.Android ? /Android ([\d\.]+)/ : /OS ([\d\_]+)/);
if(
result
){
version = result[1].replace("_", ".");
}
}
return parseFloat(version);
},
// 是否为移动端浏览器
isMobile : userAgent.indexOf("Mobile") > -1
});
return Browser;
}(
this.OSs,
this.BrowserCores,
this.BrowserAgents,
navigator.userAgent,
parseInt,
parseFloat
));
this.List = (function(NaN, define, toArray, hasOwnProperty){
function List(_list){
/// <summary>
/// 对列表进行管理、操作的类。
/// </summary>
/// <param name="_list" type="List、Array">初始化时所拥有的列表集合。</param>
this.assign({ length : 0 });
if(
!_list
){
return;
}
this.combine(_list);
};
List = new Class(List);
List.props({
alternate : function(num, _remainder){
/// <summary>
/// 交替性取出集合索引值所符合的项。
/// </summary>
/// <param name="num" type="Number">取模运算值。</param>
/// <param name="_remainder" type="Number">余数。</param>
var list = this.createList();
_remainder = _remainder || 0;
this.forEach(function(item, i){
if(
i % num === _remainder
){
list.push(item);
}
});
return list;
},
clear : function(){
/// <summary>
/// 清空整个集合。
/// </summary>
this.splice(0);
return this;
},
combine : function(list){
/// <summary>
/// 在原列表上,合并另一个集合。
/// </summary>
/// <param name="list" type="List, Array">另一个集合。</param>
this.push.apply(this, toArray(list));
return this;
},
createList : function(_list){
/// <summary>
/// 创建个新的列表。
/// </summary>
/// <param name="_list" type="List、Array">初始化时所拥有的列表集合。</param>
return new List.constructor(_list);
},
distinct : function(){
/// <summary>
/// 对列表进行去重。
/// </summary>
var list = this;
this.splice(
0
)
.forEach(function(item){
if(
list.indexOf(item) > -1
){
return;
}
list.push(item);
});
return list;
},
even : function(){
/// <summary>
/// 返回集合中偶数项集合。
/// </summary>
return this.alternate(2);
},
length : NaN,
odd : function(){
/// <summary>
/// 返回集合中奇数项集合。
/// </summary>
return this.alternate(2, 1);
}
});
Object
.getOwnPropertyNames(
Array.prototype
)
.forEach(
function(name){
if(
hasOwnProperty.call(List, name)
){
return;
}
if(
name === "toString"
){
return;
}
define(List, name, this[name]);
},
Array.prototype
);
List.override({
concat : function(array){
/// <summary>
/// 合并另外一个数组,并返回合并后的新数组。
/// </summary>
/// <param name="list" type="Array">另一个集合。</param>
return toArray(
this
)
.concat(
toArray(array)
);
}
});
return List.constructor;
}(
NaN,
jQun.define,
jQun.toArray,
Object.prototype.hasOwnProperty
));
}.call(
this
));
// 与字符串处理有关的类
(function(){
this.VerificationRegExpString = (function(EMAILS, WEB_URLS, toPerfectMatchString){
return new Enum({
Chinese : "[\\u4e00-\\u9fa5]+",
Email : toPerfectMatchString(EMAILS),
Emails : EMAILS,
Empty : "^$",
Landline : "^(?:(?:\\d{3}|\\d{4})-)?\\d{7,8}$",
NotEmpty : "\\S+",
Telephone : "^(?:(?:\\d{3}|\\d{4})-)?\\d{11}$",
UserInfo : "^\\w{6,16}$",
WebURL : toPerfectMatchString(WEB_URLS),
WebURLs : WEB_URLS
});
}(
// EMAILS
"(\\w+(?:[-+.]\\w+)*)@(\\w+(?:[-.]\\w+)*)\\.(\\w+(?:[-.]\\w+)*)",
// WEB_URLS
"\\w+:\\/\\/.+",
// toPerfectMatchString
function(str){
return "^" + str + "$";
}
));
this.JSON = (function(JSONBase, location, encodeURIComponent, decodeURIComponent){
function JSON(){
/// <summary>
/// JSON功能类。
/// </summary>
};
JSON = new StaticClass(JSON);
JSON.props({
parse : function(string){
/// <summary>
/// 将字符串转化成JSON对象。
/// </summary>
/// <param name="string" type="String">需要转化的字符串。</param>
return JSONBase.parse(string);
},
parseAddressParameters : function(_isHash){
/// <summary>
/// 解析地址栏参数。
/// </summary>
/// <param name="_isHash" type="Boolean">是否为hash(#号)参数。</param>
return this.parse(
decodeURIComponent([
"{",
(
location[
_isHash ? "hash" : "search"
]
.substring(1)
.match(
/([^&=]+)=([^\&]+)/g
) || []
)
.join(
'","'
)
.split(
"="
)
.join(
'":"'
)
.replace(
/^(.+)$/,
'"$1"'
),
"}"
].join(""))
);
},
stringify : function(object){
/// <summary>
/// 将JSON对象转化成字符串。
/// </summary>
/// <param name="object" type="Object">需要转化的JSON对象。</param>
return JSONBase.stringify(object);
},
stringifyAddressParameters : function(object){
/// <summary>
/// 将对象转化为地址参数字符串。
/// </summary>
var result = [];
forEach(
object,
function(value, key){
if(
typeof value === "object"
){
value = this.stringify(value);
}
result.push(
encodeURIComponent(key) + "=" + encodeURIComponent(value)
);
},
this
);
return result.join("&");
}
});
return JSON;
}(
JSON,
location,
encodeURIComponent,
decodeURIComponent
));
this.Text = (function(Array, T_REGX, encodeURIComponent){
function Text(text){
/// <summary>
/// 用于操作字符串文本的类。
/// </summary>
/// <param name="text" type="String, Array">字符串文本。</param>
this.assign({
text : text instanceof Array ? text.join("") : text
});
};
Text = new Class(Text);
Text.props({
removeSpace : function(){
/// <summary>
/// 移除字符串中的前后空格。
/// </summary>
return this.text.match(/^\s*([\S\s]*?)\s*$/)[1];
},
replace : function(replacement){
/// <summary>
/// 返回一个替换数据后的字符串。
/// </summary>
/// <param name="replacement" type="Object, Function">需要替换的数据或者自行替换的处理函数。</param>
return this.text.replace(
T_REGX,
typeof replacement === "function" ?
replacement :
function(str, modifier, word){
if(
modifier === ":"
){
return "{" + word + "}";
}
var val = replacement[word];
return val == null ? modifier === "~" ? "" : word : val;
}
);
},
toUrlParams : function(params){
/// <summary>
/// 返回一个替换数据后的连接字符串。
/// </summary>
/// <param name="params" type="Object, Function">需要替换的数据或者自行替换的处理函数。</param>
return this.replace(function(str, modifier, word){
return encodeURIComponent(params[word]);
});
},
text : ""
});
return Text.constructor;
}(
Array,
// T_REGX
/\{\s*(?:\?([^\{\}\s]{1}))?\s*([^\{\}]*?)\s*\}/g,
encodeURIComponent
));
this.Verification = (function(VerificationRegExpString, RegExp){
function Verification(){
/// <summary>
/// 验证。
/// </summary>
};
Verification = new StaticClass(Verification);
Verification.props({
every : function(arr){
/// <summary>
/// 验证列表中每一项是否拥有正确的返回值,函数返回错误项的索引。
/// </summary>
/// <param name="arr" type="Array, List">需要提供的验证列表。</param>
var index = -1;
arr.every(function(func, i){
if(
func()
){
return true;
}
index = i;
return false;
});
return index;
},
match : function(str, regExpString, _regxAttrs){
/// <summary>
/// 验证匹配,返回匹配值。
/// </summary>
/// <param name="str" type="String">字符串需要验证的。</param>
/// <param name="regExpString" type="String">用于匹配的正则字符串。</param>
/// <param name="_regxAttrs" type="String">正则属性。</param>
return str.match(new RegExp(regExpString, _regxAttrs));
},
result : function(str, regExpString){
/// <summary>
/// 验证结果,返回匹配结果。
/// </summary>
/// <param name="str" type="String">需要验证的字符串。</param>
/// <param name="regExpString" type="String">用于匹配的正则字符串。</param>
return !!this.match(str, regExpString);
}
});
return Verification;
}(
this.VerificationRegExpString,
RegExp
));
}.call(
this
));
// 元素属性基类
(function(List){
this.IElementPropertyCollection = (function(){
return new Interface(
["propertyName"], List.prototype
);
}());
this.ElementPropertyCollection = (function(IElementPropertyCollection){
function ElementPropertyCollection(elementList){
/// <summary>
/// 所有元素属性的基类。
/// </summary>
/// <param name="elementList" type="Array, List">元素列表。</param>
var name = this.propertyName;
this.assign({
sources : elementList
});
if(
name === ""
){
return;
}
elementList.forEach(
function(element){
this.push(element[name]);
},
this
);
};
ElementPropertyCollection = new Class(ElementPropertyCollection, IElementPropertyCollection);
ElementPropertyCollection.props({
sources : null,
valueOf : function(){
/// <summary>
/// 返回当前对象。
/// </summary>
return this;
}
});
return ElementPropertyCollection.constructor;
}(
this.IElementPropertyCollection
));
}.call(
this,
this.List
));
// 与ElementPropertyCollection(元素属性)有关的类
(function(ElementPropertyCollection){
this.AttributesCollection = (function(){
function AttributesCollection(elementList){
/// <summary>
/// 元素特性集合。
/// </summary>
/// <param name="name" type="ElementList">元素列表。</param>
};
AttributesCollection = new Class(AttributesCollection, ElementPropertyCollection.prototype);
AttributesCollection.props({
contains : function(name){
/// <summary>
/// 判断集合内的第一个元素是否包含指定名称的属性,返回true或false。
/// </summary>
/// <param name="name" type="String">属性的名称。</param>
if(
this.sources.length === 0
){
return false;
}
return this.sources[0].hasAttribute(name);
},
get : function(name){
/// <summary>
/// 获取集合内第一个元素所指定名称的属性。
/// </summary>
/// <param name="name" type="String">需要获取属性的名称。</param>
if(
this.sources.length === 0
){
return null;
}
return this.sources[0].getAttribute(name);
},
propertyName : "attributes",
remove : function(name){
/// <summary>
/// 移除集合内所有元素所指定名称的属性。
/// </summary>
/// <param name="name" type="String">需要移除属性的名称。</param>
this.sources
.forEach(function(element){
element.removeAttribute(name);
});
return this;
},
replace : function(oldAttrName, newAttrName, newAttrValue){
/// <summary>
/// 移除指定的旧属性,添加指定的新属性。
/// </summary>
/// <param name="oldAttrName" type="String">需要移除属性的名称。</param>
/// <param name="newAttrName" type="String">需要添加属性的名称。</param>
/// <param name="newAttrValue" type="*">需要添加属性的值。</param>
this.sources
.forEach(function(element){
element.removeAttribute(oldAttrName);
element.setAttribute(newAttrName, newAttrValue);
});
return this;
},
set : function(name, _value){
/// <summary>
/// 设置集合中所有元素的属性。
/// </summary>
/// <param name="name" type="String">需要设置属性的名称。</param>
/// <param name="_value" type="String">需要设置属性的值。</param>
if(
_value == null
){
_value = "";
}
this.sources.forEach(function(element){
element.setAttribute(name, _value);
});
return this;
},
toggle : function(name, _value){
/// <summary>
/// 自行判断集合中每一个元素是否含有指定的属性:有则移除,无则添加并设置指定的值。
/// </summary>
/// <param name="name" type="String">需要设置或移除属性的名称。</param>
/// <param name="_value" type="String">需要设置属性的值。</param>
if(
this.contains(name)
){
return this.remove(name);
}
return this.set(name, _value);
}
});
return AttributesCollection.constructor;
}());
this.CSSPropertyCollection = (function(isNaN, hasOwnProperty){
function CSSPropertyCollection(elementList){
/// <summary>
/// 元素CSS属性集合。
/// </summary>
};
CSSPropertyCollection = new Class(CSSPropertyCollection, ElementPropertyCollection.prototype);
CSSPropertyCollection.props({
get : function(name){
/// <summary>
/// 获取集合中第一个元素的CSS属性。
/// </summary>
/// <param name="name" type="String">CSS属性名。</param>
if(
this.length === 0
){
return "";
}
return this[0][name];
},
propertyName : "style",
set : function(name, value){
/// <summary>
/// 设置集合中所有元素的CSS属性。
/// </summary>
/// <param name="name" type="String">需要设置的CSS属性名。</param>
/// <param name="value" type="String">需要设置的CSS属性值。</param>
this.forEach(function(style){
style[name] = value;
});
return this;
}
});
forEach(
getComputedStyle(document.documentElement),
function(value, name, CSSStyle){
// firefox、chrome 与 IE 的 CSSStyleDeclaration 结构都不一样
var cssName = isNaN(name - 0) ? name : value;
if(
hasOwnProperty.call(CSSPropertyCollection, cssName)
){
return;
}
if(
typeof CSSStyle[cssName] !== "string"
){
return;
}
var property = {};
property[cssName] = {
get : function(){
return this.get(cssName);
},
set : function(value){
this.set(cssName, value);
}
};
CSSPropertyCollection.props(property, { gettable : true, settable : true });
}
);
return CSSPropertyCollection.constructor;
}(
isNaN,
Object.prototype.hasOwnProperty
));
this.ChildrenCollection = (function(){
function ChildrenCollection(elementList){
/// <summary>
/// children集合。
/// </summary>
/// <param name="elementList" type="ElementList">指定的父元素集合。</param>
};
ChildrenCollection = new Class(ChildrenCollection, ElementPropertyCollection.prototype);
ChildrenCollection.props({
append : function(element){
/// <summary>
/// 在第一个父元素最后添加一个子元素。
/// </summary>
/// <param name="element" type="Element">需要添加的子元素。</param>
return this.insert(element);
},
contains : function(element){
/// <summary>
/// 返回一个布尔值,该值表示该集合内的所有子元素是否含有指定的元素。
/// </summary>
/// <param name="element" type="Element">可能包含的子元素。</param>
return this.valueOf().indexOf(element) > -1;
},
insert : function(element, _idx){
/// <summary>
/// 在指定的索引处插入元素。
/// </summary>
/// <param name="element" type="Element">需要插入的元素。</param>
/// <param name="_idx" type="Number">指定的索引处。</param>
var sources = this.sources;
if(
sources.length > 0
){
sources.insertTo.call([element], sources[0], _idx);
}
return this;
},
propertyName : "children",
remove : function(_element){
/// <summary>
/// 移除指定的子元素,如果没指定子元素,则移除所有子元素。
/// </summary>
/// <param name="element" type="Element">需要移除的子元素。</param>
var children = this.valueOf();
if(
_element
){
var index = children.indexOf(_element);
if(
index > -1
){
children.splice(index, 1);
children.remove();
}
}
else {
children.remove();
}
return this;
},
valueOf : function(){
/// <summary>
/// 返回所有子元素的集合。
/// </summary>
return this.sources.query(">*");
}
});
return ChildrenCollection.constructor;
}());
this.ClassListCollection = (function(){
function ClassListCollection(elementList){
/// <summary>
/// classList集合。
/// </summary>
};
ClassListCollection = new Class(ClassListCollection, ElementPropertyCollection.prototype);
ClassListCollection.props({
add : function(className){
/// <summary>
/// 为集合中每一个元素添加指定的单个class。
/// </summary>
/// <param name="className" type="String">指定的单个class。</param>
this.forEach(function(classList){
classList.add(className);
});
return this;
},
contains : function(className){
/// <summary>
/// 判断集合中第一个元素是否包含指定的class。
/// </summary>
/// <param name="className" type="String">指定的单个class。</param>
if(
this.length === 0
){
return false;
}
return this[0].contains(className);
},
propertyName : "classList",
remove : function(className){
/// <summary>
/// 为集合中每一个元素移除指定的单个class。
/// </summary>
/// <param name="className" type="String">指定的单个class。</param>
this.forEach(function(classList){
classList.remove(className);
});
return this;
},
replace : function(oldClass, newClass){
/// <summary>
/// 为集合中每一个元素移除指定的旧class,添加指定的新class。
/// </summary>
/// <param name="oldClass" type="String">指定的旧class。</param>
/// <param name="newClass" type="String">指定的新class。</param>
this.forEach(function(classList){
classList.remove(oldClass);
classList.add(newClass);
});
return this;
},
toggle : function(className){
/// <summary>
/// 自行判断集合中每一个元素是否含有指定的class:有则移除,无则添加。
/// </summary>
/// <param name="className" type="String">指定的单个class。</param>
this.forEach(function(classList){
classList.toggle(className);
});
return this;
},
valueOf : function(){
/// <summary>
/// 返回集合中第一个元素的className。
/// </summary>
if(
this.length === 0
){
return "";
}
return this[0].toString();
}
});
return ClassListCollection.constructor;
}());
this.DatasetCollection = (function(AttributesCollection, PREFIX){
function DatasetCollection(elementList){
/// <summary>
/// dataset集合。
/// </summary>
};
DatasetCollection = new Class(DatasetCollection, AttributesCollection.prototype);
DatasetCollection.override({
contains : function(name){
/// <summary>
/// 判断集合中第一个元素是否包含指定名称的数据。
/// </summary>
/// <param name="name" type="String">数据的名称。</param>
return AttributesCollection.prototype.contains.call(this, PREFIX + name);
},
get : function(name){
/// <summary>
/// 通过指定名称获取数据。
/// </summary>
/// <param name="name" type="String">需要获取数据的名称。</param>
return AttributesCollection.prototype.get.call(this, PREFIX + name);
},
propertyName : "dataset",
remove : function(name){
/// <summary>
/// 移除具有指定名称的数据。
/// </summary>
/// <param name="name" type="String">需要移除数据的名称。</param>
return AttributesCollection.prototype.remove.call(this, PREFIX + name);
},
replace : function(oldDataName, newDataName, newDataValue){
/// <summary>
/// 移除指定的旧数据,添加指定的新数据。
/// </summary>
/// <param name="oldDataName" type="String">需要移除数据的名称。</param>
/// <param name="newDataName" type="String">需要添加数据的名称。</param>
/// <param name="newDataValue" type="String">需要添加数据的值。</param>
return AttributesCollection.prototype.replace.call(this, PREFIX + oldDataName, PREFIX + newDataName, newDataValue);
},
set : function(name, _value){
/// <summary>
/// 设置集合中所有元素的数据。
/// </summary>
/// <param name="name" type="String">需要设置数据的名称。</param>
/// <param name="_value" type="String">需要设置数据的值。</param>
return AttributesCollection.prototype.set.call(this, PREFIX + name, _value);
}
});
return DatasetCollection.constructor;
}(
this.AttributesCollection,
// PREFIX
"data-"
));
}.call(
this,
// AttributesCollection.prototype
this.ElementPropertyCollection
));
// 与DOM有关的类
(function(
List,
AttributesCollection, CSSPropertyCollection, ChildrenCollection, ClassListCollection, DatasetCollection,
Text, Window,
forEach, define
){
this.EventTargetList = (function(toArray){
function EventTargetList(_list){
/// <summary>
/// 事件目标列表类。
/// </summary>
/// <param name="_list" type="Array">事件目标数组。</param>
};
EventTargetList = new Class(EventTargetList, List.prototype);
EventTargetList.props({
attach : function(events, _capture, _priority, _useWeakReference){
/// <summary>
/// 向集合中所有目标注册事件侦听器。
/// </summary>
/// <param name="events" type="Object">事件侦听器键值对。</param>
/// <param name="_capture" type="Boolean">侦听器是否运行于捕获阶段。</param>
/// <param name="_priority" type="Number">优先级,数字越大,优先级越高。</param>
/// <param name="_useWeakReference" type="Boolean">是否是属于强引用。</param>
var eventTargetList = this, otherArgs = toArray(arguments, 1);
forEach(
events,
function(fn, type){
var eventArgs = [
type,
fn.length === 2 ?
function(e){
fn.call(
this,
e,
eventTargetList.createList(e.target)
);
} :
fn
].concat(
otherArgs
);
eventTargetList.forEach(function(eventTarget){
eventTarget
.addEventListener
.apply(
eventTarget,
eventArgs
);
});
}
);
return this;
},
detach : function(events){
/// <summary>
/// 移除集合中所有目标的事件侦听器。
/// </summary>
/// <param name="events" type="Object">事件侦听器键值对。</param>
this.forEach(function(eventTarget){
forEach(
events,
function(fn, type){
eventTarget.removeEventListener(type, fn);
}
);
});
return this;
},
dispatch : function(event){
/// <summary>
/// 给所有目标分配事件。
/// </summary>
/// <param name="event" type="Event">需要分配的事件。</param>
this.forEach(
function(eventTarget){
eventTarget.dispatchEvent(event);
}
);
return this;
}
});
return EventTargetList.constructor;
}(
jQun.toArray
));
this.DOM = (function(EventTargetList, document, parseFloat){
function DOM(){
/// <summary>
/// DOM类。
/// </summary>
};
DOM = new StaticClass(DOM);
DOM.props({
getCSSNumberValue : function(cssValue){
/// <summary>
/// 对css的值进行处理,获取其数值部分。
/// </summary>
/// <param name="cssValue" type="String">css值。</param>
return parseFloat(cssValue);
},
loaded : function(handler){
/// <summary>
/// 当文档内容加载完毕的时候,所执行的函数。
/// </summary>
/// <param name="handler" type="Function">处理函数。</param>
var docTarget = new EventTargetList([document]);
docTarget.attach({
DOMContentLoaded : function(){
handler.apply(this, arguments);
docTarget.detach({ DOMContentLoaded : arguments.callee });
}
});
return this;
}
});
return DOM;
}(
this.EventTargetList,
document,
parseFloat
));
this.NodeList = (function(EventTargetList, DOCUMENT_POSITION_SAME, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_FOLLOWING_AND_CONTAINED_BY){
function NodeList(_list){
/// <summary>
/// 节点列表类。
/// </summary>
/// <param name="_list" type="Array, List">初始化时所拥有的列表集合。</param>
};
NodeList = new Class(NodeList, EventTargetList.prototype);
NodeList.override({
createList : function(_list){
/// <summary>
/// 创建个新的节点集合。
/// </summary>
/// <param name="_list" type="Array, List">初始化时所拥有的列表集合。</param>
return new NodeList.constructor(_list);
}
});
NodeList.props({
appendTo : function(parentNode){
/// <summary>
/// 将集合中所有节点添加至指定的父节点。
/// </summary>
/// <param name="parentNode" type="Node">指定的父节点。</param>
this.insertTo(parentNode);
return this;
},
contains : function(childNode){
/// <summary>
/// 判断指定节点是否是该集合中某个节点的后代节点。
/// </summary>
/// <param name="childNode" type="Node">指定的节点。</param>
return !this.every(function(node){
return [
DOCUMENT_POSITION_SAME,
DOCUMENT_POSITION_CONTAINED_BY,
DOCUMENT_POSITION_FOLLOWING_AND_CONTAINED_BY
].indexOf(
node.compareDocumentPosition(childNode)
) === -1;
});
},
get : function(name){
/// <summary>
/// 获取集合中第一个节点的节点属性。
/// </summary>
/// <param name="name" type="String">属性名。</param>
if(
this.length === 0
){
return null;
}
return this[0][name];
},
insertAfter : function(targetNode){
/// <summary>
/// 将集合中所有节点插入至指定的节点之后。
/// </summary>
/// <param name="targetNode" type="Node">指定节点。</param>
var sibling = targetNode.nextElementSibling;
if(
sibling
){
this.insertBefore(sibling);
}
else {
this.appendTo(targetNode.parentNode);
}
return this;
},
insertBefore : function(targetNode){
/// <summary>
/// 将集合中所有节点插入至指定的节点之前。
/// </summary>
/// <param name="targetNode" type="Node">指定节点。</param>
this.forEach(
function(node){
this.insertBefore(node, targetNode);
},
targetNode.parentNode
);
return this;
},
insertTo : function(parentNode, _idx){
/// <summary>
/// 将集合中所有节点插入至指定索引的节点之前。
/// </summary>
/// <param name="parentNode" type="Node">指定的父节点。</param>
/// <param name="_idx" type="Number">指定节点的索引值。</param>
if(
typeof _idx !== "undefined"
){
var childNodes = parentNode.childNodes;
if(
childNodes.length > 0
){
return this.insertBefore(childNodes[_idx]);
}
}
this.forEach(function(node){
parentNode.appendChild(node);
});
return this;
},
remove : function(){
/// <summary>
/// 将集合中的节点从其父节点内移除。
/// </summary>
this.forEach(function(node){
var parentNode = node.parentNode;
if(
parentNode
){
parentNode.removeChild(node);
}
});
return this;
},
replace : function(targetNode){
/// <summary>
/// 将集合中所有节点去替换指定的节点。
/// </summary>
/// <param name="targetNode" type="Node">指定的节点。</param>
this.insertBefore(targetNode);
this.remove.call([targetNode]);
return this;
},
set : function(name, value){
/// <summary>
/// 设置集合中所有节点的属性。
/// </summary>
/// <param name="name" type="String">属性名。</param>
/// <param name="value" type="*">属性值。</param>
this.forEach(function(element){
element[name] = value;
});
return this;
}
});
NodeList.props({
textContent : {
get : function(){
return this.get("textContent");
},
set : function(value){
this.set("textContent", value);
}
}
}, { gettable : true, settable : true });
return NodeList.constructor;
}(
this.EventTargetList,
// DOCUMENT_POSITION_SAME
0,
// DOCUMENT_POSITION_CONTAINED_BY
16,
// DOCUMENT_POSITION_FOLLOWING_AND_CONTAINED_BY
20
));
this.ElementList = (function(NodeList, Node, ID, selectorText, document){
function ElementList(_selector, _parent){
/// <summary>
/// 通过指定选择器筛选元素。
/// </summary>
/// <param name="_selector" type="String, Element">选择器或dom元素。</param>
/// <param name="_parent" type="Element">指定查询的父节元素。</param>
if(
!_selector
){
return;
}
this.assign(
{ selector : _selector }
);
if(
typeof _selector === "string"
){
var parent = _parent || document;
if(
parent.id === ""
){
parent.setAttribute("id", ID);
this.combine(
parent.querySelectorAll(
selectorText.replace(["#", ID, _selector])
)
);
parent.removeAttribute("id");
}
else {
this.combine(
parent.querySelectorAll(
selectorText.replace(
parent === document ? ["", "", _selector] : ["#", parent.id, _selector]
)
)
);
}
return;
}
if(
_selector instanceof Node || _selector instanceof Window
){
if(
_parent
){
if(
_parent.contains(_selector) === false
){
return;
}
}
this.push(_selector);
return;
}
if(
"length" in _selector
){
this.combine(_selector);
return;
}
};
ElementList = new Class(ElementList, NodeList.prototype);
ElementList.override({
createList : function(_selector, _parent){
/// <summary>
/// 创建个新的元素集合。
/// </summary>
/// <param name="_selector" type="String, Element">选择器、html或dom元素。</param>
/// <param name="_parent" type="Element">指定查询的父节点。</param>
return new ElementList.constructor(_selector, _parent);
}
});
ElementList.props({
blur : function(){
/// <summary>
/// 让聚焦元素的失去焦点。
/// </summary>
this.forEach(function(element){
element.blur();
});
return this;
},
btw : function(_selector, _ancestor){
/// <summary>
/// 在该集合内的每一个元素与指定的祖先元素之间,查找其他符合条件的元素。
/// </summary>
/// <param name="_selector" type="String, Element">指定查找的祖先元素选择器。</param>
/// <param name="_ancestor" type="Element">指定的一个祖先元素。</param>
var list = [], elements = this.createList(_selector || "*", _ancestor);
elements.forEach(
function(element){
this.every(function(elem){
if(
element.contains(elem)
){
list.push(element);
return false;
}
return true;
});
},
this
);
elements.splice(0);
elements.combine(list);
return elements;
},
del : function(name){
/// <summary>
/// 将指定属性从集合的所有元素中删除。
/// </summary>
/// <param name="name" type="String">需要删除的属性名。</param>
this.forEach(function(element){
delete element[name];
});
return this;
},
focus : function(){
/// <summary>
/// 聚焦元素。
/// </summary>
var length = this.length;
if(
length > 0
){
this[length - 1].focus();
}
return this;
},
getAttribute : function(name){
/// <summary>
/// 获取集合中第一个元素的特性属性。
/// </summary>
/// <param name="name" type="String">属性名。</param>
return AttributesCollection.prototype.get.call({ sources : this }, name);
},
getData : function(name){
/// <summary>
/// 获取集合中第一个元素的数据值。
/// </summary>
/// <param name="name" type="String">数据名。</param>
return DatasetCollection.prototype.get.call({ sources : this }, name);
},
isBtw : function(_selector, _ancestor){
/// <summary>
/// 判断在该集合内的每一个元素与指定的祖先元素之间,是否能查找到其他符合条件的元素。
/// </summary>
/// <param name="_selector" type="String, Element">指定查找的祖先元素选择器。</param>
/// <param name="_ancestor" type="Element">指定的一个祖先元素。</param>
return this.btw.apply(this, arguments).length > 0;
},
parent : function(){
/// <summary>
/// 返回该集合所有元素的父元素。
/// </summary>
var list = this.createList();
this.forEach(function(element){
var parent = element.parentElement;
if(!parent || list.contains(parent))
return;
list.push(parent);
});
return list;
},
query : function(_selector){
/// <summary>
/// 通过选择器查找子孙元素。
/// </summary>
/// <param name="_selector" type="String, Element">选择器。</param>
var source = ElementList.constructor.source, list = this.createList();
this.forEach(function(Element){
source.call(list, _selector, Element);
});
if(
this.length < 2
){
return list;
}
return list.distinct();
},
removeAttribute : function(name){
/// <summary>
/// 根据指定名称,移除集合中每一个元素的特性属性。
/// </summary>
/// <param name="name" type="String">属性名。</param>
AttributesCollection.prototype.remove.call({ sources : this }, name);
return this;
},
removeData : function(name){
/// <summary>
/// 根据指定名称,移除集合中每一个元素的数据。
/// </summary>
/// <param name="name" type="String">数据名。</param>
DatasetCollection.prototype.remove.call({ sources : this }, name);
return this;
},
selector : "",
setAttribute : function(name, _value){
/// <summary>
/// 设置集合中每一个元素的特性属性。
/// </summary>
/// <param name="name" type="String">属性名。</param>
/// <param name="_value" type="String">属性值。</param>
AttributesCollection.prototype.set.call({ sources : this }, name, _value);
return this;
},
setData : function(name, _value){
/// <summary>
/// 设置集合中每一个元素的数据值。
/// </summary>
/// <param name="name" type="String">数据名。</param>
/// <param name="_value" type="String">数据值。</param>
DatasetCollection.prototype.set.call({ sources : this }, name, _value);
return this;
}
});
ElementList.props({
attributes : {
get : function(){
/// <summary>
/// 获取属性集合。
/// </summary>
return new AttributesCollection(this);
},
set : function(attrs){
/// <summary>
/// 设置属性。
/// </summary>
/// <param name="attrs" type="Object">属性键值对。</param>
var elementList = this;
forEach(
attrs,
function(value, name){
elementList.setAttribute(name, value);
}
);
}
},
children : {
get : function(){
/// <summary>
/// 获取子元素集合。
/// </summary>
return new ChildrenCollection(this);
},
set : function(elements){
/// <summary>
/// 移除所有现有子元素,添加指定的子元素。
/// </summary>
/// <param name="elements" type="Array, jQun.NodeList">需要添加的子元素集合。</param>
if(
this.length === 0
){
return;
}
this.children.remove();
this.appendTo.call(elements, this[0]);
}
},
dataset : {
get : function(){
/// <summary>
/// 获取数据集合。
/// </summary>
return new DatasetCollection(this);
},
set : function(data){
/// <summary>
/// 设置数据。
/// </summary>
/// <param name="data" type="Object">数据键值对。</param>
var elementList = this;
forEach(
data,
function(value, name){
elementList.setData(name, value);
}
);
}
}
}, { gettable : true, settable : true });
forEach(
["header", "article", "section", "footer"],
function(name){
var property = {};
property[name] = {
get : function(){
return this.query(">" + name);
},
set : function(element){
this.createList(element).replace(this[name][0]);
}
};
ElementList.props(property, this);
},
{ gettable : true, settable : true }
);
return ElementList.constructor;
}(
this.NodeList,
Node,
// ID
"__jQun__",
// selectorText
new Text("{0}{1} {2}"),
document
));
this.HTMLElementList = (function(ElementList, getCSSNumberValue, getComputedStyle, addProperty){
function HTMLElementList(_selector, _parent){
/// <summary>
/// 通过指定选择器筛选HTML元素。
/// </summary>
/// <param name="_selector" type="String, Element">选择器或dom元素。</param>
/// <param name="_parent" type="Element">指定查询的父元素。</param>
};
HTMLElementList = new Class(HTMLElementList, ElementList.prototype);
forEach(
[
"className", "hidden", "href",
"id", "innerHTML", "src",
"tabIndex", "title", "value"
],
addProperty,
HTMLElementList
);
HTMLElementList.override({
createList : function(_selector, _parent){
/// <summary>
/// 创建个新的HTML元素集合。
/// </summary>
/// <param name="_selector" type="String, Element">选择器或dom元素。</param>
/// <param name="_parent" type="Element">指定查询的父元素。</param>
return new HTMLElementList.constructor(_selector, _parent);
}
});
HTMLElementList.props({
classList : {
get : function(){
/// <summary>
/// 获取class列表集合。
/// </summary>
return new ClassListCollection(this);
},
set : function(className){
/// <summary>
/// 设置集合中所有元素的class属性。
/// </summary>
/// <param name="className" type="String">需要设置的class字符串。</param>
this.set("className", className);
}
},
height : {
get : function(){
/// <summary>
/// 获取集合中每一个元素的高。
/// </summary>
return this.getMetrics("height");
},
set : function(height){
/// <summary>
/// 设置集合中每一个元素的高。
/// </summary>
/// <param name="height" type="String, Number">元素的高。</param>
this.setMetrics("height", height);
}
},
style : {
get : function(){
/// <summary>
/// 获取style属性集合。
/// </summary>
return new CSSPropertyCollection(this);
},
set : function(cssText){
/// <summary>
/// 设置集合中每一个元素的style属性。
/// </summary>
/// <param name="cssText" type="String">需要设置的style属性字符串。</param>
AttributesCollection
.prototype
.set
.call(
{ sources : this },
"style",
cssText
);
}
},
width : {
get : function(){
/// <summary>
/// 获取集合中每一个元素的宽。
/// </summary>
return this.getMetrics("width");
},
set : function(width){
/// <summary>
/// 设置集合中每一个元素的宽。
/// </summary>
/// <param name="width" type="String, Number">元素的宽。</param>
this.setMetrics("width", width);
}
}
}, { gettable : true, settable : true });
HTMLElementList.props({
getCSSPropertyValue : function(name){
/// <summary>
/// 获取集合中第一个元素的css属性。
/// </summary>
/// <param name="name" type="String">属性名。</param>
if(
this.length === 0
){
return "";
}
return getComputedStyle(this[0])[name];
},
getMetrics : function(name){
/// <summary>
/// 获取元素指定盒模型属性值。
/// </summary>
/// <param name="name" type="String">盒模型属性名称。</param>
return getCSSNumberValue(
this.getCSSPropertyValue(name)
);
},
hasTag : function(tagName){
/// <summary>
/// 判断集合类是否含有指定标签名称的元素。
/// </summary>
/// <param name="tagName" type="String">标签名称。</param>
tagName = tagName.toUpperCase();
return !this.every(function(htmlElement){
return htmlElement.tagName !== tagName;
});
},
hide : function(){
/// <summary>
/// 隐藏元素。
/// </summary>
return this.setAttribute("hidden", "");
},
rect : function(_name){
/// <summary>
/// 获取第一个元素的客户端属性。
/// </summary>
/// <param name="_name" type="String">需要只返回单个属性值的属性名称。</param>
if(
this.length === 0
){
return _name ? 0 : null;
}
var rect = this[0].getBoundingClientRect();
return _name in rect ? rect[_name] : rect;
},
setCSSPropertyValue : function(name, value){
/// <summary>
/// 设置集合中每一个元素的css属性。
/// </summary>
/// <param name="name" type="String">属性名。</param>
/// <param name="value" type="String">属性值。</param>
this.forEach(function(element){
element.style[name] = value;
});
return this;
},
setMetrics : function(name, value){
/// <summary>
/// 设置元素指定盒模型属性值。
/// </summary>
/// <param name="name" type="String">盒模型属性名称。</param>
/// <param name="value" type="String, Number">盒模型属性值。</param>
if(
typeof value === "number"
){
value += "px";
}
this.setCSSPropertyValue(name, value);
return this;
},
show : function(){
/// <summary>
/// 显示元素。
/// </summary>
/// <param name="_display" type="String">修改元素display的css值。</param>
return this.removeAttribute("hidden");
},
toggle : function(){
/// <summary>
/// 显示或隐藏元素。
/// </summary>
return this[this.getAttribute("hidden") === null ? "hide" : "show"]();
}
});
return HTMLElementList.constructor;
}(
this.ElementList,
this.DOM.getCSSNumberValue,
getComputedStyle,
// addProperty
function(name){
define(this, name, {
get : function(){
return this.get(name);
},
set : function(value){
this.set(name, value);
}
}, { gettable : true, settable : true });
}
));
}.call(
this,
this.List,
this.AttributesCollection,
this.CSSPropertyCollection,
this.ChildrenCollection,
this.ClassListCollection,
this.DatasetCollection,
this.Text,
// Window
window.constructor,
forEach,
jQun.define
));
// 与事件有关
(function(EventTargetList, document, dispatch, set){
this.Event = (function(generate){
function Event(type, _baseType, _initEventArgs){
/// <summary>
/// DOM事件类。
/// </summary>
/// <param name="type" type="String">用户自定义事件类型。</param>
/// <param name="_baseType" type="String">事件基础类型(MouseEvent、UIEvent、WheelEvent等)。</param>
/// <param name="_initEventArgs" type="Array">初始化事件的其他参数列表。</param>
this.assign({
initEventArgs : [type].concat(_initEventArgs || [true, true]),
type : type,
baseType : _baseType
});
};
Event = new Class(Event);
Event.props({
baseType : "Event",
initEventArgs : null,
trigger : function(eventTarget, _attrs){
/// <summary>
/// 触发事件。
/// </summary>
/// <param name="eventTarget" type="EventTarget">触发该事件的目标对象。</param>
/// <param name="_attrs" type="Object">附加的属性键值对。</param>
EventTargetList
.prototype
.dispatch
.call(
[eventTarget],
generate(
this.baseType,
this.initEventArgs,
_attrs
)
);
/* 为了兼容chrome 冒泡bug,启用上面(虽然性能耗的比较多),直到chrome修复bug
dispatch.call(
[eventTarget],
generate(
this.baseType,
this.initEventArgs,
_attrs
)
);
*/
return this;
},
type : ""
});
return Event.constructor;
}(
// generate
function(baseType, initEventArgs, _attrs){
var event = document.createEvent(baseType);
event[
"init" + baseType
].apply(
event,
initEventArgs
);
return set(event, _attrs);
}
));
}.call(
this,
this.EventTargetList,
document,
this.EventTargetList.prototype.dispatch,
jQun.set
));
// 自定义事件相关
(function(HTMLElementList, Event, window, document){
this.ReposcursorEvent = (function(Browser, BrowserAgents, reposcursorEvent, selection){
function ReposcursorEvent(){
/// <summary>
/// 创建游标重新定位事件。
/// </summary>
// 简单的模拟,为firefox创建selectionchange事件
forEach(
1,
function(){
new HTMLElementList(
document
).attach(
[
BrowserAgents.Firefox,
BrowserAgents.MSIE
].indexOf(
Browser.agent
) > -1 ?
{
mouseup : this,
keyup : this
} :
{
selectionchange : this
}
);
},
function(e){
if(
selection.rangeCount === 0
){
return;
}
var range = selection.getRangeAt(0);
reposcursorEvent.trigger(
e.type === "selectionchange" ? range.commonAncestorContainer : e.target,
{ range : range }
);
}
);
};
ReposcursorEvent = new StaticClass(ReposcursorEvent);
return ReposcursorEvent;
}(
this.Browser,
this.BrowserAgents,
// reposcursorEvent
new Event("reposcursor"),
// selection
document.getSelection()
));
this.UserclickEvent = (function(){
function UserclickEvent(){
/// <summary>
/// 生成userclick事件,防止安卓点击不灵而且出现蓝框的问题。
/// </summary>
/*
安卓上,元素只要监听了click事件,浏览器会自动在点击的时候出现蓝框,
不仅有时点击不灵,而且非常难看。
此静态类目的就是解决这2个问题,ps:无法对button和a标签消除蓝框。
*/
new HTMLElementList(
window
)
.attach({
click : function(e){
new Event(
"userclick",
"MouseEvent",
[
e.bubbles, e.cancelable,
window, e.detail,
e.screenX, e.screenY, e.clientX, e.clientY,
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
e.button, e.target
]
)
.trigger(
e.target
);
}
});
};
UserclickEvent = new StaticClass(UserclickEvent);
return UserclickEvent;
}());
this.ShakeEvent = (function(shakeEvent, abs){
function ShakeEvent(){
/// <summary>
/// 摇晃事件。
/// </summary>
var lastX = 0, lastY = 0, lastZ = 0, lastTime = 0;
new HTMLElementList(
window
)
.attach({
devicemotion : function(e){
var acceleration = e.accelerationIncludingGravity,
time = new Date().getTime(), diffTime = time - lastTime;
if(
diffTime > 50
){
lastTime = time;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
var offset = abs(x + y + z - lastX - lastY - lastZ) / diffTime * 10000;
if(
offset > 1500
){
   shakeEvent.trigger(
window,
{
offset : offset
}
);
}
lastX = x;
lastY = y;
lastZ = z;
}
}
});
};
ShakeEvent = new StaticClass(ShakeEvent);
return ShakeEvent;
}(
// shakeEvent
new Event("shake"),
Math.abs
));
}.call(
this,
this.HTMLElementList,
this.Event,
window,
document
));
// 与模板相关的类
(function(HTMLElementList, Event, Text, DOM, JSON, document){
this.HTML = (function(Function, SPACE_REGEXP, FOR_REGEXP, WORD_TEXT, replace, toArray, console){
function HTML(template){
/// <summary>
/// html模板。
/// </summary>
/// <param name="template" type="String, HTMLScriptElement">html模板源字符串或script、noscript标签。</param>
this.assign({
template : [
"with(this){ return (function(){ this.push('",
// 使用Text类的replace替换参数
replace.call({
text : (typeof template === "string" ? template : template.textContent)
// 给单引号加保护
.split("'")
.join("\\'")
// 替换掉特殊的空白字符
.replace(
SPACE_REGEXP,
""
)
// 替换for循环
.replace(
FOR_REGEXP,
function(str, condition, i){
return [
"');forEach(",
condition
// 将循环参数内的单引号去保护
.split("\\'")
.join("'")
// 替换左大括号
.split("{")
.join("\t")
// 替换右大括号
.split("}")
.join("\n"),
", function(" + (i || "") + ")\t this.push('"
].join("");
}
)
}, function(str, modifier, word){
if(
modifier === ":"
){
return "\t" + word + "\n";
}
if(
modifier === ">"
){
return "');console.log(" + word + ");this.push('";
}
return replace.call(
{ text : WORD_TEXT },
{ word : word, shouldEmpty : modifier === "~" }
);
})
// 替换for循环的结束标识“}”
.split("}")
.join("');}, this);this.push('")
// 替换临时产生的大括号
.split("\t")
.join("{")
.split("\n")
.join("}"),
"');return this.join('');}.call([])); }"
].join("")
});
};
HTML = new Class(HTML);
HTML.props({
create : function(_data, _shouldReturnArray){
/// <summary>
/// 将模板转化为html节点。
/// </summary>
/// <param name="data" type="Object, Array">需要渲染的数据。</param>
/// <param name="_shouldReturnArray" type="Boolean">是否以普通数组形式返回节点。</param>
var parent = document.createElement("div");
parent.innerHTML = this.render(_data);
if(
_shouldReturnArray
){
var childNodes = toArray(parent.childNodes);
parent.textContent = "";
return childNodes;
}
var htmlElementList = new HTMLElementList("");
htmlElementList.combine(parent.childNodes);
htmlElementList.remove();
return htmlElementList;
},
render : function(_data){
/// <summary>
/// 渲染模板,生成html字符串。
/// </summary>
/// <param name="_data" type="Object, Array">需要渲染的数据。</param>
return new Function(
"forEach",
"console",
this.template
).call(
_data || {},
forEach,
console
);
},
template : ""
});
return HTML.constructor;
}(
Function,
// SPACE_REGEXP => space(查找特殊的空白字符)
/[\r\t\n]/g,
// FOR_REGEXP => for(查找for语句)
/@for\s*\(([\s\S]+?)(?:\s*->>\s*([\s\S]+?))*?\)\s*\{/g,
// WORD_TEXT
"');this.push(typeof ({word}) === 'undefined' ? ({shouldEmpty} ? '' : '{word}') : {word});this.push('",
Text.prototype.replace,
jQun.toArray,
console
));
this.CHTML = (function(componentreadyEvent, selectorText, array, contentRegx, parse){
function CHTML(){
/// <summary>
/// 组件html模板(Conponent HTML):如果里面嵌入了JSON字符串,那么将其转化为JSON数据,作为参数传入类的构造函数。
/// </summary>
array.indexOf = function(name){
var i = 0;
this.every(function(item, n){
if(
item.name === name
){
i = n;
return false;
}
return true;
});
return i;
};
DOM.loaded(
function(){
new HTMLElementList(
array
.map(function(item){
return selectorText.replace(item);
})
.join(", ")
).forEach(function(script, i){
var scriptList = new HTMLElementList(script),
result = scriptList.textContent.match(contentRegx);
new array[
array.indexOf(
new HTMLElementList(script).getData("name")
)
]
.Class(
result === null ? null : parse(result[1])
)
.replace(
script
)
.dispatch(
componentreadyEvent
);
});
},
true
);
};
CHTML = new StaticClass(CHTML);
CHTML.props({
bind : function(Class, name){
/// <summary>
/// 绑定模板的类与名称。
/// </summary>
/// <param name="Class" type="Class">需要绑定的类。</param>
/// <param name="name" type="String">需要绑定的名称。</param>
array.push({ Class : Class, name : name });
return this;
}
});
return CHTML;
}(
// componentreadyEvent
new Event("componentready"),
// selectorText
new Text([
'script[type="text/chtml"][data-name="{name}"]',
'noscript[type="text/chtml"][data-name="{name}"]'
].join(",")),
// array
[],
// contentRegx
/^\s*(\S[\s\S]*?)\s*$/,
JSON.parse
));
this.SHTML = (function(HTML){
function SHTML(){
/// <summary>
/// 静态html模板(Static HTML)。
/// </summary>
DOM.loaded(
function(){
new HTMLElementList(
[
'script[type="text/shtml"]:not(:empty)',
'noscript[type="text/shtml"]:not(:empty)'
].join(",")
).forEach(function(htmlElement, i){
new HTML(
htmlElement.textContent
)
.create()
.replace(
htmlElement
);
});
},
true
);
};
SHTML = new StaticClass(SHTML);
return SHTML;
}(
this.HTML
));
}.call(
this,
this.HTMLElementList,
this.Event,
this.Text,
this.DOM,
this.JSON,
document
));
// 与重写有关
(function(EventTargetList, Event, HTMLElementList, HTML, Browser, BrowserAgents, document, dispatch){
this.OverrideDispatch = (function(div, contains){
function OverrideDispatch(){
/// <summary>
/// 使EventTargetList的dispatch函数能够支持jQun.Event参数。
/// </summary>
EventTargetList
.prototype
.override({
dispatch : function(event, _attrs){
/// <summary>
/// 给所有目标分配事件。
/// </summary>
/// <param name="event" type="Event, jQun.Event">事件。</param>
/// <param name="_attrs" type="Object">如果第一个参数是jQun.Event实例,则该参数为附加的属性键值对,否则该参数形同虚设。</param>
if(
event instanceof Event
){
this.forEach(function(eventTarget){
event.trigger(eventTarget, _attrs);
});
return this;
}
/*
2014.5.26 chrome bug:
因为chrome的捕获事件必须由文档开始;
所以未添加到文档中的元素,不会有事件冒泡,IE、FF正常。
*/
if(
Browser.agent !== BrowserAgents.Chrome
){
dispatch.call(this, event);
return this;
}
// 如果chrome修复了bug 把上面的判断去掉 以下都可以删除了
this.forEach(function(eventTarget){
var body = document.body;
if(
!contains.call([body], eventTarget)
){
var element = eventTarget.parentElement;
if(
element
){
while(
element.parentElement
){
element = element.parentElement;
}
div.appendChild(element);
body.appendChild(div);
dispatch.call([eventTarget], event);
div.innerHTML = "";
body.removeChild(div);
return;
}
}
dispatch.call([eventTarget], event);
});
return this;
}
});
};
OverrideDispatch = new StaticClass(OverrideDispatch);
return OverrideDispatch;
}(
// div
new HTML(
'<div style="display:none !important;"></div>'
)
.create(
null,
true
)[0],
HTMLElementList.prototype.contains
));
}.call(
this,
this.EventTargetList,
this.Event,
this.HTMLElementList,
this.HTML,
this.Browser,
this.BrowserAgents,
document,
this.EventTargetList.prototype.dispatch
));
// 与数据有关的类
(function(JSON, window){
this.Storage = (function(hasOwnProperty){
function Storage(_data){
/// <summary>
/// 储存空间。一旦页面刷新,数据全部清空。
/// </summary>
/// <param name="_data" type="Object">初始化的数据</param>
if(
!_data
){
return;
}
forEach(
_data,
function(value, key){
this.set(key, value);
},
this
);
};
Storage = new Class(Storage);
Storage.props({
clear : function(){
/// <summary>
/// 清空所有储存数据。
/// </summary>
forEach(
this,
function(value, key){
this.del(key);
},
this
);
return this;
},
del : function(key){
/// <summary>
/// 删除一项储存数据。
/// </summary>
/// <param name="key" type="String">数据主键。</param>
if(
hasOwnProperty.call(this, key)
){
delete this[key];
return true;
}
return false;
},
get : function(key){
/// <summary>
/// 获取数据。
/// </summary>
/// <param name="key" type="String">数据主键。</param>
return this[key];
},
set : function(key, value){
/// <summary>
/// 设置数据。
/// </summary>
/// <param name="key" type="String">数据主键。</param>
/// <param name="value" type="*">数据值。</param>
this[key] = value;
return this;
}
});
return Storage.constructor;
}(
Object.prototype.hasOwnProperty
));
this.ICache = (function(){
return new Interface([ "type" ]);
}());
this.Cache = (function(ICache, Storage, storages, get, set, remove){
function Cache(name){
/// <summary>
/// 缓存数据。
/// </summary>
/// <param name="name" type="String">缓存数据的标识名称。</param>
var key = this.type + "-" + name, storage = storages[key];
if(
!storage
){
storage = storages[key] = new Storage(
get(
this.type,
name
)
);
}
this.assign({
name : name,
storage : storage
});
};
Cache = new Class(Cache, ICache);
Cache.props({
clear : function(){
/// <summary>
/// 删除所有缓存数据。
/// </summary>
this.storage.clear();
remove(this.type, this.name);
return this;
},
del : function(key){
/// <summary>
/// 删除某一条缓存数据。
/// </summary>
/// <param name="key" type="String">缓存数据的主键。</param>
this.storage.del(key);
this.sync();
return this;
},
get : function(key){
/// <summary>
/// 获取某一条缓存数据。
/// </summary>
/// <param name="key" type="String">缓存数据的主键。</param>
return this.storage.get(key);
},
name : "",
set : function(key, value){
/// <summary>
/// 设置某一条缓存数据。
/// </summary>
/// <param name="key" type="String">缓存数据的主键。</param>
/// <param name="value" type="*">缓存数据的值。</param>
this.storage.set(key, value);
this.sync();
return this;
},
storage : null,
sync : function(){
/// <summary>
/// 同步到浏览器缓存中。
/// </summary>
set(this.type, this.name, this.storage);
return this;
}
});
return Cache.constructor;
}(
this.ICache,
this.Storage,
// storages
{},
// get
function(type, name){
return JSON.parse(window[type + "Storage"].getItem(name));
},
// set
function(type, name, storage){
window[type + "Storage"].setItem(name, JSON.stringify(storage));
},
// remove
function(type, name){
window[type + "Storage"].removeItem(name);
}
));
this.LocalCache = (function(Cache){
function LocalCache(name){
/// <summary>
/// 本地缓存数据,只要不清空浏览器数据,就算浏览器关闭重启,也一直存在。无法跨域存储。
/// </summary>
/// <param name="name" type="String">缓存数据的标识名称。</param>
};
LocalCache = new Class(LocalCache, Cache.prototype);
LocalCache.props({
type : "local"
});
return LocalCache.constructor;
}(
this.Cache
));
this.SessionCache = (function(Cache){
function SessionCache(name){
/// <summary>
/// 创建一个会话缓存。只要所有相关页面不关闭,数据一直存在。
/// </summary>
/// <param name="name" type="String">缓存数据的标识名称。</param>
};
SessionCache = new Class(SessionCache, Cache.prototype);
SessionCache.props({
type : "session"
});
return SessionCache.constructor;
}(
this.Cache
));
}.call(
this,
this.JSON,
window // iphone safari,出于安全考虑,不能将sessionStorage赋予给其他变量..
));
// 与ajax相关的类
(function(JSON, XMLHttpRequest, forEach, set){
this.ConnectionMethods = (function(){
return new Enum(
["Get", "Post"]
);
}());
this.ResponseTypes = (function(){
return new Enum(
["Text", "Json", "ArrayBuffer", "Document", "Blob"]
);
}());
this.RequestConnection = (function(ConnectionMethods, ResponseTypes, implements, getEncodedParams){
function RequestConnection(name, url, _Interface, _method, _responseType, _key){
/// <summary>
/// ajax请求连接。
/// </summary>
/// <param name="name" type="String">请求名称字符串,在后台可以通过名称关键字获取。</param>
/// <param name="url" type="String">连接url。</param>
/// <param name="_Interface" type="Interface">发送数据的接口规范。</param>
/// <param name="_method" type="ConnectionMethods">发送数据的方式。</param>
/// <param name="_responseType" type="ResponseTypes">返回的数据格式。</param>
/// <param name="_key" type="String">名称关键字,默认:__request__。</param>
this.assign({
Interface : _Interface,
name : name,
responseType : _responseType,
method : _method,
url : url,
key : _key
});
};
RequestConnection = new Class(RequestConnection);
RequestConnection.props({
Interface : null,
create : function(urlQueryString, _formData, _load, _error, _beforeSend){
/// <summary>
/// 创建ajax连接。
/// </summary>
/// <param name="urlQueryString" type="String">url查询参数字符串。</param>
/// <param name="_formData" type="FormData">需要上传的formData。</param>
/// <param name="_load" type="Function">异步完成后所执行的回调函数。</param>
/// <param name="_error" type="Function">异步报错所执行的回调函数。</param>
/// <param name="_beforeSend" type="Function">在发送请求之前的回调函数。</param>
var request = new XMLHttpRequest(), responseType = this.responseType;
request.open(
ConnectionMethods.getNameByValue(
_formData ? ConnectionMethods.Post : this.method,
true
),
this.url + (urlQueryString ? "?" + urlQueryString : ""),
true
);
try{
// QQ浏览器下设置json等其他不支持的type,会报错
request.responseType = ResponseTypes.getNameByValue(responseType, true);
}
catch(e){}
request.addEventListener(
"load",
function(){
var response = this.response;
if(
this.status !== 200
){
if(
_error
){
_error(this.status);
}
return;
}
if(
!_load
){
return;
}
if(
responseType === ResponseTypes.Json
){
// ie不能设置json,需要转换
if(
typeof response === "string"
){
response = JSON.parse(response);
}
}
_load(response);
}
);
if(
_beforeSend
){
_beforeSend(request);
}
request.send(_formData);
return request;
},
key : "__request__",
method : ConnectionMethods.Get,
name : "",
open : function(params, _load, _error, _beforeSend){
/// <summary>
/// 开打一个ajax连接。
/// </summary>
/// <param name="params" type="Object">url的替换参数及post方法的传递参数。</param>
/// <param name="_load" type="Function">异步完成后所执行的回调函数。</param>
/// <param name="_error" type="Function">异步报错所执行的回调函数。</param>
/// <param name="_beforeSend" type="Function">在发送请求之前的回调函数。</param>
implements(this.Interface, params);
if(
this.method === ConnectionMethods.Post
){
var formData = new FormData();
forEach(
params,
function(value, name){
formData.append(name, value);
}
);
return this.create(
getEncodedParams(this.key, this.name),
formData,
_load,
_error,
_beforeSend
);
}
return this.create(
getEncodedParams(this.key, this.name, params),
null,
_load,
_error,
_beforeSend
);
},
responseType : ResponseTypes.Json,
upload : function(blob, _load, _error, _beforeSend, _progress){
/// <summary>
/// 使用ajax上传文件。
/// </summary>
/// <param name="blob" type="Blob">需要上传的二进制数据,一般是文件。</param>
/// <param name="_load" type="Function">异步完成后所执行的回调函数。</param>
/// <param name="_error" type="Function">异步报错所执行的回调函数。</param>
/// <param name="_beforeSend" type="Function">在发送请求之前的回调函数。</param>
/// <param name="_progress" type="Function">数据上传进度回调函数。</param>
var formData = new FormData();
formData.append("blob", blob);
return this.create(
getEncodedParams(this.key, this.name),
formData,
_load,
_error,
_progress ?
function(request){
request.addEventListener(
"progress",
function(e){
var loaded = e.loaded, total = e.total;
_progress(
(loaded / total).toFixed(2) - 0,
loaded,
total
);
}
);
if(
!_beforeSend
){
return;
}
_beforeSend(request);
} :
_beforeSend
);
},
url : ""
});
return RequestConnection.constructor;
}(
this.ConnectionMethods,
this.ResponseTypes,
// implements
function(Interface, params){
if(
!Interface
){
return;
}
new (
new Class(
null,
Interface
)
.props(params)
.constructor
);
},
// getEncodedParams
function(key, name, _params){
var params = set({}, _params);
params[key] = name;
return JSON.stringifyAddressParameters(params);
}
));
this.Ajax = (function(RequestConnection, Storage, Blob, ResponseTypes, console, location){
function Ajax(){
/// <summary>
/// ajax异步类。
/// </summary>
if(
!!XMLHttpRequest
){
this.enabled = true;
return;
}
console.warn("当前浏览器不支持XMLHttpRequest。");
};
Ajax = new StaticClass(Ajax, { enabled : false });
Ajax.props({
beginTesting : function(){
/// <summary>
/// 开始启动测试模式。
/// </summary>
this.isTesting = true;
return this;
},
defaultKey : "",
isTesting : false,
open : function(name, data, _load, _error, _beforeSend, _progress){
/// <summary>
/// 打开指定名称的请求连接。
/// </summary>
/// <param name="name" type="String">连接名称。</param>
/// <param name="data" type="Object, Blob">url的替换参数及post方法的传递参数 或者 二进制数据(一般是文件)。</param>
/// <param name="_load" type="Function">异步完成后所执行的回调函数。</param>
/// <param name="_error" type="Function">异步报错所执行的回调函数。</param>
/// <param name="_beforeSend" type="Function">在发送请求之前的回调函数。</param>
/// <param name="_progress" type="Function">数据上传进度回调函数。</param>
if(
!this.enabled
){
return;
}
var requestConnection = this.requestStorage.get(name);
if(
!requestConnection
){
throw 'Ajax请求信息错误:请检查连接名称"' + name + '"是否正确。';
return null;
}
if(
this.isTesting
){
if(
_load
){
_load(requestConnection.responseType === ResponseTypes.Json ? {} : "");
}
return null;
}
if(
data instanceof Blob
){
return requestConnection.upload(data, _load, _error, _beforeSend, _progress);
}
return requestConnection.open(data, _load, _error, _beforeSend);
},
requestStorage : new Storage(),
requestURLPrefix : [location.protocol, "", location.host, ""].join("/"),
save : function(allSettings){
/// <summary>
/// 存储请求连接信息。
/// </summary>
/// <param name="allSettings" type="Array">ajax连接信息。</param>
var key, requestStorage = this.requestStorage, requestURLPrefix = this.requestURLPrefix;
if(
this.defaultKey !== ""
){
key = this.defaultKey;
}
forEach(
allSettings,
function(settings){
var name = settings[0];
requestStorage.set(
name,
new RequestConnection(
name,
requestURLPrefix + settings[1],
settings[2],
settings[3],
settings[4],
key
)
);
}
);
return requestStorage;
},
setDefaultKey : function(key){
/// <summary>
/// 设置默认的请求名称关键字。
/// </summary>
/// <param name="key" type="String">名称关键字。</param>
this.defaultKey = key;
return this;
},
setRequestURLPrefix : function(urlPrefix){
/// <summary>
/// 设置请求连接地址的前缀。
/// </summary>
/// <param name="urlPrefix" type="String">地址前缀。</param>
this.requestURLPrefix = urlPrefix;
return this;
}
});
return Ajax;
}(
this.RequestConnection,
this.Storage,
Blob,
this.ResponseTypes,
console,
location
));
}.call(
this,
this.JSON,
XMLHttpRequest,
forEach,
jQun.set
));
// 与socket相关的类
(function(EventTargetList, Event, TYPE, window, attach){
this.PortList = (function(List){
function PortList(){
/// <summary>
/// 与socket相关的端口列表。
/// </summary>
};
PortList = new Class(PortList, List.prototype);
PortList.props({
getPortByName : function(portName){
/// <summary>
/// 通过端口名称获取端口。
/// </summary>
/// <param name="portName" type="String">端口名称。</param>
return this[this.indexOfName(portName)] || null;
},
indexOfName : function(portName){
/// <summary>
/// 通过名称检索端口。
/// </summary>
/// <param name="portName" type="String">端口名称。</param>
var index = -1;
this.every(function(port, i){
if(
port.name === portName
){
index = i;
return false;
}
return true;
});
return index;
}
});
return PortList.constructor;
}(
this.List
));
this.Socket = (function(portList){
function Socket(){
/// <summary>
/// 建立窗口之间的接口。
/// </summary>
};
Socket = new StaticClass(Socket);
Socket.props({
call : function(portName, value, _from){
/// <summary>
/// 发送信息给指定名称的端口。
/// </summary>
/// <param name="portName" type="String">端口名称。</param>
/// <param name="value" type="*">需要发送的数据值。</param>
/// <param name="_from" type="Port">信息来源。</param>
var port = portList.getPortByName(portName);
if(
!port
){
return false;
}
port.receive(value, _from);
return true;
},
close : function(port){
/// <summary>
/// 关闭指定的端口。
/// </summary>
/// <param name="port" type="Port">指定的端口。</param>
var index = portList.indexOfName(port.name);
if(
index > -1
){
portList.splice(index, 1);
}
return this;
},
exist : function(portName){
/// <summary>
/// 检查是否存在指定端口名称的端口。
/// </summary>
/// <param name="portName" type="String">端口名称。</param>
return portList.indexOfName(portName) > -1;
},
open : function(port){
/// <summary>
/// 在接口中开打指定端口。
/// </summary>
/// <param name="port" type="Port">指定的端口。</param>
var index = portList.indexOfName(port.name);
if(
index > -1
){
portList[index].stop();
}
portList.push(port);
return this;
}
});
return Socket;
}(
// portList
new this.PortList()
));
this.Port = (function(Socket, WINDOW_OBJECT_STRING, undefined, messageEvent, dispatch, set, toArray){
function Port(name, _eventTarget, _isArray){
/// <summary>
/// 通信端口。
/// </summary>
/// <param name="name" type="String">端口名称。</param>
/// <param name="_eventTarget" type="EventTarget">监听信息的目标。</param>
/// <param name="_isArray" type="Boolean">监听目标是否为数组。</param>
this.assign({
name : name
});
if(
_eventTarget
){
if(
_isArray
){
_eventTarget.forEach(
function(evenTarget){
if(evenTarget){
this.push(evenTarget);
}
},
this
);
}
else {
this.push(_eventTarget);
}
}
this.start();
};
Port = new Class(Port, EventTargetList.prototype);
Port.override({
attach : function(events, _capture, _priority, _useWeakReference){
/// <summary>
/// 向该端口中的所有目标附加事件监听。
/// </summary>
/// <param name="events" type="Object">事件侦听器键值对。</param>
/// <param name="_capture" type="Boolean">侦听器是否运行于捕获阶段。</param>
/// <param name="_priority" type="Number">优先级,数字越大,优先级越高。</param>
/// <param name="_useWeakReference" type="Boolean">是否是属于强引用。</param>
var func = events.message;
events = set({}, events);
if(
func
){
var port = this, name = this.name;
events.message = function(e){
var data = e.data;
if(
data
){
if(
data.type === TYPE
){
if(
data.name !== name
){
return;
}
}
}
else {
return;
}
func.call(this, e, func.length === 2 ? null : undefined);
};
}
return attach.call(this, events, _capture, _priority, _useWeakReference);
},
dispatch : function(event, _attrs){
/// <summary>
/// 给所有目标分配事件。
/// </summary>
/// <param name="event" type="Event, jQun.Event">需要分配的事件。</param>
/// <param name="_attrs" type="Object">附加的属性键值对。</param>
var list = [], data = _attrs ? _attrs.data : null;
this.forEach(
function(eventTarget){
if(
eventTarget.toString() === WINDOW_OBJECT_STRING
){
eventTarget.postMessage(data, "*");
return;
}
list.push(eventTarget);
}
);
return dispatch.call(list, event, _attrs);
}
});
Port.props({
enabled : false,
name : "",
receive : function(value, _from){
/// <summary>
/// 接收数据。
/// </summary>
/// <param name="value" type="*">数据值。</param>
/// <param name="_from" type="Port">信息来源。</param>
this.dispatch(
messageEvent,
{
data : {
from : _from ? _from.name : "",
name : this.name,
type : TYPE,
value : value
}
}
);
return this;
},
send : function(portName, value){
/// <summary>
/// 发送信息给指定名称的端口。
/// </summary>
/// <param name="portName" type="String">端口名称。</param>
/// <param name="value" type="*">需要发送的数据值。</param>
Socket.call(portName, value, this);
return this;
},
start : function(){
/// <summary>
/// 启动端口服务。
/// </summary>
if(
this.enabled
){
return this;
}
Socket.open(this);
this.enabled = true;
return this;
},
stop : function(){
/// <summary>
/// 停止端口服务。
/// </summary>
if(
!this.enabled
){
return this;
}
Socket.close(this);
this.enabled = false;
return this;
}
});
return Port.constructor;
}(
this.Socket,
// WINDOW_OBJECT_STRING
window.toString(),
undefined,
// messageEvent
new Event("message", "Event", [false, true]),
EventTargetList.prototype.dispatch,
jQun.set,
jQun.toArray
));
this.CrossWindowPort = (function(Port){
function CrossWindowPort(name, _window){
/// <summary>
/// 跨窗口通信的端口。
/// </summary>
/// <param name="name" type="String">端口名称。</param>
/// <param name="_window" type="Window">通信窗口。</param>
this.push(_window || window);
};
CrossWindowPort = new Class(CrossWindowPort, Port.prototype);
CrossWindowPort.override({
send : function(portName, value){
/// <summary>
/// 发送信息给该窗口内指定名称的端口。
/// </summary>
/// <param name="portName" type="String">端口名称。</param>
/// <param name="value" type="*">需要发送的数据值。</param>
this.forEach(
function(win){
win.postMessage(this, "*");
},
{
from : this.name,
name : portName,
type : TYPE,
value : value
}
);
return this;
}
});
return CrossWindowPort.constructor;
}(
this.Port
));
this.TransferPort = (function(CrossWindowPort, Socket){
function TransferPort(name, _window){
/// <summary>
/// 中转端口,主要用于接收其他窗体发送来的消息,并转送到指定端口。
/// </summary>
/// <param name="name" type="String">端口名称。</param>
/// <param name="_window" type="Window">中转消息的窗口。</param>
var transferPort = this;
attach.call(
this,
{
message : function(e){
var data = e.data, name = data.name;
if(
name === transferPort.name
){
return;
}
Socket.call(name, data.value, transferPort);
}
}
);
};
TransferPort = new Class(TransferPort, CrossWindowPort.prototype);
return TransferPort.constructor;
}(
this.CrossWindowPort,
this.Socket
));
}.call(
this,
this.EventTargetList,
this.Event,
// TYPE
"__jQun.Port__",
window,
this.EventTargetList.prototype.attach
));
defineProperties(jQun, this);
}(
jQun,
jQun.Class,
jQun.StaticClass,
jQun.Interface,
jQun.Enum,
jQun.defineProperties,
jQun.forEach
);

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/mic-pages-jQun.git
git@api.gitlife.ru:oschina-mirror/mic-pages-jQun.git
oschina-mirror
mic-pages-jQun
mic-pages-jQun
master