вторник, 19 января 2010 г.

Пишем свой jquery

http://blog.notdot.net/2010/01/Writing-your-own-webapp-framework-for-App-Engine

Ну раз сделал блог надо что-нибудь написать.
Приведу пример того что получилось. Это фактически то, что хотелось очень давно без всякого jquer-евского бреда и нагромождения, дальше будет больше...
Всё с комментами

001function $$(id)  // Упрощение доступа к элементу
002{
003return document.getElementById(id);
004}
005   
006function $(id)
007{
008   return new $.any(id);
009}
010 
011$.alert = function(data) // Вывести алерт Пример: $.alert('Привет');
012{
013 var id=this.id;
014 alert(data)
015return this;
016};
017      
018 $.any = function (id)
019{
020   this.id = id;
021};
022  
023 $.any.prototype = $.prototype;
024 
025$.prototype.text = function(data) // Вставка текста в область элемента
026{                                           // Пример: $('work').text('Привет');
027 var id=this.id;
028 $$(id).innerHTML=data;
029return this;
030}
031 
032$.prototype.css = function(property,data) // Задание css свойству значения Пример: $('div1').css('border','1px solid #888888');
033{
034 var id=this.id;
035$$(id).style[property]=data;
036return this;
037}
038 
039$.prototype.load = function(data) // Ajax загрузка
040{                                           // Пример: $('work').load('data/program/main.php');
041 var id=this.id;
042 var xml;
043 
044  if(window.ActiveXObject)
045  {
046   try
047    {
048    xml=new ActiveXObject("Microsoft.XMLHTTP");
049   }
050   catch (e)
051   {
052    xml=false;
053   }
054  }
055  else
056  {
057    try
058    {
059    xml=new XMLHttpRequest();
060   }
061   catch (e)
062   {
063    xml=false;
064   }
065  }
066 
067  if(!xml) alert("Error creating the XMLHttpRequest object");
068 
069  
070  xml.onreadystatechange = function()
071  {
072     if(xml.readyState != 4 || xml.readyState==0)
073     {  
074    $$(id).innerHTML=" <img src='/img/pb.gif'> ";
075     }
076     if(xml.readyState == 4)
077     {
078    if(xml.status == 200)
079    {
080       $(id).text(xml.responseText);
081    }  
082    else   
083    {
084      $(id).text("Error: returned status code " + xml.status + " " + xml.statusText);
085    }  
086     }
087  };
088 xml.open("GET", data, true);
089 xml.send(null);
090return this;
091}
092 
093$.prototype.img = function(src)  // Вставить картинку в текстовую область Пример: $('work').img('img/pb.gif');
094{
095 var id=this.id;
096 var sorce="<img src='"+src+"'>";
097 $$(id).innerHTML=source;
098  
099return this;
100}
101  
102$.prototype.show = function() // Показать элемент Пример: $('leftmenu').show();
103{
104 var id = this.id;
105 $(id).css('display','table');
106return this;
107}
108 
109$.prototype.hide = function() // Скрыть элемент Пример: $('leftmenu').hide();
110{
111 var id = this.id;
112 $(id).css('display','none');
113 
114return this;
115}
116  
117$.prototype.сlass = function(name)  // Задать имя класса
118{                                                // Пример:  $('div1').class('picked');
119 var id = this.id;
120 $$(id).className=name;
121 
122 return this;
123}
124 
125$.prototype.opacity = function(value) // Изменение прозрачности от 0 до 100
126{                                                  //Пример: $('imglogo').opacity(50);
127 var id = this.id;
128 $(id).css('opacity',value/100);
129 $(id).css('filter','alpha(opacity='+value+')');
130return this;
131}
132 
133$.prototype.position = function(x,y,type) // Устанавливаем позицию элемента
134{                   // type :absolute | fixed | relative | static Пример:$('imglogo').position(100,100,'absolute');
135 var id = this.id;
136 $(id).css('position',type);
137 $(id).css('left',x);
138 $(id).css('top',y);
139  
140return this;
141}

Комментариев нет:

Отправить комментарий