editable_list.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. App.EditableList = Ember.View.extend({
  20. templateName: require('templates/common/editable_list'),
  21. items: [], // items show on list
  22. resources: [], // original resources including all items
  23. itemsOriginal: [], //backup of items
  24. editMode: false,
  25. input: '',
  26. typeahead: [],
  27. selectedTypeahed: 0,
  28. init: function () {
  29. this._super();
  30. this.set('itemsOriginal', Em.copy(this.get('items')));
  31. this.set('input', '');
  32. this.set('editMode', false);
  33. },
  34. onPrimary: function (event) {
  35. this.set('editMode', false);
  36. this.set('input', '');
  37. if(event){
  38. event.stopPropagation();
  39. }
  40. },
  41. onSecondary: function () {
  42. // restore all items
  43. this.set('items', this.get('itemsOriginal'));
  44. this.set('input', '');
  45. this.set('editMode', false);
  46. },
  47. enableEditMode: function() {
  48. this.set('input', '');
  49. this.set('editMode', true);
  50. },
  51. removeFromItems: function(event) {
  52. var items = this.get('items');
  53. items.removeObject(event.context);
  54. this.set('input', '');
  55. },
  56. /**
  57. * available items to add, will show up typing ahead
  58. */
  59. availableItemsToAdd: function () {
  60. var allItems = Em.copy(this.get('resources'));
  61. var input = this.get('input');
  62. var toRemove = [];
  63. var existed = this.get('items');
  64. allItems.forEach(function(item) {
  65. if (item.name.indexOf(input) < 0 || existed.findProperty('name', item.name)) {
  66. toRemove.push(item);
  67. }
  68. });
  69. toRemove.forEach(function(item) {
  70. allItems.removeObject(item);
  71. });
  72. return allItems;
  73. }.property('items', 'input'),
  74. addItem: function(event) {
  75. var items = this.get('items');
  76. var toAdd;
  77. if (event.context) {
  78. toAdd = event.context;
  79. } else if (this.get('typeahead.length') > 0){
  80. toAdd = this.get('typeahead')[this.get('selectedTypeahead')];
  81. }
  82. items.pushObject(event.context);
  83. this.set('input', '');
  84. },
  85. updateTypeahead: function() {
  86. var newValue = this.get('input');
  87. var self = this;
  88. if(newValue){
  89. var newValue = newValue.split(',');
  90. if( newValue.length > 1){
  91. // If coma separated string, then just add all items to list
  92. newValue.forEach( function(item) {
  93. self.addItem(item);
  94. });
  95. self.clearInput();
  96. } else {
  97. // Load typeahed items based on current input
  98. var items = self.get('availableItemsToAdd');
  99. self.set('typeahead', items);
  100. self.set('selectedTypeahed', 0);
  101. }
  102. } else {
  103. self.set('typeahead', []);
  104. self.set('selectedTypeahed', 0);
  105. }
  106. }.observes('input')
  107. });