APIs

Show:
  1. /**
  2. This generic class manages a set of elements all designed to provide user with UI for font selection
  3. @class FontSelector
  4. @constructor
  5. @return {Object} instantiated FontSelector
  6. **/
  7. define(['jquery', 'backbone', 'minicolors', 'spinner', 'Fonts'], function ($, Backbone, minicolors, spinner, Fonts) {
  8.  
  9. BB.SERVICES.FONT_SELECTOR = 'FontSelector';
  10.  
  11. var FontSelector = BB.View.extend({
  12.  
  13. /**
  14. Init the ChannelList component and enable sortable channels UI via drag and drop operations.
  15. @method initialize
  16. **/
  17. initialize: function () {
  18. var self = this;
  19.  
  20. self.fontList = new Fonts().getFonts();
  21.  
  22. self.m_config = {
  23. bold: false,
  24. italic: false,
  25. underline: false,
  26. alignment: 'left',
  27. font: 'Arial',
  28. color: '#428bca',
  29. size: 12
  30. };
  31.  
  32. self.m_colorSelector = undefined;
  33. self.m_fontSizeSelector = undefined;
  34. self.m_fontSizeInput = undefined;
  35.  
  36. self.m_colorSettings = {
  37. animationSpeed: 50,
  38. animationEasing: 'swing',
  39. change: $.proxy(self._onColorSelected, self),
  40. changeDelay: 100,
  41. control: 'hue',
  42. value: '#428bca',
  43. defaultValue: '#428bca',
  44. hide: null,
  45. hideSpeed: 100,
  46. inline: false,
  47. letterCase: 'lowercase',
  48. opacity: false,
  49. position: 'bottom left',
  50. show: null,
  51. showSpeed: 100,
  52. theme: 'bootstrap'
  53. };
  54.  
  55. _.extend(self.m_colorSettings, self.options['colorSettings']);
  56. self.$el = $(Elements.TEMPLATE_FONT_SELECTOR).clone()
  57. self.el = self.$el[0];
  58. $(self.options.appendTo).append(self.el).fadeIn('fast');
  59. self.$el.show();
  60.  
  61. self._initColorSelector();
  62. self._initFontSelector();
  63. self._initFontSizeSelector();
  64. self._initFontList();
  65. self._delegateAnnounceChange();
  66. var currID = self.$el.attr('id');
  67. self.$el.attr('id', _.uniqueId(currID));
  68. },
  69.  
  70. /**
  71. Global event catcher for UI buttons / selection
  72. @method events
  73. **/
  74. events: {
  75. 'click': '_onClick', 'focusout': function (e) {
  76. var self = this;
  77. if ($(e.target).is("input")) {
  78. self.m_config.size = self.m_fontSizeInput.val();
  79. self._fontModified();
  80. return false;
  81. }
  82. }
  83. },
  84.  
  85. /**
  86. Announce changes to font props
  87. @method _delegateAnnounceChange
  88. **/
  89. _delegateAnnounceChange: function () {
  90. var self = this;
  91. self._fontModified = _.debounce(function (e) {
  92. BB.comBroker.fire(BB.EVENTS.FONT_SELECTION_CHANGED, self, self, self.m_config);
  93. }, 50);
  94. },
  95.  
  96. /**
  97. Redraw UI with updated config data provided in json format
  98. @method render
  99. @param {Object} i_config
  100. **/
  101. _render: function () {
  102. var self = this;
  103.  
  104. self._deSelectFontAlignments();
  105. // self.m_colorSelector.minicolors('value', self.m_config.color);
  106. $('.minicolors-swatch-color', self.$el).css({'backgroundColor': self.m_config.color});
  107.  
  108. var buttonBold = self.m_colorSelector = self.$el.find('[name="bold"]');
  109. self.m_config.bold == true ? buttonBold.addClass('active') : buttonBold.removeClass('active');
  110.  
  111. var buttonUnderline = self.m_colorSelector = self.$el.find('[name="underline"]');
  112. self.m_config.underline == true ? buttonUnderline.addClass('active') : buttonUnderline.removeClass('active');
  113.  
  114. var buttonItalic = self.m_colorSelector = self.$el.find('[name="italic"]');
  115. self.m_config.italic == true ? buttonItalic.addClass('active') : buttonItalic.removeClass('active');
  116.  
  117. self.m_fontSizeSelector.spinner('value', parseInt(self.m_config.size));
  118.  
  119. var buttonName = 'align' + BB.lib.capitaliseFirst(self.m_config.alignment);
  120. var buttonAlignment = self.m_colorSelector = self.$el.find('[name="' + buttonName + '"]');
  121. $(buttonAlignment).addClass('active');
  122.  
  123. $('option:contains("' + self.m_config.font + '")', self.$el).prop('selected', 'selected');
  124. // $('#selDiv option:contains("Selection 1")')
  125. },
  126.  
  127. /**
  128. Initialize the font size spinner UI selector
  129. @method i_config
  130. **/
  131. _initFontSizeSelector: function () {
  132. var self = this;
  133. self.m_fontSizeInput = self.$el.find(Elements.CLASS_SPINNER_INPUT);
  134. self.m_fontSizeSelector = self.m_fontSizeInput.closest('div');
  135. self.m_fontSizeSelector.spinner({value: self.m_config.size, min: 1, max: 127, step: 1});
  136. },
  137.  
  138. /**
  139. Initialize the minicolors UI widget under this created instance
  140. @method _initColorSelector
  141. **/
  142. _initColorSelector: function () {
  143. var self = this;
  144. self.m_colorSelector = self.$el.find(Elements.CLASS_FONT_SELECTOR_MINICOLOR);
  145. self.m_colorSelector.minicolors(self.m_colorSettings);
  146. },
  147.  
  148. /**
  149. Font selection
  150. @method _initFontSelector
  151. @param {Number} i_playerData
  152. @return {Number} Unique clientId.
  153. **/
  154. _initFontSelector: function () {
  155. var self = this;
  156.  
  157. $('.spinner-input', self.$el).on('focusin', function(){
  158. $('.spinner-input', self.$el).one('mouseout', function() {
  159. $('.spinner-input', self.$el).blur();
  160. self._fontModified();
  161. });
  162. });
  163.  
  164. $(Elements.CLASS_FONT_SELECTION, self.$el).on('change', function (e) {
  165. var font = $(e.target).val();
  166. if (font != self.m_config.font) {
  167. self.m_config.font = font;
  168. console.log(self.m_config.font);
  169. BB.comBroker.fire(BB.EVENTS.FONT_SELECTION_CHANGED, self, self, self.m_config);
  170. }
  171. });
  172. },
  173.  
  174. /**
  175. Build list of available fonts to chose from
  176. @method fontList
  177. **/
  178. _initFontList: function () {
  179. var self = this;
  180. var snippet = '';
  181. $.each(self.fontList, function (k, v) {
  182. snippet = snippet + '\n<option>' + v + '</option>';
  183. });
  184. $(Elements.CLASS_FONT_SELECTION, self.$el).append(snippet);
  185. },
  186.  
  187. /**
  188. On new color selected by minicolors
  189. @method _onColorSelected
  190. @param {String} i_color
  191. **/
  192. _onColorSelected: function (i_color) {
  193. var self = this;
  194. self.m_config.color = i_color;
  195. BB.comBroker.fire(BB.EVENTS.FONT_SELECTION_CHANGED, self, self, self.m_config);
  196. },
  197.  
  198. /**
  199. On new font selected by drop down
  200. @method _onFontSelected
  201. @param {Element} i_target
  202. **/
  203. _onFontSelected: function (i_target) {
  204. var self = this;
  205. return false;
  206. },
  207.  
  208. /**
  209. Deselect all font alignments buttons so all are unpressed
  210. @method i_target
  211. **/
  212. _deSelectFontAlignments: function () {
  213. var self = this;
  214. self.$el.find(Elements.CLASS_FONT_ALIGNMENT).removeClass('active');
  215. },
  216.  
  217. /**
  218. Catch all event for any UI that is clicked within this view so we can deal with each action as per the target firing the event
  219. @method _onClick
  220. @param {Event} e
  221. **/
  222. _onClick: function (e) {
  223. var self = this;
  224.  
  225. if ($(e.target).is("select")) {
  226. e.preventDefault();
  227. return;
  228. }
  229.  
  230. var $button = $(e.target).closest('button');
  231. var buttonName = $button.attr('name');
  232.  
  233. if (_.isUndefined(buttonName))
  234. return;
  235.  
  236. switch (buttonName) {
  237. case 'bold':
  238. {
  239. $button.hasClass('active') == true ? self.m_config.bold = false : self.m_config.bold = true;
  240. $button.toggleClass('active');
  241. break;
  242. }
  243. case 'underline':
  244. {
  245. $button.hasClass('active') == true ? self.m_config.underline = false : self.m_config.underline = true;
  246. $button.toggleClass('active');
  247. break;
  248. }
  249. case 'italic':
  250. {
  251. $button.hasClass('active') == true ? self.m_config.italic = false : self.m_config.italic = true;
  252. $button.toggleClass('active');
  253. break;
  254. }
  255. case 'alignLeft':
  256. {
  257. self._deSelectFontAlignments();
  258. self.m_config.alignment = 'left';
  259. $button.toggleClass('active');
  260. break;
  261. }
  262. case 'alignRight':
  263. {
  264. self._deSelectFontAlignments();
  265. self.m_config.alignment = 'right';
  266. $button.toggleClass('active');
  267. break;
  268. }
  269. case 'alignCenter':
  270. {
  271. self._deSelectFontAlignments();
  272. self.m_config.alignment = 'center';
  273. $button.toggleClass('active');
  274. break;
  275. }
  276. case 'fontSizeUp':
  277. {
  278. self.m_config.size = self.m_fontSizeInput.val();
  279. break;
  280. }
  281. case 'fontSizeDown':
  282. {
  283. self.m_config.size = self.m_fontSizeInput.val();
  284. break;
  285. }
  286. }
  287.  
  288. self._fontModified();
  289. return false;
  290. },
  291.  
  292. /**
  293. Set the configuration data for this font selector component
  294. @method setConfig
  295. @param {Object} i_config
  296. **/
  297. setConfig: function (i_config) {
  298. var self = this;
  299. self.m_config = i_config;
  300. self._render()
  301. }
  302. });
  303.  
  304. return FontSelector;
  305.  
  306. });