select.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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( 'select', function( editor ) {
  6. // Add a new option to a SELECT object (combo or list).
  7. function addOption( combo, optionText, optionValue, documentObject, index ) {
  8. combo = getSelect( combo );
  9. var oOption;
  10. if ( documentObject )
  11. oOption = documentObject.createElement( 'OPTION' );
  12. else
  13. oOption = document.createElement( 'OPTION' );
  14. if ( combo && oOption && oOption.getName() == 'option' ) {
  15. if ( CKEDITOR.env.ie ) {
  16. if ( !isNaN( parseInt( index, 10 ) ) )
  17. combo.$.options.add( oOption.$, index );
  18. else
  19. combo.$.options.add( oOption.$ );
  20. oOption.$.innerHTML = optionText.length > 0 ? optionText : '';
  21. oOption.$.value = optionValue;
  22. } else {
  23. if ( index !== null && index < combo.getChildCount() )
  24. combo.getChild( index < 0 ? 0 : index ).insertBeforeMe( oOption );
  25. else
  26. combo.append( oOption );
  27. oOption.setText( optionText.length > 0 ? optionText : '' );
  28. oOption.setValue( optionValue );
  29. }
  30. } else {
  31. return false;
  32. }
  33. return oOption;
  34. }
  35. // Remove all selected options from a SELECT object.
  36. function removeSelectedOptions( combo ) {
  37. combo = getSelect( combo );
  38. // Save the selected index
  39. var iSelectedIndex = getSelectedIndex( combo );
  40. // Remove all selected options.
  41. for ( var i = combo.getChildren().count() - 1; i >= 0; i-- ) {
  42. if ( combo.getChild( i ).$.selected )
  43. combo.getChild( i ).remove();
  44. }
  45. // Reset the selection based on the original selected index.
  46. setSelectedIndex( combo, iSelectedIndex );
  47. }
  48. //Modify option from a SELECT object.
  49. function modifyOption( combo, index, title, value ) {
  50. combo = getSelect( combo );
  51. if ( index < 0 )
  52. return false;
  53. var child = combo.getChild( index );
  54. child.setText( title );
  55. child.setValue( value );
  56. return child;
  57. }
  58. function removeAllOptions( combo ) {
  59. combo = getSelect( combo );
  60. while ( combo.getChild( 0 ) && combo.getChild( 0 ).remove() ) {
  61. }
  62. }
  63. // Moves the selected option by a number of steps (also negative).
  64. function changeOptionPosition( combo, steps, documentObject ) {
  65. combo = getSelect( combo );
  66. var iActualIndex = getSelectedIndex( combo );
  67. if ( iActualIndex < 0 )
  68. return false;
  69. var iFinalIndex = iActualIndex + steps;
  70. iFinalIndex = ( iFinalIndex < 0 ) ? 0 : iFinalIndex;
  71. iFinalIndex = ( iFinalIndex >= combo.getChildCount() ) ? combo.getChildCount() - 1 : iFinalIndex;
  72. if ( iActualIndex == iFinalIndex )
  73. return false;
  74. var oOption = combo.getChild( iActualIndex ),
  75. sText = oOption.getText(),
  76. sValue = oOption.getValue();
  77. oOption.remove();
  78. oOption = addOption( combo, sText, sValue, ( !documentObject ) ? null : documentObject, iFinalIndex );
  79. setSelectedIndex( combo, iFinalIndex );
  80. return oOption;
  81. }
  82. function getSelectedIndex( combo ) {
  83. combo = getSelect( combo );
  84. return combo ? combo.$.selectedIndex : -1;
  85. }
  86. function setSelectedIndex( combo, index ) {
  87. combo = getSelect( combo );
  88. if ( index < 0 )
  89. return null;
  90. var count = combo.getChildren().count();
  91. combo.$.selectedIndex = ( index >= count ) ? ( count - 1 ) : index;
  92. return combo;
  93. }
  94. function getOptions( combo ) {
  95. combo = getSelect( combo );
  96. return combo ? combo.getChildren() : false;
  97. }
  98. function getSelect( obj ) {
  99. if ( obj && obj.domId && obj.getInputElement().$ ) // Dialog element.
  100. return obj.getInputElement();
  101. else if ( obj && obj.$ )
  102. return obj;
  103. return false;
  104. }
  105. return {
  106. title: editor.lang.forms.select.title,
  107. minWidth: CKEDITOR.env.ie ? 460 : 395,
  108. minHeight: CKEDITOR.env.ie ? 320 : 300,
  109. onShow: function() {
  110. delete this.selectBox;
  111. this.setupContent( 'clear' );
  112. var element = this.getParentEditor().getSelection().getSelectedElement();
  113. if ( element && element.getName() == 'select' ) {
  114. this.selectBox = element;
  115. this.setupContent( element.getName(), element );
  116. // Load Options into dialog.
  117. var objOptions = getOptions( element );
  118. for ( var i = 0; i < objOptions.count(); i++ )
  119. this.setupContent( 'option', objOptions.getItem( i ) );
  120. }
  121. },
  122. onOk: function() {
  123. var editor = this.getParentEditor(),
  124. element = this.selectBox,
  125. isInsertMode = !element;
  126. if ( isInsertMode )
  127. element = editor.document.createElement( 'select' );
  128. this.commitContent( element );
  129. if ( isInsertMode ) {
  130. editor.insertElement( element );
  131. if ( CKEDITOR.env.ie ) {
  132. var sel = editor.getSelection(),
  133. bms = sel.createBookmarks();
  134. setTimeout( function() {
  135. sel.selectBookmarks( bms );
  136. }, 0 );
  137. }
  138. }
  139. },
  140. contents: [ {
  141. id: 'info',
  142. label: editor.lang.forms.select.selectInfo,
  143. title: editor.lang.forms.select.selectInfo,
  144. accessKey: '',
  145. elements: [ {
  146. id: 'txtName',
  147. type: 'text',
  148. widths: [ '25%', '75%' ],
  149. labelLayout: 'horizontal',
  150. label: editor.lang.common.name,
  151. 'default': '',
  152. accessKey: 'N',
  153. style: 'width:350px',
  154. setup: function( name, element ) {
  155. if ( name == 'clear' )
  156. this.setValue( this[ 'default' ] || '' );
  157. else if ( name == 'select' )
  158. this.setValue( element.data( 'cke-saved-name' ) || element.getAttribute( 'name' ) || '' );
  159. },
  160. commit: function( element ) {
  161. if ( this.getValue() )
  162. element.data( 'cke-saved-name', this.getValue() );
  163. else {
  164. element.data( 'cke-saved-name', false );
  165. element.removeAttribute( 'name' );
  166. }
  167. }
  168. },
  169. {
  170. id: 'txtValue',
  171. type: 'text',
  172. widths: [ '25%', '75%' ],
  173. labelLayout: 'horizontal',
  174. label: editor.lang.forms.select.value,
  175. style: 'width:350px',
  176. 'default': '',
  177. className: 'cke_disabled',
  178. onLoad: function() {
  179. this.getInputElement().setAttribute( 'readOnly', true );
  180. },
  181. setup: function( name, element ) {
  182. if ( name == 'clear' )
  183. this.setValue( '' );
  184. else if ( name == 'option' && element.getAttribute( 'selected' ) )
  185. this.setValue( element.$.value );
  186. }
  187. },
  188. {
  189. type: 'hbox',
  190. widths: [ '175px', '170px' ],
  191. children: [ {
  192. id: 'txtSize',
  193. type: 'text',
  194. labelLayout: 'horizontal',
  195. label: editor.lang.forms.select.size,
  196. 'default': '',
  197. accessKey: 'S',
  198. style: 'width:175px',
  199. validate: function() {
  200. var func = CKEDITOR.dialog.validate.integer( editor.lang.common.validateNumberFailed );
  201. return ( ( this.getValue() === '' ) || func.apply( this ) );
  202. },
  203. setup: function( name, element ) {
  204. if ( name == 'select' )
  205. this.setValue( element.getAttribute( 'size' ) || '' );
  206. if ( CKEDITOR.env.webkit )
  207. this.getInputElement().setStyle( 'width', '86px' );
  208. },
  209. commit: function( element ) {
  210. if ( this.getValue() )
  211. element.setAttribute( 'size', this.getValue() );
  212. else
  213. element.removeAttribute( 'size' );
  214. }
  215. },
  216. {
  217. type: 'html',
  218. html: '<span>' + CKEDITOR.tools.htmlEncode( editor.lang.forms.select.lines ) + '</span>'
  219. } ]
  220. },
  221. {
  222. type: 'html',
  223. html: '<span>' + CKEDITOR.tools.htmlEncode( editor.lang.forms.select.opAvail ) + '</span>'
  224. },
  225. {
  226. type: 'hbox',
  227. widths: [ '115px', '115px', '100px' ],
  228. children: [ {
  229. type: 'vbox',
  230. children: [ {
  231. id: 'txtOptName',
  232. type: 'text',
  233. label: editor.lang.forms.select.opText,
  234. style: 'width:115px',
  235. setup: function( name ) {
  236. if ( name == 'clear' )
  237. this.setValue( '' );
  238. }
  239. },
  240. {
  241. type: 'select',
  242. id: 'cmbName',
  243. label: '',
  244. title: '',
  245. size: 5,
  246. style: 'width:115px;height:75px',
  247. items: [],
  248. onChange: function() {
  249. var dialog = this.getDialog(),
  250. values = dialog.getContentElement( 'info', 'cmbValue' ),
  251. optName = dialog.getContentElement( 'info', 'txtOptName' ),
  252. optValue = dialog.getContentElement( 'info', 'txtOptValue' ),
  253. iIndex = getSelectedIndex( this );
  254. setSelectedIndex( values, iIndex );
  255. optName.setValue( this.getValue() );
  256. optValue.setValue( values.getValue() );
  257. },
  258. setup: function( name, element ) {
  259. if ( name == 'clear' )
  260. removeAllOptions( this );
  261. else if ( name == 'option' )
  262. addOption( this, element.getText(), element.getText(), this.getDialog().getParentEditor().document );
  263. },
  264. commit: function( element ) {
  265. var dialog = this.getDialog(),
  266. optionsNames = getOptions( this ),
  267. optionsValues = getOptions( dialog.getContentElement( 'info', 'cmbValue' ) ),
  268. selectValue = dialog.getContentElement( 'info', 'txtValue' ).getValue();
  269. removeAllOptions( element );
  270. for ( var i = 0; i < optionsNames.count(); i++ ) {
  271. var oOption = addOption( element, optionsNames.getItem( i ).getValue(), optionsValues.getItem( i ).getValue(), dialog.getParentEditor().document );
  272. if ( optionsValues.getItem( i ).getValue() == selectValue ) {
  273. oOption.setAttribute( 'selected', 'selected' );
  274. oOption.selected = true;
  275. }
  276. }
  277. }
  278. } ]
  279. },
  280. {
  281. type: 'vbox',
  282. children: [ {
  283. id: 'txtOptValue',
  284. type: 'text',
  285. label: editor.lang.forms.select.opValue,
  286. style: 'width:115px',
  287. setup: function( name ) {
  288. if ( name == 'clear' )
  289. this.setValue( '' );
  290. }
  291. },
  292. {
  293. type: 'select',
  294. id: 'cmbValue',
  295. label: '',
  296. size: 5,
  297. style: 'width:115px;height:75px',
  298. items: [],
  299. onChange: function() {
  300. var dialog = this.getDialog(),
  301. names = dialog.getContentElement( 'info', 'cmbName' ),
  302. optName = dialog.getContentElement( 'info', 'txtOptName' ),
  303. optValue = dialog.getContentElement( 'info', 'txtOptValue' ),
  304. iIndex = getSelectedIndex( this );
  305. setSelectedIndex( names, iIndex );
  306. optName.setValue( names.getValue() );
  307. optValue.setValue( this.getValue() );
  308. },
  309. setup: function( name, element ) {
  310. if ( name == 'clear' )
  311. removeAllOptions( this );
  312. else if ( name == 'option' ) {
  313. var oValue = element.getValue();
  314. addOption( this, oValue, oValue, this.getDialog().getParentEditor().document );
  315. if ( element.getAttribute( 'selected' ) == 'selected' )
  316. this.getDialog().getContentElement( 'info', 'txtValue' ).setValue( oValue );
  317. }
  318. }
  319. } ]
  320. },
  321. {
  322. type: 'vbox',
  323. padding: 5,
  324. children: [ {
  325. type: 'button',
  326. id: 'btnAdd',
  327. label: editor.lang.forms.select.btnAdd,
  328. title: editor.lang.forms.select.btnAdd,
  329. style: 'width:100%;',
  330. onClick: function() {
  331. //Add new option.
  332. var dialog = this.getDialog(),
  333. optName = dialog.getContentElement( 'info', 'txtOptName' ),
  334. optValue = dialog.getContentElement( 'info', 'txtOptValue' ),
  335. names = dialog.getContentElement( 'info', 'cmbName' ),
  336. values = dialog.getContentElement( 'info', 'cmbValue' );
  337. addOption( names, optName.getValue(), optName.getValue(), dialog.getParentEditor().document );
  338. addOption( values, optValue.getValue(), optValue.getValue(), dialog.getParentEditor().document );
  339. optName.setValue( '' );
  340. optValue.setValue( '' );
  341. }
  342. },
  343. {
  344. type: 'button',
  345. id: 'btnModify',
  346. label: editor.lang.forms.select.btnModify,
  347. title: editor.lang.forms.select.btnModify,
  348. style: 'width:100%;',
  349. onClick: function() {
  350. //Modify selected option.
  351. var dialog = this.getDialog(),
  352. optName = dialog.getContentElement( 'info', 'txtOptName' ),
  353. optValue = dialog.getContentElement( 'info', 'txtOptValue' ),
  354. names = dialog.getContentElement( 'info', 'cmbName' ),
  355. values = dialog.getContentElement( 'info', 'cmbValue' ),
  356. iIndex = getSelectedIndex( names );
  357. if ( iIndex >= 0 ) {
  358. modifyOption( names, iIndex, optName.getValue(), optName.getValue() );
  359. modifyOption( values, iIndex, optValue.getValue(), optValue.getValue() );
  360. }
  361. }
  362. },
  363. {
  364. type: 'button',
  365. id: 'btnUp',
  366. style: 'width:100%;',
  367. label: editor.lang.forms.select.btnUp,
  368. title: editor.lang.forms.select.btnUp,
  369. onClick: function() {
  370. //Move up.
  371. var dialog = this.getDialog(),
  372. names = dialog.getContentElement( 'info', 'cmbName' ),
  373. values = dialog.getContentElement( 'info', 'cmbValue' );
  374. changeOptionPosition( names, -1, dialog.getParentEditor().document );
  375. changeOptionPosition( values, -1, dialog.getParentEditor().document );
  376. }
  377. },
  378. {
  379. type: 'button',
  380. id: 'btnDown',
  381. style: 'width:100%;',
  382. label: editor.lang.forms.select.btnDown,
  383. title: editor.lang.forms.select.btnDown,
  384. onClick: function() {
  385. //Move down.
  386. var dialog = this.getDialog(),
  387. names = dialog.getContentElement( 'info', 'cmbName' ),
  388. values = dialog.getContentElement( 'info', 'cmbValue' );
  389. changeOptionPosition( names, 1, dialog.getParentEditor().document );
  390. changeOptionPosition( values, 1, dialog.getParentEditor().document );
  391. }
  392. } ]
  393. } ]
  394. },
  395. {
  396. type: 'hbox',
  397. widths: [ '40%', '20%', '40%' ],
  398. children: [ {
  399. type: 'button',
  400. id: 'btnSetValue',
  401. label: editor.lang.forms.select.btnSetValue,
  402. title: editor.lang.forms.select.btnSetValue,
  403. onClick: function() {
  404. //Set as default value.
  405. var dialog = this.getDialog(),
  406. values = dialog.getContentElement( 'info', 'cmbValue' ),
  407. txtValue = dialog.getContentElement( 'info', 'txtValue' );
  408. txtValue.setValue( values.getValue() );
  409. }
  410. },
  411. {
  412. type: 'button',
  413. id: 'btnDelete',
  414. label: editor.lang.forms.select.btnDelete,
  415. title: editor.lang.forms.select.btnDelete,
  416. onClick: function() {
  417. // Delete option.
  418. var dialog = this.getDialog(),
  419. names = dialog.getContentElement( 'info', 'cmbName' ),
  420. values = dialog.getContentElement( 'info', 'cmbValue' ),
  421. optName = dialog.getContentElement( 'info', 'txtOptName' ),
  422. optValue = dialog.getContentElement( 'info', 'txtOptValue' );
  423. removeSelectedOptions( names );
  424. removeSelectedOptions( values );
  425. optName.setValue( '' );
  426. optValue.setValue( '' );
  427. }
  428. },
  429. {
  430. type: 'vbox',
  431. children: [ {
  432. id: 'chkMulti',
  433. type: 'checkbox',
  434. label: editor.lang.forms.select.chkMulti,
  435. 'default': '',
  436. accessKey: 'M',
  437. value: 'checked',
  438. setup: function( name, element ) {
  439. if ( name == 'select' )
  440. this.setValue( element.getAttribute( 'multiple' ) );
  441. },
  442. commit: function( element ) {
  443. if ( this.getValue() )
  444. element.setAttribute( 'multiple', this.getValue() );
  445. else
  446. element.removeAttribute( 'multiple' );
  447. }
  448. },
  449. {
  450. id: 'required',
  451. type: 'checkbox',
  452. label: editor.lang.forms.select.required,
  453. 'default': '',
  454. accessKey: 'Q',
  455. value: 'checked',
  456. setup: function( name, element ) {
  457. if ( name == 'select' )
  458. this.setValue( element.getAttribute( 'required' ) );
  459. },
  460. commit: function( element ) {
  461. if ( this.getValue() )
  462. element.setAttribute( 'required', 'required' );
  463. else
  464. element.removeAttribute( 'required' );
  465. }
  466. } ]
  467. } ]
  468. } ]
  469. } ]
  470. };
  471. } );