radio.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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( 'radio', function( editor ) {
  6. return {
  7. title: editor.lang.forms.checkboxAndRadio.radioTitle,
  8. minWidth: 350,
  9. minHeight: 140,
  10. onShow: function() {
  11. delete this.radioButton;
  12. var element = this.getParentEditor().getSelection().getSelectedElement();
  13. if ( element && element.getName() == 'input' && element.getAttribute( 'type' ) == 'radio' ) {
  14. this.radioButton = element;
  15. this.setupContent( element );
  16. }
  17. },
  18. onOk: function() {
  19. var editor,
  20. element = this.radioButton,
  21. isInsertMode = !element;
  22. if ( isInsertMode ) {
  23. editor = this.getParentEditor();
  24. element = editor.document.createElement( 'input' );
  25. element.setAttribute( 'type', 'radio' );
  26. }
  27. if ( isInsertMode )
  28. editor.insertElement( element );
  29. this.commitContent( { element: element } );
  30. },
  31. contents: [ {
  32. id: 'info',
  33. label: editor.lang.forms.checkboxAndRadio.radioTitle,
  34. title: editor.lang.forms.checkboxAndRadio.radioTitle,
  35. elements: [ {
  36. id: 'name',
  37. type: 'text',
  38. label: editor.lang.common.name,
  39. 'default': '',
  40. accessKey: 'N',
  41. setup: function( element ) {
  42. this.setValue( element.data( 'cke-saved-name' ) || element.getAttribute( 'name' ) || '' );
  43. },
  44. commit: function( data ) {
  45. var element = data.element;
  46. if ( this.getValue() )
  47. element.data( 'cke-saved-name', this.getValue() );
  48. else {
  49. element.data( 'cke-saved-name', false );
  50. element.removeAttribute( 'name' );
  51. }
  52. }
  53. },
  54. {
  55. id: 'value',
  56. type: 'text',
  57. label: editor.lang.forms.checkboxAndRadio.value,
  58. 'default': '',
  59. accessKey: 'V',
  60. setup: function( element ) {
  61. this.setValue( element.getAttribute( 'value' ) || '' );
  62. },
  63. commit: function( data ) {
  64. var element = data.element;
  65. if ( this.getValue() )
  66. element.setAttribute( 'value', this.getValue() );
  67. else
  68. element.removeAttribute( 'value' );
  69. }
  70. },
  71. {
  72. id: 'checked',
  73. type: 'checkbox',
  74. label: editor.lang.forms.checkboxAndRadio.selected,
  75. 'default': '',
  76. accessKey: 'S',
  77. value: 'checked',
  78. setup: function( element ) {
  79. this.setValue( element.getAttribute( 'checked' ) );
  80. },
  81. commit: function( data ) {
  82. var element = data.element;
  83. if ( !CKEDITOR.env.ie ) {
  84. if ( this.getValue() )
  85. element.setAttribute( 'checked', 'checked' );
  86. else
  87. element.removeAttribute( 'checked' );
  88. } else {
  89. var isElementChecked = element.getAttribute( 'checked' );
  90. var isChecked = !!this.getValue();
  91. if ( isElementChecked != isChecked ) {
  92. var replace = CKEDITOR.dom.element.createFromHtml( '<input type="radio"' + ( isChecked ? ' checked="checked"' : '' ) +
  93. '></input>', editor.document );
  94. element.copyAttributes( replace, { type: 1, checked: 1 } );
  95. replace.replace( element );
  96. editor.getSelection().selectElement( replace );
  97. data.element = replace;
  98. }
  99. }
  100. }
  101. },
  102. {
  103. id: 'required',
  104. type: 'checkbox',
  105. label: editor.lang.forms.checkboxAndRadio.required,
  106. 'default': '',
  107. accessKey: 'Q',
  108. value: 'required',
  109. setup: function( element ) {
  110. this.setValue( element.getAttribute( 'required' ) );
  111. },
  112. commit: function( data ) {
  113. var element = data.element;
  114. if ( this.getValue() )
  115. element.setAttribute( 'required', 'required' );
  116. else
  117. element.removeAttribute( 'required' );
  118. }
  119. } ]
  120. } ]
  121. };
  122. } );