materio_flag.min.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. // * date 2011/10/19 *
  2. /**
  3. * misc
  4. */
  5. // ------ TRACE ------
  6. function trace(s,o){
  7. if(window.console && window.console.debug){
  8. arguments.length === 2 ? window.console.debug(s,o) : window.console.debug(s);
  9. }else if(console && console.log){
  10. arguments.length === 2 ? console.log(s,o) : console.log(s);
  11. }
  12. };
  13. // ------ LOADJSCSSFILE ------
  14. function loadjscssfile(filename, filetype){
  15. if (filetype=="js"){ //if filename is a external JavaScript file
  16. var fileref=document.createElement('script')
  17. fileref.setAttribute("type","text/javascript")
  18. fileref.setAttribute("src", filename)
  19. }
  20. else if (filetype=="css"){ //if filename is an external CSS file
  21. var fileref=document.createElement("link")
  22. fileref.setAttribute("rel", "stylesheet")
  23. fileref.setAttribute("type", "text/css")
  24. fileref.setAttribute("href", filename)
  25. }
  26. if (typeof fileref!="undefined")
  27. document.getElementsByTagName("head")[0].appendChild(fileref)
  28. };
  29. // ------ TIMEOUT
  30. function Timeout(fn, interval) {
  31. var id = setTimeout(fn, interval);
  32. this.cleared = false;
  33. this.clear = function () {
  34. this.cleared = true;
  35. clearTimeout(id);
  36. };
  37. }
  38. // ------ HEXTORGB convert hex to rgb color ------
  39. function HexToRGB(h){
  40. if(h.charAt(0)=="#"){
  41. var coul = h.substring(1,7);
  42. var r = parseInt(coul.substring(0,2),16);
  43. var g = parseInt(coul.substring(2,4),16);
  44. var b = parseInt(coul.substring(4,6),16);
  45. return 'rgb('+r+', '+g+', '+b+')';
  46. }else{
  47. return null;
  48. }
  49. }
  50. // ------ PAIR check if number is pair of impair ------
  51. function pair(n){return typeof n=='number'?(n%2==0?true:false):null;}
  52. // ------ RANDOM ------
  53. function random(n1,n2){
  54. return arguments.length === 1 && typeof n1 === 'number'? Math.random()*n1 : ( arguments.length === 2 && typeof n1 === typeof n2 && typeof n1 === 'number' ? n1 + Math.random()*(n2-n1) : NaN );
  55. }
  56. function randomInt(n1,n2){
  57. var rand = arguments.length === 1 && typeof n1 === 'number'? Math.random()*n1 : ( arguments.length === 2 && typeof n1 === typeof n2 && typeof n1 === 'number' ? n1 + Math.random()*(n2-n1) : NaN );
  58. return parseInt(rand);
  59. }
  60. // ------ ROUNDED ------
  61. function round(n,granularity){
  62. if(arguments.length == 1){
  63. n = Math.round(n);
  64. }else{
  65. granularity = granularity.toString();
  66. granularity = granularity.replace(/./gi, 0);
  67. granularity = granularity.replace(/^./gi, 1);
  68. granularity = parseInt(granularity);
  69. n = Math.round(n*granularity)/granularity;
  70. }
  71. return n;
  72. }
  73. // ------ FLOOR ------
  74. function floor(n,granularity){
  75. if(arguments.length == 1){
  76. n = Math.floor(n);
  77. }else{
  78. granularity = granularity.toString();
  79. granularity = granularity.replace(/./gi, 0);
  80. granularity = granularity.replace(/^./gi, 1);
  81. granularity = parseInt(granularity);
  82. n = Math.floor(n*granularity)/granularity;
  83. }
  84. return n;
  85. }
  86. // ------ CEIL ------
  87. function ceil(n,granularity){
  88. if(arguments.length == 1){
  89. n = Math.ceil(n);
  90. }else{
  91. granularity = granularity.toString();
  92. granularity = granularity.replace(/./gi, 0);
  93. granularity = granularity.replace(/^./gi, 1);
  94. granularity = parseInt(granularity);
  95. n = Math.ceil(n*granularity)/granularity;
  96. }
  97. return n;
  98. }
  99. /**
  100. * String
  101. */
  102. // ------ TRIM remove multiple, leading or trailing spaces ------
  103. String.prototype.trim = function() {
  104. return this.replace(/(^\s*)|(\s*$)/gi,"").replace(/[ ]{2,}/gi," ").replace(/\n /,"\n");
  105. }
  106. // ------ remove all accents, for sort() by example ------
  107. var Latinise={};Latinise.latin_map={"Á":"A","Ă":"A","Ắ":"A","Ặ":"A","Ằ":"A","Ẳ":"A","Ẵ":"A","Ǎ":"A","Â":"A","Ấ":"A","Ậ":"A","Ầ":"A","Ẩ":"A","Ẫ":"A","Ä":"A","Ǟ":"A","Ȧ":"A","Ǡ":"A","Ạ":"A","Ȁ":"A","À":"A","Ả":"A","Ȃ":"A","Ā":"A","Ą":"A","Å":"A","Ǻ":"A","Ḁ":"A","Ⱥ":"A","Ã":"A","Ꜳ":"AA","Æ":"AE","Ǽ":"AE","Ǣ":"AE","Ꜵ":"AO","Ꜷ":"AU","Ꜹ":"AV","Ꜻ":"AV","Ꜽ":"AY","Ḃ":"B","Ḅ":"B","Ɓ":"B","Ḇ":"B","Ƀ":"B","Ƃ":"B","Ć":"C","Č":"C","Ç":"C","Ḉ":"C","Ĉ":"C","Ċ":"C","Ƈ":"C","Ȼ":"C","Ď":"D","Ḑ":"D","Ḓ":"D","Ḋ":"D","Ḍ":"D","Ɗ":"D","Ḏ":"D","Dz":"D","Dž":"D","Đ":"D","Ƌ":"D","DZ":"DZ","DŽ":"DZ","É":"E","Ĕ":"E","Ě":"E","Ȩ":"E","Ḝ":"E","Ê":"E","Ế":"E","Ệ":"E","Ề":"E","Ể":"E","Ễ":"E","Ḙ":"E","Ë":"E","Ė":"E","Ẹ":"E","Ȅ":"E","È":"E","Ẻ":"E","Ȇ":"E","Ē":"E","Ḗ":"E","Ḕ":"E","Ę":"E","Ɇ":"E","Ẽ":"E","Ḛ":"E","Ꝫ":"ET","Ḟ":"F","Ƒ":"F","Ǵ":"G","Ğ":"G","Ǧ":"G","Ģ":"G","Ĝ":"G","Ġ":"G","Ɠ":"G","Ḡ":"G","Ǥ":"G","Ḫ":"H","Ȟ":"H","Ḩ":"H","Ĥ":"H","Ⱨ":"H","Ḧ":"H","Ḣ":"H","Ḥ":"H","Ħ":"H","Í":"I","Ĭ":"I","Ǐ":"I","Î":"I","Ï":"I","Ḯ":"I","İ":"I","Ị":"I","Ȉ":"I","Ì":"I","Ỉ":"I","Ȋ":"I","Ī":"I","Į":"I","Ɨ":"I","Ĩ":"I","Ḭ":"I","Ꝺ":"D","Ꝼ":"F","Ᵹ":"G","Ꞃ":"R","Ꞅ":"S","Ꞇ":"T","Ꝭ":"IS","Ĵ":"J","Ɉ":"J","Ḱ":"K","Ǩ":"K","Ķ":"K","Ⱪ":"K","Ꝃ":"K","Ḳ":"K","Ƙ":"K","Ḵ":"K","Ꝁ":"K","Ꝅ":"K","Ĺ":"L","Ƚ":"L","Ľ":"L","Ļ":"L","Ḽ":"L","Ḷ":"L","Ḹ":"L","Ⱡ":"L","Ꝉ":"L","Ḻ":"L","Ŀ":"L","Ɫ":"L","Lj":"L","Ł":"L","LJ":"LJ","Ḿ":"M","Ṁ":"M","Ṃ":"M","Ɱ":"M","Ń":"N","Ň":"N","Ņ":"N","Ṋ":"N","Ṅ":"N","Ṇ":"N","Ǹ":"N","Ɲ":"N","Ṉ":"N","Ƞ":"N","Nj":"N","Ñ":"N","NJ":"NJ","Ó":"O","Ŏ":"O","Ǒ":"O","Ô":"O","Ố":"O","Ộ":"O","Ồ":"O","Ổ":"O","Ỗ":"O","Ö":"O","Ȫ":"O","Ȯ":"O","Ȱ":"O","Ọ":"O","Ő":"O","Ȍ":"O","Ò":"O","Ỏ":"O","Ơ":"O","Ớ":"O","Ợ":"O","Ờ":"O","Ở":"O","Ỡ":"O","Ȏ":"O","Ꝋ":"O","Ꝍ":"O","Ō":"O","Ṓ":"O","Ṑ":"O","Ɵ":"O","Ǫ":"O","Ǭ":"O","Ø":"O","Ǿ":"O","Õ":"O","Ṍ":"O","Ṏ":"O","Ȭ":"O","Ƣ":"OI","Ꝏ":"OO","Ɛ":"E","Ɔ":"O","Ȣ":"OU","Ṕ":"P","Ṗ":"P","Ꝓ":"P","Ƥ":"P","Ꝕ":"P","Ᵽ":"P","Ꝑ":"P","Ꝙ":"Q","Ꝗ":"Q","Ŕ":"R","Ř":"R","Ŗ":"R","Ṙ":"R","Ṛ":"R","Ṝ":"R","Ȑ":"R","Ȓ":"R","Ṟ":"R","Ɍ":"R","Ɽ":"R","Ꜿ":"C","Ǝ":"E","Ś":"S","Ṥ":"S","Š":"S","Ṧ":"S","Ş":"S","Ŝ":"S","Ș":"S","Ṡ":"S","Ṣ":"S","Ṩ":"S","Ť":"T","Ţ":"T","Ṱ":"T","Ț":"T","Ⱦ":"T","Ṫ":"T","Ṭ":"T","Ƭ":"T","Ṯ":"T","Ʈ":"T","Ŧ":"T","Ɐ":"A","Ꞁ":"L","Ɯ":"M","Ʌ":"V","Ꜩ":"TZ","Ú":"U","Ŭ":"U","Ǔ":"U","Û":"U","Ṷ":"U","Ü":"U","Ǘ":"U","Ǚ":"U","Ǜ":"U","Ǖ":"U","Ṳ":"U","Ụ":"U","Ű":"U","Ȕ":"U","Ù":"U","Ủ":"U","Ư":"U","Ứ":"U","Ự":"U","Ừ":"U","Ử":"U","Ữ":"U","Ȗ":"U","Ū":"U","Ṻ":"U","Ų":"U","Ů":"U","Ũ":"U","Ṹ":"U","Ṵ":"U","Ꝟ":"V","Ṿ":"V","Ʋ":"V","Ṽ":"V","Ꝡ":"VY","Ẃ":"W","Ŵ":"W","Ẅ":"W","Ẇ":"W","Ẉ":"W","Ẁ":"W","Ⱳ":"W","Ẍ":"X","Ẋ":"X","Ý":"Y","Ŷ":"Y","Ÿ":"Y","Ẏ":"Y","Ỵ":"Y","Ỳ":"Y","Ƴ":"Y","Ỷ":"Y","Ỿ":"Y","Ȳ":"Y","Ɏ":"Y","Ỹ":"Y","Ź":"Z","Ž":"Z","Ẑ":"Z","Ⱬ":"Z","Ż":"Z","Ẓ":"Z","Ȥ":"Z","Ẕ":"Z","Ƶ":"Z","IJ":"IJ","Œ":"OE","ᴀ":"A","ᴁ":"AE","ʙ":"B","ᴃ":"B","ᴄ":"C","ᴅ":"D","ᴇ":"E","ꜰ":"F","ɢ":"G","ʛ":"G","ʜ":"H","ɪ":"I","ʁ":"R","ᴊ":"J","ᴋ":"K","ʟ":"L","ᴌ":"L","ᴍ":"M","ɴ":"N","ᴏ":"O","ɶ":"OE","ᴐ":"O","ᴕ":"OU","ᴘ":"P","ʀ":"R","ᴎ":"N","ᴙ":"R","ꜱ":"S","ᴛ":"T","ⱻ":"E","ᴚ":"R","ᴜ":"U","ᴠ":"V","ᴡ":"W","ʏ":"Y","ᴢ":"Z","á":"a","ă":"a","ắ":"a","ặ":"a","ằ":"a","ẳ":"a","ẵ":"a","ǎ":"a","â":"a","ấ":"a","ậ":"a","ầ":"a","ẩ":"a","ẫ":"a","ä":"a","ǟ":"a","ȧ":"a","ǡ":"a","ạ":"a","ȁ":"a","à":"a","ả":"a","ȃ":"a","ā":"a","ą":"a","ᶏ":"a","ẚ":"a","å":"a","ǻ":"a","ḁ":"a","ⱥ":"a","ã":"a","ꜳ":"aa","æ":"ae","ǽ":"ae","ǣ":"ae","ꜵ":"ao","ꜷ":"au","ꜹ":"av","ꜻ":"av","ꜽ":"ay","ḃ":"b","ḅ":"b","ɓ":"b","ḇ":"b","ᵬ":"b","ᶀ":"b","ƀ":"b","ƃ":"b","ɵ":"o","ć":"c","č":"c","ç":"c","ḉ":"c","ĉ":"c","ɕ":"c","ċ":"c","ƈ":"c","ȼ":"c","ď":"d","ḑ":"d","ḓ":"d","ȡ":"d","ḋ":"d","ḍ":"d","ɗ":"d","ᶑ":"d","ḏ":"d","ᵭ":"d","ᶁ":"d","đ":"d","ɖ":"d","ƌ":"d","ı":"i","ȷ":"j","ɟ":"j","ʄ":"j","dz":"dz","dž":"dz","é":"e","ĕ":"e","ě":"e","ȩ":"e","ḝ":"e","ê":"e","ế":"e","ệ":"e","ề":"e","ể":"e","ễ":"e","ḙ":"e","ë":"e","ė":"e","ẹ":"e","ȅ":"e","è":"e","ẻ":"e","ȇ":"e","ē":"e","ḗ":"e","ḕ":"e","ⱸ":"e","ę":"e","ᶒ":"e","ɇ":"e","ẽ":"e","ḛ":"e","ꝫ":"et","ḟ":"f","ƒ":"f","ᵮ":"f","ᶂ":"f","ǵ":"g","ğ":"g","ǧ":"g","ģ":"g","ĝ":"g","ġ":"g","ɠ":"g","ḡ":"g","ᶃ":"g","ǥ":"g","ḫ":"h","ȟ":"h","ḩ":"h","ĥ":"h","ⱨ":"h","ḧ":"h","ḣ":"h","ḥ":"h","ɦ":"h","ẖ":"h","ħ":"h","ƕ":"hv","í":"i","ĭ":"i","ǐ":"i","î":"i","ï":"i","ḯ":"i","ị":"i","ȉ":"i","ì":"i","ỉ":"i","ȋ":"i","ī":"i","į":"i","ᶖ":"i","ɨ":"i","ĩ":"i","ḭ":"i","ꝺ":"d","ꝼ":"f","ᵹ":"g","ꞃ":"r","ꞅ":"s","ꞇ":"t","ꝭ":"is","ǰ":"j","ĵ":"j","ʝ":"j","ɉ":"j","ḱ":"k","ǩ":"k","ķ":"k","ⱪ":"k","ꝃ":"k","ḳ":"k","ƙ":"k","ḵ":"k","ᶄ":"k","ꝁ":"k","ꝅ":"k","ĺ":"l","ƚ":"l","ɬ":"l","ľ":"l","ļ":"l","ḽ":"l","ȴ":"l","ḷ":"l","ḹ":"l","ⱡ":"l","ꝉ":"l","ḻ":"l","ŀ":"l","ɫ":"l","ᶅ":"l","ɭ":"l","ł":"l","lj":"lj","ſ":"s","ẜ":"s","ẛ":"s","ẝ":"s","ḿ":"m","ṁ":"m","ṃ":"m","ɱ":"m","ᵯ":"m","ᶆ":"m","ń":"n","ň":"n","ņ":"n","ṋ":"n","ȵ":"n","ṅ":"n","ṇ":"n","ǹ":"n","ɲ":"n","ṉ":"n","ƞ":"n","ᵰ":"n","ᶇ":"n","ɳ":"n","ñ":"n","nj":"nj","ó":"o","ŏ":"o","ǒ":"o","ô":"o","ố":"o","ộ":"o","ồ":"o","ổ":"o","ỗ":"o","ö":"o","ȫ":"o","ȯ":"o","ȱ":"o","ọ":"o","ő":"o","ȍ":"o","ò":"o","ỏ":"o","ơ":"o","ớ":"o","ợ":"o","ờ":"o","ở":"o","ỡ":"o","ȏ":"o","ꝋ":"o","ꝍ":"o","ⱺ":"o","ō":"o","ṓ":"o","ṑ":"o","ǫ":"o","ǭ":"o","ø":"o","ǿ":"o","õ":"o","ṍ":"o","ṏ":"o","ȭ":"o","ƣ":"oi","ꝏ":"oo","ɛ":"e","ᶓ":"e","ɔ":"o","ᶗ":"o","ȣ":"ou","ṕ":"p","ṗ":"p","ꝓ":"p","ƥ":"p","ᵱ":"p","ᶈ":"p","ꝕ":"p","ᵽ":"p","ꝑ":"p","ꝙ":"q","ʠ":"q","ɋ":"q","ꝗ":"q","ŕ":"r","ř":"r","ŗ":"r","ṙ":"r","ṛ":"r","ṝ":"r","ȑ":"r","ɾ":"r","ᵳ":"r","ȓ":"r","ṟ":"r","ɼ":"r","ᵲ":"r","ᶉ":"r","ɍ":"r","ɽ":"r","ↄ":"c","ꜿ":"c","ɘ":"e","ɿ":"r","ś":"s","ṥ":"s","š":"s","ṧ":"s","ş":"s","ŝ":"s","ș":"s","ṡ":"s","ṣ":"s","ṩ":"s","ʂ":"s","ᵴ":"s","ᶊ":"s","ȿ":"s","ɡ":"g","ᴑ":"o","ᴓ":"o","ᴝ":"u","ť":"t","ţ":"t","ṱ":"t","ț":"t","ȶ":"t","ẗ":"t","ⱦ":"t","ṫ":"t","ṭ":"t","ƭ":"t","ṯ":"t","ᵵ":"t","ƫ":"t","ʈ":"t","ŧ":"t","ᵺ":"th","ɐ":"a","ᴂ":"ae","ǝ":"e","ᵷ":"g","ɥ":"h","ʮ":"h","ʯ":"h","ᴉ":"i","ʞ":"k","ꞁ":"l","ɯ":"m","ɰ":"m","ᴔ":"oe","ɹ":"r","ɻ":"r","ɺ":"r","ⱹ":"r","ʇ":"t","ʌ":"v","ʍ":"w","ʎ":"y","ꜩ":"tz","ú":"u","ŭ":"u","ǔ":"u","û":"u","ṷ":"u","ü":"u","ǘ":"u","ǚ":"u","ǜ":"u","ǖ":"u","ṳ":"u","ụ":"u","ű":"u","ȕ":"u","ù":"u","ủ":"u","ư":"u","ứ":"u","ự":"u","ừ":"u","ử":"u","ữ":"u","ȗ":"u","ū":"u","ṻ":"u","ų":"u","ᶙ":"u","ů":"u","ũ":"u","ṹ":"u","ṵ":"u","ᵫ":"ue","ꝸ":"um","ⱴ":"v","ꝟ":"v","ṿ":"v","ʋ":"v","ᶌ":"v","ⱱ":"v","ṽ":"v","ꝡ":"vy","ẃ":"w","ŵ":"w","ẅ":"w","ẇ":"w","ẉ":"w","ẁ":"w","ⱳ":"w","ẘ":"w","ẍ":"x","ẋ":"x","ᶍ":"x","ý":"y","ŷ":"y","ÿ":"y","ẏ":"y","ỵ":"y","ỳ":"y","ƴ":"y","ỷ":"y","ỿ":"y","ȳ":"y","ẙ":"y","ɏ":"y","ỹ":"y","ź":"z","ž":"z","ẑ":"z","ʑ":"z","ⱬ":"z","ż":"z","ẓ":"z","ȥ":"z","ẕ":"z","ᵶ":"z","ᶎ":"z","ʐ":"z","ƶ":"z","ɀ":"z","ff":"ff","ffi":"ffi","ffl":"ffl","fi":"fi","fl":"fl","ij":"ij","œ":"oe","st":"st","ₐ":"a","ₑ":"e","ᵢ":"i","ⱼ":"j","ₒ":"o","ᵣ":"r","ᵤ":"u","ᵥ":"v","ₓ":"x"};
  108. String.prototype.latinise=function(){return this.replace(/[^A-Za-z0-9\[\] ]/g,function(a){return Latinise.latin_map[a]||a})};
  109. String.prototype.latinize=String.prototype.latinise;
  110. String.prototype.isLatin=function(){return this==this.latinise()}
  111. /**
  112. * Array
  113. */
  114. // ------ UNIQUE reduces duplicates ------
  115. array_unique = function(ar){
  116. var r = new Array();
  117. o:for(var i = 0, n = ar.length; i < n; i++)
  118. {
  119. for(var x = 0, y = r.length; x < y; x++)
  120. {
  121. if(r[x]==ar[i])
  122. {
  123. continue o;
  124. }
  125. }
  126. r[r.length] = ar[i];
  127. }
  128. return r;
  129. };
  130. // ------ SUBSTRACT substract two array ------
  131. array_subtract = function(ara1,ara2) {
  132. var aRes = new Array() ;
  133. for (var i = ara1.length-1; i >= 0; i--)
  134. if(array_contains(ara2,ara1[i]))
  135. aRes.push(ara1[i]);
  136. return aRes ;
  137. }
  138. /*
  139. SHUFFLE Add a shuffle function to Array object prototype
  140. author: Stephane Roucheray
  141. src: http://sroucheray.org/blog/2009/11/array-sort-should-not-be-used-to-shuffle-an-array/
  142. found 2011/09/08.
  143. */
  144. array_shuffle = function(ar){
  145. var i = ar.length, j, temp;
  146. if ( i == 0 ) return;
  147. while ( --i ) {
  148. j = Math.floor( Math.random() * ( i + 1 ) );
  149. temp = ar[i];
  150. ar[i] = ar[j];
  151. ar[j] = temp;
  152. }
  153. return ar;
  154. };
  155. // ------ CONTAINS ------
  156. array_contains = function(ar, obj) {
  157. var i = ar.length;
  158. while (i--)
  159. if (ar[i] === obj)
  160. return true;
  161. return false;
  162. };
  163. // ------ REMOVE ------
  164. // /!\ Found on the Internet, not sure of efficiency.
  165. // Array.prototype.remove = function(v) {
  166. // var x, _i, _len, _results;
  167. // _results = [];
  168. // for (_i = 0, _len = this.length; _i < _len; _i++) {
  169. // x = this[_i];
  170. // if (x !== v) {
  171. // _results.push(x);
  172. // }
  173. // }
  174. // return _results;
  175. // };
  176. array_remove = function(ar){
  177. var what, a= arguments, L= a.length, ax;
  178. while(L && ar.length){
  179. what= a[--L];
  180. while((ax= ar.indexOf(what))!= -1){
  181. ar.splice(ax, 1);
  182. }
  183. }
  184. return ar;
  185. };
  186. // for IE
  187. // if(!Array.prototype.indexOf){
  188. // Array.prototype.indexOf= function(what, i){
  189. // i= i || 0;
  190. // var L= this.length;
  191. // while(i< L){
  192. // if(this[i]=== what) return i;
  193. // ++i;
  194. // }
  195. // return -1;
  196. // };
  197. // }
  198. array_indexOf = function(ar, what, i){
  199. i= i || 0;
  200. var L= ar.length;
  201. while(i< L){
  202. if(ar[i]=== what) return i;
  203. ++i;
  204. }
  205. return -1;
  206. };
  207. // ------ SUBSTRACT substract two array ------
  208. function array_subtract(ara1,ara2) {
  209. var aRes = new Array() ;
  210. for (var i = ara1.length-1; i >= 0; i--)
  211. if( !array_contains(ara1[i],ara2) )
  212. aRes.push(ara1[i]);
  213. return aRes ;
  214. };
  215. /**
  216. * Objects
  217. *
  218. */
  219. // ------ OBJECTSIZE get the length of objects ------
  220. function objectSize(o) {
  221. var len = o.length ? --o.length : 0;
  222. for (var k in o)
  223. len++;
  224. return len;
  225. }
  226. // Object.prototype.size = function () {
  227. // // var len = this.length ? --this.length : -1; // POURQUOI NE FONCTIONNE PAS?
  228. // var len = this.length ? --this.length : -2;
  229. // for (var k in this)
  230. // len++;
  231. // return len;
  232. // }
  233. function objectIsEmpty(obj){
  234. for (var prop in obj) {
  235. if (obj.hasOwnProperty(prop))
  236. return false;
  237. }
  238. return true;
  239. };
  240. // ------ OBJECTGETKEY get key of index ------
  241. function objectGetKey(o,index){
  242. var i = 0;
  243. for (var k in o){
  244. if (k === 'length' || !o.hasOwnProperty(k))
  245. continue;
  246. if(i == index)
  247. return k;
  248. i ++;
  249. }
  250. }
  251. // Object.prototype.getKey = function(index){
  252. // var i = 0;
  253. // for (var key in this){
  254. // if (key === 'length' || !this.hasOwnProperty(key))
  255. // continue;
  256. // if(i == index)
  257. // return key;
  258. // i ++;
  259. // }
  260. // }
  261. // ------ OBJECTSORT sort object ------
  262. function objectSort(o,value) {
  263. var connection = new Array();
  264. for (var i = objectSize(o) - 1; i >= 0; i--){
  265. var k = objectGetKey(o,i);
  266. for (var j = objectSize(o[k].wit) - 1; j >= 0; j--){
  267. var k2 = objectGetKey(o[k].wit,j);
  268. if (k2==value) {
  269. var the_wit = parseFloat(o[k].wit[k2]);
  270. connection.push( {wit:the_wit,nid:k} );
  271. };
  272. };
  273. };
  274. connection.sort(function(a,b){
  275. // return (a.wit - b.wit);//ascending
  276. return (b.wit - a.wit);//descending
  277. });
  278. var o_sort = {};
  279. for (var i = connection.length - 1; i >= 0; i--){
  280. // o_sort[connection[i].wit+'-'+connection[i].nid] = o[connection[i].nid];
  281. o_sort[connection[i].nid] = o[connection[i].nid];
  282. };
  283. return o_sort;
  284. }
  285. /**
  286. * trigo
  287. */
  288. // -------- getElementAngle ----------
  289. // L'angle 0 correspond au nord (vers le haut)
  290. // Exemple : getElementAngle(posElementX, posElementY, posSourisX, posSourisY)
  291. // Ceci retournera l'angle de l'élément par rapport à la position de la souris
  292. function getElementAngle(x1, y1, x2, y2) {
  293. var adj = x2 - x1;
  294. var opp = y2 - y1;
  295. var angle = Math.abs(Math.atan(opp/adj) * 180/Math.PI);
  296. if (adj > 0 && opp < 0 ) {
  297. angle = 90 - angle;
  298. }
  299. else if (adj >= 0 && opp >= 0) {
  300. angle += 90;
  301. }
  302. else if (adj < 0 && opp >= 0) {
  303. angle = 180 + (90 - angle);
  304. }
  305. else {
  306. angle += 270;
  307. }
  308. return angle;
  309. }
  310. // @codekit-prepend "gui.js"
  311. // @koala-prepend "gui.js"
  312. (function($) {
  313. MaterioFlag = function(){
  314. var _isLoadingList = false ;
  315. /**
  316. * init()
  317. */
  318. function init(){
  319. //trace('MaterioFlag :: init MaterioFlag');
  320. buildBlocks();
  321. $(document)
  322. .bind('flagGlobalAfterLinkUpdate', onFlaging)
  323. .bind('resultscompleted resultschanged previewloaded', onResultsUpdated)
  324. .bind('init-scroller-pager', onInitScrollerPager)
  325. .bind('load-scroller-pager', onLoadScrollerPager)
  326. .bind('view-mode-changed', onViewModeChanged)
  327. .bind('history-state-change', onHistoryStateChange);
  328. // ajaxifyLinks();
  329. // trigger updated event for direct html loading
  330. if(isList()){
  331. setTimeout(function(){
  332. triggerContentChanged();
  333. }, 10);
  334. }
  335. };
  336. function onFlaging(event){
  337. //trace('MaterioFlag :: onFlaging', event);
  338. refreshBlocks();
  339. };
  340. function onResultsUpdated(event){
  341. //trace('MaterioFlag :: onResultsUpdated', event);
  342. ajaxifyLinks(event.container);
  343. };
  344. function buildBlocks(activename){
  345. //trace('MaterioFlag :: buildBlocks | activename', activename);
  346. if($('#block-materio-flag-materio-flag-mybookmarks').length){
  347. var type = 'bookmarks';
  348. var block = '#block-materio-flag-materio-flag-mybookmarks';
  349. }else if($('#block-materio-flag-materio-flag-mylists').length){
  350. var type = 'lists';
  351. var block = '#block-materio-flag-materio-flag-mylists';
  352. }
  353. switch(type){
  354. case 'bookmarks':
  355. var name = type;
  356. $('h2 .listname', block).attr('name', name).bind('click', onClickShowPreview);
  357. $('<i class="icon-remove"></i>').appendTo($('h2', block)).attr('name', name).bind('click', onClickClosePreview);
  358. // $('<span class="preview"><i class="icon-eye-open"></i></span>').appendTo($('h2', block)).bind('click', onClickShowPreview);
  359. // if(!readCookie('materiobookmarkspreviewopened')){
  360. // showPreview('bookmarks', block);
  361. // }else{
  362. // }
  363. break;
  364. case 'lists':
  365. // nav block
  366. $('a.open-list:not(.ajax-processed)', '#block-materio-flag-materio-flag-mylists-nav').each(function(index){
  367. //trace('nav block a.open-list this', this);
  368. $this = $(this)
  369. .bind('click', onClickOpenLink)
  370. .addClass('ajax-processed');
  371. var name = $this.attr('class').match(/flag_lists_[^_]+_[0-9]+/);
  372. // trace('MaterioFlag :: name', name);
  373. $('<span class="preview"><i class="icon-eye-open"></i></span>').attr('name', name).insertAfter($this).bind('click', onClickShowPreview);
  374. });
  375. $('a.edit-list:not(.ajax-processed)', '#block-materio-flag-materio-flag-mylists-nav')
  376. .bind('click', onCLickEditList)
  377. .addClass('ajax-processed');
  378. $('a.flag-lists-create:not(.ajax-processed)', '#block-materio-flag-materio-flag-mylists-nav')
  379. .bind('click', onClickCreatLink)
  380. .addClass('ajax-processed');
  381. // preview block
  382. $('section.flag-list:not(.ajax-processed)', '#block-materio-flag-materio-flag-mylists').each(function(index){
  383. var name = $(this).attr('class').match(/flag_lists_[^_]+_[0-9]+/);
  384. $('<i class="icon-remove"></i>').appendTo($('h2.listname', this)).attr('name', name).bind('click', onClickClosePreview);
  385. $('a.open-list', this).bind('click', onClickOpenLink);
  386. }).addClass('ajax-processed');
  387. break;
  388. }
  389. // trigger refresh block event for enabling lazyload images
  390. setTimeout(function(){
  391. $.event.trigger({
  392. type : 'my'+type+'-block-builded',
  393. block : block,
  394. name : name
  395. });
  396. },10);
  397. // trace('MaterioFlag :: activename', activename);
  398. if(activename == undefined)
  399. activename = readCookie('materiomyflaglistsopened');
  400. // trace('MaterioFlag :: activename', activename);
  401. if(activename)
  402. showPreview(activename, block);
  403. };
  404. function refreshBlocks(name){
  405. //trace('MaterioFlag :: refreshBlocks | name', name);
  406. if($('#block-materio-flag-materio-flag-mybookmarks').length){
  407. var type = 'bookmarks';
  408. }else if($('#block-materio-flag-materio-flag-mylists').length){
  409. var type = 'lists';
  410. }
  411. if(type != undefined){
  412. var id = '#block-materio-flag-materio-flag-my'+type;
  413. var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/refresh/block/'+type;
  414. $.getJSON(url, function(json){
  415. // trace('MaterioFlag :: block refreshed '+type, json);
  416. $(id).replaceWith(json.block);
  417. $('#block-materio-flag-materio-flag-mylists-nav').replaceWith(json.block_nav);
  418. buildBlocks(name);
  419. $.event.trigger({
  420. type : 'my'+type+'-block-updated',
  421. listname : name
  422. });
  423. });
  424. }
  425. };
  426. function ajaxifyLinks(container){
  427. //trace('MaterioFlag :: ajaxifyLinks', container);
  428. container = ((container != null) ? container : 'body');
  429. // trace('MaterioFlag :: typeof Drupal.flagLink', typeof Drupal.flagLink);
  430. if (typeof Drupal.flagLink != 'undefined')
  431. Drupal.flagLink(container);
  432. if(isList()){
  433. var fid = $('.materio-flags-list', '#content').attr('fid');
  434. $('li.unflag-action.fid-'+fid+' a:not(.ajax-processed), li.flag-bookmarks a.unflag-action:not(.ajax-processed)')
  435. .bind('click', onUnflagList)
  436. .addClass('ajax-processed');
  437. }
  438. $('a.flag-lists-create:not(.ajax-processed)', container)
  439. .bind('click', onClickCreatLink)
  440. .addClass('ajax-processed');
  441. };
  442. /**
  443. * show hide preview
  444. */
  445. function onClickShowPreview(event){
  446. //trace('MaterioFlag :: onClickShowPreview', event);
  447. showPreview($(this).attr('name'), $(this).parent('.block').attr('id'));
  448. };
  449. function showPreview(name, block){
  450. //trace('MaterioFlag :: showPreview', name);
  451. $('section.'+name, block).addClass('active')
  452. .siblings('section').removeClass('active');
  453. createCookie('materiomyflaglistsopened', name, 1);
  454. $.event.trigger('init-layout');
  455. };
  456. function onClickClosePreview(event){
  457. //trace('MaterioFlag :: onClickClosePreview', event);
  458. eraseCookie('materiomyflaglistsopened');
  459. if($(this).attr('name') == 'bookmarks'){
  460. $(this).parents('.block').find('section.bookmarks').removeClass('active');
  461. }else{
  462. $(this).parents('section.flag-list').removeClass('active');
  463. }
  464. $.event.trigger('init-layout');
  465. };
  466. /**
  467. * onClickOpenLink
  468. */
  469. function onClickOpenLink(event){
  470. event.preventDefault();
  471. var $link = $(event.currentTarget);
  472. var fid = $link.attr('href').match(/lists\/([0-9]+)$/);
  473. // trace('MaterioFlag :: type', type);
  474. loadList(fid[1]);
  475. return false;
  476. };
  477. function loadList(fid){
  478. //trace('MaterioFlag :: loadList | fid', fid);
  479. var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/ajax/list/'+fid;
  480. $.event.trigger('loading-content');
  481. $.getJSON(url, {'current_path':document.location.href},function(json){
  482. //trace('MaterioFlag :: json', json);
  483. if(json.redirect){
  484. window.location = json.redirect;
  485. }else{
  486. changeContent(json);
  487. }
  488. });
  489. };
  490. function changeContent(json){
  491. if(json.rendered){
  492. $('.inner-content','#content').html(json.rendered);
  493. $.event.trigger('loaded-content');
  494. // no need of ajaxifylinks because it's triggered with resultschanged
  495. // ajaxifyLinks('#content');
  496. var path = Drupal.settings.basePath + Drupal.settings.pathPrefix + json.path;
  497. $.event.trigger({
  498. type : 'new-history-page',
  499. path : path,
  500. title : json.title,
  501. content : json.rendered
  502. });
  503. // TODO: change language links for folders
  504. // for (language in Drupal.settings.materio_search_api_ajax.languages) {
  505. // var l = Drupal.settings.materio_search_api_ajax.languages[language];
  506. // $('#block-locale-language li.'+language+' a').attr('href', Drupal.settings.basePath + l.prefix+'/' + json.search_path + '/' + json.keys)
  507. // };
  508. triggerContentChanged();
  509. }else{
  510. //trace('MaterioFlag :: no results');
  511. }
  512. };
  513. function triggerContentChanged(){
  514. $.event.trigger({
  515. type: 'resultschanged',
  516. container : '#content .flaglist-items'
  517. });
  518. };
  519. /**
  520. * onClickCreatLink(event)
  521. */
  522. function onClickCreatLink(event){
  523. //trace('MaterioFlag :: onClickCreatLink | event', event);
  524. event.preventDefault();
  525. var $link = $(event.currentTarget);
  526. var type = $link.attr('href').match(/[^\/]*$/);
  527. // trace('MaterioFlag :: type', type);
  528. var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/createlist/form/'+type[0];
  529. $.getJSON(url, function(json){
  530. //trace('MaterioFlag :: creat list : json', json);
  531. showCreateListForm(json, $link);
  532. });
  533. return false;
  534. };
  535. function showCreateListForm(json, $link){
  536. //trace('MaterioFlag :: showCreateListForm | json', json);
  537. // google analytics
  538. $.event.trigger({
  539. type:"record-stat",
  540. categorie:"flagLists",
  541. action: 'show create form'
  542. });
  543. var $modal = $('<div id="modal" class="modal"/>').appendTo('body');
  544. $modal
  545. .css({
  546. position:'absolute',
  547. top:'40%', left:'50%',
  548. marginLeft:'-150px', width:'300px',
  549. zIndex:"99999"
  550. })
  551. .append(json.rendered_form)
  552. .find('input[type="submit"]', '#materio-flag-create-list-form').bind('click', function(event) {
  553. event.preventDefault();
  554. switch($(this).attr('name')){
  555. case 'cancel':
  556. //trace('MaterioFlag :: cancel',event);
  557. $(this).parents('#modal').remove();
  558. // google analytics
  559. $.event.trigger({
  560. type:"record-stat",
  561. categorie:"flagLists",
  562. action: 'cancel create form'
  563. });
  564. break;
  565. case 'create':
  566. //trace('MaterioFlag :: create',event);
  567. var title = $(this).parents('form').find('input[name*="flag-lists-name"]').val();
  568. var type = $(this).parents('form').find('input[name*="type"]').val();
  569. // google analytics
  570. $.event.trigger({
  571. type : "record-stat",
  572. categorie : "flagLists",
  573. action : "submit create form",
  574. label : 'title : '+title
  575. });
  576. createList($modal, type, title, $link);
  577. break;
  578. }
  579. return false;
  580. })
  581. .parents('form').find('input[type="text"]').focus();
  582. // TODO: esc keypressed close the form
  583. };
  584. function createList($modal, type, title, $link){
  585. //trace('materioflag :: createList | title', title);
  586. $('.flag-lists-create').addClass('loading');
  587. var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'flag-lists/add/'+type+'/js';
  588. $.getJSON(url, {name:title}, function(data) {
  589. if (data.error) {
  590. //trace(data.error);
  591. }
  592. else {
  593. // select.append('<option value="'+data.flag.fid+'">'+data.flag.title+'</option>');
  594. // $('input.name', $(this)).val('');
  595. // dialog.dialog('close');
  596. //trace('MaterioFlag :: created list : data', data);
  597. if($link.attr('nid') && $link.attr('token')){
  598. flagEntityWithList(data.flag.name, $link.attr('nid'), $link.attr('token'));
  599. }else{
  600. refreshBlocks(data.flag.name);
  601. refreshNodeLinks();
  602. }
  603. $modal.remove();
  604. }
  605. });
  606. };
  607. function flagEntityWithList(name, nid, token){
  608. //trace('MaterioFlag :: flagEntityWithList | name', name);
  609. // var ret;
  610. // Send POST request
  611. $.ajax({
  612. type: 'POST',
  613. url: Drupal.settings.basePath+Drupal.settings.pathPrefix+'flag-lists/flag/'+name+'/'+nid,
  614. data: { js: true, token: token },
  615. dataType: 'json',
  616. success: function (data2) {
  617. //trace('MaterioFlag :: node taged with newly created list : data2', data2)
  618. if (data2.status) {
  619. // google analytics
  620. $.event.trigger({
  621. type : "record-stat",
  622. categorie : 'FlagLists',
  623. action : 'node flaged',
  624. label : 'nid : '+nid+' | flag : '+name
  625. });
  626. refreshBlocks(name);
  627. refreshNodeLinks();
  628. }else {
  629. // Failure.
  630. alert(data2.errorMessage);
  631. }
  632. },
  633. error: function (xmlhttp) {
  634. alert('An HTTP error '+ xmlhttp.status +' occurred.\n'+ element.href);
  635. }
  636. });
  637. };
  638. function refreshNodeLinks(){
  639. //trace('MaterioFlag :: refreshNodeLinks');
  640. var nids = new Array();
  641. $('.flag-lists-entity-links')
  642. // .addClass('loading')
  643. .parents('.node')
  644. .each(function(index) {
  645. nids.push($(this).attr('class').match(/node-([0-9]+)/)[1]);
  646. });
  647. // trace('MaterioFlag :: nids', nids);
  648. var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/nodelinks';
  649. $.getJSON(url, {nids:nids.join(";")}, function(data) {
  650. // trace('MaterioFlag :: data', data);
  651. for(nid in data.links){
  652. // trace('MaterioFlag :: nid', nid);
  653. // trace('MaterioFlag :: data.links[nid]', data.links[nid]);
  654. $('.node-'+nid+' .flag-lists-entity-links').replaceWith(data.links[nid]);
  655. // trace('MaterioFlag :: typeof Drupal.flagLink', typeof Drupal.flagLink);
  656. // if (typeof Drupal.flagLink != 'undefined')
  657. // Drupal.flagLink($('.node-'+nid+' .flag-lists-entity-links'));
  658. // TODO: sortir ajaxifyLinks de la boucle, je pense que ça prend trop de ressources
  659. ajaxifyLinks('.node-'+nid+' .flag-lists-entity-links');
  660. }
  661. });
  662. $.event.trigger({
  663. type : 'materioflag-nodelinks-updated',
  664. nids : nids
  665. });
  666. };
  667. /**
  668. * onCLickEditList(event)
  669. */
  670. function onCLickEditList(event){
  671. //trace('MaterioFlag :: onCLickEditList | event', event);
  672. // TODO: empécher le double formulaire
  673. event.preventDefault();
  674. var $link = $(event.currentTarget);
  675. var lid = $link.attr('href').match(/[^\/]*$/);
  676. var type = 'materiau'; // this is cheap
  677. var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/editlistform/'+type+'/'+lid[0];
  678. $.getJSON(url, function(json){
  679. //trace('MaterioFlag :: editlist : json', json);
  680. showEditListForm(json, $link);
  681. });
  682. return false;
  683. };
  684. function showEditListForm(json, $link){
  685. //trace('MaterioFlag :: showEditListForm | json', json);
  686. // google analytics
  687. $.event.trigger({
  688. type:"record-stat",
  689. categorie:"flagLists",
  690. action: 'show edit form'
  691. });
  692. var $modal = $('<div id="modal" class="modal"/>').appendTo('body');
  693. $modal
  694. .css({
  695. position:'absolute',
  696. top:'40%', left:'50%',
  697. marginLeft:'-150px', width:'300px',
  698. zIndex:"99999"
  699. })
  700. .append(json.rendered_form)
  701. .find('input[type="submit"]', '#materio-flag-edit-list-form').bind('click', function(event) {
  702. event.preventDefault();
  703. var $form = $(this).parents('form');
  704. var title = $form.find('input[name*="flag-lists-title"]').val();
  705. var fid = $form.find('input[name*="fid"]').val();
  706. var name = $form.find('input[name*="name"]').val();
  707. switch($(this).attr('name')){
  708. case 'cancel':
  709. //trace('MaterioFlag :: cancel',event);
  710. $(this).parents('#modal').remove();
  711. // google analytics
  712. var action = 'cancel edit form';
  713. break;
  714. case 'save':
  715. //trace('MaterioFlag :: create',event);
  716. // google analytics
  717. var action = "submit edit form";
  718. saveList($modal, fid, name, title);
  719. break;
  720. case 'delete':
  721. //trace('MaterioFlag :: delete',event);
  722. if(confirm('Do you realy want to delete your '+title+' folder ?')){
  723. var action = "submit delete form";
  724. deleteList($modal, fid);
  725. }else{
  726. var action = "cancel delete form";
  727. }
  728. break;
  729. }
  730. // google analytics
  731. $.event.trigger({
  732. type:"record-stat",
  733. categorie:"flagLists",
  734. action: action
  735. });
  736. return false;
  737. })
  738. .parents('form').find('input[type="text"]').focus();
  739. // TODO: esc keypressed close the form
  740. };
  741. function saveList($modal, fid, name, title){
  742. //trace('MaterioFlag :: saveList | fid : '+fid+'| name', name);
  743. $('.flag-lists-link.fid-'+fid).addClass('loading');
  744. var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/editlist/'+fid+'/'+name+'/'+title;
  745. $.getJSON(url, function(data) {
  746. if (data.error) {
  747. // trace(data.error);
  748. if(data.message)
  749. alert(data.message);
  750. }
  751. else {
  752. //trace('MaterioFlag :: saved list : data', data);
  753. $.event.trigger({
  754. type : 'list-edited',
  755. name : data.listname,
  756. title : data.title,
  757. });
  758. refreshBlocks();
  759. refreshNodeLinks();
  760. $modal.remove();
  761. }
  762. });
  763. };
  764. function deleteList($modal, fid){
  765. //trace('MaterioFlag :: deletelist | fid', fid);
  766. $('.flag-lists-link.fid-'+fid).hide();
  767. var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/deletelist/'+fid;
  768. $.getJSON(url, function(data) {
  769. if (data.error) {
  770. // trace(data.error);
  771. if(data.message)
  772. alert(data.message);
  773. }
  774. else {
  775. //trace('MaterioFlag :: deleted list : data', data);
  776. refreshBlocks();
  777. refreshNodeLinks();
  778. // TODO: if the deleted list was the current displayed list ??
  779. $modal.remove();
  780. }
  781. });
  782. };
  783. /**
  784. * onUnflagList()
  785. */
  786. function onUnflagList(event){
  787. //trace('onUnflagList', event);
  788. $(this).parents('article.node').addClass('removed');
  789. };
  790. /**
  791. *
  792. */
  793. function onInitScrollerPager(event){
  794. // trace('MaterioFlag :: MaterioFlag :: onInitScrollerPager');
  795. if (isList()){
  796. // trace('MaterioFlag :: event.pager', event);
  797. event.pager.hide();
  798. }
  799. };
  800. function onLoadScrollerPager(event){
  801. if (isList())
  802. loadNextListPage(event.href);
  803. };
  804. function loadNextListPage(href){
  805. // trace('MaterioFlag :: loadNextListPage', href);
  806. if(!_isLoadingList){
  807. var fid = href.match(/lists\/([^\/|\?]+)/);
  808. var page = href.match(/\?page=([0-9]+)/);
  809. var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/ajax/list/'+fid[1]+'/'+page[1];
  810. // trace('MaterioFlag :: url', url);
  811. loadNextPage(url, $('.materio-flags-list', '#content'), '.flaglist-items');
  812. }
  813. };
  814. function loadNextPage(url, $container, target){
  815. //trace('MaterioFlag :: loadNextPage');
  816. _isLoadingList = true;
  817. $container.addClass('loading');
  818. $.getJSON(url, function(json){
  819. //trace('json', json);
  820. _isLoadingList = false;
  821. $container.removeClass('loading');
  822. addNextpage(json, target);
  823. });
  824. };
  825. function addNextpage(json, container_class){
  826. var $newcontent = $(json.rendered),
  827. $newitems = $(container_class, $newcontent).children('article').addClass('just-added'),
  828. $newpager = $('ul.pager', $newcontent);
  829. $(container_class, '#content').append($newitems);
  830. $('ul.pager', '#content').replaceWith($newpager.hide());
  831. // TODO: animation, this should be on theme side
  832. $(container_class, '#content').children('.just-added').each(function(i){
  833. // $(this).delay(5000*i).removeClass('just-added');
  834. var $this = $(this);
  835. setTimeout(function(){
  836. $this.removeClass('just-added');
  837. }, 150*i);
  838. });
  839. $.event.trigger({
  840. type : 'resultscompleted',
  841. container : $(container_class, '#content')
  842. });
  843. };
  844. function onViewModeChanged(event){
  845. if (isList())
  846. loadList(getFid());
  847. };
  848. /**
  849. * history
  850. */
  851. function onHistoryStateChange(event){
  852. if(isList())
  853. triggerContentChanged();
  854. };
  855. /**
  856. * Helpers
  857. */
  858. function getFid(){
  859. return $('.materio-flags-list', '#content').attr('fid');;
  860. };
  861. function isList(){
  862. return $('.materio-flags-list', '#content').length;
  863. };
  864. /**
  865. * cookies
  866. */
  867. function createCookie(name,value,days) {
  868. if (days) {
  869. var date = new Date();
  870. date.setTime(date.getTime()+(days*24*60*60*1000));
  871. var expires = "; expires="+date.toGMTString();
  872. }
  873. else var expires = "";
  874. document.cookie = name+"="+value+expires+"; path=/";
  875. }
  876. function readCookie(name) {
  877. var nameEQ = name + "=";
  878. var ca = document.cookie.split(';');
  879. for(var i=0;i < ca.length;i++) {
  880. var c = ca[i];
  881. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  882. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  883. }
  884. return null;
  885. }
  886. function eraseCookie(name) {
  887. createCookie(name,"",-1);
  888. }
  889. init();
  890. };
  891. $(document).ready(function() {
  892. var materioflag = new MaterioFlag();
  893. });
  894. })(jQuery);