Failed Conditions
Pull Request — master (#72)
by Sander
01:51
created

js/app/controllers/NoteEditCtrl.js (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
/**
2
 * Nextcloud - NextNotes
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
26
	/**
27
	 * @ngdoc function
28
	 * @name passmanApp.controller:MainCtrl
29
	 * @description
30
	 * # MainCtrl
31
	 * Controller of the passmanApp
32
	 */
33
	angular.module('NextNotesApp').controller('NoteEditCtrl', [
34
		'$scope',
35
		'$rootScope',
36
		'NoteService',
37
		'$routeParams',
38
		'$location',
39
		'$timeout',
40
		'NoteFactory',
41
		'NotebookFactory',
42
		function ($scope, $rootScope, NoteService, $routeParams, $location, $timeout,
43
				  NoteFactory, NotebookFactory) {
44
			$scope.noteShadowCopy = {
45
				title: '',
46
				content: '',
47
				owner: {
48
					uid: OC.getCurrentUser().uid
49
				},
50
				permissions: OC.PERMISSION_ALL
51
			};
52
53
			function saveNote () {
54
				var isNewNote = ($scope.noteShadowCopy.id === undefined);
55
				$scope.noteShadowCopy.$save().then( function (result) {
56
					result.mtime = result.mtime * 1000;
57
					$rootScope.notes[result.id] = result;
58
					$scope.autoSaved = true;
59
					$timeout(function () {
60
						$scope.autoSaved = false;
61
					}, 2500);
62
					if(isNewNote){
63
						window.location = '#!/note/edit/'+result.id;
64
					}
65
					$rootScope.$emit('refresh_notes');
66
				});
67
			}
68
69
			function initGroups () {
70
				$scope.local_notebooks = angular.copy($rootScope.note_groups);
71
				$scope.local_notebooks['empty'] = {
0 ignored issues
show
['empty'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
72
					id: '',
73
					name: 'Not grouped'
74
				};
75
76
				$scope.local_notebooks['_new'] = {
0 ignored issues
show
['_new'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
77
					id: '_new',
78
					name: 'New group'
79
				};
80
			}
81
82
			$scope.new_group = '';
83
84
			function loadNote () {
85
				var noteId = ($routeParams.noteId) ? $routeParams.noteId : null;
86
				if (noteId) {
87
					NoteService.getNoteById(noteId).then(function (note) {
88
						$scope.note = note;
89
						$scope.noteShadowCopy = new NoteFactory(angular.copy(note));
90
					});
91
				} else {
92
					$scope.note = NoteService.newNote();
93
					$scope.noteShadowCopy = new NoteFactory(angular.copy($scope.note));
94
				}
95
			}
96
97
			if (!$rootScope.note_groups) {
98
				$rootScope.$on('nextnotes_notebooks_loaded', function () {
99
					initGroups();
100
				});
101
			} else {
102
				initGroups();
103
			}
104
105
106
			if ($rootScope.notes) {
107
				loadNote();
108
			} else {
109
				$rootScope.$on('nextnotes_notes_loaded', function () {
110
					loadNote();
111
				});
112
			}
113
114
			var o = $('#ownnote').offset();
115
			var h = $(window).height() - o.top;
116
			$scope.tinymceOptions = {
117
				menubar: false,
118
				theme: 'modern',
119
				skin: 'lightgray',
120
				plugins: [
121
					'advlist autolink lists link image charmap print preview hr anchor pagebreak',
122
					'searchreplace wordcount visualblocks visualchars code fullscreen',
123
					'insertdatetime media nonbreaking save table contextmenu directionality',
124
					'emoticons template paste textcolor colorpicker textpattern imagetools codesample toc help bdesk_photo',
125
				],
126
				extended_valid_elements: 'form[name|id|action|method|enctype|accept-charset|onsubmit|onreset|target],input[id|name|type|value|size|maxlength|checked|accept|src|width|height|disabled|readonly|tabindex|accesskey|onfocus|onblur|onchange|onselect|onclick|onkeyup|onkeydown|required|style],textarea[id|name|rows|cols|maxlength|disabled|readonly|tabindex|accesskey|onfocus|onblur|onchange|onselect|onclick|onkeyup|onkeydown|required|style],option[name|id|value|selected|style],select[id|name|type|value|size|maxlength|checked|width|height|disabled|readonly|tabindex|accesskey|onfocus|onblur|onchange|onselect|onclick|multiple|style]',
127
				toolbar1: 'print | undo redo | styleselect fontselect | bold italic strikethrough | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist table outdent indent | link image bdesk_photo | codesample help | code ',
128
				image_advtab: true,
129
				allow_html_data_urls: true,
130
				allow_script_urls: true,
131
				paste_data_images: true,
132
				width: '100%',
133
				height: h - 155,
134
				browser_spellcheck: true,
135
				autoresize_min_height: h - 140,
136
				autoresize_max_height: h - 140,
137
				file_picker_types: 'file image media',
138
				file_browser_callback: NextCloudFileBrowserDialogue,
139
				textpattern_patterns: [
140
					{start: '*', end: '*', format: 'italic'},
141
					{start: '**', end: '**', format: 'bold'},
142
					{start: '#', format: 'h1'},
143
					{start: '##', format: 'h2'},
144
					{start: '###', format: 'h3'},
145
					{start: '####', format: 'h4'},
146
					{start: '#####', format: 'h5'},
147
					{start: '######', format: 'h6'},
148
					{start: '1. ', cmd: 'InsertOrderedList'},
149
					{start: '* ', cmd: 'InsertUnorderedList'},
150
					{start: '- ', cmd: 'InsertUnorderedList'},
151
				],
152
			};
153
154
			$scope.autoSaved = false;
155
			$scope.saveNote = function (autoSave) {
156
				if (autoSaveTimer) {
157
					$timeout.cancel(autoSaveTimer);
158
				}
159
				if (!$scope.noteShadowCopy.title) {
160
					return;
161
				}
162
				console.log($scope.noteShadowCopy)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
163
				if ($scope.noteShadowCopy.notebook && $scope.noteShadowCopy.notebook.hasOwnProperty('id')) {
164
					$scope.noteShadowCopy.notebook_id = $scope.noteShadowCopy.notebook.id;
165
				}
166
				console.log($scope.noteShadowCopy)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
167
				if ($scope.noteShadowCopy.notebook.id === '_new' &&
168
					$scope.new_group !== '') {
169
170
					NotebookFactory.save({
171
						name: angular.copy($scope.new_group),
172
						color: '',
173
						parent_id: 0
174
					}).$promise.then(function (notebook) {
175
						$scope.noteShadowCopy.notebook_id = notebook.id;
176
						saveNote();
177
					});
178
				} else {
179
					saveNote();
180
				}
181
182
			};
183
184
			var autoSaveTimer;
185
			var initialSave = true;
186
187
188
			var watcher = $scope.$watch('[noteShadowCopy.title, noteShadowCopy.content]',
189
				function () {
190
					if (autoSaveTimer) {
191
						$timeout.cancel(autoSaveTimer);
192
					}
193
					if (!$scope.hasPermission($scope.noteShadowCopy, 'update')) {
194
						watcher();
195
						console.log('Disabling auto save, no edit permissions');
196
						return;
197
					}
198
					if(!$scope.noteShadowCopy.id){
199
						return;
200
					}
201
					if ($scope.noteShadowCopy.title &&
202
						$scope.noteShadowCopy.title !== '') {
203
						if (initialSave) {
204
							initialSave = false;
205
							return;
206
						}
207
						autoSaveTimer = $timeout(function () {
208
							if ($routeParams.noteId) {
209
								$scope.saveNote(true);
210
							}
211
						}, 1000);
212
					}
213
				});
214
215
		}]);
216
}());
217