You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

366 lines
8.5 KiB

  1. semantic.playground = {};
  2. window.ui = {};
  3. $.api.settings.api = {
  4. 'getSpecification': 'spec/{$type}.json'
  5. };
  6. $.fn.dropdown.settings.animation.hide = 'none';
  7. $.fn.dropdown.debug = false;
  8. $.fn.checkbox.debug = false;
  9. $.fn.api.debug = false;
  10. $.fn.popup.debug = false;
  11. // ready event
  12. semantic.playground.ready = function() {
  13. // selector cache
  14. var
  15. $uiMenu = $('.ui.menu'),
  16. $elements = $('.element.menu'),
  17. $elementChoice = $elements.find('.selection.dropdown'),
  18. $variations = $('.variation.menu'),
  19. $variationForm = $variations.find('.form'),
  20. $variationChoice = $variations.find('.selection'),
  21. $types = $('.type.menu'),
  22. $typeForm = $types.find('.form'),
  23. $typeChoice = $types.find('.selection'),
  24. $preview = $('.preview.segment'),
  25. $text = $('.text'),
  26. $page = $('.page.column'),
  27. $view = $('.view.buttons .button'),
  28. $previewView = $view.filter('.preview'),
  29. $htmlView = $view.filter('.html'),
  30. $download = $('.download.button'),
  31. $addElement = $('.add.button'),
  32. template = {},
  33. // alias
  34. handler = {
  35. initialize: function() {
  36. template.select = handler.template.compile('select'),
  37. template.checkbox = handler.template.compile('checkbox'),
  38. handler.interface.get('button');
  39. handler.attachEvents();
  40. },
  41. attachEvents: function() {
  42. $elementChoice
  43. .dropdown({
  44. action : 'form',
  45. onChange : function(type) {
  46. $.proxy(handler.interface.setExclusive, this)();
  47. handler.interface.get(type);
  48. }
  49. })
  50. ;
  51. $addElement
  52. .on('click', handler.preview.add)
  53. ;
  54. $previewView
  55. .on('click', handler.view.preview)
  56. ;
  57. $htmlView
  58. .on('click', handler.view.html)
  59. ;
  60. },
  61. preview: {
  62. add: function() {
  63. handler.item.insert( $preview.html() );
  64. },
  65. get: function() {
  66. console.log('Making preview');
  67. var
  68. koan =$typeForm.find('.dropdown:not(.default) input').val() || false,
  69. classNames,
  70. $element,
  71. dummyText = ['Submit', 'Add', 'Create']
  72. ;
  73. classNames = handler.interface.getValues($variationForm);
  74. if(koan) {
  75. $element = $
  76. .zc(koan)
  77. .addClass(classNames.join(' '))
  78. ;
  79. handler.preview.addText($element);
  80. $preview
  81. .empty()
  82. .append($element)
  83. ;
  84. }
  85. // add class names
  86. },
  87. addText: function($element) {
  88. var
  89. $parts = $element.children()
  90. ;
  91. if($parts.size() === 0) {
  92. $element.text('Example');
  93. }
  94. else {
  95. $parts
  96. .each(function() {
  97. handler.preview.preview.addText($(this));
  98. })
  99. ;
  100. }
  101. },
  102. update: function() {
  103. $preview.html( handler.preview.get() );
  104. }
  105. },
  106. types: {
  107. create: function(type) {
  108. var
  109. html = '',
  110. ui = window.ui[type] || false,
  111. format = (ui)
  112. ? ui['Types']
  113. : {}
  114. ;
  115. handler.interface.addForm($typeForm, format);
  116. $typeForm
  117. .find('.dropdown')
  118. .dropdown('setting', 'onChange', handler.interface.setExclusive)
  119. ;
  120. },
  121. toggle: function() {
  122. }
  123. },
  124. variations: {
  125. create: function(type) {
  126. var
  127. html = '',
  128. ui = window.ui[type] || false,
  129. format = (ui)
  130. ? ui['Variations']
  131. : {}
  132. ;
  133. handler.interface.addForm($variationForm, format);
  134. },
  135. toggle: function() {
  136. }
  137. },
  138. interface: {
  139. addForm: function($element, list) {
  140. var
  141. html = ''
  142. ;
  143. $.each(list, function(name, variation) {
  144. // complex variation
  145. if( $.isPlainObject(variation) && variation.selector !== undefined) {
  146. }
  147. // select box
  148. else if( $.isArray(variation) || $.isPlainObject(variation) ) {
  149. html += template.select({
  150. name : name,
  151. values : variation
  152. });
  153. }
  154. // checkbox
  155. else if( typeof variation == 'string') {
  156. html += template.checkbox({
  157. value: variation
  158. });
  159. }
  160. });
  161. $(html)
  162. .appendTo($element)
  163. ;
  164. $element
  165. .find('.dropdown')
  166. .dropdown({
  167. action : 'form',
  168. onChange : handler.preview.update
  169. })
  170. .end()
  171. .find('.checkbox')
  172. .checkbox({
  173. onChange: handler.preview.update
  174. })
  175. ;
  176. return $element;
  177. },
  178. getValues: function($form) {
  179. var
  180. $inputs = $form.find('input'),
  181. classNames = []
  182. ;
  183. $inputs
  184. .each(function() {
  185. var
  186. type = $(this).attr('type'),
  187. value = $(this).val()
  188. ;
  189. if( type == 'hidden' && value != 'none') {
  190. classNames.push(value);
  191. }
  192. else if( type == 'checkbox' && $(this).is(':checked') ) {
  193. classNames.push(value);
  194. }
  195. })
  196. ;
  197. console.log(classNames);
  198. return classNames;
  199. },
  200. setExclusive: function() {
  201. $(this)
  202. .removeClass('default')
  203. .closest('.item').find('.dropdown')
  204. .not( $(this) )
  205. .addClass('default')
  206. .find('.text')
  207. .html('---')
  208. .end()
  209. .find('input')
  210. .val('')
  211. ;
  212. },
  213. clear: function() {
  214. $typeForm.empty();
  215. $variationForm.empty();
  216. },
  217. update: function(type) {
  218. handler.types.create(type);
  219. handler.variations.create(type);
  220. },
  221. get: function(type) {
  222. if(type !== undefined) {
  223. if(window.ui[type] === undefined) {
  224. $.api({
  225. action: 'getSpecification',
  226. urlData: {
  227. type: type
  228. },
  229. success: function(json) {
  230. window.ui[type] = json;
  231. handler.interface.update(type);
  232. },
  233. failure: function() {
  234. window.ui[type] = {};
  235. handler.interface.clear();
  236. }
  237. });
  238. }
  239. else {
  240. handler.interface.update(type);
  241. }
  242. }
  243. }
  244. },
  245. item: {
  246. update: function(type) {
  247. },
  248. change: function(type) {
  249. },
  250. highlight: function() {
  251. },
  252. insert: function(html) {
  253. if( $page.hasClass('default') ) {
  254. $page
  255. .removeClass('default')
  256. .empty()
  257. ;
  258. }
  259. $(html)
  260. .appendTo($page)
  261. ;
  262. },
  263. remove: function() {
  264. }
  265. },
  266. template: {
  267. compile: function(name) {
  268. var template = Handlebars.compile($('script.'+name).html());
  269. return ($.isFunction(template))
  270. ? template
  271. : false
  272. ;
  273. }
  274. },
  275. view: {
  276. preview: function() {
  277. $(this)
  278. .addClass('active')
  279. .siblings()
  280. .removeClass('active')
  281. ;
  282. },
  283. html: function() {
  284. $(this)
  285. .addClass('active')
  286. .siblings()
  287. .removeClass('active')
  288. ;
  289. }
  290. },
  291. components: {
  292. add: function() {
  293. },
  294. remove: function() {
  295. }
  296. },
  297. activate: function(value) {
  298. if(!$(this).hasClass('dropdown')) {
  299. $(this)
  300. .addClass('active')
  301. .closest('.ui.playground')
  302. .find('.item')
  303. .not($(this))
  304. .removeClass('active')
  305. ;
  306. }
  307. }
  308. }
  309. ;
  310. handler.initialize();
  311. };
  312. // attach ready event
  313. $(document)
  314. .ready(semantic.playground.ready)
  315. ;