theme.js 10 KB

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