materio_user.min.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. MaterioUser = function(){
  314. var _isAdhesion = $('body').is('.page-node-11187') || $('body').is('.page-node-11186');
  315. function init() {
  316. //trace('MaterioUser init compile test 3');
  317. if(_isAdhesion)
  318. initAdhesion();
  319. };
  320. function initAdhesion(){
  321. // trace('initAdhesion');
  322. $('.get-link a').bind('click', function(event) {
  323. // trace('get-link click');
  324. // do not show the registration form if already logged-in
  325. if($('body').is('.logged-in'))
  326. return true;
  327. // else show the ajaxified registration form
  328. event.preventDefault();
  329. var $this = $(this),
  330. href = $this.attr("href"),
  331. destination = href.match('/\?destination=([^,]+)');
  332. destination = "/" + destination[1].replace('%23', '#');
  333. destination = destination.replace('//', '/');
  334. // trace('destination', destination);
  335. if($this.parents('.gratos').size()){
  336. loadRegisterBlock(destination);
  337. }else{
  338. loadLoginAndRegisterBlock(destination);
  339. }
  340. return false;
  341. });
  342. };
  343. function loadRegisterBlock (destination) {
  344. // trace("loadRegisterBlock :: dest = "+destination);
  345. $.getJSON(Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_user/registerblock', function(json){formLoaded(json, destination);});
  346. }
  347. function loadLoginAndRegisterBlock(destination){
  348. // trace('loadRegistrationBlock :: dest = '+destination);
  349. $.getJSON(Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_user/loginandregisterblock', function(json){formLoaded(json, destination);});
  350. };
  351. function formLoaded(json, destination){
  352. //trace('formLoaded | json', json);
  353. var $modal = $('<div>').addClass('modal-content').append(json.block);
  354. var id,label, description;
  355. $('input.form-text', $modal).each(function(i) {
  356. id = $(this).attr('id');
  357. label = $('label[for='+id+']').hide().text();
  358. description = $(this).parent().find('.description').hide().text();
  359. $(this).attr('placeholder', label).attr('title', description);
  360. });
  361. // $('.homepage-textfield', $modal).hide();
  362. $('body').append($('<div>').addClass('modal-wrapper').append($modal));
  363. $('.modal-wrapper').bind('click', function(event) {
  364. $(this).remove();
  365. });
  366. $('.modal-content').bind('click', function(event) {
  367. event.stopPropagation();
  368. });
  369. // doesn't work
  370. // Drupal.attachBehaviors('bodi>.modal-wrapper');
  371. $.event.trigger('ajax-register-block-loaded');
  372. $("#user-register-form #edit-submit, #user-register-form #edit-submit--2", $modal).click(function(event){
  373. event.preventDefault();
  374. // disable submit button to avoid duplicate user creation
  375. // console.log('click', this);
  376. $(this).attr('disabled', 'true');
  377. $.ajax({
  378. type: 'POST',
  379. dataType:'json',
  380. url: Drupal.settings.basePath+Drupal.settings.pathPrefix+"materio_user/register/submit",
  381. data: $('#user-register-form', $modal).serialize(),
  382. success: function(data) { onUserLoginRegisterSubmit($('#user-register-form'), data, destination);},
  383. error: function(jqXHR, textStatus, errorThrown) { trace('error : '+textStatus+' | '+errorThrown); }
  384. });
  385. return false;
  386. });
  387. $("#user-login #edit-submit", $modal).click(function(event){
  388. event.preventDefault();
  389. $.ajax({
  390. type: 'POST',
  391. dataType:'json',
  392. url: Drupal.settings.basePath+Drupal.settings.pathPrefix+"materio_user/login/submit",
  393. data: $('#user-login', $modal).serialize(),
  394. success: function(data){ onUserLoginRegisterSubmit($('#user-login'), data, destination); },
  395. error: function(jqXHR, textStatus, errorThrown) { trace('error : '+textStatus+' | '+errorThrown); }
  396. });
  397. return false;
  398. });
  399. // google analytics
  400. // $.event.trigger({
  401. // type : "record-stat",
  402. // categorie : 'Search',
  403. // action : keys,
  404. // label : 'filters : '+ stringTypes.join(' ,'),
  405. // value : json.count
  406. // });
  407. };
  408. function onUserLoginRegisterSubmit($form, data, destination){
  409. // console.log('data', data);
  410. cleanModalErrors();
  411. if(data.errors != null){
  412. for(field in data.errors){
  413. $('input[name='+field+']', $form).addClass('error');
  414. $form.prepend($('<div>').addClass('message-error').html(data.errors[field]));
  415. }
  416. (function($form){
  417. setTimeout(function(){
  418. console.log('enabled');
  419. $('input[type=submit]', $form).removeAttr('disabled');
  420. }, 0.5*1000);
  421. })($form);
  422. }else{
  423. // trace('destination = '+destination);
  424. window.location.pathname = destination;
  425. }
  426. };
  427. function cleanModalErrors() {
  428. $('.message-error', '.modal-content').remove();
  429. $('input', '.modal-content').removeClass('error');
  430. }
  431. init();
  432. };
  433. $(document).ready(function() {
  434. var materiouser = new MaterioUser();
  435. });
  436. })(jQuery);