forked from CartoDB/cdb-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
189 lines (159 loc) · 5.12 KB
/
functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
api.factory('Function', ["SQLClient", function (SQLClient) {
return function (functionFromDB) {
angular.extend(this, functionFromDB);
this.api = new SQLClient();
this.updateDefinition = function (action, error) {
self.api.send(self.definition, action, error);
};
};
}]);
cdbmanager.service("functions", ["SQLClient", "Function", function (SQLClient, Function) {
let self = this;
this.api = new SQLClient();
this.current = null;
let order = null;
this.get = function (action, error, extraQuery) {
let _action = function () {
if (self.api && self.api.items) {
for (let i = 0; i < self.api.items.length; i++) {
self.api.items[i] = new Function(self.api.items[i], self);
}
}
if (action) {
action();
}
};
let query = "select pg_proc.oid as _oid, pg_proc.*, pg_get_functiondef(pg_proc.oid) as definition from pg_proc, pg_roles where pg_proc.proowner = pg_roles.oid and pg_roles.rolname = current_user";
if (extraQuery) {
query += " " + extraQuery;
}
self.api.send(query, _action, error);
};
this.order = function (parameter, pageSize, newOrder) {
// pageSize needs to be there because it might be sent from a result table, but we don't really need it here
if (self.api && self.api.items) {
if (newOrder == "desc" || (!newOrder && order == "asc")) {
order = "desc";
self.api.items.sort(function (a, b) {
if (a[parameter] > b[parameter]) return -1;
if (a[parameter] < b[parameter]) return 1;
return 0;
});
} else {
order = "asc";
self.api.items.sort(function (a, b) {
if (a[parameter] < b[parameter]) return -1;
if (a[parameter] > b[parameter]) return 1;
return 0;
});
}
}
};
}]);
cdbmanager.controller('functionSelectorCtrl', ["$scope", "functions", "endpoints", "nav", function ($scope, functions, endpoints, nav) {
$scope.nav = nav;
$scope.showFunction = function (func) {
nav.setCurrentView("function");
functions.current = func;
};
$scope.refreshList = function () {
functions.get(function () {
functions.order("proname", "asc");
});
};
// update function list in scope when current endpoint changes
$scope.$watch(function () {
return endpoints.current;
}, function () {
$scope.functions = $scope.refreshList();
}, true);
// update function list in scope when actual function list change
$scope.$watch(function () {
return functions.api.items;
}, function (functionList) {
$scope.functions = functionList;
});
// update current function in scope when current function changes
$scope.$watch(function () {
return functions.current;
}, function (currentFunction) {
$scope.currentFunction = currentFunction;
});
}]);
cdbmanager.controller('functionsCtrl', ["$scope", "functions", "endpoints", "nav", "settings", function ($scope, functions, endpoints, nav, settings) {
$scope.nav = nav;
// Result table config
$scope.cdbrt = {
id: "functions",
rowsPerPage: settings.sqlConsoleRowsPerPage,
skip: ["prosrc", "definition"],
actions: [{
text: "View source code",
onClick: function (func) {
nav.setCurrentView("function");
functions.current = func;
}
}],
orderBy: functions.order
};
// update function list in scope when actual function list change
$scope.$watch(function () {
return functions.api.items;
}, function (functionList) {
$scope.functions = functionList;
});
}]);
cdbmanager.controller('functionCtrl', ["$scope", "nav", "functions", "$timeout", function ($scope, nav, functions, $timeout) {
$scope.nav = nav;
$scope.running = null;
$scope.error = null;
$scope.updated = null;
let editor = null;
// codemirror configuration
let mime = 'text/x-mariadb';
if (window.location.href.indexOf('mime=') > -1) {
mime = window.location.href.substr(window.location.href.indexOf('mime=') + 5);
}
$scope.editorOptions = {
mode: 'text/x-sql',
indentWithTabs: false,
smartIndent: true,
lineNumbers: true,
matchBrackets: true,
autofocus: true,
extraKeys: {
"Ctrl-Space": "autocomplete"
}
};
$scope.updateFunction = function (func) {
func.updateDefinition(function () {
functions.get();
});
};
// update function in editor when current function changes
$scope.$watch(function () {
return functions.current;
}, function (currentFunction) {
$scope.functionInEditor = angular.copy(currentFunction);
});
// codemirror must be refreshed when not hidden anymore, otherwise the text won't show until you click on the editor
$scope.$watch(function () {
return nav.current;
}, function () {
if (nav.isCurrentView("function")) {
// Need to refresh after digest cycle is over
$timeout(function () {
editor.refresh();
});
}
});
$scope.codemirrorLoaded = function (_editor) {
editor = _editor;
let ctrlEnter = {
"Ctrl-Enter": function () {
$scope.updateFunction($scope.functionInEditor);
}
};
editor.addKeyMap(ctrlEnter);
};
}]);