|
| 1 | +import Component, { Input } from "@ember/component"; |
| 2 | +import { concat, fn } from "@ember/helper"; |
| 3 | +import { action, computed } from "@ember/object"; |
| 4 | +import { equal, or } from "@ember/object/computed"; |
| 5 | +import { underscore } from "@ember/string"; |
| 6 | +import { tagName } from "@ember-decorators/component"; |
| 7 | +import BufferedProxy from "ember-buffered-proxy/proxy"; |
| 8 | +import DButton from "discourse/components/d-button"; |
| 9 | +import RadioButton from "discourse/components/radio-button"; |
| 10 | +import replaceEmoji from "discourse/helpers/replace-emoji"; |
| 11 | +import { popupAjaxError } from "discourse/lib/ajax-error"; |
| 12 | +import { i18n } from "discourse-i18n"; |
| 13 | +import ComboBox from "select-kit/components/combo-box"; |
| 14 | +import { Types } from "discourse/plugins/discourse-teambuild/discourse/models/teambuild-target"; |
| 15 | + |
| 16 | +@tagName("") |
| 17 | +export default class TeambuildTarget extends Component { |
| 18 | + editSelected = false; |
| 19 | + |
| 20 | + @equal("buffered.target_type_id", Types.USER_GROUP) needsGroup; |
| 21 | + @or("editSelected", "target.isNew") editing; |
| 22 | + |
| 23 | + @computed("target") |
| 24 | + get buffered() { |
| 25 | + return BufferedProxy.create({ |
| 26 | + content: this.get("target"), |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + @computed("editing", "index") |
| 31 | + get canMoveUp() { |
| 32 | + return !this.editing && this.index > 0; |
| 33 | + } |
| 34 | + |
| 35 | + @computed("editing", "index", "length") |
| 36 | + get canMoveDown() { |
| 37 | + return !this.editing && this.index < this.length - 1; |
| 38 | + } |
| 39 | + |
| 40 | + get targetTypes() { |
| 41 | + return Object.keys(Types).map((key) => { |
| 42 | + return { id: Types[key], name: underscore(key) }; |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + @computed( |
| 47 | + "buffered.name", |
| 48 | + "target.isSaving", |
| 49 | + "needsGroup", |
| 50 | + "buffered.group_id" |
| 51 | + ) |
| 52 | + get saveDisabled() { |
| 53 | + if (this.target.isSaving) { |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + let name = this.get("buffered.name"); |
| 58 | + if (!name || name.length === 0) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + if (this.needsGroup && !this.get("buffered.group_id")) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + @action |
| 70 | + save() { |
| 71 | + this.target |
| 72 | + .save(this.buffered.getProperties("name", "target_type_id", "group_id")) |
| 73 | + .then(() => { |
| 74 | + this.set("editSelected", false); |
| 75 | + }) |
| 76 | + .catch(popupAjaxError); |
| 77 | + } |
| 78 | + |
| 79 | + @action |
| 80 | + cancel() { |
| 81 | + if (this.target.isNew) { |
| 82 | + return this.removeTarget(); |
| 83 | + } else { |
| 84 | + this.set("editSelected", false); |
| 85 | + this.buffered.discardChanges(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @action |
| 90 | + destroyTarget() { |
| 91 | + this.target.destroyRecord().then(() => this.removeTarget()); |
| 92 | + } |
| 93 | + |
| 94 | + <template> |
| 95 | + <div class="teambuild-target {{if this.editing 'editing'}}"> |
| 96 | + <div class="target-details"> |
| 97 | + {{#if this.editing}} |
| 98 | + <div class="fields"> |
| 99 | + <div class="target-types"> |
| 100 | + {{#each this.targetTypes as |type|}} |
| 101 | + <label> |
| 102 | + <RadioButton |
| 103 | + @value={{type.id}} |
| 104 | + @selection={{this.buffered.target_type_id}} |
| 105 | + @onChange={{fn (mut this.buffered.target_type_id) type.id}} |
| 106 | + class={{type.name}} |
| 107 | + /> |
| 108 | + {{i18n |
| 109 | + (concat "discourse_teambuild.targets.types." type.name) |
| 110 | + }} |
| 111 | + </label> |
| 112 | + {{/each}} |
| 113 | + </div> |
| 114 | + |
| 115 | + <div class="target-name"> |
| 116 | + <Input |
| 117 | + @value={{this.buffered.name}} |
| 118 | + placeholder={{i18n "discourse_teambuild.targets.name"}} |
| 119 | + autofocus="true" |
| 120 | + /> |
| 121 | + </div> |
| 122 | + |
| 123 | + {{#if this.needsGroup}} |
| 124 | + <ComboBox |
| 125 | + @content={{this.groups}} |
| 126 | + @value={{this.buffered.group_id}} |
| 127 | + @none="discourse_teambuild.targets.choose_group" |
| 128 | + /> |
| 129 | + {{/if}} |
| 130 | + </div> |
| 131 | + {{else}} |
| 132 | + <div class="target-name"> |
| 133 | + {{replaceEmoji this.target.name}} |
| 134 | + </div> |
| 135 | + <div class="target-group-name"> |
| 136 | + {{this.target.group_name}} |
| 137 | + </div> |
| 138 | + {{/if}} |
| 139 | + </div> |
| 140 | + <div class="controls"> |
| 141 | + {{#if this.canMoveUp}} |
| 142 | + <DButton @icon="arrow-up" @action={{this.moveUp}} /> |
| 143 | + {{/if}} |
| 144 | + |
| 145 | + {{#if this.canMoveDown}} |
| 146 | + <DButton @icon="arrow-down" @action={{this.moveDown}} /> |
| 147 | + {{/if}} |
| 148 | + |
| 149 | + {{#if this.editing}} |
| 150 | + <DButton |
| 151 | + @icon="check" |
| 152 | + @title="discourse_teambuild.targets.save" |
| 153 | + @action={{this.save}} |
| 154 | + @disabled={{this.saveDisabled}} |
| 155 | + class="btn-primary save" |
| 156 | + /> |
| 157 | + |
| 158 | + <DButton |
| 159 | + @icon="xmark" |
| 160 | + @action={{this.cancel}} |
| 161 | + @title="discourse_teambuild.targets.cancel" |
| 162 | + class="btn-danger cancel" |
| 163 | + /> |
| 164 | + {{else}} |
| 165 | + <DButton |
| 166 | + @icon="pencil" |
| 167 | + @title="discourse_teambuild.targets.edit" |
| 168 | + @action={{fn (mut this.editSelected) true}} |
| 169 | + class="edit" |
| 170 | + /> |
| 171 | + |
| 172 | + <DButton |
| 173 | + @icon="trash-can" |
| 174 | + @title="discourse_teambuild.targets.delete" |
| 175 | + @action={{this.destroyTarget}} |
| 176 | + class="btn-danger destroy" |
| 177 | + /> |
| 178 | + {{/if}} |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + </template> |
| 182 | +} |
0 commit comments