sample.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*************************************************************************
  2. (c) 2008-2012 Martin Wendt
  3. *************************************************************************/
  4. /*******************************************************************************
  5. * jQuery.skinswitcher plugin.
  6. *
  7. * Change CSS include when combobox selection changes.
  8. * Copyright (c) 2012 Martin Wendt
  9. *
  10. * Usage:
  11. $("select#skinswitcher").skinswitcher({
  12. base: "../src/",
  13. choices: [{name: "XP", value: "xp", href: "skin/ui.fancytree.css"},
  14. {name: "Vista", value: "vista", href: "skin-vista/ui.fancytree.css"},
  15. {name: "Lion", value: "lion", href: "skin-lion/ui.fancytree.css"}
  16. ],
  17. init: "lion"
  18. });
  19. */
  20. (function( $ ) {
  21. var PLUGIN_NAME = "skinswitcher",
  22. defaultOptions = {
  23. /**RegEx that returns prefix, tag, and suffix of the CSS href.*/
  24. skinPattern: "^(\W/skin-)().css$",
  25. mode: "combo", // {String} mode 'combo' or 'radio'
  26. base: "",
  27. choices: []
  28. },
  29. methods = {
  30. init: function(options) {
  31. var opts = $.extend({}, defaultOptions, options),
  32. hrefs = [],
  33. $link = null,
  34. initialChoice = undefined;
  35. this.data("options", opts);
  36. // Find <link> tag, figure out current setting and mark for
  37. // later access
  38. $.each(opts.choices, function(){
  39. hrefs.push(this.href.toLowerCase());
  40. });
  41. $("head link").each(function(){
  42. for(var i=0; i<hrefs.length; i++){
  43. if(this.href.toLowerCase().indexOf(hrefs[i]) >= 0){
  44. $link = $(this);
  45. $link.addClass(PLUGIN_NAME);
  46. initialChoice = opts.choices[i];
  47. }
  48. }
  49. });
  50. return this.each(function() {
  51. // Add options to dropdown list
  52. var $combo = $(this);
  53. $combo.empty();
  54. $.each(opts.choices, function(i, choice){
  55. var $opt = $("<option>", {
  56. text: choice.name,
  57. value: choice.value
  58. }).data("choice", choice);
  59. $combo.append($opt);
  60. });
  61. // Switch include
  62. $combo.change(function(){
  63. var choice = $(":selected", this).data("choice");
  64. $("link.skinswitcher").attr("href", opts.base + choice.href);
  65. });
  66. if(opts.init){
  67. $combo.val(opts.init).change();
  68. }else if (initialChoice){
  69. // select combobox value to match current <link> tag
  70. // decouple this call to prevent IE6 exception
  71. setTimeout(function(){
  72. $combo.val(initialChoice.value);
  73. }, 100);
  74. }
  75. });
  76. },
  77. option: function(name, value) {
  78. var opts = this.data("options");
  79. if(typeof value !== "undefined"){
  80. opts[name] = value;
  81. }else{
  82. return opts[name];
  83. }
  84. },
  85. change: function(href) {
  86. this.val("");
  87. },
  88. reset: function() {
  89. this.val("");
  90. }
  91. };
  92. $.fn[PLUGIN_NAME] = function(method) {
  93. // Method calling logic
  94. if ( methods[method] ) {
  95. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  96. } else if ( typeof method === "object" || ! method ) {
  97. return methods.init.apply(this, arguments);
  98. } else {
  99. $.error("Method " + method + " does not exist on jQuery." + PLUGIN_NAME);
  100. }
  101. };
  102. })( jQuery );
  103. /**
  104. * Replacement for $().toggle(func1, func2), which was deprecated with jQuery 1.8
  105. * and removed in 1.9.;
  106. * Taken from http://stackoverflow.com/a/4911660/19166
  107. * By Felix Kling
  108. */
  109. (function($) {
  110. $.fn.clickToggle = function(func1, func2) {
  111. var funcs = [func1, func2];
  112. this.data('toggleclicked', 0);
  113. this.click(function() {
  114. var data = $(this).data();
  115. var tc = data.toggleclicked;
  116. $.proxy(funcs[tc], this)();
  117. data.toggleclicked = (tc + 1) % 2;
  118. });
  119. return this;
  120. };
  121. }(jQuery));
  122. SAMPLE_BUTTON_DEFAULTS = {
  123. id: undefined,
  124. label: "Sample",
  125. newline: true,
  126. code: function(){ alert("not implemented"); }
  127. };
  128. function addSampleButton(options)
  129. {
  130. var opts = $.extend({}, SAMPLE_BUTTON_DEFAULTS, options),
  131. $container;
  132. $container = $("<span>", {
  133. "class": "sampleButtonContainer"
  134. });
  135. $("<button>", {
  136. id: opts.id,
  137. title: opts.tooltip,
  138. text: opts.label
  139. }).click(function(e){
  140. e.preventDefault();
  141. opts.code();
  142. }).appendTo($container);
  143. $("<a>", {
  144. text: "Source code",
  145. href: "#",
  146. "class": "showCode"
  147. }).appendTo($container)
  148. .click(function(e){
  149. try {
  150. prettyPrint();
  151. } catch (e) {
  152. alert(e);
  153. }
  154. var $pre = $container.find("pre");
  155. if($pre.is(":visible")){
  156. $(this).text("Source code");
  157. }else{
  158. $(this).text("Hide source");
  159. }
  160. $pre.toggle("slow");
  161. return false;
  162. });
  163. var sourceCode = "" + opts.code;
  164. // Remove outer function(){ CODE }
  165. // sourceCode = sourceCode.match(/[]\{(.*)\}/);
  166. sourceCode = sourceCode.substring(
  167. sourceCode.indexOf("{") + 1,
  168. sourceCode.lastIndexOf("}"));
  169. // sourceCode = $.trim(sourceCode);
  170. // Reduce tabs from 8 to 2 characters
  171. sourceCode = sourceCode.replace(/\t/g, " ");
  172. // Format code samples
  173. $("<pre>", {
  174. text: sourceCode,
  175. "class": "prettyprint"
  176. }).hide().appendTo($container);
  177. if(opts.newline){
  178. $container.append($("<br>"));
  179. }
  180. if(opts.header){
  181. $("<h5>", {text: opts.header}).appendTo($("p#sampleButtons"));
  182. }
  183. $container.appendTo($("p#sampleButtons"));
  184. }
  185. function initCodeSamples()
  186. {
  187. var $source = $("#sourceCode");
  188. $("#codeExample").clickToggle(
  189. function(){
  190. $source.show("fast");
  191. if( !this.old ){
  192. this.old = $(this).html();
  193. $.get(this.href, function(code){
  194. // Remove <!-- Start_Exclude [...] End_Exclude --> blocks:
  195. code = code.replace(/<!-- Start_Exclude(.|\n|\r)*?End_Exclude -->/gi, "<!-- (Irrelevant source removed.) -->");
  196. // Reduce tabs from 8 to 2 characters
  197. code = code.replace(/\t/g, " ");
  198. $source.text(code);
  199. // Format code samples
  200. try {
  201. prettyPrint();
  202. } catch (e) {
  203. alert(e);
  204. }
  205. }, "html");
  206. }
  207. $(this).html("Hide source code");
  208. },
  209. function(){
  210. $(this).html(this.old);
  211. $source.hide("fast");
  212. }
  213. );
  214. if(jQuery.ui){
  215. var info = "Fancytree " + jQuery.ui.fancytree.version
  216. + ", jQuery UI " + jQuery.ui.version
  217. + ", jQuery " + jQuery.fn.jquery;
  218. /*
  219. info += "\n<br>";
  220. info += "document.compatMode: " + document.compatMode + "\n";
  221. for(e in jQuery.support){
  222. info += "<br>\n" + e + ": " + jQuery.support[e];
  223. }
  224. */
  225. $("p.sample-links").after("<p class='version-info'>" + info + "</p>");
  226. }
  227. }
  228. var _gaq = _gaq || [];
  229. $(function(){
  230. // Log to Google Analytics, when not running locally
  231. if ( document.URL.toLowerCase().indexOf("wwwendt.de/") >= 0 ) {
  232. _gaq.push(["_setAccount", "UA-316028-1"]);
  233. _gaq.push(["_trackPageview"]);
  234. (function() {
  235. var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;
  236. ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
  237. var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);
  238. })();
  239. }
  240. // Show some elements only, if (not) inside the Example Browser
  241. if (top.location == self.location){
  242. $(".hideOutsideFS").hide();
  243. }else{
  244. $(".hideInsideFS").hide();
  245. }
  246. initCodeSamples();
  247. $("select#skinswitcher").skinswitcher({
  248. base: "../src/",
  249. choices: [{name: "XP", value: "xp", href: "skin-xp/ui.fancytree.css"},
  250. {name: "Vista (classic Dynatree)", value: "vista", href: "skin-vista/ui.fancytree.css"},
  251. {name: "Win7", value: "win7", href: "skin-win7/ui.fancytree.css"},
  252. {name: "Win8", value: "win8", href: "skin-win8/ui.fancytree.css"},
  253. {name: "Lion", value: "lion", href: "skin-lion/ui.fancytree.css"}
  254. ]
  255. // init: "lion"
  256. });
  257. });