// * date 2011/10/19 * /** * misc */ // ------ TRACE ------ function trace(s,o){ if(window.console && window.console.debug){ arguments.length === 2 ? window.console.debug(s,o) : window.console.debug(s); }else if(console && console.log){ arguments.length === 2 ? console.log(s,o) : console.log(s); } }; // ------ LOADJSCSSFILE ------ function loadjscssfile(filename, filetype){ if (filetype=="js"){ //if filename is a external JavaScript file var fileref=document.createElement('script') fileref.setAttribute("type","text/javascript") fileref.setAttribute("src", filename) } else if (filetype=="css"){ //if filename is an external CSS file var fileref=document.createElement("link") fileref.setAttribute("rel", "stylesheet") fileref.setAttribute("type", "text/css") fileref.setAttribute("href", filename) } if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref) }; // ------ TIMEOUT function Timeout(fn, interval) { var id = setTimeout(fn, interval); this.cleared = false; this.clear = function () { this.cleared = true; clearTimeout(id); }; } // ------ HEXTORGB convert hex to rgb color ------ function HexToRGB(h){ if(h.charAt(0)=="#"){ var coul = h.substring(1,7); var r = parseInt(coul.substring(0,2),16); var g = parseInt(coul.substring(2,4),16); var b = parseInt(coul.substring(4,6),16); return 'rgb('+r+', '+g+', '+b+')'; }else{ return null; } } // ------ PAIR check if number is pair of impair ------ function pair(n){return typeof n=='number'?(n%2==0?true:false):null;} // ------ RANDOM ------ function random(n1,n2){ 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 ); } function randomInt(n1,n2){ 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 ); return parseInt(rand); } // ------ ROUNDED ------ function round(n,granularity){ if(arguments.length == 1){ n = Math.round(n); }else{ granularity = granularity.toString(); granularity = granularity.replace(/./gi, 0); granularity = granularity.replace(/^./gi, 1); granularity = parseInt(granularity); n = Math.round(n*granularity)/granularity; } return n; } // ------ FLOOR ------ function floor(n,granularity){ if(arguments.length == 1){ n = Math.floor(n); }else{ granularity = granularity.toString(); granularity = granularity.replace(/./gi, 0); granularity = granularity.replace(/^./gi, 1); granularity = parseInt(granularity); n = Math.floor(n*granularity)/granularity; } return n; } // ------ CEIL ------ function ceil(n,granularity){ if(arguments.length == 1){ n = Math.ceil(n); }else{ granularity = granularity.toString(); granularity = granularity.replace(/./gi, 0); granularity = granularity.replace(/^./gi, 1); granularity = parseInt(granularity); n = Math.ceil(n*granularity)/granularity; } return n; } /** * String */ // ------ TRIM remove multiple, leading or trailing spaces ------ String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/gi,"").replace(/[ ]{2,}/gi," ").replace(/\n /,"\n"); } // ------ remove all accents, for sort() by example ------ 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"}; String.prototype.latinise=function(){return this.replace(/[^A-Za-z0-9\[\] ]/g,function(a){return Latinise.latin_map[a]||a})}; String.prototype.latinize=String.prototype.latinise; String.prototype.isLatin=function(){return this==this.latinise()} /** * Array */ // ------ UNIQUE reduces duplicates ------ array_unique = function(ar){ var r = new Array(); o:for(var i = 0, n = ar.length; i < n; i++) { for(var x = 0, y = r.length; x < y; x++) { if(r[x]==ar[i]) { continue o; } } r[r.length] = ar[i]; } return r; }; // ------ SUBSTRACT substract two array ------ array_subtract = function(ara1,ara2) { var aRes = new Array() ; for (var i = ara1.length-1; i >= 0; i--) if(array_contains(ara2,ara1[i])) aRes.push(ara1[i]); return aRes ; } /* SHUFFLE Add a shuffle function to Array object prototype author: Stephane Roucheray src: http://sroucheray.org/blog/2009/11/array-sort-should-not-be-used-to-shuffle-an-array/ found 2011/09/08. */ array_shuffle = function(ar){ var i = ar.length, j, temp; if ( i == 0 ) return; while ( --i ) { j = Math.floor( Math.random() * ( i + 1 ) ); temp = ar[i]; ar[i] = ar[j]; ar[j] = temp; } return ar; }; // ------ CONTAINS ------ array_contains = function(ar, obj) { var i = ar.length; while (i--) if (ar[i] === obj) return true; return false; }; // ------ REMOVE ------ // /!\ Found on the Internet, not sure of efficiency. // Array.prototype.remove = function(v) { // var x, _i, _len, _results; // _results = []; // for (_i = 0, _len = this.length; _i < _len; _i++) { // x = this[_i]; // if (x !== v) { // _results.push(x); // } // } // return _results; // }; array_remove = function(ar){ var what, a= arguments, L= a.length, ax; while(L && ar.length){ what= a[--L]; while((ax= ar.indexOf(what))!= -1){ ar.splice(ax, 1); } } return ar; }; // for IE // if(!Array.prototype.indexOf){ // Array.prototype.indexOf= function(what, i){ // i= i || 0; // var L= this.length; // while(i< L){ // if(this[i]=== what) return i; // ++i; // } // return -1; // }; // } array_indexOf = function(ar, what, i){ i= i || 0; var L= ar.length; while(i< L){ if(ar[i]=== what) return i; ++i; } return -1; }; // ------ SUBSTRACT substract two array ------ function array_subtract(ara1,ara2) { var aRes = new Array() ; for (var i = ara1.length-1; i >= 0; i--) if( !array_contains(ara1[i],ara2) ) aRes.push(ara1[i]); return aRes ; }; /** * Objects * */ // ------ OBJECTSIZE get the length of objects ------ function objectSize(o) { var len = o.length ? --o.length : 0; for (var k in o) len++; return len; } // Object.prototype.size = function () { // // var len = this.length ? --this.length : -1; // POURQUOI NE FONCTIONNE PAS? // var len = this.length ? --this.length : -2; // for (var k in this) // len++; // return len; // } function objectIsEmpty(obj){ for (var prop in obj) { if (obj.hasOwnProperty(prop)) return false; } return true; }; // ------ OBJECTGETKEY get key of index ------ function objectGetKey(o,index){ var i = 0; for (var k in o){ if (k === 'length' || !o.hasOwnProperty(k)) continue; if(i == index) return k; i ++; } } // Object.prototype.getKey = function(index){ // var i = 0; // for (var key in this){ // if (key === 'length' || !this.hasOwnProperty(key)) // continue; // if(i == index) // return key; // i ++; // } // } // ------ OBJECTSORT sort object ------ function objectSort(o,value) { var connection = new Array(); for (var i = objectSize(o) - 1; i >= 0; i--){ var k = objectGetKey(o,i); for (var j = objectSize(o[k].wit) - 1; j >= 0; j--){ var k2 = objectGetKey(o[k].wit,j); if (k2==value) { var the_wit = parseFloat(o[k].wit[k2]); connection.push( {wit:the_wit,nid:k} ); }; }; }; connection.sort(function(a,b){ // return (a.wit - b.wit);//ascending return (b.wit - a.wit);//descending }); var o_sort = {}; for (var i = connection.length - 1; i >= 0; i--){ // o_sort[connection[i].wit+'-'+connection[i].nid] = o[connection[i].nid]; o_sort[connection[i].nid] = o[connection[i].nid]; }; return o_sort; } /** * trigo */ // -------- getElementAngle ---------- // L'angle 0 correspond au nord (vers le haut) // Exemple : getElementAngle(posElementX, posElementY, posSourisX, posSourisY) // Ceci retournera l'angle de l'élément par rapport à la position de la souris function getElementAngle(x1, y1, x2, y2) { var adj = x2 - x1; var opp = y2 - y1; var angle = Math.abs(Math.atan(opp/adj) * 180/Math.PI); if (adj > 0 && opp < 0 ) { angle = 90 - angle; } else if (adj >= 0 && opp >= 0) { angle += 90; } else if (adj < 0 && opp >= 0) { angle = 180 + (90 - angle); } else { angle += 270; } return angle; } // @codekit-prepend "gui.js" // @koala-prepend "gui.js" (function($) { MaterioFlag = function(){ var _isLoadingList = false ; /** * init() */ function init(){ //trace('MaterioFlag :: init MaterioFlag'); buildBlocks(); $(document) .bind('flagGlobalAfterLinkUpdate', onFlaging) .bind('resultscompleted resultschanged previewloaded', onResultsUpdated) .bind('init-scroller-pager', onInitScrollerPager) .bind('load-scroller-pager', onLoadScrollerPager) .bind('view-mode-changed', onViewModeChanged) .bind('history-state-change', onHistoryStateChange); // ajaxifyLinks(); // trigger updated event for direct html loading if(isList()){ setTimeout(function(){ triggerContentChanged(); }, 10); } }; function onFlaging(event){ //trace('MaterioFlag :: onFlaging', event); refreshBlocks(); }; function onResultsUpdated(event){ //trace('MaterioFlag :: onResultsUpdated', event); ajaxifyLinks(event.container); }; function buildBlocks(activename){ //trace('MaterioFlag :: buildBlocks | activename', activename); if($('#block-materio-flag-materio-flag-mybookmarks').length){ var type = 'bookmarks'; var block = '#block-materio-flag-materio-flag-mybookmarks'; }else if($('#block-materio-flag-materio-flag-mylists').length){ var type = 'lists'; var block = '#block-materio-flag-materio-flag-mylists'; } switch(type){ case 'bookmarks': var name = type; $('h2 .listname', block).attr('name', name).bind('click', onClickShowPreview); $('').appendTo($('h2', block)).attr('name', name).bind('click', onClickClosePreview); // $('').appendTo($('h2', block)).bind('click', onClickShowPreview); // if(!readCookie('materiobookmarkspreviewopened')){ // showPreview('bookmarks', block); // }else{ // } break; case 'lists': // nav block $('a.open-list:not(.ajax-processed)', '#block-materio-flag-materio-flag-mylists-nav').each(function(index){ //trace('nav block a.open-list this', this); $this = $(this) .bind('click', onClickOpenLink) .addClass('ajax-processed'); var name = $this.attr('class').match(/flag_lists_[^_]+_[0-9]+/); // trace('MaterioFlag :: name', name); $('').attr('name', name).insertAfter($this).bind('click', onClickShowPreview); }); $('a.edit-list:not(.ajax-processed)', '#block-materio-flag-materio-flag-mylists-nav') .bind('click', onCLickEditList) .addClass('ajax-processed'); $('a.flag-lists-create:not(.ajax-processed)', '#block-materio-flag-materio-flag-mylists-nav') .bind('click', onClickCreatLink) .addClass('ajax-processed'); // preview block $('section.flag-list:not(.ajax-processed)', '#block-materio-flag-materio-flag-mylists').each(function(index){ var name = $(this).attr('class').match(/flag_lists_[^_]+_[0-9]+/); $('').appendTo($('h2.listname', this)).attr('name', name).bind('click', onClickClosePreview); $('a.open-list', this).bind('click', onClickOpenLink); }).addClass('ajax-processed'); break; } // trigger refresh block event for enabling lazyload images setTimeout(function(){ $.event.trigger({ type : 'my'+type+'-block-builded', block : block, name : name }); },10); // trace('MaterioFlag :: activename', activename); if(activename == undefined) activename = readCookie('materiomyflaglistsopened'); // trace('MaterioFlag :: activename', activename); if(activename) showPreview(activename, block); }; function refreshBlocks(name){ //trace('MaterioFlag :: refreshBlocks | name', name); if($('#block-materio-flag-materio-flag-mybookmarks').length){ var type = 'bookmarks'; }else if($('#block-materio-flag-materio-flag-mylists').length){ var type = 'lists'; } if(type != undefined){ var id = '#block-materio-flag-materio-flag-my'+type; var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/refresh/block/'+type; $.getJSON(url, function(json){ // trace('MaterioFlag :: block refreshed '+type, json); $(id).replaceWith(json.block); $('#block-materio-flag-materio-flag-mylists-nav').replaceWith(json.block_nav); buildBlocks(name); $.event.trigger({ type : 'my'+type+'-block-updated', listname : name }); }); } }; function ajaxifyLinks(container){ //trace('MaterioFlag :: ajaxifyLinks', container); container = ((container != null) ? container : 'body'); // trace('MaterioFlag :: typeof Drupal.flagLink', typeof Drupal.flagLink); if (typeof Drupal.flagLink != 'undefined') Drupal.flagLink(container); if(isList()){ var fid = $('.materio-flags-list', '#content').attr('fid'); $('li.unflag-action.fid-'+fid+' a:not(.ajax-processed), li.flag-bookmarks a.unflag-action:not(.ajax-processed)') .bind('click', onUnflagList) .addClass('ajax-processed'); } $('a.flag-lists-create:not(.ajax-processed)', container) .bind('click', onClickCreatLink) .addClass('ajax-processed'); }; /** * show hide preview */ function onClickShowPreview(event){ //trace('MaterioFlag :: onClickShowPreview', event); showPreview($(this).attr('name'), $(this).parent('.block').attr('id')); }; function showPreview(name, block){ //trace('MaterioFlag :: showPreview', name); $('section.'+name, block).addClass('active') .siblings('section').removeClass('active'); createCookie('materiomyflaglistsopened', name, 1); $.event.trigger('init-layout'); }; function onClickClosePreview(event){ //trace('MaterioFlag :: onClickClosePreview', event); eraseCookie('materiomyflaglistsopened'); if($(this).attr('name') == 'bookmarks'){ $(this).parents('.block').find('section.bookmarks').removeClass('active'); }else{ $(this).parents('section.flag-list').removeClass('active'); } $.event.trigger('init-layout'); }; /** * onClickOpenLink */ function onClickOpenLink(event){ event.preventDefault(); var $link = $(event.currentTarget); var fid = $link.attr('href').match(/lists\/([0-9]+)$/); // trace('MaterioFlag :: type', type); loadList(fid[1]); return false; }; function loadList(fid){ //trace('MaterioFlag :: loadList | fid', fid); var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/ajax/list/'+fid; $.event.trigger('loading-content'); $.getJSON(url, {'current_path':document.location.href},function(json){ //trace('MaterioFlag :: json', json); if(json.redirect){ window.location = json.redirect; }else{ changeContent(json); } }); }; function changeContent(json){ if(json.rendered){ $('.inner-content','#content').html(json.rendered); $.event.trigger('loaded-content'); // no need of ajaxifylinks because it's triggered with resultschanged // ajaxifyLinks('#content'); var path = Drupal.settings.basePath + Drupal.settings.pathPrefix + json.path; $.event.trigger({ type : 'new-history-page', path : path, title : json.title, content : json.rendered }); // TODO: change language links for folders // for (language in Drupal.settings.materio_search_api_ajax.languages) { // var l = Drupal.settings.materio_search_api_ajax.languages[language]; // $('#block-locale-language li.'+language+' a').attr('href', Drupal.settings.basePath + l.prefix+'/' + json.search_path + '/' + json.keys) // }; triggerContentChanged(); }else{ //trace('MaterioFlag :: no results'); } }; function triggerContentChanged(){ $.event.trigger({ type: 'resultschanged', container : '#content .flaglist-items' }); }; /** * onClickCreatLink(event) */ function onClickCreatLink(event){ //trace('MaterioFlag :: onClickCreatLink | event', event); event.preventDefault(); var $link = $(event.currentTarget); var type = $link.attr('href').match(/[^\/]*$/); // trace('MaterioFlag :: type', type); var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/createlist/form/'+type[0]; $.getJSON(url, function(json){ //trace('MaterioFlag :: creat list : json', json); showCreateListForm(json, $link); }); return false; }; function showCreateListForm(json, $link){ //trace('MaterioFlag :: showCreateListForm | json', json); // google analytics $.event.trigger({ type:"record-stat", categorie:"flagLists", action: 'show create form' }); var $modal = $('
').appendTo('body'); $modal .css({ position:'absolute', top:'40%', left:'50%', marginLeft:'-150px', width:'300px', zIndex:"99999" }) .append(json.rendered_form) .find('input[type="submit"]', '#materio-flag-create-list-form').bind('click', function(event) { event.preventDefault(); switch($(this).attr('name')){ case 'cancel': //trace('MaterioFlag :: cancel',event); $(this).parents('#modal').remove(); // google analytics $.event.trigger({ type:"record-stat", categorie:"flagLists", action: 'cancel create form' }); break; case 'create': //trace('MaterioFlag :: create',event); var title = $(this).parents('form').find('input[name*="flag-lists-name"]').val(); var type = $(this).parents('form').find('input[name*="type"]').val(); // google analytics $.event.trigger({ type : "record-stat", categorie : "flagLists", action : "submit create form", label : 'title : '+title }); createList($modal, type, title, $link); break; } return false; }) .parents('form').find('input[type="text"]').focus(); // TODO: esc keypressed close the form }; function createList($modal, type, title, $link){ //trace('materioflag :: createList | title', title); $('.flag-lists-create').addClass('loading'); var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'flag-lists/add/'+type+'/js'; $.getJSON(url, {name:title}, function(data) { if (data.error) { //trace(data.error); } else { // select.append(''); // $('input.name', $(this)).val(''); // dialog.dialog('close'); //trace('MaterioFlag :: created list : data', data); if($link.attr('nid') && $link.attr('token')){ flagEntityWithList(data.flag.name, $link.attr('nid'), $link.attr('token')); }else{ refreshBlocks(data.flag.name); refreshNodeLinks(); } $modal.remove(); } }); }; function flagEntityWithList(name, nid, token){ //trace('MaterioFlag :: flagEntityWithList | name', name); // var ret; // Send POST request $.ajax({ type: 'POST', url: Drupal.settings.basePath+Drupal.settings.pathPrefix+'flag-lists/flag/'+name+'/'+nid, data: { js: true, token: token }, dataType: 'json', success: function (data2) { //trace('MaterioFlag :: node taged with newly created list : data2', data2) if (data2.status) { // google analytics $.event.trigger({ type : "record-stat", categorie : 'FlagLists', action : 'node flaged', label : 'nid : '+nid+' | flag : '+name }); refreshBlocks(name); refreshNodeLinks(); }else { // Failure. alert(data2.errorMessage); } }, error: function (xmlhttp) { alert('An HTTP error '+ xmlhttp.status +' occurred.\n'+ element.href); } }); }; function refreshNodeLinks(){ //trace('MaterioFlag :: refreshNodeLinks'); var nids = new Array(); $('.flag-lists-entity-links') // .addClass('loading') .parents('.node') .each(function(index) { nids.push($(this).attr('class').match(/node-([0-9]+)/)[1]); }); // trace('MaterioFlag :: nids', nids); var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/nodelinks'; $.getJSON(url, {nids:nids.join(";")}, function(data) { // trace('MaterioFlag :: data', data); for(nid in data.links){ // trace('MaterioFlag :: nid', nid); // trace('MaterioFlag :: data.links[nid]', data.links[nid]); $('.node-'+nid+' .flag-lists-entity-links').replaceWith(data.links[nid]); // trace('MaterioFlag :: typeof Drupal.flagLink', typeof Drupal.flagLink); // if (typeof Drupal.flagLink != 'undefined') // Drupal.flagLink($('.node-'+nid+' .flag-lists-entity-links')); // TODO: sortir ajaxifyLinks de la boucle, je pense que ça prend trop de ressources ajaxifyLinks('.node-'+nid+' .flag-lists-entity-links'); } }); $.event.trigger({ type : 'materioflag-nodelinks-updated', nids : nids }); }; /** * onCLickEditList(event) */ function onCLickEditList(event){ //trace('MaterioFlag :: onCLickEditList | event', event); // TODO: empécher le double formulaire event.preventDefault(); var $link = $(event.currentTarget); var lid = $link.attr('href').match(/[^\/]*$/); var type = 'materiau'; // this is cheap var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/editlistform/'+type+'/'+lid[0]; $.getJSON(url, function(json){ //trace('MaterioFlag :: editlist : json', json); showEditListForm(json, $link); }); return false; }; function showEditListForm(json, $link){ //trace('MaterioFlag :: showEditListForm | json', json); // google analytics $.event.trigger({ type:"record-stat", categorie:"flagLists", action: 'show edit form' }); var $modal = $('').appendTo('body'); $modal .css({ position:'absolute', top:'40%', left:'50%', marginLeft:'-150px', width:'300px', zIndex:"99999" }) .append(json.rendered_form) .find('input[type="submit"]', '#materio-flag-edit-list-form').bind('click', function(event) { event.preventDefault(); var $form = $(this).parents('form'); var title = $form.find('input[name*="flag-lists-title"]').val(); var fid = $form.find('input[name*="fid"]').val(); var name = $form.find('input[name*="name"]').val(); switch($(this).attr('name')){ case 'cancel': //trace('MaterioFlag :: cancel',event); $(this).parents('#modal').remove(); // google analytics var action = 'cancel edit form'; break; case 'save': //trace('MaterioFlag :: create',event); // google analytics var action = "submit edit form"; saveList($modal, fid, name, title); break; case 'delete': //trace('MaterioFlag :: delete',event); if(confirm('Do you realy want to delete your '+title+' folder ?')){ var action = "submit delete form"; deleteList($modal, fid); }else{ var action = "cancel delete form"; } break; } // google analytics $.event.trigger({ type:"record-stat", categorie:"flagLists", action: action }); return false; }) .parents('form').find('input[type="text"]').focus(); // TODO: esc keypressed close the form }; function saveList($modal, fid, name, title){ //trace('MaterioFlag :: saveList | fid : '+fid+'| name', name); $('.flag-lists-link.fid-'+fid).addClass('loading'); var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/editlist/'+fid+'/'+name+'/'+title; $.getJSON(url, function(data) { if (data.error) { // trace(data.error); if(data.message) alert(data.message); } else { //trace('MaterioFlag :: saved list : data', data); $.event.trigger({ type : 'list-edited', name : data.listname, title : data.title, }); refreshBlocks(); refreshNodeLinks(); $modal.remove(); } }); }; function deleteList($modal, fid){ //trace('MaterioFlag :: deletelist | fid', fid); $('.flag-lists-link.fid-'+fid).hide(); var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/deletelist/'+fid; $.getJSON(url, function(data) { if (data.error) { // trace(data.error); if(data.message) alert(data.message); } else { //trace('MaterioFlag :: deleted list : data', data); refreshBlocks(); refreshNodeLinks(); // TODO: if the deleted list was the current displayed list ?? $modal.remove(); } }); }; /** * onUnflagList() */ function onUnflagList(event){ //trace('onUnflagList', event); $(this).parents('article.node').addClass('removed'); }; /** * */ function onInitScrollerPager(event){ // trace('MaterioFlag :: MaterioFlag :: onInitScrollerPager'); if (isList()){ // trace('MaterioFlag :: event.pager', event); event.pager.hide(); } }; function onLoadScrollerPager(event){ if (isList()) loadNextListPage(event.href); }; function loadNextListPage(href){ // trace('MaterioFlag :: loadNextListPage', href); if(!_isLoadingList){ var fid = href.match(/lists\/([^\/|\?]+)/); var page = href.match(/\?page=([0-9]+)/); var url = Drupal.settings.basePath+Drupal.settings.pathPrefix+'materioflag/ajax/list/'+fid[1]+'/'+page[1]; // trace('MaterioFlag :: url', url); loadNextPage(url, $('.materio-flags-list', '#content'), '.flaglist-items'); } }; function loadNextPage(url, $container, target){ //trace('MaterioFlag :: loadNextPage'); _isLoadingList = true; $container.addClass('loading'); $.getJSON(url, function(json){ //trace('json', json); _isLoadingList = false; $container.removeClass('loading'); addNextpage(json, target); }); }; function addNextpage(json, container_class){ var $newcontent = $(json.rendered), $newitems = $(container_class, $newcontent).children('article').addClass('just-added'), $newpager = $('ul.pager', $newcontent); $(container_class, '#content').append($newitems); $('ul.pager', '#content').replaceWith($newpager.hide()); // TODO: animation, this should be on theme side $(container_class, '#content').children('.just-added').each(function(i){ // $(this).delay(5000*i).removeClass('just-added'); var $this = $(this); setTimeout(function(){ $this.removeClass('just-added'); }, 150*i); }); $.event.trigger({ type : 'resultscompleted', container : $(container_class, '#content') }); }; function onViewModeChanged(event){ if (isList()) loadList(getFid()); }; /** * history */ function onHistoryStateChange(event){ if(isList()) triggerContentChanged(); }; /** * Helpers */ function getFid(){ return $('.materio-flags-list', '#content').attr('fid');; }; function isList(){ return $('.materio-flags-list', '#content').length; }; /** * cookies */ function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } function eraseCookie(name) { createCookie(name,"",-1); } init(); }; $(document).ready(function() { var materioflag = new MaterioFlag(); }); })(jQuery);