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.

326 lines
13 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. ---
  2. layout : 'default'
  3. css : 'module'
  4. title : 'UI Modules'
  5. description : 'Modules are interface elements whose behavior is an essential part of their definition'
  6. type : 'Semantic'
  7. ---
  8. <%- @partial('header', { tabs: { overview: 'Overview', init: 'Initializing', behavior: 'Behaviors', settings: 'Settings'} }) %>
  9. <div class="main container">
  10. <div class="ui tab" data-tab="overview">
  11. <p>All official javascript modules in Semantic are designed using a singular design pattern. This pattern allows several useful features common to all javascript components</p>
  12. <div class="ui relaxed list">
  13. <div class="item">
  14. <i class="check icon"></i>
  15. <div class="content">
  16. <div class="header">Run-time Performance Analysis</div>
  17. <p>Semantic modules all provide the ability to log performance traces to the console, allowing you to see which aspects of the module are more or less performant and to track total init time <code>onDomReady</code></p>
  18. </div>
  19. </div>
  20. <div class="item">
  21. <i class="check icon"></i>
  22. <div class="content">
  23. <div class="header">Human Readable Traces</div>
  24. <p>Unlike other component libraries which hides explanations of behavior in inline comments which can only be read by combing the source, semantic modules provide run-time debug output to the javascript console telling you what each component is doing as it is doing it.</p>
  25. </div>
  26. </div>
  27. <div class="item">
  28. <i class="check icon"></i>
  29. <div class="content">
  30. <div class="header">Settings can be overwritten after initialization</div>
  31. <p>Semantic provides methods to set default settings, set settings at initialization, and set settings after initialization, allowing complete flexibility over component behaviors.</p>
  32. </div>
  33. </div>
  34. <div class="item">
  35. <i class="check icon"></i>
  36. <div class="content">
  37. <div class="header">All modules include an initialize and destroy method</div>
  38. <p>All events and metadata are namespaced and can be removed after initialization, modules automatically handle destroy/init events to allow users to lazy-initialize a plugin multiple times with no issues.</p>
  39. </div>
  40. </div>
  41. <div class="item">
  42. <i class="check icon"></i>
  43. <div class="content">
  44. <div class="header">Instance available in metadata</div>
  45. <p>Modules store their instance in metadata meaning that, in a pinch, you can directly modify the instance of a UI element by modifying its properties.</p>
  46. </div>
  47. </div>
  48. </div>
  49. <div class="ui divider"></div>
  50. <a href="/spec/docs/modal.commented.html" class="ui large secondary labeled icon button">
  51. <i class="info icon"></i>
  52. Real World Commented Example
  53. </a>
  54. <a href="/spec/docs/module.commented.html" class="ui large black labeled icon button">
  55. <i class="info icon"></i>
  56. Commented Design Pattern
  57. </a>
  58. </div>
  59. <div class="ui tab" data-tab="init">
  60. <h3 class="ui header">Overview</h3>
  61. <p>Semantic does not automatically attach any events on page load. You must decide which modules to initialize on each page by initializing a module in javascript.</p>
  62. <div class="ui info message">It's okay to initialize an element multiple times, the element will automatically destroy the previous instance and re-initialize with the settings provided.</div>
  63. <h3 class="ui header">Example of Initializing</h3>
  64. <p>The following example shows how to attach behavior to elements on a page using Semantic</p>
  65. <div class="code" data-type="html" data-preview="true">
  66. <div class="ui medium image">
  67. <div class="ui corner label">
  68. <i class="help link icon" data-content="This appears to the right"></i>
  69. </div>
  70. <img src="/images/demo/photo.jpg" data-content="This appears in the default location">
  71. </div>
  72. </div>
  73. <div class="evaluated code" data-type="javascript">
  74. // just initializing
  75. $('.ui.image img')
  76. .popup()
  77. ;
  78. </div>
  79. <div class="evaluated code" data-type="javascript">
  80. // initializing with settings
  81. $('.ui.image .help')
  82. .popup({
  83. position: 'right center'
  84. })
  85. ;
  86. </div>
  87. </div>
  88. <div class="ui tab" data-tab="behavior">
  89. <div class="fixed column">
  90. <div class="content">
  91. <h3 class="ui header">Demo Element</h3>
  92. <i class="demo circular help link icon" data-content="Popup #1"></i>
  93. <i class="demo circular help link icon" data-content="Popup #2"></i>
  94. <div class="ui raised segment">
  95. <div class="ui top attached label">Console</div>
  96. <pre class="console"></pre>
  97. </div>
  98. </div>
  99. </div>
  100. <div class="examples">
  101. <h2 class="ui dividing header">Using Module Behaviors</h2>
  102. <p>Behaviors are an element's API. They invoke functionality or return aspects of the current state for an element</p>
  103. <p>Behaviors can be called using spaced words, camelcase or dot notation. The preferred method however is <b>spaced words</b>. Method lookup is done automatically internally.</p>
  104. <p>Behaviors can be called using the syntax:</p>
  105. <div class="code">
  106. // both are the same
  107. $('.your.element')
  108. .module('behavior name', argumentOne, argumentTwo)
  109. .module('behaviorName', argumentOne, argumentTwo)
  110. ;
  111. </div>
  112. <h3 class="ui header">Common Behaviors</h3>
  113. <p>All modules have a set of core behaviors which allow you to configure the component</p>
  114. <table class="ui celled definition sortable table segment">
  115. <thead>
  116. <th>Name</th>
  117. <th>Usage</th>
  118. </thead>
  119. <tbody>
  120. <tr>
  121. <td>initialize</td>
  122. <td>Initializes an element, adding page event handlers and init data</td>
  123. </tr>
  124. <tr>
  125. <td>destroy</td>
  126. <td>Removes all changes to the page made by initialization</td>
  127. </tr>
  128. <tr>
  129. <td>refresh</td>
  130. <td>Refreshes cached values for a component</td>
  131. </tr>
  132. <tr>
  133. <td>setting(setting, value)</td>
  134. <td>Allows you to modify or read a component setting</td>
  135. </tr>
  136. <tr>
  137. <td>invoke(query, passedArguments, instance)</td>
  138. <td>Internal method used for translating sentence case method into an internal method</td>
  139. </tr>
  140. <tr>
  141. <td>debug(comment, values)</td>
  142. <td>Displays a log if a user has logging enabled</td>
  143. </tr>
  144. <tr>
  145. <td>verbose(comment, values)</td>
  146. <td>Displays a log if a user has verbose logging enabled</td>
  147. </tr>
  148. <tr>
  149. <td>error(name)</td>
  150. <td>Displays a name error message from the component's settings</td>
  151. </tr>
  152. <tr>
  153. <td>performance log(comment, value)</td>
  154. <td>Adds a performance trace for an element</td>
  155. </tr>
  156. <tr>
  157. <td>performance display</td>
  158. <td>Displays current element performance trace</td>
  159. </tr>
  160. </tbody>
  161. </table>
  162. <h2 class="ui dividing header">Examples</h2>
  163. <h3 class="ui header">Overriding Internals</h3>
  164. <p>Internal methods can be overwritten at run-time for individual instances of a module</p>
  165. <div class="evaluated code">
  166. // initialize both popups inline
  167. $('.demo.icon')
  168. .popup({
  169. inline: true
  170. })
  171. ;
  172. //output the first popup's logs to the page
  173. $('.demo.icon').first()
  174. .popup('internal', 'debug', function() {
  175. $('.console')
  176. .append(arguments[0] + "\n")
  177. // scroll to bottom
  178. .prop('scrollTop', $('.console').prop('scrollHeight') )
  179. ;
  180. })
  181. ;
  182. </div>
  183. <h3 class="ui header">Triggering Behavior</h3>
  184. <p>Some behaviors can accept arguments, for example a popup <b>show</b> behavior can accept a callback function. This arbitrary example shows opening a popup then changing its position programatically</p>
  185. <div class="code" data-demo="true">
  186. // Sets a popup to top left position with an offset of negative five
  187. $('.demo.icon').first()
  188. .popup('setting', 'position', 'top right')
  189. .popup('show', function() {
  190. $(this)
  191. .popup('set position', 'right center')
  192. ;
  193. })
  194. ;
  195. </div>
  196. <h3 class="ui header">Returning values</h3>
  197. <p>Behaviors may also provide an API for accessing a module's internal state. For example popups have a method <code>is visible</code> which returns true or false for whether a popup is currently visible.</p>
  198. <div class="code" data-demo="true">
  199. // returns boolean value instead of jQuery chain
  200. $('.demo.icon')
  201. .popup('debug', $('.demo.icon').first().popup('is visible') )
  202. ;
  203. </div>
  204. <div class="code" data-demo="true">
  205. // if selector size > 1 returns array of values [boolean, boolean...]
  206. $('.demo.icon')
  207. .popup('debug', $('.demo.icon').popup('is visible') )
  208. ;
  209. </div>
  210. </div>
  211. </div>
  212. <div class="ui tab" data-tab="settings">
  213. <p>Unlike many javascript components, anything arbitrary in Semantic is a setting. This means no need to dig inside the internals of the component to alter an expected css selector or class name, simply alter the settings object</p>
  214. <h3 class="ui header">Common Settings</h3>
  215. <p>The following is a list of common settings usually found in javascript modules.
  216. <table class="ui celled sortable definition table segment">
  217. <thead>
  218. <th>Name</th>
  219. <th>Usage</th>
  220. </thead>
  221. <tbody>
  222. <tr>
  223. <td>name</td>
  224. <td>Name used in debug logs to differentiate this widget from other debug statements.</td>
  225. </tr>
  226. <tr>
  227. <td>debug</td>
  228. <td>Whether to provide standard debug output to console.</td>
  229. </tr>
  230. <tr>
  231. <td>performance</td>
  232. <td>Whether to provide performance logging to console of internal method calls.</td>
  233. </tr>
  234. <tr>
  235. <td>verbose</td>
  236. <td>Whether to provide extra debug output to console</td>
  237. </tr>
  238. <tr>
  239. <td>namespace</td>
  240. <td>Namespace used for DOM event and metadata namespacing. Allows module's destroy method to not affect other modules.</td>
  241. </tr>
  242. <tr>
  243. <td>metadata</td>
  244. <td>An object containing any metadata attributes used.</td>
  245. </tr>
  246. <tr>
  247. <td>selectors</td>
  248. <td>An object containing all selectors used in the module, these are usually children of the module.</td>
  249. </tr>
  250. <tr>
  251. <td>classNames</td>
  252. <td>An object containing all class names used in the module.</td>
  253. </tr>
  254. <tr>
  255. <td>errors</td>
  256. <td colspan="2">A javascript array of error statements used in the plugin. These may sometimes appear to the user, but most often appear in debug statements.
  257. </td>
  258. </tr>
  259. </tbody>
  260. </table>
  261. <h3 class="ui header">Changing Settings</h3>
  262. <p>The following examples use <a href="/modules/popup.html">popup</a> as an example for how to modify settings</p>
  263. <div class="ui ordered very relaxed list">
  264. <div class="item">
  265. <div class="header">Setting module defaults</div>
  266. Default settings for the module can be overridden by modifying <b>$.fn.module.settings</b>.
  267. <br>
  268. <div class="code">
  269. $.fn.popup.settings.moduleName = 'Godzilla';
  270. </div>
  271. </div>
  272. <div class="item">
  273. <div class="header">At initialization</div>
  274. A settings object can be passed in when initializing the plugin
  275. <br>
  276. <div class="code">
  277. $('.foo')
  278. .popup({
  279. moduleName : 'Godzilla',
  280. verbose : true
  281. })
  282. ;
  283. </div>
  284. </div>
  285. <div class="item">
  286. <div class="header">After initialization</div>
  287. Settings can be changed after a module is initialized by calling the 'settings' method on the module with either a settings object or a name, value pair.
  288. <br>
  289. <div class="code">
  290. $('.foo')
  291. // lets initialize that!
  292. .popup()
  293. // oh wait forgot something
  294. .popup('setting', 'moduleName', 'Godzilla')
  295. // and some more things
  296. .popup('setting', {
  297. debug : true,
  298. verbose : true
  299. })
  300. ;
  301. </div>
  302. </div>
  303. </div>
  304. <h3 class="ui header">Reading Settings</h3>
  305. <p>Settings can also be read programmatically:
  306. <div class="code">
  307. // outputs 'godzilla' (1) or ['godzilla', 'godzilla'...] (2 or more)
  308. $('.foo').popup('setting', 'moduleName');
  309. </div>
  310. </div>
  311. </div>