controls_view.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. /**
  20. * Abstract view for config fields.
  21. * Add popover support to control
  22. */
  23. App.ServiceConfigPopoverSupport = Ember.Mixin.create({
  24. /**
  25. * Config object. It will instance of App.ServiceConfigProperty
  26. */
  27. serviceConfig: null,
  28. placeholderBinding: 'serviceConfig.defaultValue',
  29. isPopoverEnabled: true,
  30. didInsertElement: function () {
  31. if (this.get('isPopoverEnabled') !== 'false') {
  32. this.$().popover({
  33. title: this.get('serviceConfig.displayName') + '<br><small>' + this.get('serviceConfig.name') + '</small>',
  34. content: this.get('serviceConfig.description'),
  35. placement: 'right',
  36. trigger: 'hover'
  37. });
  38. }
  39. }
  40. });
  41. /**
  42. * Default input control
  43. * @type {*}
  44. */
  45. App.ServiceConfigTextField = Ember.TextField.extend(App.ServiceConfigPopoverSupport, {
  46. valueBinding: 'serviceConfig.value',
  47. classNameBindings: 'textFieldClassName',
  48. placeholderBinding: 'serviceConfig.defaultValue',
  49. textFieldClassName: function () {
  50. // sets the width of the field depending on display type
  51. if (['directory', 'url', 'email', 'user', 'host'].contains(this.get('serviceConfig.displayType'))) {
  52. return ['span6'];
  53. } else if(this.get('serviceConfig.displayType') === 'advanced'){
  54. return ['span6'];
  55. } else{
  56. return ['input-small'];
  57. }
  58. }.property('serviceConfig.displayType'),
  59. disabled: function () {
  60. return !this.get('serviceConfig.isEditable');
  61. }.property('serviceConfig.isEditable')
  62. });
  63. /**
  64. * Customized input control with Utits type specified
  65. * @type {*}
  66. */
  67. App.ServiceConfigTextFieldWithUnit = Ember.View.extend(App.ServiceConfigPopoverSupport, {
  68. valueBinding: 'serviceConfig.value',
  69. classNames: [ 'input-append' ],
  70. placeholderBinding: 'serviceConfig.defaultValue',
  71. template: Ember.Handlebars.compile('{{view App.ServiceConfigTextField serviceConfigBinding="view.serviceConfig" isPopoverEnabled="false"}}<span class="add-on">{{view.serviceConfig.unit}}</span>'),
  72. disabled: function () {
  73. return !this.get('serviceConfig.isEditable');
  74. }.property('serviceConfig.isEditable')
  75. });
  76. /**
  77. * Password control
  78. * @type {*}
  79. */
  80. App.ServiceConfigPasswordField = Ember.TextField.extend({
  81. serviceConfig: null,
  82. type: 'password',
  83. valueBinding: 'serviceConfig.value',
  84. classNames: [ 'span3' ],
  85. placeholder: 'Type password',
  86. template: Ember.Handlebars.compile('{{view view.retypePasswordView placeholder="Retype password"}}'),
  87. retypePasswordView: Ember.TextField.extend({
  88. type: 'password',
  89. classNames: [ 'span3', 'retyped-password' ],
  90. valueBinding: 'parentView.serviceConfig.retypedPassword'
  91. })
  92. });
  93. /**
  94. * Textarea control
  95. * @type {*}
  96. */
  97. App.ServiceConfigTextArea = Ember.TextArea.extend(App.ServiceConfigPopoverSupport, {
  98. valueBinding: 'serviceConfig.value',
  99. rows: 4,
  100. classNames: ['span6'],
  101. placeholderBinding: 'serviceConfig.defaultValue',
  102. disabled: function () {
  103. return !this.get('serviceConfig.isEditable');
  104. }.property('serviceConfig.isEditable')
  105. });
  106. /**
  107. * Textarea control with bigger height
  108. * @type {*}
  109. */
  110. App.ServiceConfigBigTextArea = App.ServiceConfigTextArea.extend({
  111. rows: 10
  112. });
  113. /**
  114. * Checkbox control
  115. * @type {*}
  116. */
  117. App.ServiceConfigCheckbox = Ember.Checkbox.extend(App.ServiceConfigPopoverSupport, {
  118. checkedBinding: 'serviceConfig.value',
  119. disabled: function () {
  120. return !this.get('serviceConfig.isEditable');
  121. }.property('serviceConfig.isEditable')
  122. });
  123. <!-- {{bindAttr name="view.name" value="option"}} '<input type="radio" {{bindAttr name = "view.name" value="view.obj"}}>',-->
  124. App.ServiceConfigRadioButtons = Ember.View.extend({
  125. template: Ember.Handlebars.compile([
  126. '{{#each option in view.options}}',
  127. '<label class="radio">',
  128. '{{#view App.ServiceConfigRadioButton nameBinding = "view.name" valueBinding = "option.displayName"}}',
  129. '{{/view}}',
  130. '{{option.displayName}} &nbsp;',
  131. '</label>',
  132. '{{/each}}'
  133. ].join('\n')),
  134. serviceConfig: null,
  135. categoryConfigs: null,
  136. nameBinding: 'serviceConfig.radioName',
  137. optionsBinding: 'serviceConfig.options'
  138. });
  139. App.ServiceConfigRadioButton = Ember.Checkbox.extend({
  140. tagName: 'input',
  141. attributeBindings: ['type', 'name', 'value', 'checked'],
  142. checked: false,
  143. type: 'radio',
  144. name: null,
  145. value: null,
  146. didInsertElement: function () {
  147. if (this.get('parentView.serviceConfig.value') === this.get('value')) {
  148. this.set('checked', true);
  149. }
  150. },
  151. click: function () {
  152. this.set('checked', true);
  153. this.onChecked();
  154. },
  155. onChecked: function () {
  156. this.set('parentView.serviceConfig.value', this.get('value'));
  157. var components = this.get('parentView.serviceConfig.options');
  158. components.forEach(function (_component) {
  159. _component.foreignKeys.forEach(function (_componentName) {
  160. var component = this.get('parentView.categoryConfigs').findProperty('name', _componentName);
  161. if (_component.displayName === this.get('value')) {
  162. component.set('isVisible', true);
  163. } else {
  164. component.set('isVisible', false);
  165. }
  166. }, this);
  167. }, this);
  168. }.observes('checked')
  169. });
  170. App.ServiceConfigComboBox = Ember.Select.extend(App.ServiceConfigPopoverSupport, {
  171. contentBinding: 'serviceConfig.options',
  172. selectionBinding: 'serviceConfig.value',
  173. classNames: [ 'span3' ]
  174. });
  175. /**
  176. * Base component for host config with popover support
  177. */
  178. App.ServiceConfigHostPopoverSupport = Ember.Mixin.create({
  179. /**
  180. * Config object. It will instance of App.ServiceConfigProperty
  181. */
  182. serviceConfig: null,
  183. didInsertElement: function () {
  184. this.$().popover({
  185. title: this.get('serviceConfig.displayName'),
  186. content: this.get('serviceConfig.description'),
  187. placement: 'right',
  188. trigger: 'hover'
  189. });
  190. }
  191. });
  192. /**
  193. * Master host component.
  194. * Show hostname without ability to edit it
  195. * @type {*}
  196. */
  197. App.ServiceConfigMasterHostView = Ember.View.extend(App.ServiceConfigHostPopoverSupport, {
  198. classNames: ['master-host', 'span6'],
  199. valueBinding: 'serviceConfig.value',
  200. template: Ember.Handlebars.compile('{{value}}')
  201. });
  202. /**
  203. * Base component to display Multiple hosts
  204. * @type {*}
  205. */
  206. App.ServiceConfigMultipleHostsDisplay = Ember.Mixin.create(App.ServiceConfigHostPopoverSupport, {
  207. hasNoHosts: function () {
  208. console.log('view', this.get('viewName')); //to know which View cause errors
  209. console.log('controller', this.get('controller').name); //should be slaveComponentGroupsController
  210. if(!this.get('value')){
  211. // debugger;
  212. return true;
  213. }
  214. return this.get('value').length === 0;
  215. }.property('value'),
  216. hasOneHost: function () {
  217. return this.get('value').length > 0;
  218. }.property('value'),
  219. hasMultipleHosts: function () {
  220. return (this.get('value').length > 1 && typeof(this.get('value')) == 'object');
  221. }.property('value'),
  222. otherLength: function () {
  223. var len = this.get('value').length;
  224. if (len > 2) {
  225. return (len - 1) + ' others';
  226. } else {
  227. return '1 other';
  228. }
  229. }.property('value')
  230. })
  231. /**
  232. * Multiple master host component.
  233. * Show hostnames without ability to edit it
  234. * @type {*}
  235. */
  236. App.ServiceConfigMasterHostsView = Ember.View.extend(App.ServiceConfigMultipleHostsDisplay, {
  237. viewName : "serviceConfigMasterHostsView",
  238. valueBinding: 'serviceConfig.value',
  239. classNames: ['master-hosts', 'span6'],
  240. templateName: require('templates/wizard/master_hosts'),
  241. /**
  242. * Onclick handler for link
  243. */
  244. showHosts: function () {
  245. var serviceConfig = this.get('serviceConfig');
  246. App.ModalPopup.show({
  247. header: serviceConfig. category + ' Hosts',
  248. bodyClass: Ember.View.extend({
  249. serviceConfig: serviceConfig,
  250. templateName: require('templates/wizard/master_hosts_popup')
  251. }),
  252. onPrimary:function(){
  253. this.hide();
  254. }
  255. });
  256. }
  257. });
  258. /**
  259. * Show tabs list for slave hosts
  260. * @type {*}
  261. */
  262. App.SlaveComponentGroupsMenu = Em.CollectionView.extend({
  263. content: function(){
  264. return this.get('controller.componentGroups');
  265. }.property('controller.componentGroups'),
  266. tagName:'ul',
  267. classNames: ["nav", "nav-tabs"],
  268. itemViewClass:Em.View.extend({
  269. classNameBindings:["active"],
  270. active:function(){
  271. return this.get('content.active');
  272. }.property('content.active'),
  273. template:Ember.Handlebars.compile('<a {{action showSlaveComponentGroup view.content target="controller"}} href="#"> {{view.content.name}}</a><i {{action removeSlaveComponentGroup view.content target="controller"}} class="icon-remove"></i>')
  274. })
  275. });
  276. /**
  277. * <code>Add group</code> button
  278. * @type {*}
  279. */
  280. App.AddSlaveComponentGroupButton = Ember.View.extend({
  281. tagName: 'span',
  282. slaveComponentName: null,
  283. didInsertElement: function () {
  284. this.$().popover({
  285. title: 'Add a ' + this.get('slaveComponentName') + ' Group',
  286. content: 'If you need different settings on certain ' + this.get('slaveComponentName') + 's, you can add a ' + this.get('slaveComponentName') + ' group.<br>' +
  287. 'All ' + this.get('slaveComponentName') + 's within the same group will have the same set of settings. You can create multiple groups.',
  288. placement: 'right',
  289. trigger: 'hover'
  290. });
  291. }
  292. });
  293. /**
  294. * Multiple Slave Hosts component
  295. * @type {*}
  296. */
  297. App.ServiceConfigSlaveHostsView = Ember.View.extend(App.ServiceConfigMultipleHostsDisplay, {
  298. viewName : 'serviceConfigSlaveHostsView',
  299. classNames: ['slave-hosts', 'span6'],
  300. valueBinding: 'hosts',
  301. group: function(){
  302. return this.get('controller.activeGroup');
  303. }.property('controller.activeGroup'),
  304. hosts: function(){
  305. if (this.get('group')){
  306. return this.get('controller').getHostsByGroup(this.get('group'))
  307. }
  308. }.property('controller.hosts.@each.group', 'group'),
  309. templateName: require('templates/wizard/slave_component_hosts'),
  310. disabled: function () {
  311. return !this.get('serviceConfig.isEditable');
  312. }.property('serviceConfig.isEditable')
  313. });
  314. /**
  315. * DropDown component for <code>select hosts for groups</code> popup
  316. * @type {*}
  317. */
  318. App.SlaveComponentDropDownGroupView = Ember.View.extend({
  319. viewName : "slaveComponentDropDownGroupView",
  320. /**
  321. * On change handler for <code>select hosts for groups</code> popup
  322. * @param event
  323. */
  324. changeGroup: function(event) {
  325. var host = this.get('content');
  326. var groupName = $('#'+this.get('elementId') + ' select').val();
  327. this.get('controller').changeHostGroup(host, groupName);
  328. },
  329. optionTag: Ember.View.extend({
  330. /**
  331. * Whether current value(OptionTag value) equals to host value(assigned to SlaveComponentDropDownGroupView.content)
  332. */
  333. selected: function(){
  334. return this.get('parentView.content.group') === this.get('content');
  335. }.property('content')
  336. })
  337. });
  338. /**
  339. * Show info about current group
  340. * @type {*}
  341. */
  342. App.SlaveComponentChangeGroupNameView = Ember.View.extend({
  343. contentBinding: 'controller.activeGroup',
  344. classNames: ['control-group'],
  345. classNameBindings: 'error',
  346. error: false,
  347. setError: function(){
  348. this.set('error', false);
  349. }.observes('controller.activeGroup'),
  350. errorMessage: function(){
  351. return this.get('error') ? 'group with this name already exist' : '';
  352. }.property('error'),
  353. /**
  354. * Onclick handler for saving updated group name
  355. * @param event
  356. */
  357. changeGroupName: function(event) {
  358. var inputVal = $('#'+this.get('elementId') + ' input[type="text"]').val();
  359. if (inputVal !== this.get('content.name')){
  360. var result = this.get('controller').changeSlaveGroupName(this.get('content'), inputVal);
  361. this.set('error', result);
  362. }
  363. }
  364. });