1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 13:19:43 +02:00
documize/gui/app/services/pinned.js

159 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-11-21 19:27:18 -08:00
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
import { A } from '@ember/array';
2016-11-21 19:27:18 -08:00
import ArrayProxy from '@ember/array/proxy';
import RSVP, { Promise as EmberPromise } from 'rsvp';
import Service, { inject as service } from '@ember/service';
2016-11-21 19:27:18 -08:00
export default Service.extend({
2016-11-21 19:27:18 -08:00
session: service('session'),
ajax: service(),
appMeta: service(),
store: service(),
pins: [],
2017-03-09 12:51:21 +00:00
initialized: false,
2016-11-21 19:27:18 -08:00
getUserPins() {
let userId = this.get('session.user.id');
if (!this.get('session.authenticated')) {
return new RSVP.resolve([]);
}
2016-11-21 19:27:18 -08:00
return this.get('ajax').request(`pin/${userId}`, {
method: 'GET'
}).then((response) => {
if (is.not.array(response)) {
response = [];
}
let pins = ArrayProxy.create({
content: A([])
2016-11-21 19:27:18 -08:00
});
pins = response.map((pin) => {
let data = this.get('store').normalize('pin', pin);
return this.get('store').push(data);
});
this.set('pins', pins);
2017-03-09 12:51:21 +00:00
this.set('initialized', true);
2016-11-21 19:27:18 -08:00
return pins;
});
},
// Pin an item.
pinItem(data) {
let userId = this.get('session.user.id');
if(this.get('session.authenticated')) {
return this.get('ajax').request(`pin/${userId}`, {
method: 'POST',
data: JSON.stringify(data)
}).then((response) => {
let data = this.get('store').normalize('pin', response);
return this.get('store').push(data);
});
}
},
// Unpin an item.
unpinItem(pinId) {
let userId = this.get('session.user.id');
if(this.get('session.authenticated')) {
return this.get('ajax').request(`pin/${userId}/${pinId}`, {
method: 'DELETE'
});
}
},
// updateSequence persists order after use drag-drop sorting.
updateSequence(data) {
let userId = this.get('session.user.id');
if(this.get('session.authenticated')) {
return this.get('ajax').request(`pin/${userId}/sequence`, {
method: 'POST',
data: JSON.stringify(data)
}).then((response) => {
if (is.not.array(response)) {
response = [];
}
let pins = ArrayProxy.create({
content: A([])
2016-11-21 19:27:18 -08:00
});
pins = response.map((pin) => {
let data = this.get('store').normalize('pin', pin);
return this.get('store').push(data);
});
this.set('pins', pins);
return pins;
});
}
},
isDocumentPinned(documentId) {
let userId = this.get('session.user.id');
2017-10-02 13:48:37 -04:00
let pins = this.get('pins');
2016-11-21 19:27:18 -08:00
return new EmberPromise((resolve) => {
2017-10-02 13:48:37 -04:00
if (this.get('initialized') === false) {
this.getUserPins().then((pins) => {
pins.forEach((pin) => {
if (pin.get('userId') === userId && pin.get('documentId') === documentId) {
resolve(pin.get('id'));
}
});
});
} else {
2017-03-09 12:51:21 +00:00
pins.forEach((pin) => {
if (pin.get('userId') === userId && pin.get('documentId') === documentId) {
2017-10-02 13:48:37 -04:00
resolve(pin.get('id'));
2017-03-09 12:51:21 +00:00
}
});
2017-10-02 13:48:37 -04:00
}
2017-03-09 12:51:21 +00:00
2017-10-02 13:48:37 -04:00
resolve('');
});
2016-11-21 19:27:18 -08:00
},
isSpacePinned(spaceId) {
let userId = this.get('session.user.id');
let pins = this.get('pins');
return new EmberPromise((resolve) => {
2017-10-02 13:48:37 -04:00
if (!this.get('initialized')) {
this.getUserPins().then((pins) => {
pins.forEach((pin) => {
if (pin.get('userId') === userId && pin.get('documentId') === '' && pin.get('folderId') === spaceId) {
resolve(pin.get('id'));
}
});
});
} else {
pins.forEach((pin) => {
if (pin.get('userId') === userId && pin.get('documentId') === '' && pin.get('folderId') === spaceId) {
resolve(pin.get('id'));
}
});
2016-11-21 19:27:18 -08:00
}
2017-10-02 13:48:37 -04:00
resolve('');
});
2016-11-21 19:27:18 -08:00
}
});