PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/js/fontpicker/fontpicker.js

https://bitbucket.org/maleck13/storybooks
JavaScript | 140 lines | 108 code | 9 blank | 23 comment | 6 complexity | b08105c448b4bf4351111c893ca1d768 MD5 | raw file
  1. /**
  2. * Font Selector - jQuery plugin 0.1
  3. *
  4. * Copyright (c) 2012 Chris Dyer
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
  12. * disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
  13. * and the following disclaimer in the documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  16. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  17. * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  19. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  21. * SUCH DAMAGE.
  22. *
  23. */
  24. (function( $ ) {
  25. var settings;
  26. var methods = {
  27. init : function(options) {
  28. settings = $.extend( {
  29. 'hide_fallbacks' : false,
  30. 'selected' : function(style) {},
  31. 'initial' : '',
  32. 'fonts' : []
  33. }, options);
  34. var root = this;
  35. root.callback = settings['selected'];
  36. var visible = false;
  37. var selected = false;
  38. var displayName = function(font) {
  39. if (settings['hide_fallbacks'])
  40. return font.substr(0, font.indexOf(','));
  41. else
  42. return font;
  43. }
  44. var select = function(font) {
  45. root.find('span').html(displayName(font).replace(/["']{1}/gi,""));
  46. root.css('font-family', font);
  47. selected = font;
  48. root.callback(selected);
  49. }
  50. var positionUl = function() {
  51. var left, top;
  52. left = $(root).offset().left;
  53. top = $(root).offset().top + $(root).outerHeight();
  54. $(ul).css({
  55. 'position': 'absolute',
  56. 'left': left + 'px',
  57. 'top': top + 'px'
  58. });
  59. }
  60. // Setup markup
  61. $(this).prepend('<span>' + settings['initial'].replace(/'/g,'&#039;') + '</span>');
  62. var ul = $('<ul class="fontSelectUl"></ul>').appendTo('body');
  63. ul.hide();
  64. positionUl();
  65. for (var i = 0; i < settings['fonts'].length; i++) {
  66. var item = $('<li>' + displayName(settings['fonts'][i]) + '</li>').appendTo(ul);
  67. $(item).css('font-family', settings['fonts'][i]);
  68. }
  69. if (settings['initial'] != '')
  70. select(settings['initial']);
  71. ul.find('li').click(function() {
  72. if (!visible)
  73. return;
  74. positionUl();
  75. ul.slideUp('fast', function() {
  76. visible = false;
  77. });
  78. select($(this).css('font-family'));
  79. });
  80. $(this).click(function(event) {
  81. if (visible)
  82. return;
  83. event.stopPropagation();
  84. positionUl();
  85. ul.slideDown('fast', function() {
  86. visible = true;
  87. });
  88. });
  89. $('html').click(function() {
  90. if (visible)
  91. {
  92. ul.slideUp('fast', function() {
  93. visible = false;
  94. });
  95. }
  96. })
  97. },
  98. selected : function() {
  99. return this.css('font-family');
  100. },
  101. select : function(font) {
  102. this.find('span').html(font.substr(0, font.indexOf(',')).replace(/["']{1}/gi,""));
  103. this.css('font-family', font);
  104. selected = font;
  105. }
  106. };
  107. $.fn.fontSelector = function(method) {
  108. if ( methods[method] ) {
  109. return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
  110. } else if ( typeof method === 'object' || ! method ) {
  111. return methods.init.apply( this, arguments );
  112. } else {
  113. $.error( 'Method ' + method + ' does not exist on jQuery.fontSelector' );
  114. }
  115. }
  116. }) ( jQuery );