theme.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. dropdown: '#dropdown',
  53. 'toolbarText': '#toolbar-text',
  54. 'toolbarButton': '#toolbar-button',
  55. 'toolbarRounded': '#toolbar-rounded',
  56. 'toolbarWrap': '#toolbar-wrap',
  57. 'toolbarSeparator': '#toolbar-separator',
  58. 'toolbarMultiRow': '#toolbar-multi-row',
  59. 'inputLabeled': '#input-labeled',
  60. 'inputReadOnly': '#input-read-only'
  61. } );
  62. renderIcon();
  63. renderButton();
  64. renderDropdown();
  65. renderToolbar();
  66. renderInput();
  67. function renderIcon() {
  68. // --- In-text ------------------------------------------------------------
  69. ui.iconPlain1.add( icon( boldIcon ) );
  70. ui.iconPlain2.add( icon( italicIcon ) );
  71. ui.iconColor1.add( icon( boldIcon ) );
  72. ui.iconColor2.add( icon( italicIcon ) );
  73. }
  74. function renderButton() {
  75. // --- States ------------------------------------------------------------
  76. ui.buttonStates.add( button( {
  77. label: 'State: normal (none)',
  78. } ) );
  79. ui.buttonStates.add( button( {
  80. label: 'State: disabled',
  81. isEnabled: false
  82. } ) );
  83. ui.buttonStates.add( button( {
  84. label: 'State: on',
  85. isOn: true
  86. } ) );
  87. // --- Types ------------------------------------------------------------
  88. const actionButton = button( { label: 'Action button' } );
  89. const roundedButton = button( { label: 'Rounded corners' } );
  90. const boldButton = button( { label: 'Bold text' } );
  91. // TODO: It requires model interface.
  92. actionButton.element.classList.add( 'ck-button-action' );
  93. // TODO: It requires model interface.
  94. roundedButton.element.classList.add( 'ck-rounded-corners' );
  95. // TODO: It requires model interface.
  96. boldButton.element.classList.add( 'ck-button-bold' );
  97. ui.buttonTypes.add( actionButton );
  98. ui.buttonTypes.add( roundedButton );
  99. ui.buttonTypes.add( boldButton );
  100. // --- Icon ------------------------------------------------------------
  101. ui.buttonIcon.add( button( {
  102. label: 'Bold',
  103. icon: boldIcon
  104. } ) );
  105. const styledButton = button( {
  106. label: 'Button with an icon and custom styles',
  107. icon: italicIcon
  108. } );
  109. // TODO: It probably requires model interface.
  110. styledButton.element.setAttribute( 'style', 'border-radius: 100px; border: 0' );
  111. ui.buttonIconCustom.add( styledButton );
  112. ui.buttonIconStates.add( button( {
  113. label: 'Disabled',
  114. icon: boldIcon,
  115. isEnabled: false
  116. } ) );
  117. const disabledActionButton = button( {
  118. label: 'Disabled action',
  119. icon: boldIcon,
  120. isEnabled: false
  121. } );
  122. // TODO: It requires model interface.
  123. disabledActionButton.element.classList.add( 'ck-button-action' );
  124. ui.buttonIconStates.add( disabledActionButton );
  125. ui.buttonIconStates.add( button( {
  126. label: 'Bold',
  127. withText: false,
  128. tooltip: true,
  129. icon: boldIcon
  130. } ) );
  131. // --- Responsive ------------------------------------------------------------
  132. for ( let i = 1; i < 4; i++ ) {
  133. ui[ `buttonResponsive${ i }` ].add( button( {
  134. label: 'A button',
  135. isEnabled: true
  136. } ) );
  137. ui[ `buttonResponsive${ i }` ].add( button( {
  138. label: 'Bold',
  139. icon: boldIcon,
  140. isEnabled: true
  141. } ) );
  142. const notextButton = button( {
  143. label: 'Bold',
  144. withText: false,
  145. tooltip: true,
  146. icon: boldIcon
  147. } );
  148. // TODO: It requires model interface.
  149. notextButton.element.classList.add( 'ck-button-action' );
  150. ui[ `buttonResponsive${ i }` ].add( notextButton );
  151. }
  152. }
  153. function renderDropdown() {
  154. // --- ListDropdown ------------------------------------------------------------
  155. const collection = new Collection( { idProperty: 'label' } );
  156. [ 'Arial', 'Tahoma', 'Georgia' ].forEach( font => {
  157. collection.add( new Model( {
  158. label: font,
  159. style: `font-family: ${ font }`
  160. } ) );
  161. } );
  162. const itemListModel = new Model( {
  163. items: collection
  164. } );
  165. ui.dropdown.add( dropdown( {
  166. label: 'Normal state',
  167. isEnabled: true,
  168. content: itemListModel
  169. } ) );
  170. ui.dropdown.add( dropdown( {
  171. label: 'Disabled',
  172. isEnabled: false,
  173. content: itemListModel
  174. } ) );
  175. }
  176. function renderToolbar() {
  177. // --- Text ------------------------------------------------------------
  178. ui.toolbarText.add( toolbar( [
  179. icon( boldIcon ),
  180. text()
  181. ] ) );
  182. // --- Button ------------------------------------------------------------
  183. ui.toolbarButton.add( toolbar( [
  184. button(),
  185. text(),
  186. button( {
  187. label: 'Button with an icon',
  188. icon: boldIcon
  189. } ),
  190. dropdown(),
  191. button()
  192. ] ) );
  193. // --- Rounded ------------------------------------------------------------
  194. ui.toolbarRounded.add( toolbar( [
  195. button( {
  196. label: 'A button which corners are also rounded because of toolbar class'
  197. } ),
  198. button( {
  199. label: 'Button with an icon',
  200. icon: boldIcon
  201. } )
  202. ] ) );
  203. // --- Wrap ------------------------------------------------------------
  204. const wrapToolbar = toolbar( [
  205. button(),
  206. button(),
  207. button()
  208. ] );
  209. wrapToolbar.element.style.width = '150px';
  210. ui.toolbarWrap.add( wrapToolbar );
  211. // --- Separator ------------------------------------------------------------
  212. ui.toolbarSeparator.add( toolbar( [
  213. button(),
  214. button(),
  215. toolbarSeparator(),
  216. button( {
  217. label: 'Foo',
  218. icon: boldIcon
  219. } ),
  220. toolbarSeparator(),
  221. button( {
  222. label: 'Bar RTL',
  223. icon: boldIcon
  224. } )
  225. ] ) );
  226. // --- Multi row ------------------------------------------------------------
  227. ui.toolbarMultiRow.add( toolbar( [
  228. button(),
  229. button(),
  230. toolbarNewLine(),
  231. button( {
  232. label: 'Foo',
  233. icon: boldIcon
  234. } ),
  235. button( {
  236. label: 'Bar',
  237. icon: boldIcon
  238. } ),
  239. button( {
  240. label: 'Baz',
  241. icon: boldIcon
  242. } )
  243. ] ) );
  244. }
  245. function renderInput() {
  246. ui.inputLabeled.add( input() );
  247. ui.inputReadOnly.add( input( {
  248. label: 'A read–only input',
  249. isReadOnly: true,
  250. value: 'Read–only input value'
  251. } ) );
  252. }
  253. function text() {
  254. return new TextView();
  255. }
  256. function icon( content ) {
  257. const icon = new IconView();
  258. icon.content = content;
  259. return icon;
  260. }
  261. function button( {
  262. label = 'Button',
  263. isEnabled = true,
  264. isOn = false,
  265. withText = true,
  266. tooltip,
  267. icon
  268. } = {} ) {
  269. const button = new ButtonView();
  270. button.set( { label, isEnabled, isOn, withText, icon, tooltip } );
  271. return button;
  272. }
  273. function toolbar( children = [] ) {
  274. const toolbar = new ToolbarView();
  275. children.forEach( c => toolbar.items.add( c ) );
  276. return toolbar;
  277. }
  278. function dropdown( {
  279. label = 'Dropdown',
  280. isEnabled = true,
  281. isOn = false,
  282. withText = true,
  283. items = new Collection( { idProperty: 'label' } )
  284. } = {} ) {
  285. const model = new Model( { label, isEnabled, items, isOn, withText } );
  286. const dropdown = createListDropdown( model, {} );
  287. return dropdown;
  288. }
  289. function toolbarSeparator() {
  290. return new ToolbarSeparatorView();
  291. }
  292. function toolbarNewLine() {
  293. return new ToolbarNewlineView();
  294. }
  295. function input( {
  296. label = 'Labeled input',
  297. isReadOnly = false,
  298. value = 'The value of the input'
  299. } = {} ) {
  300. const labeledInput = new LabeledInputView( {}, InputTextView );
  301. labeledInput.set( { isReadOnly, label, value } );
  302. return labeledInput;
  303. }