source: trunk/www.guidonia.net/wp/wp-content/plugins/tubepress/ui/lib/tubepress.js@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 6.5 KB
Line 
1jQuery.getScript = function(url, callback, cache) {
2 jQuery.ajax({ type: "GET", url: url, success: callback, dataType: "script", cache: cache });
3};
4
5function tubepress_init(baseUrl) {
6 tubepress_load_embedded_js(baseUrl);
7 tubepress_load_players(baseUrl);
8 tubepress_attach_listeners();
9}
10
11function tubepress_attach_listeners()
12{
13 /* any link with an id that starts with "tubepress_" */
14 jQuery("a[id^='tubepress_']").click(function () {
15 var rel_split = jQuery(this).attr("rel").split("_"),
16 galleryId = rel_split[3],
17 playerName = rel_split[2],
18 embeddedName = rel_split[1],
19 videoId = jQuery(this).attr("id").substring(16, 27);
20
21 /* swap the gallery's embedded object */
22 _tubepress_swap_embedded(galleryId, videoId, embeddedName);
23
24 /* then call the player to load up / play the video */
25 _tubepress_call_player_js(galleryId, videoId, embeddedName, playerName);
26 });
27}
28
29/**
30 * This function is very carefully constructed to work with both IE 7-8 and FF.
31 * Modify at your own risk!!
32 *
33 * @param galleryId The unique gallery ID containing the embedded object
34 * to swap
35 * @param videoId The "new" YouTube video ID
36 * @param embeddedName Longtail, YouTube, etc
37 * @return void
38 */
39function _tubepress_swap_embedded(galleryId, videoId, embeddedName) {
40
41 var wrapperId = "#tubepress_embedded_object_" + galleryId,
42 wrapper = jQuery(wrapperId);
43
44 /* if we can't find the embedded object, just bail */
45 if (wrapper.length == 0) {
46 return;
47 }
48
49 var matcher = window["tubepress_" + embeddedName + "_matcher"](),
50 paramName = window["tubepress_" + embeddedName + "_param"](),
51 obj = jQuery(wrapperId + " > object"),
52 oldVideoId = obj.children("param[name='" + paramName + "']").attr("value").match(matcher)[1];
53
54 /* save the params but remove them from the DOM for now */
55 var params = obj.children("param");
56 params.remove();
57
58 /* create the new embedded object */
59 newHtml = tubepress_deep_construct_object(wrapper, params).replace(new RegExp(oldVideoId, 'g'), videoId);
60
61 /* add it back in */
62 wrapper.html(newHtml);
63
64 /* now pat yourself on the back */
65}
66
67function tubepress_deep_construct_object(wrapper, params) {
68
69 var newHtml = wrapper.html();
70
71 /* chop off the closing </object> */
72 var newHtml = newHtml.substring(0, newHtml.length - 9);
73
74 /* now add back the params, but this time with the new video ID */
75 params.each(function() {
76 newHtml += '<param name="' + this.name + '" value="' + this.value + '" />';
77 });
78
79 /* re-close the object */
80 newHtml += '</object>';
81 return newHtml;
82}
83
84function _tubepress_call_player_js(galleryId, videoId, embeddedName, playerName) {
85 var playerFunctionName = "tubepress_" + playerName + "_player";
86 window[playerFunctionName](galleryId, videoId);
87}
88
89
90function tubepress_load_players(baseUrl) {
91 var playerNames = _tubepress_rel_parser(2), i;
92 for(i = 0; i < playerNames.length; i++) {
93 var name = playerNames[i];
94 jQuery.getScript(baseUrl + "/ui/players/" + name + "/" + name + ".js",
95 _tubepress_player_loaded(name, baseUrl) , true);
96 }
97}
98
99function _tubepress_player_loaded(playerName, baseUrl) {
100 var funcName = 'tubepress_' + playerName + '_player_init',
101 f = function() { window[funcName](baseUrl); }
102 _tubepress_call_when_true(function() { return typeof window[funcName] == 'function'; }, f);
103}
104
105function tubepress_load_embedded_js(baseUrl) {
106 var embeddedNames = _tubepress_rel_parser(1), i;
107 for(i = 0; i < embeddedNames.length; i++) {
108 jQuery.getScript(baseUrl + "/ui/embedded/" + embeddedNames[i] + "/" + embeddedNames[i] + ".js", function() {}, true);
109 }
110}
111
112/**
113 * Utility function to do some DOM parsing for other functions here
114 * @param index
115 * @return
116 */
117function _tubepress_rel_parser(index) {
118 var returnValue = [];
119 jQuery("a[rel^='tubepress_']").each( function() {
120 var thisName = jQuery(this).attr("rel").split("_")[index];
121 if (jQuery.inArray(thisName, returnValue) == -1) {
122 returnValue.push(thisName);
123 }
124 });
125 return returnValue;
126}
127
128/**
129 * Waits until the given test is true (tests every .4 seconds),
130 * and then executes the given callback.
131 * @param test
132 * @param callback
133 * @return
134 */
135function _tubepress_call_when_true(test, callback) {
136 /* if the test doesn't pass, try again in .4 seconds */
137 if (!test()) {
138 setTimeout(function() {_tubepress_call_when_true(test, callback);}, 400);
139 return;
140 }
141 /* the test passed, so call the callback */
142 callback();
143}
144
145/**
146 * Convenience method to wait for a script to load, then call some function
147 * from that script.
148 * @param scriptPath
149 * @param test
150 * @param callback
151 * @return
152 */
153function _tubepress_get_wait_call(scriptPath, test, callback) {
154 jQuery.getScript(scriptPath, function() {
155 _tubepress_call_when_true(test, callback);
156 }, true);
157}
158
159 /*
160 * includeMany 1.1.0
161 *
162 * Copyright (c) 2009 Arash Karimzadeh (arashkarimzadeh.com)
163 * Licensed under the MIT (MIT-LICENSE.txt)
164 * http://www.opensource.org/licenses/mit-license.php
165 *
166 * Date: Feb 28 2009
167 */
168 (function(a){a.include=function(e,b){var f=a.include.luid++;var d=function(h,g){if(a.isFunction(h)){h(g);}if(--a.include.counter[f]==0&&a.isFunction(b)){b();}};if(typeof e=="object"&&typeof e.length=="undefined"){a.include.counter[f]=0;for(var c in e){a.include.counter[f]++;}return a.each(e,function(g,h){a.include.load(g,d,h);
169 });}e=a.makeArray(e);a.include.counter[f]=e.length;a.each(e,function(){a.include.load(this,d,null);});};a.extend(a.include,{luid:0,counter:[],load:function(b,c,d){if(a.include.exist(b)){return c(d);}if(/.css$/.test(b)){a.include.loadCSS(b,c,d);}else{if(/.js$/.test(b)){a.include.loadJS(b,c,d);}else{a.get(b,function(e){c(d,e);
170 });}}},loadCSS:function(b,d,e){var c=document.createElement("link");c.setAttribute("type","text/css");c.setAttribute("rel","stylesheet");c.setAttribute("href",b);a("head").get(0).appendChild(c);a.browser.msie?a.include.IEonload(c,d,e):d(e);},loadJS:function(b,d,e){var c=document.createElement("script");
171 c.setAttribute("type","text/javascript");c.setAttribute("src",b);a.browser.msie?a.include.IEonload(c,d,e):c.onload=function(){d(e);};a("head").get(0).appendChild(c);},IEonload:function(d,b,c){d.onreadystatechange=function(){if(this.readyState=="loaded"||this.readyState=="complete"){b(c);}};},exist:function(c){var b=false;
172 a("head script").each(function(){if(/.css$/.test(c)&&this.href==c){return b=true;}else{if(/.js$/.test(c)&&this.src==c){return b=true;}}});return b;}});})(jQuery);
Note: See TracBrowser for help on using the repository browser.