controls_view.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. App.ServiceConfigRadioButtons = Ember.View.extend({
  124. template: Ember.Handlebars.compile([
  125. '{{#each option in view.options}}',
  126. '<label class="radio">',
  127. '{{#view App.ServiceConfigRadioButton nameBinding = "view.name" valueBinding = "option.displayName"}}',
  128. '{{/view}}',
  129. '{{option.displayName}} &nbsp;',
  130. '</label>',
  131. '{{/each}}'
  132. ].join('\n')),
  133. serviceConfig: null,
  134. categoryConfigs: null,
  135. nameBinding: 'serviceConfig.radioName',
  136. optionsBinding: 'serviceConfig.options'
  137. });
  138. App.ServiceConfigRadioButton = Ember.Checkbox.extend({
  139. tagName: 'input',
  140. attributeBindings: ['type', 'name', 'value', 'checked'],
  141. checked: false,
  142. type: 'radio',
  143. name: null,
  144. value: null,
  145. didInsertElement: function () {
  146. if (this.get('parentView.serviceConfig.value') === this.get('value')) {
  147. this.set('checked', true);
  148. }
  149. },
  150. click: function () {
  151. this.set('checked', true);
  152. this.onChecked();
  153. },
  154. onChecked: function () {
  155. this.set('parentView.serviceConfig.value', this.get('value'));
  156. var components = this.get('parentView.serviceConfig.options');
  157. components.forEach(function (_component) {
  158. _component.foreignKeys.forEach(function (_componentName) {
  159. var component = this.get('parentView.categoryConfigs').findProperty('name', _componentName);
  160. if (_component.displayName === this.get('value')) {
  161. component.set('isVisible', true);
  162. } else {
  163. component.set('isVisible', false);
  164. }
  165. }, this);
  166. }, this);
  167. }.observes('checked')
  168. });
  169. App.ServiceConfigComboBox = Ember.Select.extend(App.ServiceConfigPopoverSupport, {
  170. contentBinding: 'serviceConfig.options',
  171. selectionBinding: 'serviceConfig.value',
  172. classNames: [ 'span3' ]
  173. });
  174. /**
  175. * Base component for host config with popover support
  176. */
  177. App.ServiceConfigHostPopoverSupport = Ember.Mixin.create({
  178. /**
  179. * Config object. It will instance of App.ServiceConfigProperty
  180. */
  181. serviceConfig: null,
  182. didInsertElement: function () {
  183. this.$().popover({
  184. title: this.get('serviceConfig.displayName'),
  185. content: this.get('serviceConfig.description'),
  186. placement: 'right',
  187. trigger: 'hover'
  188. });
  189. }
  190. });
  191. /**
  192. * Master host component.
  193. * Show hostname without ability to edit it
  194. * @type {*}
  195. */
  196. App.ServiceConfigMasterHostView = Ember.View.extend(App.ServiceConfigHostPopoverSupport, {
  197. classNames: ['master-host', 'span6'],
  198. valueBinding: 'serviceConfig.value',
  199. template: Ember.Handlebars.compile('{{value}}')
  200. });
  201. /**
  202. * Base component to display Multiple hosts
  203. * @type {*}
  204. */
  205. App.ServiceConfigMultipleHostsDisplay = Ember.Mixin.create(App.ServiceConfigHostPopoverSupport, {
  206. hasNoHosts: function () {
  207. console.log('view', this.get('viewName')); //to know which View cause errors
  208. console.log('controller', this.get('controller').name); //should be slaveComponentGroupsController
  209. if (!this.get('value')) {
  210. // debugger;
  211. return true;
  212. }
  213. return this.get('value').length === 0;
  214. }.property('value'),
  215. hasOneHost: function () {
  216. return this.get('value').length > 0;
  217. }.property('value'),
  218. hasMultipleHosts: function () {
  219. return (this.get('value').length > 1 && typeof(this.get('value')) == 'object');
  220. }.property('value'),
  221. otherLength: function () {
  222. var len = this.get('value').length;
  223. if (len > 2) {
  224. return (len - 1) + ' others';
  225. } else {
  226. return '1 other';
  227. }
  228. }.property('value')
  229. })
  230. /**
  231. * Multiple master host component.
  232. * Show hostnames without ability to edit it
  233. * @type {*}
  234. */
  235. App.ServiceConfigMasterHostsView = Ember.View.extend(App.ServiceConfigMultipleHostsDisplay, {
  236. viewName: "serviceConfigMasterHostsView",
  237. valueBinding: 'serviceConfig.value',
  238. classNames: ['master-hosts', 'span6'],
  239. templateName: require('templates/wizard/master_hosts'),
  240. /**
  241. * Onclick handler for link
  242. */
  243. showHosts: function () {
  244. var serviceConfig = this.get('serviceConfig');
  245. App.ModalPopup.show({
  246. header: serviceConfig.category + ' Hosts',
  247. bodyClass: Ember.View.extend({
  248. serviceConfig: serviceConfig,
  249. templateName: require('templates/wizard/master_hosts_popup')
  250. }),
  251. onPrimary: function () {
  252. this.hide();
  253. },
  254. secondary: null
  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. errorCount: function () {
  274. return this.get('content.properties').filterProperty('isValid', false).filterProperty('isVisible', true).get('length');
  275. }.property('content.properties.@each.isValid','content.properties.@each.isVisible'),
  276. template: Ember.Handlebars.compile('<a {{action showSlaveComponentGroup view.content target="controller"}} href="#"> {{view.content.name}}{{#if view.errorCount}}<span class="badge badge-important">{{view.errorCount}}</span>{{/if}}</a><i {{action removeSlaveComponentGroup view.content target="controller"}} class="icon-remove"></i>')
  277. })
  278. });
  279. /**
  280. * <code>Add group</code> button
  281. * @type {*}
  282. */
  283. App.AddSlaveComponentGroupButton = Ember.View.extend({
  284. tagName: 'span',
  285. slaveComponentName: null,
  286. didInsertElement: function () {
  287. this.$().popover({
  288. title: 'Add a ' + this.get('slaveComponentName') + ' Group',
  289. content: 'If you need different settings on certain ' + this.get('slaveComponentName') + 's, you can add a ' + this.get('slaveComponentName') + ' group.<br>' +
  290. 'All ' + this.get('slaveComponentName') + 's within the same group will have the same set of settings. You can create multiple groups.',
  291. placement: 'right',
  292. trigger: 'hover'
  293. });
  294. }
  295. });
  296. /**
  297. * Multiple Slave Hosts component
  298. * @type {*}
  299. */
  300. App.ServiceConfigSlaveHostsView = Ember.View.extend(App.ServiceConfigMultipleHostsDisplay, {
  301. viewName: 'serviceConfigSlaveHostsView',
  302. classNames: ['slave-hosts', 'span6'],
  303. valueBinding: 'hosts',
  304. group: function () {
  305. return this.get('controller.activeGroup');
  306. }.property('controller.activeGroup'),
  307. hosts: function () {
  308. if (this.get('group')) {
  309. return this.get('controller').getHostsByGroup(this.get('group'))
  310. }
  311. }.property('controller.hosts.@each.group', 'group'),
  312. templateName: require('templates/wizard/slave_component_hosts'),
  313. disabled: function () {
  314. return !this.get('serviceConfig.isEditable');
  315. }.property('serviceConfig.isEditable')
  316. });
  317. /**
  318. * properties for present active slave group
  319. * @type {*}
  320. */
  321. App.SlaveGroupPropertiesView = Ember.View.extend({
  322. viewName: 'serviceConfigSlaveHostsView',
  323. group: function () {
  324. return this.get('controller.activeGroup');
  325. }.property('controller.activeGroup'),
  326. groupConfigs: function () {
  327. console.log("************************************************************************");
  328. console.log("The value of group is: " + this.get('group'));
  329. console.log("************************************************************************");
  330. return this.get('group.properties');
  331. }.property('group.properties.@each').cacheable(),
  332. errorCount: function () {
  333. return this.get('group.properties').filterProperty('isValid', false).filterProperty('isVisible', true).get('length');
  334. }.property('configs.@each.isValid', 'configs.@each.isVisible')
  335. });
  336. /**
  337. * DropDown component for <code>select hosts for groups</code> popup
  338. * @type {*}
  339. */
  340. App.SlaveComponentDropDownGroupView = Ember.View.extend({
  341. viewName: "slaveComponentDropDownGroupView",
  342. /**
  343. * On change handler for <code>select hosts for groups</code> popup
  344. * @param event
  345. */
  346. changeGroup: function (event) {
  347. var host = this.get('content');
  348. var groupName = $('#' + this.get('elementId') + ' select').val();
  349. this.get('controller').changeHostGroup(host, groupName);
  350. },
  351. optionTag: Ember.View.extend({
  352. /**
  353. * Whether current value(OptionTag value) equals to host value(assigned to SlaveComponentDropDownGroupView.content)
  354. */
  355. selected: function () {
  356. return this.get('parentView.content.group') === this.get('content');
  357. }.property('content')
  358. })
  359. });
  360. /**
  361. * Show info about current group
  362. * @type {*}
  363. */
  364. App.SlaveComponentChangeGroupNameView = Ember.View.extend({
  365. contentBinding: 'controller.activeGroup',
  366. classNames: ['control-group'],
  367. classNameBindings: 'error',
  368. error: false,
  369. setError: function () {
  370. this.set('error', false);
  371. }.observes('controller.activeGroup'),
  372. errorMessage: function () {
  373. return this.get('error') ? 'group with this name already exist' : '';
  374. }.property('error'),
  375. /**
  376. * Onclick handler for saving updated group name
  377. * @param event
  378. */
  379. changeGroupName: function (event) {
  380. var inputVal = $('#' + this.get('elementId') + ' input[type="text"]').val();
  381. if (inputVal !== this.get('content.name')) {
  382. var result = this.get('controller').changeSlaveGroupName(this.get('content'), inputVal);
  383. this.set('error', result);
  384. }
  385. }
  386. });