Updated application sources
[apps/web/sample/FileManager.git] / project / js / app.clipboard.js
1 /*
2  *      Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  *      Licensed under the Flora License, Version 1.1 (the "License");
5  *      you may not use this file except in compliance with the License.
6  *      You may obtain a copy of the License at
7  *
8  *              http://floralicense.org/license/
9  *
10  *      Unless required by applicable law or agreed to in writing, software
11  *      distributed under the License is distributed on an "AS IS" BASIS,
12  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *      See the License for the specific language governing permissions and
14  *      limitations under the License.
15  */
16
17 /*jslint devel: true*/
18 /*global $*/
19
20 /**
21  * @class Config
22  */
23 function Clipboard() {
24     'use strict';
25     this.mode = this.INACTIVE_MODE;
26 }
27
28 (function () { // strict mode wrapper
29     'use strict';
30     Clipboard.prototype = {
31         /**
32          * Clipboard mode for copying
33          */
34         COPY_MODE_ID: 0,
35
36         /**
37          * Clipboard mode for moving
38          */
39         MOVE_MODE_ID: 1,
40
41         /**
42          * Clipbboard inactive mode
43          */
44         INACTIVE_MODE: -1,
45
46         /**
47          * Clipboard data
48          */
49         data: [],
50
51         /**
52          * Clipboard mode: [copy | move | inactive]
53          */
54         mode: undefined,
55
56         /**
57          * Returns all paths in clipboard
58          * @returns {array}
59          */
60         get: function Clipboard_get() {
61             return this.data;
62         },
63
64         /**
65          * Add new path to clipboard
66          * @param {array} paths array of full paths
67          * @returns {number} current length of clipboard objects
68          */
69         add: function Clipboard_add(paths) {
70             var len = paths.length,
71                 i;
72
73             // clear clipboard
74             this.clear();
75             for (i = 0; i < len; i += 1) {
76                 if (this.has(paths[i]) === false) {
77                     this.data.push(paths[i]);
78                 }
79             }
80
81             return this.data.length;
82         },
83
84         /**
85          * Remove specified path is already in clipboard
86          * @param {string} path full path
87          * @returns {number} current length of clipboard objects
88          */
89         remove: function Clipboard_remove(path) {
90             var index = $.inArray(path, this.data),
91                 length;
92             if (index >= 0) {
93                 this.data.splice(index, 1);
94                 length = this.data.length;
95                 if (length === 0) {
96                     this.mode = this.INACTIVE_MODE;
97                 }
98             }
99             return length;
100         },
101
102         /**
103          * Remove specified path and all children paths if already in clipboard
104          * @param {string} path full path
105          * @returns {number} current length of clipboard objects
106          */
107         removeRecursively: function Clipboard_removeRecursively(path) {
108             var escapeRegExp = function (str) {
109                     return str
110                         .replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
111                 },
112                 childPattern = new RegExp(escapeRegExp(path)),
113                 index = this.data.length - 1;
114             while (index >= 0) {
115                 if (childPattern.test(this.data[index])) {
116                     this.data.splice(index, 1);
117                 }
118                 index -= 1;
119             }
120             return this.data.length;
121         },
122
123         /**
124          * Checks if specified path is already in clipboard
125          * @param {string} path full path
126          * @returns {boolean}
127          */
128         has: function Clipboard_has(path) {
129             return $.inArray(path, this.data) === -1 ? false : true;
130         },
131
132         /**
133          * Clears all clipboard data and resets clipboard mode
134          */
135         clear: function Clipboard_clear() {
136             this.data = [];
137             this.mode = this.INACTIVE_MODE;
138         },
139
140         /**
141          * Sets clipboard mode
142          * @param {number} mode
143          * @returns {boolean}
144          */
145         setMode: function Clipboard_setMode(mode) {
146             if (
147                 $.inArray(
148                     mode,
149                     [this.MOVE_MODE_ID, this.COPY_MODE_ID]
150                 ) === false
151             ) {
152                 console.error('Incorrect clipboard mode');
153                 return false;
154             }
155             this.mode = mode;
156             return true;
157         },
158
159         /**
160          * @returns {number} mode Clipboard mode
161          */
162         getMode: function Clipboard_getMode() {
163             return this.mode;
164         },
165
166         /**
167          * @returns {boolean}
168          */
169         isEmpty: function Clipboard_isEmpty() {
170             return this.data.length === 0;
171         }
172     };
173 }());