Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Romain Courteaud
erp5
Commits
1f2bf3bf
Commit
1f2bf3bf
authored
Feb 25, 2019
by
Romain Courteaud
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[erp5_core/erp5_web_renderjs_ui] Update jIO 3.37.0
parent
881b4f4f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
782 deletions
+26
-782
bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_jio_js.js
...enderjs_ui/PathTemplateItem/web_page_module/rjs_jio_js.js
+12
-390
bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_jio_js.xml
...nderjs_ui/PathTemplateItem/web_page_module/rjs_jio_js.xml
+2
-2
product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/jio.js.js
...p5_core/SkinTemplateItem/portal_skins/erp5_core/jio.js.js
+12
-390
No files found.
bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_jio_js.js
View file @
1f2bf3bf
...
...
@@ -8080,17 +8080,11 @@ return new Parser;
* See https://www.nexedi.com/licensing for rationale and options.
*/
/*global window, RSVP, Blob, XMLHttpRequest, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array
, navigator
*/
FileReader, ArrayBuffer, Uint8Array */
(
function
(
window
,
RSVP
,
Blob
,
QueryFactory
,
Query
,
atob
,
FileReader
,
ArrayBuffer
,
Uint8Array
,
navigator
)
{
FileReader
,
ArrayBuffer
,
Uint8Array
)
{
"
use strict
"
;
if
(
window
.
openDatabase
===
undefined
)
{
window
.
openDatabase
=
function
()
{
throw
new
Error
(
'
WebSQL is not supported by
'
+
navigator
.
userAgent
);
};
}
/* Safari does not define DOMError */
if
(
window
.
DOMError
===
undefined
)
{
window
.
DOMError
=
{};
...
...
@@ -8536,6 +8530,7 @@ return new Parser;
if
(
context
.
hasCapacity
(
"
list
"
)
&&
((
options
.
query
===
undefined
)
||
context
.
hasCapacity
(
"
query
"
))
&&
((
options
.
sort_on
===
undefined
)
||
context
.
hasCapacity
(
"
sort
"
))
&&
((
options
.
group_by
===
undefined
)
||
context
.
hasCapacity
(
"
group
"
))
&&
((
options
.
select_list
===
undefined
)
||
context
.
hasCapacity
(
"
select
"
))
&&
((
options
.
include_docs
===
undefined
)
||
...
...
@@ -8622,7 +8617,7 @@ return new Parser;
window
.
jIO
=
jIO
;
}(
window
,
RSVP
,
Blob
,
QueryFactory
,
Query
,
atob
,
FileReader
,
ArrayBuffer
,
Uint8Array
,
navigator
));
FileReader
,
ArrayBuffer
,
Uint8Array
));
/*
* Rusha, a JavaScript implementation of the Secure Hash Algorithm, SHA-1,
* as defined in FIPS PUB 180-1, tuned for high performance with large inputs.
...
...
@@ -13695,7 +13690,7 @@ return new Parser;
ERP5Storage
.
prototype
.
hasCapacity
=
function
(
name
)
{
return
((
name
===
"
list
"
)
||
(
name
===
"
query
"
)
||
(
name
===
"
select
"
)
||
(
name
===
"
limit
"
)
||
(
name
===
"
sort
"
));
(
name
===
"
sort
"
)
||
(
name
===
"
group
"
)
);
};
function
isSingleLocalRoles
(
parsed_query
)
{
...
...
@@ -13763,7 +13758,8 @@ return new Parser;
local_roles
,
local_role_found
=
false
,
selection_domain
,
sort_list
=
[];
sort_list
=
[],
group_list
=
[];
if
(
options
.
query
)
{
parsed_query
=
jIO
.
QueryFactory
.
create
(
options
.
query
);
result_list
=
isSingleLocalRoles
(
parsed_query
);
...
...
@@ -13835,6 +13831,10 @@ return new Parser;
}
}
if
(
options
.
group_by
)
{
group_list
=
options
.
group_by
;
}
if
(
selection_domain
)
{
selection_domain
=
JSON
.
stringify
(
selection_domain
);
}
...
...
@@ -13848,6 +13848,7 @@ return new Parser;
select_list
:
options
.
select_list
||
[
"
title
"
,
"
reference
"
],
limit
:
options
.
limit
,
sort_on
:
sort_list
,
group_by
:
group_list
,
local_roles
:
local_roles
,
selection_domain
:
selection_domain
})
...
...
@@ -15686,385 +15687,6 @@ return new Parser;
jIO
.
addStorage
(
'
crypt
'
,
CryptStorage
);
}(
jIO
,
RSVP
,
DOMException
,
Blob
,
crypto
,
Uint8Array
,
ArrayBuffer
));
/*
* Copyright 2013, Nexedi SA
*
* This program is free software: you can Use, Study, Modify and Redistribute
* it under the terms of the GNU General Public License version 3, or (at your
* option) any later version, as published by the Free Software Foundation.
*
* You can also Link and Combine this program with other software covered by
* the terms of any of the Free Software licenses or any of the Open Source
* Initiative approved licenses and Convey the resulting work. Corresponding
* source of such a combination shall include the source code for all other
* software used.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See COPYING file for full licensing terms.
* See https://www.nexedi.com/licensing for rationale and options.
*/
/**
* JIO Websql Storage. Type = "websql".
* websql "database" storage.
*/
/*global Blob, jIO, RSVP, openDatabase*/
/*jslint nomen: true*/
(
function
(
jIO
,
RSVP
,
Blob
,
openDatabase
)
{
"
use strict
"
;
/**
* The JIO Websql Storage extension
*
* @class WebSQLStorage
* @constructor
*/
function
queueSql
(
db
,
query_list
,
argument_list
)
{
return
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
/*jslint unparam: true*/
db
.
transaction
(
function
(
tx
)
{
var
len
=
query_list
.
length
,
result_list
=
[],
i
;
function
resolveTransaction
(
tx
,
result
)
{
result_list
.
push
(
result
);
if
(
result_list
.
length
===
len
)
{
resolve
(
result_list
);
}
}
function
rejectTransaction
(
tx
,
error
)
{
reject
(
error
);
return
true
;
}
for
(
i
=
0
;
i
<
len
;
i
+=
1
)
{
tx
.
executeSql
(
query_list
[
i
],
argument_list
[
i
],
resolveTransaction
,
rejectTransaction
);
}
},
function
(
tx
,
error
)
{
reject
(
error
);
});
/*jslint unparam: false*/
});
}
function
initDatabase
(
db
)
{
var
query_list
=
[
"
CREATE TABLE IF NOT EXISTS document
"
+
"
(id VARCHAR PRIMARY KEY NOT NULL, data TEXT)
"
,
"
CREATE TABLE IF NOT EXISTS attachment
"
+
"
(id VARCHAR, attachment VARCHAR, part INT, blob TEXT)
"
,
"
CREATE TRIGGER IF NOT EXISTS removeAttachment
"
+
"
BEFORE DELETE ON document FOR EACH ROW
"
+
"
BEGIN DELETE from attachment WHERE id = OLD.id;END;
"
,
"
CREATE INDEX IF NOT EXISTS index_document ON document (id);
"
,
"
CREATE INDEX IF NOT EXISTS index_attachment
"
+
"
ON attachment (id, attachment);
"
];
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
queueSql
(
db
,
query_list
,
[]);
});
}
function
WebSQLStorage
(
spec
)
{
if
(
typeof
spec
.
database
!==
'
string
'
||
!
spec
.
database
)
{
throw
new
TypeError
(
"
database must be a string
"
+
"
which contains more than one character.
"
);
}
this
.
_database
=
openDatabase
(
"
jio:
"
+
spec
.
database
,
'
1.0
'
,
''
,
2
*
1024
*
1024
);
if
(
spec
.
blob_length
&&
(
typeof
spec
.
blob_length
!==
"
number
"
||
spec
.
blob_length
<
20
))
{
throw
new
TypeError
(
"
blob_len parameter must be a number >= 20
"
);
}
this
.
_blob_length
=
spec
.
blob_length
||
2000000
;
this
.
_init_db_promise
=
initDatabase
(
this
.
_database
);
}
WebSQLStorage
.
prototype
.
put
=
function
(
id
,
param
)
{
var
db
=
this
.
_database
,
that
=
this
,
data_string
=
JSON
.
stringify
(
param
);
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
return
queueSql
(
db
,
[
"
INSERT OR REPLACE INTO
"
+
"
document(id, data) VALUES(?,?)
"
],
[[
id
,
data_string
]]);
})
.
push
(
function
()
{
return
id
;
});
};
WebSQLStorage
.
prototype
.
remove
=
function
(
id
)
{
var
db
=
this
.
_database
,
that
=
this
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
return
queueSql
(
db
,
[
"
DELETE FROM document WHERE id = ?
"
],
[[
id
]]);
})
.
push
(
function
(
result_list
)
{
if
(
result_list
[
0
].
rowsAffected
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
return
id
;
});
};
WebSQLStorage
.
prototype
.
get
=
function
(
id
)
{
var
db
=
this
.
_database
,
that
=
this
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
return
queueSql
(
db
,
[
"
SELECT data FROM document WHERE id = ?
"
],
[[
id
]]);
})
.
push
(
function
(
result_list
)
{
if
(
result_list
[
0
].
rows
.
length
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
return
JSON
.
parse
(
result_list
[
0
].
rows
[
0
].
data
);
});
};
WebSQLStorage
.
prototype
.
allAttachments
=
function
(
id
)
{
var
db
=
this
.
_database
,
that
=
this
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
return
queueSql
(
db
,
[
"
SELECT id FROM document WHERE id = ?
"
,
"
SELECT DISTINCT attachment FROM attachment WHERE id = ?
"
],
[[
id
],
[
id
]]);
})
.
push
(
function
(
result_list
)
{
if
(
result_list
[
0
].
rows
.
length
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
var
len
=
result_list
[
1
].
rows
.
length
,
obj
=
{},
i
;
for
(
i
=
0
;
i
<
len
;
i
+=
1
)
{
obj
[
result_list
[
1
].
rows
[
i
].
attachment
]
=
{};
}
return
obj
;
});
};
function
sendBlobPart
(
blob
,
argument_list
,
index
,
queue
)
{
queue
.
push
(
function
()
{
return
jIO
.
util
.
readBlobAsDataURL
(
blob
);
})
.
push
(
function
(
strBlob
)
{
argument_list
[
index
+
2
].
push
(
strBlob
.
target
.
result
);
return
;
});
}
WebSQLStorage
.
prototype
.
putAttachment
=
function
(
id
,
name
,
blob
)
{
var
db
=
this
.
_database
,
that
=
this
,
part_size
=
this
.
_blob_length
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
return
queueSql
(
db
,
[
"
SELECT id FROM document WHERE id = ?
"
],
[[
id
]]);
})
.
push
(
function
(
result
)
{
var
query_list
=
[],
argument_list
=
[],
blob_size
=
blob
.
size
,
queue
=
new
RSVP
.
Queue
(),
i
,
index
;
if
(
result
[
0
].
rows
.
length
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot access subdocument
"
,
404
);
}
query_list
.
push
(
"
DELETE FROM attachment WHERE id = ?
"
+
"
AND attachment = ?
"
);
argument_list
.
push
([
id
,
name
]);
query_list
.
push
(
"
INSERT INTO attachment(id, attachment, part, blob)
"
+
"
VALUES(?, ?, ?, ?)
"
);
argument_list
.
push
([
id
,
name
,
-
1
,
blob
.
type
||
"
application/octet-stream
"
]);
for
(
i
=
0
,
index
=
0
;
i
<
blob_size
;
i
+=
part_size
,
index
+=
1
)
{
query_list
.
push
(
"
INSERT INTO attachment(id, attachment, part, blob)
"
+
"
VALUES(?, ?, ?, ?)
"
);
argument_list
.
push
([
id
,
name
,
index
]);
sendBlobPart
(
blob
.
slice
(
i
,
i
+
part_size
),
argument_list
,
index
,
queue
);
}
queue
.
push
(
function
()
{
return
queueSql
(
db
,
query_list
,
argument_list
);
});
return
queue
;
});
};
WebSQLStorage
.
prototype
.
getAttachment
=
function
(
id
,
name
,
options
)
{
var
db
=
this
.
_database
,
that
=
this
,
part_size
=
this
.
_blob_length
,
start
,
end
,
start_index
,
end_index
;
if
(
options
===
undefined
)
{
options
=
{};
}
start
=
options
.
start
||
0
;
end
=
options
.
end
||
-
1
;
if
(
start
<
0
||
(
options
.
end
!==
undefined
&&
options
.
end
<
0
))
{
throw
new
jIO
.
util
.
jIOError
(
"
_start and _end must be positive
"
,
400
);
}
if
(
start
>
end
&&
end
!==
-
1
)
{
throw
new
jIO
.
util
.
jIOError
(
"
_start is greater than _end
"
,
400
);
}
start_index
=
Math
.
floor
(
start
/
part_size
);
if
(
start
===
0
)
{
start_index
-=
1
;
}
end_index
=
Math
.
floor
(
end
/
part_size
);
if
(
end
%
part_size
===
0
)
{
end_index
-=
1
;
}
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
var
command
=
"
SELECT part, blob FROM attachment WHERE id = ? AND
"
+
"
attachment = ? AND part >= ?
"
,
argument_list
=
[
id
,
name
,
start_index
];
if
(
end
!==
-
1
)
{
command
+=
"
AND part <= ?
"
;
argument_list
.
push
(
end_index
);
}
return
queueSql
(
db
,
[
command
],
[
argument_list
]);
})
.
push
(
function
(
response_list
)
{
var
i
,
response
,
blob_array
=
[],
blob
,
type
;
response
=
response_list
[
0
].
rows
;
if
(
response
.
length
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
for
(
i
=
0
;
i
<
response
.
length
;
i
+=
1
)
{
if
(
response
[
i
].
part
===
-
1
)
{
type
=
response
[
i
].
blob
;
start_index
+=
1
;
}
else
{
blob_array
.
push
(
jIO
.
util
.
dataURItoBlob
(
response
[
i
].
blob
));
}
}
if
((
start
===
0
)
&&
(
options
.
end
===
undefined
))
{
return
new
Blob
(
blob_array
,
{
type
:
type
});
}
blob
=
new
Blob
(
blob_array
,
{});
return
blob
.
slice
(
start
-
(
start_index
*
part_size
),
end
===
-
1
?
blob
.
size
:
end
-
(
start_index
*
part_size
),
"
application/octet-stream
"
);
});
};
WebSQLStorage
.
prototype
.
removeAttachment
=
function
(
id
,
name
)
{
var
db
=
this
.
_database
,
that
=
this
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
return
queueSql
(
db
,
[
"
DELETE FROM attachment WHERE
"
+
"
id = ? AND attachment = ?
"
],
[[
id
,
name
]]);
})
.
push
(
function
(
result
)
{
if
(
result
[
0
].
rowsAffected
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
return
name
;
});
};
WebSQLStorage
.
prototype
.
hasCapacity
=
function
(
name
)
{
return
(
name
===
"
list
"
||
(
name
===
"
include
"
));
};
WebSQLStorage
.
prototype
.
buildQuery
=
function
(
options
)
{
var
db
=
this
.
_database
,
that
=
this
,
query
=
"
SELECT id
"
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
if
(
options
===
undefined
)
{
options
=
{};
}
if
(
options
.
include_docs
===
true
)
{
query
+=
"
, data AS doc
"
;
}
query
+=
"
FROM document
"
;
return
queueSql
(
db
,
[
query
],
[[]]);
})
.
push
(
function
(
result
)
{
var
array
=
[],
len
=
result
[
0
].
rows
.
length
,
i
;
for
(
i
=
0
;
i
<
len
;
i
+=
1
)
{
array
.
push
(
result
[
0
].
rows
[
i
]);
array
[
i
].
value
=
{};
if
(
array
[
i
].
doc
!==
undefined
)
{
array
[
i
].
doc
=
JSON
.
parse
(
array
[
i
].
doc
);
}
}
return
array
;
});
};
jIO
.
addStorage
(
'
websql
'
,
WebSQLStorage
);
}(
jIO
,
RSVP
,
Blob
,
openDatabase
));
/*
* Copyright 2017, Nexedi SA
*
...
...
bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_jio_js.xml
View file @
1f2bf3bf
...
...
@@ -234,7 +234,7 @@
</item>
<item>
<key>
<string>
serial
</string>
</key>
<value>
<string>
973.
43645.19782.57207
</string>
</value>
<value>
<string>
973.
59499.42683.33604
</string>
</value>
</item>
<item>
<key>
<string>
state
</string>
</key>
...
...
@@ -252,7 +252,7 @@
</tuple>
<state>
<tuple>
<float>
155
0160394.98
</float>
<float>
155
1454459.95
</float>
<string>
UTC
</string>
</tuple>
</state>
...
...
product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/jio.js.js
View file @
1f2bf3bf
...
...
@@ -8080,17 +8080,11 @@ return new Parser;
* See https://www.nexedi.com/licensing for rationale and options.
*/
/*global window, RSVP, Blob, XMLHttpRequest, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array
, navigator
*/
FileReader, ArrayBuffer, Uint8Array */
(
function
(
window
,
RSVP
,
Blob
,
QueryFactory
,
Query
,
atob
,
FileReader
,
ArrayBuffer
,
Uint8Array
,
navigator
)
{
FileReader
,
ArrayBuffer
,
Uint8Array
)
{
"
use strict
"
;
if
(
window
.
openDatabase
===
undefined
)
{
window
.
openDatabase
=
function
()
{
throw
new
Error
(
'
WebSQL is not supported by
'
+
navigator
.
userAgent
);
};
}
/* Safari does not define DOMError */
if
(
window
.
DOMError
===
undefined
)
{
window
.
DOMError
=
{};
...
...
@@ -8536,6 +8530,7 @@ return new Parser;
if
(
context
.
hasCapacity
(
"
list
"
)
&&
((
options
.
query
===
undefined
)
||
context
.
hasCapacity
(
"
query
"
))
&&
((
options
.
sort_on
===
undefined
)
||
context
.
hasCapacity
(
"
sort
"
))
&&
((
options
.
group_by
===
undefined
)
||
context
.
hasCapacity
(
"
group
"
))
&&
((
options
.
select_list
===
undefined
)
||
context
.
hasCapacity
(
"
select
"
))
&&
((
options
.
include_docs
===
undefined
)
||
...
...
@@ -8622,7 +8617,7 @@ return new Parser;
window
.
jIO
=
jIO
;
}(
window
,
RSVP
,
Blob
,
QueryFactory
,
Query
,
atob
,
FileReader
,
ArrayBuffer
,
Uint8Array
,
navigator
));
FileReader
,
ArrayBuffer
,
Uint8Array
));
/*
* Rusha, a JavaScript implementation of the Secure Hash Algorithm, SHA-1,
* as defined in FIPS PUB 180-1, tuned for high performance with large inputs.
...
...
@@ -13695,7 +13690,7 @@ return new Parser;
ERP5Storage
.
prototype
.
hasCapacity
=
function
(
name
)
{
return
((
name
===
"
list
"
)
||
(
name
===
"
query
"
)
||
(
name
===
"
select
"
)
||
(
name
===
"
limit
"
)
||
(
name
===
"
sort
"
));
(
name
===
"
sort
"
)
||
(
name
===
"
group
"
)
);
};
function
isSingleLocalRoles
(
parsed_query
)
{
...
...
@@ -13763,7 +13758,8 @@ return new Parser;
local_roles
,
local_role_found
=
false
,
selection_domain
,
sort_list
=
[];
sort_list
=
[],
group_list
=
[];
if
(
options
.
query
)
{
parsed_query
=
jIO
.
QueryFactory
.
create
(
options
.
query
);
result_list
=
isSingleLocalRoles
(
parsed_query
);
...
...
@@ -13835,6 +13831,10 @@ return new Parser;
}
}
if
(
options
.
group_by
)
{
group_list
=
options
.
group_by
;
}
if
(
selection_domain
)
{
selection_domain
=
JSON
.
stringify
(
selection_domain
);
}
...
...
@@ -13848,6 +13848,7 @@ return new Parser;
select_list
:
options
.
select_list
||
[
"
title
"
,
"
reference
"
],
limit
:
options
.
limit
,
sort_on
:
sort_list
,
group_by
:
group_list
,
local_roles
:
local_roles
,
selection_domain
:
selection_domain
})
...
...
@@ -15686,385 +15687,6 @@ return new Parser;
jIO
.
addStorage
(
'
crypt
'
,
CryptStorage
);
}(
jIO
,
RSVP
,
DOMException
,
Blob
,
crypto
,
Uint8Array
,
ArrayBuffer
));
/*
* Copyright 2013, Nexedi SA
*
* This program is free software: you can Use, Study, Modify and Redistribute
* it under the terms of the GNU General Public License version 3, or (at your
* option) any later version, as published by the Free Software Foundation.
*
* You can also Link and Combine this program with other software covered by
* the terms of any of the Free Software licenses or any of the Open Source
* Initiative approved licenses and Convey the resulting work. Corresponding
* source of such a combination shall include the source code for all other
* software used.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See COPYING file for full licensing terms.
* See https://www.nexedi.com/licensing for rationale and options.
*/
/**
* JIO Websql Storage. Type = "websql".
* websql "database" storage.
*/
/*global Blob, jIO, RSVP, openDatabase*/
/*jslint nomen: true*/
(
function
(
jIO
,
RSVP
,
Blob
,
openDatabase
)
{
"
use strict
"
;
/**
* The JIO Websql Storage extension
*
* @class WebSQLStorage
* @constructor
*/
function
queueSql
(
db
,
query_list
,
argument_list
)
{
return
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
/*jslint unparam: true*/
db
.
transaction
(
function
(
tx
)
{
var
len
=
query_list
.
length
,
result_list
=
[],
i
;
function
resolveTransaction
(
tx
,
result
)
{
result_list
.
push
(
result
);
if
(
result_list
.
length
===
len
)
{
resolve
(
result_list
);
}
}
function
rejectTransaction
(
tx
,
error
)
{
reject
(
error
);
return
true
;
}
for
(
i
=
0
;
i
<
len
;
i
+=
1
)
{
tx
.
executeSql
(
query_list
[
i
],
argument_list
[
i
],
resolveTransaction
,
rejectTransaction
);
}
},
function
(
tx
,
error
)
{
reject
(
error
);
});
/*jslint unparam: false*/
});
}
function
initDatabase
(
db
)
{
var
query_list
=
[
"
CREATE TABLE IF NOT EXISTS document
"
+
"
(id VARCHAR PRIMARY KEY NOT NULL, data TEXT)
"
,
"
CREATE TABLE IF NOT EXISTS attachment
"
+
"
(id VARCHAR, attachment VARCHAR, part INT, blob TEXT)
"
,
"
CREATE TRIGGER IF NOT EXISTS removeAttachment
"
+
"
BEFORE DELETE ON document FOR EACH ROW
"
+
"
BEGIN DELETE from attachment WHERE id = OLD.id;END;
"
,
"
CREATE INDEX IF NOT EXISTS index_document ON document (id);
"
,
"
CREATE INDEX IF NOT EXISTS index_attachment
"
+
"
ON attachment (id, attachment);
"
];
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
queueSql
(
db
,
query_list
,
[]);
});
}
function
WebSQLStorage
(
spec
)
{
if
(
typeof
spec
.
database
!==
'
string
'
||
!
spec
.
database
)
{
throw
new
TypeError
(
"
database must be a string
"
+
"
which contains more than one character.
"
);
}
this
.
_database
=
openDatabase
(
"
jio:
"
+
spec
.
database
,
'
1.0
'
,
''
,
2
*
1024
*
1024
);
if
(
spec
.
blob_length
&&
(
typeof
spec
.
blob_length
!==
"
number
"
||
spec
.
blob_length
<
20
))
{
throw
new
TypeError
(
"
blob_len parameter must be a number >= 20
"
);
}
this
.
_blob_length
=
spec
.
blob_length
||
2000000
;
this
.
_init_db_promise
=
initDatabase
(
this
.
_database
);
}
WebSQLStorage
.
prototype
.
put
=
function
(
id
,
param
)
{
var
db
=
this
.
_database
,
that
=
this
,
data_string
=
JSON
.
stringify
(
param
);
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
return
queueSql
(
db
,
[
"
INSERT OR REPLACE INTO
"
+
"
document(id, data) VALUES(?,?)
"
],
[[
id
,
data_string
]]);
})
.
push
(
function
()
{
return
id
;
});
};
WebSQLStorage
.
prototype
.
remove
=
function
(
id
)
{
var
db
=
this
.
_database
,
that
=
this
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
return
queueSql
(
db
,
[
"
DELETE FROM document WHERE id = ?
"
],
[[
id
]]);
})
.
push
(
function
(
result_list
)
{
if
(
result_list
[
0
].
rowsAffected
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
return
id
;
});
};
WebSQLStorage
.
prototype
.
get
=
function
(
id
)
{
var
db
=
this
.
_database
,
that
=
this
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
return
queueSql
(
db
,
[
"
SELECT data FROM document WHERE id = ?
"
],
[[
id
]]);
})
.
push
(
function
(
result_list
)
{
if
(
result_list
[
0
].
rows
.
length
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
return
JSON
.
parse
(
result_list
[
0
].
rows
[
0
].
data
);
});
};
WebSQLStorage
.
prototype
.
allAttachments
=
function
(
id
)
{
var
db
=
this
.
_database
,
that
=
this
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
return
queueSql
(
db
,
[
"
SELECT id FROM document WHERE id = ?
"
,
"
SELECT DISTINCT attachment FROM attachment WHERE id = ?
"
],
[[
id
],
[
id
]]);
})
.
push
(
function
(
result_list
)
{
if
(
result_list
[
0
].
rows
.
length
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
var
len
=
result_list
[
1
].
rows
.
length
,
obj
=
{},
i
;
for
(
i
=
0
;
i
<
len
;
i
+=
1
)
{
obj
[
result_list
[
1
].
rows
[
i
].
attachment
]
=
{};
}
return
obj
;
});
};
function
sendBlobPart
(
blob
,
argument_list
,
index
,
queue
)
{
queue
.
push
(
function
()
{
return
jIO
.
util
.
readBlobAsDataURL
(
blob
);
})
.
push
(
function
(
strBlob
)
{
argument_list
[
index
+
2
].
push
(
strBlob
.
target
.
result
);
return
;
});
}
WebSQLStorage
.
prototype
.
putAttachment
=
function
(
id
,
name
,
blob
)
{
var
db
=
this
.
_database
,
that
=
this
,
part_size
=
this
.
_blob_length
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
return
queueSql
(
db
,
[
"
SELECT id FROM document WHERE id = ?
"
],
[[
id
]]);
})
.
push
(
function
(
result
)
{
var
query_list
=
[],
argument_list
=
[],
blob_size
=
blob
.
size
,
queue
=
new
RSVP
.
Queue
(),
i
,
index
;
if
(
result
[
0
].
rows
.
length
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot access subdocument
"
,
404
);
}
query_list
.
push
(
"
DELETE FROM attachment WHERE id = ?
"
+
"
AND attachment = ?
"
);
argument_list
.
push
([
id
,
name
]);
query_list
.
push
(
"
INSERT INTO attachment(id, attachment, part, blob)
"
+
"
VALUES(?, ?, ?, ?)
"
);
argument_list
.
push
([
id
,
name
,
-
1
,
blob
.
type
||
"
application/octet-stream
"
]);
for
(
i
=
0
,
index
=
0
;
i
<
blob_size
;
i
+=
part_size
,
index
+=
1
)
{
query_list
.
push
(
"
INSERT INTO attachment(id, attachment, part, blob)
"
+
"
VALUES(?, ?, ?, ?)
"
);
argument_list
.
push
([
id
,
name
,
index
]);
sendBlobPart
(
blob
.
slice
(
i
,
i
+
part_size
),
argument_list
,
index
,
queue
);
}
queue
.
push
(
function
()
{
return
queueSql
(
db
,
query_list
,
argument_list
);
});
return
queue
;
});
};
WebSQLStorage
.
prototype
.
getAttachment
=
function
(
id
,
name
,
options
)
{
var
db
=
this
.
_database
,
that
=
this
,
part_size
=
this
.
_blob_length
,
start
,
end
,
start_index
,
end_index
;
if
(
options
===
undefined
)
{
options
=
{};
}
start
=
options
.
start
||
0
;
end
=
options
.
end
||
-
1
;
if
(
start
<
0
||
(
options
.
end
!==
undefined
&&
options
.
end
<
0
))
{
throw
new
jIO
.
util
.
jIOError
(
"
_start and _end must be positive
"
,
400
);
}
if
(
start
>
end
&&
end
!==
-
1
)
{
throw
new
jIO
.
util
.
jIOError
(
"
_start is greater than _end
"
,
400
);
}
start_index
=
Math
.
floor
(
start
/
part_size
);
if
(
start
===
0
)
{
start_index
-=
1
;
}
end_index
=
Math
.
floor
(
end
/
part_size
);
if
(
end
%
part_size
===
0
)
{
end_index
-=
1
;
}
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
var
command
=
"
SELECT part, blob FROM attachment WHERE id = ? AND
"
+
"
attachment = ? AND part >= ?
"
,
argument_list
=
[
id
,
name
,
start_index
];
if
(
end
!==
-
1
)
{
command
+=
"
AND part <= ?
"
;
argument_list
.
push
(
end_index
);
}
return
queueSql
(
db
,
[
command
],
[
argument_list
]);
})
.
push
(
function
(
response_list
)
{
var
i
,
response
,
blob_array
=
[],
blob
,
type
;
response
=
response_list
[
0
].
rows
;
if
(
response
.
length
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
for
(
i
=
0
;
i
<
response
.
length
;
i
+=
1
)
{
if
(
response
[
i
].
part
===
-
1
)
{
type
=
response
[
i
].
blob
;
start_index
+=
1
;
}
else
{
blob_array
.
push
(
jIO
.
util
.
dataURItoBlob
(
response
[
i
].
blob
));
}
}
if
((
start
===
0
)
&&
(
options
.
end
===
undefined
))
{
return
new
Blob
(
blob_array
,
{
type
:
type
});
}
blob
=
new
Blob
(
blob_array
,
{});
return
blob
.
slice
(
start
-
(
start_index
*
part_size
),
end
===
-
1
?
blob
.
size
:
end
-
(
start_index
*
part_size
),
"
application/octet-stream
"
);
});
};
WebSQLStorage
.
prototype
.
removeAttachment
=
function
(
id
,
name
)
{
var
db
=
this
.
_database
,
that
=
this
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
return
queueSql
(
db
,
[
"
DELETE FROM attachment WHERE
"
+
"
id = ? AND attachment = ?
"
],
[[
id
,
name
]]);
})
.
push
(
function
(
result
)
{
if
(
result
[
0
].
rowsAffected
===
0
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
,
404
);
}
return
name
;
});
};
WebSQLStorage
.
prototype
.
hasCapacity
=
function
(
name
)
{
return
(
name
===
"
list
"
||
(
name
===
"
include
"
));
};
WebSQLStorage
.
prototype
.
buildQuery
=
function
(
options
)
{
var
db
=
this
.
_database
,
that
=
this
,
query
=
"
SELECT id
"
;
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
that
.
_init_db_promise
;
})
.
push
(
function
()
{
if
(
options
===
undefined
)
{
options
=
{};
}
if
(
options
.
include_docs
===
true
)
{
query
+=
"
, data AS doc
"
;
}
query
+=
"
FROM document
"
;
return
queueSql
(
db
,
[
query
],
[[]]);
})
.
push
(
function
(
result
)
{
var
array
=
[],
len
=
result
[
0
].
rows
.
length
,
i
;
for
(
i
=
0
;
i
<
len
;
i
+=
1
)
{
array
.
push
(
result
[
0
].
rows
[
i
]);
array
[
i
].
value
=
{};
if
(
array
[
i
].
doc
!==
undefined
)
{
array
[
i
].
doc
=
JSON
.
parse
(
array
[
i
].
doc
);
}
}
return
array
;
});
};
jIO
.
addStorage
(
'
websql
'
,
WebSQLStorage
);
}(
jIO
,
RSVP
,
Blob
,
openDatabase
));
/*
* Copyright 2017, Nexedi SA
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment