materio_search_api_ajax.min.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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(window.console && window.console.log){
  10. arguments.length === 2 ? window.console.log(s,o) : window.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_ck_fw/gui.js"
  312. (function($) {
  313. MaterioSearchApiAjax = function(){
  314. var _isloadingresults = false;
  315. var _$content = $('#content');
  316. // TODO: define $content by module settings
  317. /**
  318. * init()
  319. */
  320. function init(){
  321. //trace('init MaterioSearchApiAjax');
  322. initSearchAjax();
  323. initViewMode();
  324. };
  325. /**
  326. * searchAjax
  327. */
  328. function initSearchAjax(){
  329. // trace('initSearchAjax');
  330. $('#materio-search-api-search-form').bind('submit', function(event) {
  331. // trace('search submited', event);
  332. // var $this = $(this);
  333. setTimeout(function(){
  334. loadResults(getSearchKeys());
  335. },10);
  336. return false;
  337. });
  338. // /!\ AUTOCOMPLETE SELECT EVENT need a patch http://drupal.org/node/365241#comment-5374686
  339. $("#edit-searchfield")
  340. .bind('autocompleteSelect', function(event) {
  341. // $(this).parents('.form').trigger('submit');
  342. setTimeout(function(){
  343. loadResults(getSearchKeys(), "taxonomy");
  344. },10);
  345. })
  346. .bind('focus', function(event){
  347. $(this).select();
  348. });
  349. $(document)
  350. .bind('theme-ready', onThemeReady)
  351. .bind('init-scroller-pager', onInitScrollerPager)
  352. .bind('load-scroller-pager', onLoadScrollerPager)
  353. .bind('view-mode-changed', onViewModeChanged)
  354. .bind('history-state-change', onHistoryStateChange);
  355. };
  356. function onThemeReady(event){
  357. // trace('MaterioSearchApiAjax :: onThemeReady');
  358. if( isActuality() || isExplore() ){
  359. $.event.trigger({
  360. type : 'resultschanged',
  361. container : isActuality() ? '#content .actuality-items' : '#content .search-results'
  362. });
  363. }
  364. };
  365. function getSearchKeys(){
  366. return $('#materio-search-api-search-form').find('input[name*="searchfield"]').val();
  367. };
  368. function loadResults(keys, searchmode){
  369. //trace('keys', keys);
  370. if(keys !== undefined && keys !== '' && keys.length >= 2){
  371. keys = keys.replace('/', ' ');
  372. // define mode (between full text or only term selected on autocompletion)
  373. searchmode = searchmode || "fulltext";
  374. // record the "node type filter" form item
  375. var types = {}, stringTypes = [];
  376. $('#edit-bundles-filter', '#materio-search-api-search-form').find('input[type*="checkbox"]').each(function(i){
  377. $this = $(this);
  378. if ( $this.attr('checked') )
  379. stringTypes.push($this.val());
  380. types[$this.val()] = $this.attr('checked');
  381. });
  382. //trace('types', types);
  383. if(!_isloadingresults){
  384. $.event.trigger('loading-content');
  385. _isloadingresults = true;
  386. $('#materio-search-api-search-form').addClass('loading');
  387. // trace('window.location.href',window.location.href);
  388. // TODO: record ajax path in a variable from materio_search_api_ajax_init
  389. $.getJSON(Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_search_api_ajax/search/',
  390. {'types':types,'current_path':document.location.href, 'keys':keys, 'searchmode':searchmode},
  391. function(json){
  392. //trace('json', json);
  393. // google analytics
  394. $.event.trigger({
  395. type : "record-stat",
  396. categorie : 'Search',
  397. action : keys,
  398. label : 'filters : '+ stringTypes.join(' ,'),
  399. value : json.count
  400. });
  401. if(json.redirect){
  402. window.location = json.redirect;
  403. }else{
  404. $.event.trigger('loaded-content');
  405. _isloadingresults = false;
  406. $('#materio-search-api-search-form').removeClass('loading');
  407. changeContent(json);
  408. }
  409. });
  410. }
  411. }
  412. };
  413. function loadActuality(){
  414. if(!_isloadingresults){
  415. $.event.trigger('loading-content');
  416. _isloadingresults = true;
  417. $('#materio-search-api-search-form').addClass('loading');
  418. var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_search_api_ajax/actuality';
  419. $.getJSON(url,
  420. function(json){
  421. //trace('json', json);
  422. $.event.trigger('loaded-content');
  423. _isloadingresults = false;
  424. $('#materio-search-api-search-form').removeClass('loading');
  425. changeContent(json);
  426. });
  427. }
  428. };
  429. function changeContent(json){
  430. //trace('MaterioSearchApiAjax changeContent | json', json);
  431. if(json.returned){
  432. $.event.trigger('loaded-content');
  433. $('.inner-content',_$content).html(json.returned).find('ul.pager').hide();
  434. triggerContentChanged();
  435. }else{
  436. //trace('no results');
  437. }
  438. $.event.trigger({
  439. type : 'new-history-page',
  440. path : Drupal.settings.basePath + Drupal.settings.pathPrefix + json.path,
  441. title : json.title,
  442. content : json.returned
  443. });
  444. for (language in Drupal.settings.materio_search_api_ajax.languages) {
  445. var l = Drupal.settings.materio_search_api_ajax.languages[language];
  446. $('#block-locale-language li.'+language+' a').attr('href', Drupal.settings.basePath + l.prefix+'/' + json.search_path + '/' + json.keys)
  447. };
  448. };
  449. function triggerContentChanged(){
  450. //trace('MaterioSearchApiAjax :: triggerContentChanged');
  451. $.event.trigger({
  452. type : 'resultschanged',
  453. container : '#content .search-results, #content .actuality-items'
  454. });
  455. };
  456. function onInitScrollerPager(event){
  457. // trace("onInitScrollerPager");
  458. if(isActuality() || isExplore())
  459. event.pager.hide();
  460. };
  461. function onLoadScrollerPager(event){
  462. // trace("onLoadScrollerPager");
  463. if(!_isloadingresults){
  464. if (isExplore())
  465. loadNextResultsPage(event.href);
  466. if(isActuality())
  467. loadNextActualityPage(event.href);
  468. }
  469. };
  470. function loadNextResultsPage(href){
  471. // trace('loadNextResultsPage');
  472. var keys = href.match(/explore\/([^\/|\?]+)/);
  473. var page = href.match(/\?page=([0-9]+)/);
  474. var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_search_api_ajax/search/'+page[1];
  475. loadNextPage(url, keys[1], $('.materiobase-results', _$content), '.search-results');
  476. };
  477. function loadNextActualityPage(href){
  478. // trace('loadNextActualityPage');
  479. var page = href.match(/\?page=([0-9]+)/);
  480. var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_search_api_ajax/actuality/'+page[1];
  481. loadNextPage(url, '', $('.materiobase-actuality', _$content), '.actuality-items');
  482. };
  483. function loadNextPage(url, keys, $container, target){
  484. // trace('MaterioSearchApiAjax :: loadNextPage()');
  485. _isloadingresults = true;
  486. $container.addClass('loading');
  487. $.getJSON(url, {'keys':keys}, function(json){
  488. //trace('json', json);
  489. $container.removeClass('loading');
  490. addNextpage(json, target);
  491. // addNextpageItemByItem($(json.return), target);
  492. });
  493. };
  494. function addNextpage(json, container_class){
  495. // trace('json',json);
  496. if(json){
  497. var $newcontent = $(json.returned),
  498. $newitems = $(container_class, $newcontent).children('article'), //.addClass('just-added'),
  499. $newpager = $('ul.pager', $newcontent);
  500. $(container_class, _$content).append($newitems);
  501. $('ul.pager', _$content).replaceWith($newpager.hide());
  502. // TODO: animation, this should be on theme side
  503. $(container_class, _$content).children('.just-added').each(function(i){
  504. var $this = $(this);
  505. setTimeout(function(){
  506. $this.removeClass('just-added');
  507. }, 150*i);
  508. });
  509. $.event.trigger({
  510. type : 'resultscompleted',
  511. container : $(container_class, _$content)
  512. });
  513. }
  514. _isloadingresults = false;
  515. };
  516. // TEST not used
  517. function addNextpageItemByItem($newcontent, container_class){
  518. //trace('MaterioSearchApiAjax :: addNextpageItemByItem()');
  519. $('ul.pager', _$content).remove();
  520. $(container_class, _$content).append($(container_class, $newcontent).children('article').eq(0));
  521. if($(container_class, $newcontent).children('article').length){
  522. setTimeout(function(){
  523. addNextpageItemByItem($newcontent, container_class);
  524. }, 200);
  525. }else{
  526. _isloadingresults = false;
  527. $('ul.pager', _$content).replaceWith($('ul.pager', $newcontent).hide());
  528. $.event.trigger({
  529. type : 'resultscompleted',
  530. container : $(container_class, _$content)
  531. });
  532. }
  533. };
  534. /**
  535. * viewmode
  536. */
  537. function initViewMode(){
  538. $('.viewmode-link').click(function(event){
  539. event.preventDefault();
  540. if(!$(this).is('.active')){
  541. $(this).trigger({type:'vm-clicked'});
  542. changeViewMode($(this).attr('rel'), $(this));
  543. }else{
  544. $(this).trigger({type:'vm-clicked-active'});
  545. }
  546. return false;
  547. });
  548. };
  549. function changeViewMode(vm, $btn){
  550. if(!_isloadingresults){
  551. _isloadingresults = true;
  552. $.getJSON(Drupal.settings.basePath+'materio_search_api_ajax/viewmode/change/'+vm, function(json){
  553. //trace('viewmode json', json);
  554. _isloadingresults = false;
  555. if (json.statut == "saved"){
  556. // google analytics
  557. $.event.trigger({
  558. type : "record-stat",
  559. categorie : 'Viewmode',
  560. action : vm,
  561. label : isActuality() ? 'Actualities' : 'Search results'
  562. });
  563. $.event.trigger('view-mode-changed');
  564. $('.viewmode-link, .viewmode-link i').removeClass('active');
  565. $btn.addClass('active').find('i').addClass('active');
  566. }
  567. });
  568. }
  569. };
  570. function onViewModeChanged(event){
  571. if (isExplore())
  572. loadResults(getSearchKeys());
  573. if(isActuality())
  574. loadActuality();
  575. };
  576. /**
  577. * history
  578. */
  579. function onHistoryStateChange(event){
  580. if(isExplore() || isActuality())
  581. triggerContentChanged();
  582. // TODO: pushstate trogger state change, so we have a retriggerring here on loading new results … how to avoid this
  583. };
  584. /**
  585. * helpers
  586. */
  587. function isExplore(){
  588. return $('.search-results', '#content').length;
  589. };
  590. function isActuality(){
  591. return $('.actuality-items', '#content').length;
  592. };
  593. init();
  594. };
  595. $(document).ready(function() {
  596. var materiosearchapiajax = new MaterioSearchApiAjax();
  597. });
  598. })(jQuery);