Permalink
Browse files

initial import - mini.ajax only

  • Loading branch information...
0 parents commit b027eef8d4049a9e868ad9cc101de008d06ba97a @seven1m committed Dec 19, 2008
Showing with 118 additions and 0 deletions.
  1. +37 −0 README.textile
  2. +19 −0 build.rb
  3. +19 −0 license.txt
  4. +11 −0 pkg/ajax.js
  5. +32 −0 src/ajax.src.js
37 README.textile
@@ -0,0 +1,37 @@
+h1. Tim's Mini JavaScript Library
+
+"github.com/seven1m/mini":http://github.com/seven1m/mini
+
+Some handy functions for building light Web 5.0 (beta of course) applications.
+
+h2. mini.ajax
+
+@mini.form.serialize(f)@
+Serializes all the fields in a form so that they can be passed as a query string in the form "arg1=val1&arg2=val2".
+
+@mini.ajax.x()@
+The XMLHttpRequest object (or MS equivalent) used for communication
+
+@mini.ajax.send(u,f,m,a)@
+Send a basic Ajax request.
+
+@mini.ajax.get(url,func)@
+Uses a GET request to query the specified url and return a response to the specified function.
+
+@mini.ajax.gets(url)@
+Uses a GET request to query the specified url and return a response synchronously. Use this sparingly, as synchronous calls can lock up the browser.
+
+@mini.ajax.post(url,func,args)@
+Uses a POST request to query the specified url and return a response to the specified function.
+
+@mini.ajax.update(url,elm,method,args)@
+Uses a POST request to query the specified url and insert the result into the specified element.
+* @method@ = GET or POST (default)
+* @args@ = arguments as string (not used if specifying GET -- instead pass args with url like this: "url?arg=val")
+
+@mini.ajax.submit(url,frm,elm)@
+Used in the onsubmit handler of a function. The form is not submitted the usual way; the form is instead serialized using "ajax.serialize" and submitted using "ajax.post". The result is then inserted into the specified element.
+* @frm@ = form element
+* @elm@ (optional) = element to update with returned content; if blank, returned content will be assumed to be javascript, and will be evaluated
+Example: @<form action="fallback/url" onsubmit="mini.ajax.submit('ajax/form/url', this, 'div_to_update')@
+
19 build.rb
@@ -0,0 +1,19 @@
+#!/usr/bin/env ruby
+
+NAMESPACE = 'mini'
+LIBS = %w(ajax)
+README = 'README.textile'
+
+content = File.read(README).split(/^h2./).first.strip + "\n\n"
+
+LIBS.each do |lib|
+ content << "h2. #{NAMESPACE}.#{lib}\n\n"
+ code = File.read(File.join('src', lib + '.src.js'))
+ code.scan(/(^\/\/.+?)(mini\.[a-z0-9_]+\.[a-z0-9_]+)=function\(([a-z0-9_,]*)\)/m).each do |doc, name, args|
+ content << "@#{name}(#{args})@\n"
+ content << doc.gsub(/^\/\/\s*/, '').sub(/\n+$/, '') + "\n\n"
+ end
+ File.open(File.join('pkg', lib + '.js'), 'w') { |f| f.write(code.gsub(/^\/\/.+\n/, '').gsub(/^\n/, '').gsub(/\n$/, '')) }
+end
+
+File.open(README, 'w') { |f| f.write(content) }
19 license.txt
@@ -0,0 +1,19 @@
+Copyright (c) Timothy R. Morgan
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
11 pkg/ajax.js
@@ -0,0 +1,11 @@
+if(!window.mini)mini={};if(!mini.form)mini.form={};if(!mini.ajax)mini.ajax={};
+if(!window.$)$=function(e){if(typeof e=='string')e=document.getElementById(e);return e};
+function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};
+mini.form.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');};
+mini.ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
+mini.ajax.send=function(u,f,m,a){var x=mini.ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
+mini.ajax.get=function(url,func){mini.ajax.send(url,func,'GET')};
+mini.ajax.gets=function(url){var x=mini.ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
+mini.ajax.post=function(url,func,args){mini.ajax.send(url,func,'POST',args)};
+mini.ajax.update=function(url,elm,method,args){method=method||'POST';var e=$(elm);var f=function(r){e.innerHTML=r};mini.ajax.send(url,f,method,args)};
+mini.ajax.submit=function(url,frm,elm){var e=$(elm);var f=function(r){e?e.innerHTML=r:eval(r)};mini.ajax.post(url,f,mini.form.serialize(frm))};
32 src/ajax.src.js
@@ -0,0 +1,32 @@
+if(!window.mini)mini={};if(!mini.form)mini.form={};if(!mini.ajax)mini.ajax={};
+if(!window.$)$=function(e){if(typeof e=='string')e=document.getElementById(e);return e};
+function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};
+
+// Serializes all the fields in a form so that they can be passed as a query string in the form "arg1=val1&arg2=val2".
+mini.form.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');};
+
+// The XMLHttpRequest object (or MS equivalent) used for communication
+mini.ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
+
+// Send a basic Ajax request.
+mini.ajax.send=function(u,f,m,a){var x=mini.ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
+
+// Uses a GET request to query the specified url and return a response to the specified function.
+mini.ajax.get=function(url,func){mini.ajax.send(url,func,'GET')};
+
+// Uses a GET request to query the specified url and return a response synchronously. Use this sparingly, as synchronous calls can lock up the browser.
+mini.ajax.gets=function(url){var x=mini.ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
+
+// Uses a POST request to query the specified url and return a response to the specified function.
+mini.ajax.post=function(url,func,args){mini.ajax.send(url,func,'POST',args)};
+
+// Uses a POST request to query the specified url and insert the result into the specified element.
+// * @method@ = GET or POST (default)
+// * @args@ = arguments as string (not used if specifying GET -- instead pass args with url like this: "url?arg=val")
+mini.ajax.update=function(url,elm,method,args){method=method||'POST';var e=$(elm);var f=function(r){e.innerHTML=r};mini.ajax.send(url,f,method,args)};
+
+// Used in the onsubmit handler of a function. The form is not submitted the usual way; the form is instead serialized using "ajax.serialize" and submitted using "ajax.post". The result is then inserted into the specified element.
+// * @frm@ = form element
+// * @elm@ (optional) = element to update with returned content; if blank, returned content will be assumed to be javascript, and will be evaluated
+// Example: @<form action="fallback/url" onsubmit="mini.ajax.submit('ajax/form/url', this, 'div_to_update')@
+mini.ajax.submit=function(url,frm,elm){var e=$(elm);var f=function(r){e?e.innerHTML=r:eval(r)};mini.ajax.post(url,f,mini.form.serialize(frm))};

0 comments on commit b027eef

Please sign in to comment.