Commit 1ddcaf5b by Marcus Efraimsson

minor fixes and formatting after review

parent 0bfedfe4
...@@ -43,4 +43,4 @@ ...@@ -43,4 +43,4 @@
{{ctrl.validationError}} {{ctrl.validationError}}
</label> </label>
</div> </div>
</div> </div>
\ No newline at end of file
///<reference path="../../../headers/common.d.ts" /> import _ from "lodash";
import coreModule from "app/core/core_module";
import _ from 'lodash'; import appEvents from "app/core/app_events";
import coreModule from 'app/core/core_module';
import appEvents from 'app/core/app_events';
export class FolderPickerCtrl { export class FolderPickerCtrl {
initialTitle: string; initialTitle: string;
...@@ -14,7 +12,7 @@ export class FolderPickerCtrl { ...@@ -14,7 +12,7 @@ export class FolderPickerCtrl {
enterFolderCreation: any; enterFolderCreation: any;
exitFolderCreation: any; exitFolderCreation: any;
enableCreateNew: boolean; enableCreateNew: boolean;
rootName = 'Root'; rootName = "Root";
folder: any; folder: any;
createNewFolder: boolean; createNewFolder: boolean;
newFolderName: string; newFolderName: string;
...@@ -34,24 +32,26 @@ export class FolderPickerCtrl { ...@@ -34,24 +32,26 @@ export class FolderPickerCtrl {
getOptions(query) { getOptions(query) {
var params = { var params = {
query: query, query: query,
type: 'dash-folder', type: "dash-folder"
}; };
return this.backendSrv.search(params).then(result => { return this.backendSrv.search(params).then(result => {
if (query === '' || if (
query.toLowerCase() === "r" || query === "" ||
query.toLowerCase() === "ro" || query.toLowerCase() === "r" ||
query.toLowerCase() === "roo" || query.toLowerCase() === "ro" ||
query.toLowerCase() === "root") { query.toLowerCase() === "roo" ||
result.unshift({title: this.rootName, id: 0}); query.toLowerCase() === "root"
) {
result.unshift({ title: this.rootName, id: 0 });
} }
if (this.enableCreateNew && query === '') { if (this.enableCreateNew && query === "") {
result.unshift({title: '-- New Folder --', id: -1}); result.unshift({ title: "-- New Folder --", id: -1 });
} }
return _.map(result, item => { return _.map(result, item => {
return {text: item.title, value: item.id}; return { text: item.title, value: item.id };
}); });
}); });
} }
...@@ -62,13 +62,14 @@ export class FolderPickerCtrl { ...@@ -62,13 +62,14 @@ export class FolderPickerCtrl {
this.enterFolderCreation(); this.enterFolderCreation();
return; return;
} }
this.onChange({$folder: {id: option.value, title: option.text}}); this.onChange({ $folder: { id: option.value, title: option.text } });
} }
newFolderNameChanged() { newFolderNameChanged() {
this.newFolderNameTouched = true; this.newFolderNameTouched = true;
this.validationSrv.validateNewDashboardOrFolderName(this.newFolderName) this.validationSrv
.validateNewDashboardOrFolderName(this.newFolderName)
.then(() => { .then(() => {
this.hasValidationError = false; this.hasValidationError = false;
}) })
...@@ -84,13 +85,18 @@ export class FolderPickerCtrl { ...@@ -84,13 +85,18 @@ export class FolderPickerCtrl {
evt.preventDefault(); evt.preventDefault();
} }
return this.backendSrv.createDashboardFolder(this.newFolderName).then(result => { return this.backendSrv
appEvents.emit('alert-success', ['Folder Created', 'OK']); .createDashboardFolder(this.newFolderName)
.then(result => {
this.closeCreateFolder(); appEvents.emit("alert-success", ["Folder Created", "OK"]);
this.folder = {text: result.dashboard.title, value: result.dashboard.id};
this.onFolderChange(this.folder); this.closeCreateFolder();
}); this.folder = {
text: result.dashboard.title,
value: result.dashboard.id
};
this.onFolderChange(this.folder);
});
} }
cancelCreateFolder(evt) { cancelCreateFolder(evt) {
...@@ -108,21 +114,21 @@ export class FolderPickerCtrl { ...@@ -108,21 +114,21 @@ export class FolderPickerCtrl {
this.createNewFolder = false; this.createNewFolder = false;
this.hasValidationError = false; this.hasValidationError = false;
this.validationError = null; this.validationError = null;
this.newFolderName = ''; this.newFolderName = "";
this.newFolderNameTouched = false; this.newFolderNameTouched = false;
} }
private loadInitialValue() { private loadInitialValue() {
if (this.initialFolderId && this.initialFolderId > 0) { if (this.initialFolderId && this.initialFolderId > 0) {
this.getOptions('').then(result => { this.getOptions("").then(result => {
this.folder = _.find(result, {value: this.initialFolderId}); this.folder = _.find(result, { value: this.initialFolderId });
this.onFolderLoad(); this.onFolderLoad();
}); });
} else { } else {
if (this.initialTitle) { if (this.initialTitle) {
this.folder = {text: this.initialTitle, value: null}; this.folder = { text: this.initialTitle, value: null };
} else { } else {
this.folder = {text: this.rootName, value: 0}; this.folder = { text: this.rootName, value: 0 };
} }
this.onFolderLoad(); this.onFolderLoad();
...@@ -131,31 +137,34 @@ export class FolderPickerCtrl { ...@@ -131,31 +137,34 @@ export class FolderPickerCtrl {
private onFolderLoad() { private onFolderLoad() {
if (this.onLoad) { if (this.onLoad) {
this.onLoad({$folder: {id: this.folder.value, title: this.folder.text}}); this.onLoad({
$folder: { id: this.folder.value, title: this.folder.text }
});
} }
} }
} }
export function folderPicker() { export function folderPicker() {
return { return {
restrict: 'E', restrict: "E",
templateUrl: 'public/app/features/dashboard/folder_picker/folder_picker.html', templateUrl:
"public/app/features/dashboard/folder_picker/folder_picker.html",
controller: FolderPickerCtrl, controller: FolderPickerCtrl,
bindToController: true, bindToController: true,
controllerAs: 'ctrl', controllerAs: "ctrl",
scope: { scope: {
initialTitle: '<', initialTitle: "<",
initialFolderId: '<', initialFolderId: "<",
labelClass: '@', labelClass: "@",
rootName: '@', rootName: "@",
onChange: '&', onChange: "&",
onLoad: '&', onLoad: "&",
onCreateFolder: '&', onCreateFolder: "&",
enterFolderCreation: '&', enterFolderCreation: "&",
exitFolderCreation: '&', exitFolderCreation: "&",
enableCreateNew: '@' enableCreateNew: "@"
} }
}; };
} }
coreModule.directive('folderPicker', folderPicker); coreModule.directive("folderPicker", folderPicker);
///<reference path="../../headers/common.d.ts" /> import coreModule from "app/core/core_module";
import coreModule from 'app/core/core_module';
export class ValidationSrv { export class ValidationSrv {
rootName = 'root'; rootName = "root";
/** @ngInject */ /** @ngInject */
constructor(private $q, private backendSrv) {} constructor(private $q, private backendSrv) {}
validateNewDashboardOrFolderName(name) { validateNewDashboardOrFolderName(name) {
name = (name || '').trim(); name = (name || "").trim();
if (name.length === 0) { if (name.length === 0) {
return this.$q.reject({ return this.$q.reject({
type: 'REQUIRED', type: "REQUIRED",
message: 'Name is required' message: "Name is required"
}); });
} }
if (name.toLowerCase() === this.rootName) { if (name.toLowerCase() === this.rootName) {
return this.$q.reject({ return this.$q.reject({
type: 'EXISTING', type: "EXISTING",
message: 'A folder or dashboard with the same name already exists' message: "A folder or dashboard with the same name already exists"
}); });
} }
...@@ -31,8 +29,8 @@ export class ValidationSrv { ...@@ -31,8 +29,8 @@ export class ValidationSrv {
for (let hit of res) { for (let hit of res) {
if (name.toLowerCase() === hit.title.toLowerCase()) { if (name.toLowerCase() === hit.title.toLowerCase()) {
deferred.reject({ deferred.reject({
type: 'EXISTING', type: "EXISTING",
message: 'A folder or dashboard with the same name already exists' message: "A folder or dashboard with the same name already exists"
}); });
break; break;
} }
...@@ -45,4 +43,4 @@ export class ValidationSrv { ...@@ -45,4 +43,4 @@ export class ValidationSrv {
} }
} }
coreModule.service('validationSrv', ValidationSrv); coreModule.service("validationSrv", ValidationSrv);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment