theme.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import testUtils from '@ckeditor/ckeditor5-ui/tests/_utils/utils';
  7. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  8. import Model from '@ckeditor/ckeditor5-ui/src/model';
  9. import View from '@ckeditor/ckeditor5-ui/src/view';
  10. import Template from '@ckeditor/ckeditor5-ui/src/template';
  11. import IconView from '@ckeditor/ckeditor5-ui/src/icon/iconview';
  12. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  13. import createListDropdown from '@ckeditor/ckeditor5-ui/src/dropdown/list/createlistdropdown';
  14. import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  15. import ToolbarSeparatorView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarseparatorview';
  16. import InputTextView from '@ckeditor/ckeditor5-ui/src/inputtext/inputtextview';
  17. import LabeledInputView from '@ckeditor/ckeditor5-ui/src/labeledinput/labeledinputview';
  18. import boldIcon from '@ckeditor/ckeditor5-basic-styles/theme/icons/bold.svg';
  19. import italicIcon from '@ckeditor/ckeditor5-basic-styles/theme/icons/italic.svg';
  20. import '../../theme/theme.scss';
  21. class TextView extends View {
  22. constructor() {
  23. super();
  24. this.element = document.createTextNode( 'Sample text' );
  25. }
  26. }
  27. class ToolbarNewlineView extends View {
  28. constructor() {
  29. super();
  30. this.template = new Template( {
  31. tag: 'span',
  32. attributes: {
  33. class: 'ck-toolbar__newline'
  34. }
  35. } );
  36. }
  37. }
  38. const ui = testUtils.createTestUIView( {
  39. 'iconPlain1': '#icon-plain-1',
  40. 'iconPlain2': '#icon-plain-2',
  41. 'iconColor1': '#icon-color-1',
  42. 'iconColor2': '#icon-color-2',
  43. 'buttonStates': '#button-states',
  44. 'buttonTypes': '#button-types',
  45. 'buttonIcon': '#button-icon',
  46. 'buttonCustom': '#button-custom',
  47. 'buttonIconCustom': '#button-icon-custom',
  48. 'buttonIconStates': '#button-icon-states',
  49. 'buttonResponsive1': '#button-responsive-1',
  50. 'buttonResponsive2': '#button-responsive-2',
  51. 'buttonResponsive3': '#button-responsive-3',
  52. 'buttonTooltip': '#button-tooltip',
  53. dropdown: '#dropdown',
  54. 'toolbarText': '#toolbar-text',
  55. 'toolbarButton': '#toolbar-button',
  56. 'toolbarRounded': '#toolbar-rounded',
  57. 'toolbarWrap': '#toolbar-wrap',
  58. 'toolbarSeparator': '#toolbar-separator',
  59. 'toolbarMultiRow': '#toolbar-multi-row',
  60. 'inputLabeled': '#input-labeled',
  61. 'inputReadOnly': '#input-read-only'
  62. } );
  63. renderIcon();
  64. renderButton();
  65. renderDropdown();
  66. renderToolbar();
  67. renderInput();
  68. function renderIcon() {
  69. // --- In-text ------------------------------------------------------------
  70. ui.iconPlain1.add( icon( boldIcon ) );
  71. ui.iconPlain2.add( icon( italicIcon ) );
  72. ui.iconColor1.add( icon( boldIcon ) );
  73. ui.iconColor2.add( icon( italicIcon ) );
  74. }
  75. function renderButton() {
  76. // --- States ------------------------------------------------------------
  77. ui.buttonStates.add( button( {
  78. label: 'State: normal (none)',
  79. } ) );
  80. ui.buttonStates.add( button( {
  81. label: 'State: disabled',
  82. isEnabled: false
  83. } ) );
  84. ui.buttonStates.add( button( {
  85. label: 'State: on',
  86. isOn: true
  87. } ) );
  88. // --- Types ------------------------------------------------------------
  89. const actionButton = button( { label: 'Action button' } );
  90. const roundedButton = button( { label: 'Rounded corners' } );
  91. const boldButton = button( { label: 'Bold text' } );
  92. // TODO: It requires model interface.
  93. actionButton.element.classList.add( 'ck-button-action' );
  94. // TODO: It requires model interface.
  95. roundedButton.element.classList.add( 'ck-rounded-corners' );
  96. // TODO: It requires model interface.
  97. boldButton.element.classList.add( 'ck-button-bold' );
  98. ui.buttonTypes.add( actionButton );
  99. ui.buttonTypes.add( roundedButton );
  100. ui.buttonTypes.add( boldButton );
  101. // --- Icon ------------------------------------------------------------
  102. ui.buttonIcon.add( button( {
  103. label: 'Bold',
  104. icon: boldIcon
  105. } ) );
  106. const styledButton = button( {
  107. label: 'Button with an icon and custom styles',
  108. icon: italicIcon
  109. } );
  110. // TODO: It probably requires model interface.
  111. styledButton.element.setAttribute( 'style', 'border-radius: 100px; border: 0' );
  112. ui.buttonIconCustom.add( styledButton );
  113. ui.buttonIconStates.add( button( {
  114. label: 'Disabled',
  115. icon: boldIcon,
  116. isEnabled: false
  117. } ) );
  118. const disabledActionButton = button( {
  119. label: 'Disabled action',
  120. icon: boldIcon,
  121. isEnabled: false
  122. } );
  123. // TODO: It requires model interface.
  124. disabledActionButton.element.classList.add( 'ck-button-action' );
  125. ui.buttonIconStates.add( disabledActionButton );
  126. ui.buttonIconStates.add( button( {
  127. label: 'Bold',
  128. withText: false,
  129. tooltip: true,
  130. icon: boldIcon
  131. } ) );
  132. // --- Responsive ------------------------------------------------------------
  133. for ( let i = 1; i < 4; i++ ) {
  134. ui[ `buttonResponsive${ i }` ].add( button( {
  135. label: 'A button',
  136. isEnabled: true
  137. } ) );
  138. ui[ `buttonResponsive${ i }` ].add( button( {
  139. label: 'Bold',
  140. icon: boldIcon,
  141. isEnabled: true
  142. } ) );
  143. const notextButton = button( {
  144. label: 'Bold',
  145. withText: false,
  146. tooltip: true,
  147. icon: boldIcon
  148. } );
  149. // TODO: It requires model interface.
  150. notextButton.element.classList.add( 'ck-button-action' );
  151. ui[ `buttonResponsive${ i }` ].add( notextButton );
  152. }
  153. // --- Tooltip ------------------------------------------------------------
  154. ui.buttonTooltip.add( button( {
  155. label: 'This button has a tooltip (south)',
  156. withText: true,
  157. tooltip: 'The content of the tooltip',
  158. } ) );
  159. const northTooltipButton = button( {
  160. label: 'This one too – north',
  161. withText: true,
  162. keystroke: 'Ctrl+N',
  163. tooltip: true,
  164. } );
  165. northTooltipButton.init();
  166. northTooltipButton.tooltipView.position = 'n';
  167. ui.buttonTooltip.add( northTooltipButton );
  168. }
  169. function renderDropdown() {
  170. // --- ListDropdown ------------------------------------------------------------
  171. const collection = new Collection( { idProperty: 'label' } );
  172. [ 'Arial', 'Tahoma', 'Georgia' ].forEach( font => {
  173. collection.add( new Model( {
  174. label: font,
  175. style: `font-family: ${ font }`
  176. } ) );
  177. } );
  178. const itemListModel = new Model( {
  179. items: collection
  180. } );
  181. ui.dropdown.add( dropdown( {
  182. label: 'Normal state',
  183. isEnabled: true,
  184. content: itemListModel
  185. } ) );
  186. ui.dropdown.add( dropdown( {
  187. label: 'Disabled',
  188. isEnabled: false,
  189. content: itemListModel
  190. } ) );
  191. }
  192. function renderToolbar() {
  193. // --- Text ------------------------------------------------------------
  194. ui.toolbarText.add( toolbar( [
  195. icon( boldIcon ),
  196. text()
  197. ] ) );
  198. // --- Button ------------------------------------------------------------
  199. ui.toolbarButton.add( toolbar( [
  200. button(),
  201. text(),
  202. button( {
  203. label: 'Button with an icon',
  204. icon: boldIcon
  205. } ),
  206. dropdown(),
  207. button()
  208. ] ) );
  209. // --- Rounded ------------------------------------------------------------
  210. ui.toolbarRounded.add( toolbar( [
  211. button( {
  212. label: 'A button which corners are also rounded because of toolbar class'
  213. } ),
  214. button( {
  215. label: 'Button with an icon',
  216. icon: boldIcon
  217. } )
  218. ] ) );
  219. // --- Wrap ------------------------------------------------------------
  220. const wrapToolbar = toolbar( [
  221. button(),
  222. button(),
  223. button()
  224. ] );
  225. wrapToolbar.element.style.width = '150px';
  226. ui.toolbarWrap.add( wrapToolbar );
  227. // --- Separator ------------------------------------------------------------
  228. ui.toolbarSeparator.add( toolbar( [
  229. button(),
  230. button(),
  231. toolbarSeparator(),
  232. button( {
  233. label: 'Foo',
  234. icon: boldIcon
  235. } ),
  236. toolbarSeparator(),
  237. button( {
  238. label: 'Bar RTL',
  239. icon: boldIcon
  240. } )
  241. ] ) );
  242. // --- Multi row ------------------------------------------------------------
  243. ui.toolbarMultiRow.add( toolbar( [
  244. button(),
  245. button(),
  246. toolbarNewLine(),
  247. button( {
  248. label: 'Foo',
  249. icon: boldIcon
  250. } ),
  251. button( {
  252. label: 'Bar',
  253. icon: boldIcon
  254. } ),
  255. button( {
  256. label: 'Baz',
  257. icon: boldIcon
  258. } )
  259. ] ) );
  260. }
  261. function renderInput() {
  262. ui.inputLabeled.add( input() );
  263. ui.inputReadOnly.add( input( {
  264. label: 'A read–only input',
  265. isReadOnly: true,
  266. value: 'Read–only input value'
  267. } ) );
  268. }
  269. function text() {
  270. return new TextView();
  271. }
  272. function icon( content ) {
  273. const icon = new IconView();
  274. icon.content = content;
  275. return icon;
  276. }
  277. function button( {
  278. label = 'Button',
  279. isEnabled = true,
  280. isOn = false,
  281. withText = true,
  282. keystroke = null,
  283. tooltip,
  284. icon
  285. } = {} ) {
  286. const button = new ButtonView();
  287. button.set( { label, isEnabled, isOn, withText, icon, keystroke, tooltip } );
  288. return button;
  289. }
  290. function toolbar( children = [] ) {
  291. const toolbar = new ToolbarView();
  292. children.forEach( c => toolbar.items.add( c ) );
  293. return toolbar;
  294. }
  295. function dropdown( {
  296. label = 'Dropdown',
  297. isEnabled = true,
  298. isOn = false,
  299. withText = true,
  300. items = new Collection( { idProperty: 'label' } )
  301. } = {} ) {
  302. const model = new Model( { label, isEnabled, items, isOn, withText } );
  303. const dropdown = createListDropdown( model, {} );
  304. return dropdown;
  305. }
  306. function toolbarSeparator() {
  307. return new ToolbarSeparatorView();
  308. }
  309. function toolbarNewLine() {
  310. return new ToolbarNewlineView();
  311. }
  312. function input( {
  313. label = 'Labeled input',
  314. isReadOnly = false,
  315. value = 'The value of the input'
  316. } = {} ) {
  317. const labeledInput = new LabeledInputView( {}, InputTextView );
  318. labeledInput.set( { isReadOnly, label, value } );
  319. return labeledInput;
  320. }