Skip to content
Browse files

add start of FilosofoJS from SVN export

  • Loading branch information...
0 parents commit 5c24eaa2cad3c0a43f3084231c2393ebf4d7857d @matzko committed
Showing with 387 additions and 0 deletions.
  1. +354 −0 client-files/js/filosofo-js.dev.js
  2. +1 −0 client-files/js/filosofo-js.js
  3. +32 −0 wp-filosofo-js-library.php
354 client-files/js/filosofo-js.dev.js
@@ -0,0 +1,354 @@
+var FilosofoJS = function(scope) {
+ var addEvent = function( obj, type, fn ) {
+ if (obj.addEventListener)
+ obj.addEventListener(type, fn, false);
+ else if (obj.attachEvent)
+ obj.attachEvent('on' + type, function() { return fn.call(obj, window.event);});
+ },
+
+ d = document,
+
+ XHR = (function() {
+ var i,
+ fs = [
+ function() { // for legacy eg. IE 5
+ return new scope.ActiveXObject("Microsoft.XMLHTTP");
+ },
+ function() { // for fully patched Win2k SP4 and up
+ return new scope.ActiveXObject("Msxml2.XMLHTTP.3.0");
+ },
+ function() { // IE 6 users that have updated their msxml dll files.
+ return new scope.ActiveXObject("Msxml2.XMLHTTP.6.0");
+ },
+ function() { // IE7, Safari, Mozilla, Opera, etc (NOTE: IE7 native version does not support overrideMimeType or local file requests)
+ return new XMLHttpRequest();
+ }];
+
+ // Loop through the possible factories to try and find one that
+ // can instantiate an XMLHttpRequest object that works.
+
+ for ( i = fs.length; i--; ) {
+ try {
+ if ( fs[i]() ) {
+ return fs[i];
+ }
+ } catch (e) {}
+ }
+ })(),
+
+ /**
+ * Post a xhr request
+ * @param url The url to which to post
+ * @data The associative array of data to post, or a string of already-encoded data
+ * @callback The method to call upon success
+ */
+ postReq = function(url, data, callback) {
+ url = url || 'admin-ajax.php';
+ data = data || {};
+ var dataString, request = new XHR;
+ dataString = serialize(data);
+ try {
+ if ( 'undefined' == typeof callback ) {
+ callback = function() {};
+ }
+ request.open('POST', url, true);
+ request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
+ request.onreadystatechange = function() {
+ if ( 4 == request.readyState ) {
+ request.onreadystatechange = function() {};
+ if ( 200 <= request.status && 300 > request.status || ( 'undefined' == typeof request.status ) )
+ callback(request.responseText);
+ }
+ }
+ request.send(dataString);
+ } catch(e) {};
+ },
+
+ /**
+ * Whether the property is of this particular object's
+ * @param obj The object whose property we're interested in.
+ * @param property The property which we're interested in.
+ * @return true if The property does not originate higher in the prototype chain.
+ */
+ isObjProp = function(obj, property) {
+ var p = obj.constructor.prototype[property];
+ return ( 'undefined' == typeof p || property !== obj[p] );
+ },
+
+ /**
+ * Serialize an associative array
+ * @param array a The associative array to serialize.
+ * @uses urlencode, isObjProp
+ * @return string The serialized string.
+ */
+ serialize = function(a) {
+ var i, j, s = [];
+ for( i in a ) {
+ if ( isObjProp(a, i) ) {
+ // if the object is an array itself
+ if ( '[]' == i.substr(i.length - 2, i.length) ) {
+ for ( j = 0; j < a[i].length; j++ ) {
+ s[s.length] = urlencode(i) + '=' + urlencode(a[i][j]);
+ }
+ } else {
+ s[s.length] = urlencode(i) + '=' + urlencode(a[i]);
+ }
+ }
+ }
+ return s.join('&');
+ },
+
+ urlencode = (function() {
+ var f = function(s) {
+ return encodeURIComponent(s).replace(/%20/,'+').replace(/(.{0,3})(%0A)/g,
+ function(m, a, b) {return a+(a=='%0D'?'':'%0D')+b;}).replace(/(%0D)(.{0,3})/g,
+ function(m, a, b) {return a+(b=='%0A'?'':'%0A')+b;});
+ };
+
+ if (typeof encodeURIComponent != 'undefined' && String.prototype.replace && f('\n \r') == '%0D%0A+%0D%0A') {
+ return f;
+ }
+ })(),
+
+ /**
+ * Get the object that was the target of an event
+ * @param object e The event object (or null for ie)
+ * @return object The target object.
+ */
+ getEventTarget = function(e) {
+ e = e || window.event;
+ return e.target || e.srcElement;
+ },
+
+
+ /**
+ * Animation methods
+ */
+ lerp = function(start, end, value) {
+ return ( ( 1 - value ) * start ) + ( value * end );
+ },
+
+ hermite = function(start, end, value) {
+ var i = lerp(start, end, value * value * ( 3 - 2 * value ));
+ return i;
+ },
+
+ inProgress = false,
+ /**
+ * Scroll to the given element.
+ *
+ * @uses hermite
+ * @uses inProgress
+ */
+ scrollToElement = function(el) {
+ if ( inProgress )
+ return;
+
+ var elCopy = el,
+ elTop = 0,
+ browserTop = 0,
+ rate = 25,
+ time = 400,
+ steps = time / rate,
+ distance, inc, i;
+ // assign element's position from the top to elTop
+ while ( elCopy.offsetParent && elCopy != d.dElement ) {
+ elTop += elCopy.offsetTop;
+ elCopy = elCopy.offsetParent;
+ }
+
+ elTop = elTop - 30;
+ elTop = 0 > elTop ? 0 : elTop;
+
+ // assign browser's position from the top to browserTop
+ if ( d.documentElement && d.documentElement.scrollTop ) {
+ browserTop = d.documentElement.scrollTop;
+ } else if ( d.body && d.body.scrollTop ) {
+ browserTop = d.body.scrollTop;
+ } else if ( d.getElementsByTagName('body') ) {
+ browserTop = d.getElementsByTagName('body')[0].scrollTop;
+ }
+
+ // distance = Math.abs(browserTop - elTop);
+ distance = browserTop - elTop;
+ inc = distance / steps;
+ for ( i = 0; i < steps; i++ ) {
+ (function() {
+ var pos = Math.ceil(browserTop - (hermite(0, 1, (i / steps)) * distance)),
+ k = i,
+ last = ( i + 1 ) < steps ? false : true;
+
+ setTimeout(function() {
+ if ( last ) {
+ inProgress = false;
+ }
+ scrollTo(0, pos);
+ }, k * rate);
+ })();
+ }
+ },
+
+ Animation = function(diff, callback) {
+ return {
+ animate:function() {
+ if ( this.inProgress )
+ return;
+ this.inProgress = true;
+
+ callback = callback || function() {};
+
+ var rate = 20,
+ time = 500,
+ steps = time / rate,
+ i,
+ last = false,
+ state,
+ that = this;
+
+
+ for ( i = 0; i < steps; i++ ) {
+ last = ( i + 1 ) < steps ? false : true;
+ state = 0 < diff ? hermite(0, 1, (i / steps)) * diff : hermite(1, 0, (i / steps)) * diff;
+ (function(cb) {
+ var k = i,
+ l = last,
+ curDiff = state;
+ setTimeout(function() {
+ if ( l )
+ that.inProgress = false;
+ cb.apply(that, [curDiff, l]);
+ }, k * rate);
+ })(callback);
+ }
+ }
+ }
+ },
+
+ fade = function(obj, dir, callback) {
+ if ( ! obj )
+ return;
+ dir = dir || -1;
+ callback = callback || function(){};
+ if ( -1 === dir ) {
+ obj.style.opacity = 1;
+ obj.style.filter = 'alpha(opacity=100)';
+ } else if ( 1 === dir ) {
+ obj.style.opacity = 0;
+ obj.style.filter = 'alpha(opacity=0)';
+ obj.style.display = 'block';
+ }
+
+
+ var fadeCallback = function(curDiff, isLast) {
+ var o = 100 + curDiff * dir;
+ obj.style.opacity = o / 100;
+ obj.style.filter = 'alpha(opacity=' + o + ')';
+ if ( isLast ) {
+ callback.call(obj);
+ if ( -1 === dir )
+ obj.style.display = 'none';
+ else
+ obj.style.display = 'block';
+ }
+ },
+ animator;
+
+ if ( obj ) {
+ if ( -1 === dir ) {
+ animator = new Animation(100, fadeCallback),
+ animator.animate();
+ } else {
+ animator = new Animation(-100, fadeCallback);
+ animator.animate();
+ }
+ }
+ },
+
+ /**
+ * End animation
+ */
+
+ /**
+ * Start custom event handlers
+ */
+
+ /**
+ * Assign the callback to be triggered when an element of that class
+ * or one of its descendents is clicked.
+ * @param string className The name of the class to check for.
+ * @param function callback The callback to call.
+ * The first argument passed to callback is the event object.
+ * The value of -this- within the callback is the element with the given class.
+ * @param DOMElement parentEl Optional. The element to which to attach the delegated event listener.
+ * Default is document
+ */
+ attachClassClickListener = function( className, callback, parentEl ) {
+ if ( ! parentEl )
+ parentEl = d;
+ if ( ! className || ! callback )
+ return false;
+
+ (function(className, callback, parentEl) {
+ var re = new RegExp( '\\b' + className + '\\b' );
+ addEvent( parentEl, 'click', function(e) {
+ var result = true,
+ target = getEventTarget(e);
+ do {
+ if ( target.className && re.exec( target.className ) ) {
+ result = callback.call( target, e );
+ if ( ! result ) {
+ if ( e.stopPropagation )
+ e.stopPropagation();
+ if ( e.preventDefault )
+ e.preventDefault();
+ e.cancelBubble = true;
+ e.returnValue = false;
+ return false;
+ } else {
+ return true;
+ }
+ } else {
+ target = target.parentNode;
+ }
+ } while ( target && target != parentEl );
+ });
+ })(className, callback, parentEl);
+ },
+
+ /**
+ * End custom event handlers
+ */
+
+ /**
+ * Ready a callback for whichever occurs first: DOMContentLoaded or window.onload
+ *
+ * @param function callback The callback to call at that event.
+ */
+ ready = function( callback ) {
+ if ( callback )
+ loadedCallback = callback;
+ addEvent(d, 'DOMContentLoaded', eventDOMLoaded );
+ addEvent(window, 'load', eventDOMLoaded );
+ },
+
+ initialized = false,
+ loadedCallback = function() {},
+ eventDOMLoaded = function() {
+ if ( initialized ) {
+ return false;
+ }
+ initialized = true;
+
+ loadedCallback();
+ }
+
+ return {
+ addEvent:addEvent,
+ attachClassClickListener:attachClassClickListener,
+ doWhenReady:ready,
+ fade:fade,
+ getEventTarget:getEventTarget,
+ postReq:postReq,
+ scrollToElement:scrollToElement
+ }
+}
1 client-files/js/filosofo-js.js
@@ -0,0 +1 @@
+var FilosofoJS=function(a){var o=function(w,v,d){if(w.addEventListener){w.addEventListener(v,d,false)}else{if(w.attachEvent){w.attachEvent("on"+v,function(){return d.call(w,window.event)})}}},s=document,j=(function(){var v,d=[function(){return new a.ActiveXObject("Microsoft.XMLHTTP")},function(){return new a.ActiveXObject("Msxml2.XMLHTTP.3.0")},function(){return new a.ActiveXObject("Msxml2.XMLHTTP.6.0")},function(){return new XMLHttpRequest()}];for(v=d.length;v--;){try{if(d[v]()){return d[v]}}catch(w){}}})(),u=function(d,x,z){d=d||"admin-ajax.php";x=x||{};var v,w=new j;v=n(x);try{if("undefined"==typeof z){z=function(){}}w.open("POST",d,true);w.setRequestHeader("Content-Type","application/x-www-form-urlencoded");w.onreadystatechange=function(){if(4==w.readyState){w.onreadystatechange=function(){};if(200<=w.status&&300>w.status||("undefined"==typeof w.status)){z(w.responseText)}}};w.send(v)}catch(y){}},b=function(w,d){var v=w.constructor.prototype[d];return("undefined"==typeof v||d!==w[v])},n=function(d){var w,v,x=[];for(w in d){if(b(d,w)){if("[]"==w.substr(w.length-2,w.length)){for(v=0;v<d[w].length;v++){x[x.length]=k(w)+"="+k(d[w][v])}}else{x[x.length]=k(w)+"="+k(d[w])}}}return x.join("&")},k=(function(){var d=function(v){return encodeURIComponent(v).replace(/%20/,"+").replace(/(.{0,3})(%0A)/g,function(x,y,w){return y+(y=="%0D"?"":"%0D")+w}).replace(/(%0D)(.{0,3})/g,function(x,y,w){return y+(w=="%0A"?"":"%0A")+w})};if(typeof encodeURIComponent!="undefined"&&String.prototype.replace&&d("\n \r")=="%0D%0A+%0D%0A"){return d}})(),r=function(d){d=d||window.event;return d.target||d.srcElement},h=function(w,d,v){return((1-v)*w)+(v*d)},e=function(x,d,w){var v=h(x,d,w*w*(3-2*w));return v},f=false,c=function(w){if(f){return}var d=w,D=0,B=0,A=25,y=400,C=y/A,v,x,z;while(d.offsetParent&&d!=s.dElement){D+=d.offsetTop;d=d.offsetParent}D=D-30;D=0>D?0:D;if(s.documentElement&&s.documentElement.scrollTop){B=s.documentElement.scrollTop}else{if(s.body&&s.body.scrollTop){B=s.body.scrollTop}else{if(s.getElementsByTagName("body")){B=s.getElementsByTagName("body")[0].scrollTop}}}v=B-D;x=v/C;for(z=0;z<C;z++){(function(){var G=Math.ceil(B-(e(0,1,(z/C))*v)),E=z,F=(z+1)<C?false:true;setTimeout(function(){if(F){f=false}scrollTo(0,G)},E*A)})()}},t=function(d,v){return{animate:function(){if(this.inProgress){return}this.inProgress=true;v=v||function(){};var y=20,C=500,w=C/y,x,A=false,B,z=this;for(x=0;x<w;x++){A=(x+1)<w?false:true;B=0<d?e(0,1,(x/w))*d:e(1,0,(x/w))*d;(function(D){var F=x,E=A,G=B;setTimeout(function(){if(E){z.inProgress=false}D.apply(z,[G,E])},F*y)})(v)}}}},q=function(x,v,y){if(!x){return}v=v||-1;y=y||function(){};if(-1===v){x.style.opacity=1;x.style.filter="alpha(opacity=100)"}else{if(1===v){x.style.opacity=0;x.style.filter="alpha(opacity=0)";x.style.display="block"}}var w=function(A,z){var B=100+A*v;x.style.opacity=B/100;x.style.filter="alpha(opacity="+B+")";if(z){y.call(x);if(-1===v){x.style.display="none"}else{x.style.display="block"}}},d;if(x){if(-1===v){d=new t(100,w),d.animate()}else{d=new t(-100,w);d.animate()}}},p=function(v,w,d){if(!d){d=s}if(!v||!w){return false}(function(z,A,y){var x=new RegExp("\\b"+z+"\\b");o(y,"click",function(D){var B=true,C=r(D);do{if(C.className&&x.exec(C.className)){B=A.call(C,D);if(!B){if(D.stopPropagation){D.stopPropagation()}if(D.preventDefault){D.preventDefault()}D.cancelBubble=true;D.returnValue=false;return false}else{return true}}else{C=C.parentNode}}while(C&&C!=y)})})(v,w,d)},l=function(d){if(d){m=d}o(s,"DOMContentLoaded",i);o(window,"load",i)},g=false,m=function(){},i=function(){if(g){return false}g=true;m()};return{addEvent:o,attachClassClickListener:p,doWhenReady:l,fade:q,getEventTarget:r,postReq:u,scrollToElement:c}};
32 wp-filosofo-js-library.php
@@ -0,0 +1,32 @@
+<?php
+/*
+Plugin Name: Filosofo JavaScript Library
+Plugin URI:
+Description: A library of common JS functionality used by Austin Matzko (filosofo) in WordPress plugins.
+Author: Austin Matzko
+Author URI: http://austinmatzko.com
+Version: 1.0
+*/
+
+if ( ! function_exists( 'load_filosofo_js_common_library' ) ) {
+
+ function load_filosofo_js_common_library()
+ {
+ wp_register_script(
+ 'filosofo-common-js',
+ plugin_dir_url( __FILE__ ) . 'client-files/js/filosofo-js.js',
+ null,
+ '1.0'
+ );
+ }
+
+ function init_filosofo_js_common_library()
+ {
+ wp_enqueue_script( 'filosofo-common-js' );
+ }
+
+ add_action( 'init', 'init_filosofo_js_common_library' );
+ add_action( 'plugins_loaded', 'load_filosofo_js_common_library' );
+}
+
+// eof

0 comments on commit 5c24eaa

Please sign in to comment.
Something went wrong with that request. Please try again.