anapajari s/travail/glanding on hfr/gs; | tiens comme j'avais un peu de temps entre midi et deux et que ton truc m'amusait j'ai fait ça:
Code :
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
- <head>
- <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
- <title>Exemple</title>
- <style type="text/css">
- .grid{
- background-color: #00FFFF;
- }
- .used{
- position: absolute;
- background-color: #0000FF;
- }
- .selected{
- position: absolute;
- background-color: #00FF00;
- }
- .error{
- position: absolute;
- background-color: #FF0000;
- }
- </style>
- <script type="text/javascript">
- function Grid( width, height, arWidth, arHeight){
- this.width = width==null?100:width;
- this.height = height==null?100:height;
- this.arWidth = arWidth==null?10:arWidth;
- this.arHeight = arHeight==null?10:arHeight;
- this.gridNode = document.createElement('div');
- this.gridNode.className = "grid";
- this.gridNode.style.height = this.height+'px';
- this.gridNode.style.width = this.width+'px';
- this.selArray = new Array();
- var self = this;
- this.gridNode.onclick= function (event){
- if(!event) var event=window.event;
- if (document.all){ cX=event.clientX; cY=event.clientY; }
- else {cX=event.pageX; cY=event.pageY; }
- self.addElem(Math.floor((cX - self.pX)/self.arWidth), Math.floor((cY - self.pY)/self.arHeight), 'selected');
-
- }
- document.getElementsByTagName('body')[0].appendChild(this.gridNode);
- this.pX = 0;
- this.pY = 0;
- var element = this.gridNode;
- while (element!=null){
- this.pX +=element.offsetLeft-element.scrollLeft;
- this.pY +=element.offsetTop-element.scrollTop;
- element=element.offsetParent;
- }
- return true;
- }
- Grid.prototype.addElem = function (row,col, type){
- if ( this.isSet(row, col) ){
- return false;
- }
- this.selArray.push({'x':row, 'y':col, 'type':type});
- var arNode = document.createElement('div');
- arNode.row = row;
- arNode.col = col;
- arNode.className=type;
- arNode.style.left = ( arNode.row*this.arWidth + this.pX)+'px';
- arNode.style.top = ( arNode.col*this.arHeight + this.pY)+'px';
- arNode.style.width = this.arWidth+'px';
- arNode.style.height= this.arHeight+'px';
- document.getElementsByTagName('body')[0].appendChild(arNode);
- if ( type!= 'error' && type!='used'){
- var self = this;
- arNode.onclick = function (e){
- self.removeElem(this);
- }
- }
- return true;
- }
- Grid.prototype.removeElem = function ( elem){
- var nA = new Array();
- for (var i=0; i<this.selArray.length; i++){
- if ( this.selArray[i]['x'] != elem.row || this.selArray[i]['y'] != elem.col){
- nA.push( this.selArray[i]);
- }
- }
- this.selArray = nA;
- elem.parentNode.removeChild(elem);
- return true;
- }
- Grid.prototype.dispContent = function(){
- var r = '';
- for(var i =0; i<this.selArray.length; i++){
- r+="row:"+this.selArray[i]['x'] +"\tcol:"+this.selArray[i]['y']+"\ttype:"+this.selArray[i]['type']+"\n";
- }
- return r;
- }
- Grid.prototype.fill = function( type, arr){
- for(var i in arr){
- this.addElem(arr[i]['x'], arr[i]['y'], type)
- }
- return true;
- }
- Grid.prototype.isSet = function (row, col){
- for(var i =0; i<this.selArray.length; i++){
- if ( this.selArray[i]['x'] == row && this.selArray[i]['y'] == col ){
- return true;
- }
- }
- return false;
- }
- function main(){
- var gs = new Grid(400,400, 20, 20);
- var usedArea = new Array(
- {'x':1, 'y':1},
- {'x':4, 'y':6},
- {'x':7, 'y':5},
- {'x':3, 'y':4}
- );
- gs.fill('used', usedArea);
- var errorArea = new Array(
- {'x':7, 'y':7},
- {'x':5, 'y':4},
- {'x':7, 'y':6},
- {'x':10, 'y':10}
- );
- gs.fill('error', errorArea);
- var expNode = document.createElement('input');
- expNode.setAttribute('type','button');
- expNode.value='Afficher';
- expNode.onclick = function (e){
- alert(gs.dispContent());
- }
- document.getElementsByTagName('body')[0].appendChild(expNode);
- }
- window.onload=main;
- </script>
- </head>
- <body>
- </body>
- </html>
|
Ca doit faire ce que tu veux ( si j'ai tout compris )...
Il ne te reste qu'a initialiser les tableaux used et error pour que ça remplisse correctement en fonction de la bdd.
Et oui je sais y'a pas de commentaires mais j'avais pas trop le temps! |