checkbox.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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( 'checkbox', function( editor ) {
  6. return {
  7. title: editor.lang.forms.checkboxAndRadio.checkboxTitle,
  8. minWidth: 350,
  9. minHeight: 140,
  10. onShow: function() {
  11. delete this.checkbox;
  12. var element = this.getParentEditor().getSelection().getSelectedElement();
  13. if ( element && element.getAttribute( 'type' ) == 'checkbox' ) {
  14. this.checkbox = element;
  15. this.setupContent( element );
  16. }
  17. },
  18. onOk: function() {
  19. var editor,
  20. element = this.checkbox,
  21. isInsertMode = !element;
  22. if ( isInsertMode ) {
  23. editor = this.getParentEditor();
  24. element = editor.document.createElement( 'input' );
  25. element.setAttribute( 'type', 'checkbox' );
  26. editor.insertElement( element );
  27. }
  28. this.commitContent( { element: element } );
  29. },
  30. contents: [ {
  31. id: 'info',
  32. label: editor.lang.forms.checkboxAndRadio.checkboxTitle,
  33. title: editor.lang.forms.checkboxAndRadio.checkboxTitle,
  34. startupFocus: 'txtName',
  35. elements: [ {
  36. id: 'txtName',
  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. // IE failed to update 'name' property on input elements, protect it now.
  47. if ( this.getValue() )
  48. element.data( 'cke-saved-name', this.getValue() );
  49. else {
  50. element.data( 'cke-saved-name', false );
  51. element.removeAttribute( 'name' );
  52. }
  53. }
  54. },
  55. {
  56. id: 'txtValue',
  57. type: 'text',
  58. label: editor.lang.forms.checkboxAndRadio.value,
  59. 'default': '',
  60. accessKey: 'V',
  61. setup: function( element ) {
  62. var value = element.getAttribute( 'value' );
  63. // IE Return 'on' as default attr value.
  64. this.setValue( CKEDITOR.env.ie && value == 'on' ? '' : value );
  65. },
  66. commit: function( data ) {
  67. var element = data.element,
  68. value = this.getValue();
  69. if ( value && !( CKEDITOR.env.ie && value == 'on' ) )
  70. element.setAttribute( 'value', value );
  71. else {
  72. if ( CKEDITOR.env.ie ) {
  73. // Remove attribute 'value' of checkbox (#4721).
  74. var checkbox = new CKEDITOR.dom.element( 'input', element.getDocument() );
  75. element.copyAttributes( checkbox, { value: 1 } );
  76. checkbox.replace( element );
  77. editor.getSelection().selectElement( checkbox );
  78. data.element = checkbox;
  79. } else {
  80. element.removeAttribute( 'value' );
  81. }
  82. }
  83. }
  84. },
  85. {
  86. id: 'cmbSelected',
  87. type: 'checkbox',
  88. label: editor.lang.forms.checkboxAndRadio.selected,
  89. 'default': '',
  90. accessKey: 'S',
  91. value: 'checked',
  92. setup: function( element ) {
  93. this.setValue( element.getAttribute( 'checked' ) );
  94. },
  95. commit: function( data ) {
  96. var element = data.element;
  97. if ( CKEDITOR.env.ie ) {
  98. var isElementChecked = !!element.getAttribute( 'checked' ),
  99. isChecked = !!this.getValue();
  100. if ( isElementChecked != isChecked ) {
  101. var replace = CKEDITOR.dom.element.createFromHtml( '<input type="checkbox"' + ( isChecked ? ' checked="checked"' : '' ) +
  102. '/>', editor.document );
  103. element.copyAttributes( replace, { type: 1, checked: 1 } );
  104. replace.replace( element );
  105. editor.getSelection().selectElement( replace );
  106. data.element = replace;
  107. }
  108. } else {
  109. var value = this.getValue();
  110. if ( value )
  111. element.setAttribute( 'checked', 'checked' );
  112. else
  113. element.removeAttribute( 'checked' );
  114. }
  115. }
  116. },
  117. {
  118. id: 'required',
  119. type: 'checkbox',
  120. label: editor.lang.forms.checkboxAndRadio.required,
  121. 'default': '',
  122. accessKey: 'Q',
  123. value: 'required',
  124. setup: function( element ) {
  125. this.setValue( element.getAttribute( 'required' ) );
  126. },
  127. commit: function( data ) {
  128. var element = data.element;
  129. if ( this.getValue() )
  130. element.setAttribute( 'required', 'required' );
  131. else
  132. element.removeAttribute( 'required' );
  133. }
  134. } ]
  135. } ]
  136. };
  137. } );