editable_list.js 3.4 KB

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