button.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. CKEDITOR.dialog.add( 'button', function( editor ) {
  6. function commitAttributes( element ) {
  7. var val = this.getValue();
  8. if ( val ) {
  9. element.attributes[ this.id ] = val;
  10. if ( this.id == 'name' )
  11. element.attributes[ 'data-cke-saved-name' ] = val;
  12. } else {
  13. delete element.attributes[ this.id ];
  14. if ( this.id == 'name' )
  15. delete element.attributes[ 'data-cke-saved-name' ];
  16. }
  17. }
  18. return {
  19. title: editor.lang.forms.button.title,
  20. minWidth: 350,
  21. minHeight: 150,
  22. onShow: function() {
  23. delete this.button;
  24. var element = this.getParentEditor().getSelection().getSelectedElement();
  25. if ( element && element.is( 'input' ) ) {
  26. var type = element.getAttribute( 'type' );
  27. if ( type in { button: 1, reset: 1, submit: 1 } ) {
  28. this.button = element;
  29. this.setupContent( element );
  30. }
  31. }
  32. },
  33. onOk: function() {
  34. var editor = this.getParentEditor(),
  35. element = this.button,
  36. isInsertMode = !element;
  37. var fake = element ? CKEDITOR.htmlParser.fragment.fromHtml( element.getOuterHtml() ).children[ 0 ] : new CKEDITOR.htmlParser.element( 'input' );
  38. this.commitContent( fake );
  39. var writer = new CKEDITOR.htmlParser.basicWriter();
  40. fake.writeHtml( writer );
  41. var newElement = CKEDITOR.dom.element.createFromHtml( writer.getHtml(), editor.document );
  42. if ( isInsertMode )
  43. editor.insertElement( newElement );
  44. else {
  45. newElement.replace( element );
  46. editor.getSelection().selectElement( newElement );
  47. }
  48. },
  49. contents: [ {
  50. id: 'info',
  51. label: editor.lang.forms.button.title,
  52. title: editor.lang.forms.button.title,
  53. elements: [
  54. {
  55. id: 'name',
  56. type: 'text',
  57. bidi: true,
  58. label: editor.lang.common.name,
  59. 'default': '',
  60. setup: function( element ) {
  61. this.setValue( element.data( 'cke-saved-name' ) || element.getAttribute( 'name' ) || '' );
  62. },
  63. commit: commitAttributes
  64. },
  65. {
  66. id: 'value',
  67. type: 'text',
  68. label: editor.lang.forms.button.text,
  69. accessKey: 'V',
  70. 'default': '',
  71. setup: function( element ) {
  72. this.setValue( element.getAttribute( 'value' ) || '' );
  73. },
  74. commit: commitAttributes
  75. },
  76. {
  77. id: 'type',
  78. type: 'select',
  79. label: editor.lang.forms.button.type,
  80. 'default': 'button',
  81. accessKey: 'T',
  82. items: [
  83. [ editor.lang.forms.button.typeBtn, 'button' ],
  84. [ editor.lang.forms.button.typeSbm, 'submit' ],
  85. [ editor.lang.forms.button.typeRst, 'reset' ]
  86. ],
  87. setup: function( element ) {
  88. this.setValue( element.getAttribute( 'type' ) || '' );
  89. },
  90. commit: commitAttributes
  91. }
  92. ]
  93. } ]
  94. };
  95. } );