├── README.md └── qml ├── XHR.js ├── Log.js └── URLQuery.js /README.md: -------------------------------------------------------------------------------- 1 | qml-utils 2 | ========= 3 | 4 | Useful utilities for QML -------------------------------------------------------------------------------- /qml/XHR.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nikita Krupenko 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 5 | * associated documentation files (the "Software"), to deal in the Software without restriction, 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all copies or 11 | * substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 14 | * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 18 | */ 19 | 20 | function sendXHR(method, url, callback, data, contentType) { 21 | if (!method || !url) { 22 | return null 23 | } 24 | 25 | var request = new XMLHttpRequest() 26 | var requestUrl = (method === 'GET' ? '%1%2'.arg(url).arg(data ? "?%1".arg(data) : "") : url) 27 | request.open(method, requestUrl) 28 | request.onreadystatechange = function() { 29 | if (request.readyState === XMLHttpRequest.DONE) { 30 | if (callback) { 31 | callback(request) 32 | } 33 | } 34 | } 35 | if (method !== 'GET') { 36 | request.setRequestHeader('Content-Type', contentType ? contentType 37 | : 'application/x-www-form-urlencoded') 38 | request.setRequestHeader('Content-Length', data ? data.length : 0) 39 | request.send(data) 40 | } else { 41 | request.send() 42 | } 43 | 44 | return request 45 | } 46 | -------------------------------------------------------------------------------- /qml/Log.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nikita Krupenko 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 5 | * associated documentation files (the "Software"), to deal in the Software without restriction, 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all copies or 11 | * substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 14 | * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 18 | */ 19 | 20 | .pragma library 21 | 22 | function serialize(object, maxDepth) { 23 | function _processObject(object, maxDepth, level) { 24 | var output = Array() 25 | var pad = " " 26 | if (maxDepth == undefined) { 27 | maxDepth = -1 28 | } 29 | if (level == undefined) { 30 | level = 0 31 | } 32 | var padding = Array(level + 1).join(pad) 33 | 34 | output.push((Array.isArray(object) ? "[" : "{")) 35 | var fields = Array() 36 | for (var key in object) { 37 | var keyText = Array.isArray(object) ? "" : ("\"" + key + "\": ") 38 | if (typeof (object[key]) == "object" && key != "parent" && maxDepth != 0) { 39 | var res = _processObject(object[key], maxDepth > 0 ? maxDepth - 1 : -1, level + 1) 40 | fields.push(padding + pad + keyText + res) 41 | } else { 42 | fields.push(padding + pad + keyText + "\"" + object[key] + "\"") 43 | } 44 | } 45 | output.push(fields.join(",\n")) 46 | output.push(padding + (Array.isArray(object) ? "]" : "}")) 47 | 48 | return output.join("\n") 49 | } 50 | 51 | return _processObject(object, maxDepth) 52 | } 53 | -------------------------------------------------------------------------------- /qml/URLQuery.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nikita Krupenko 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 5 | * associated documentation files (the "Software"), to deal in the Software without restriction, 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all copies or 11 | * substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 14 | * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 18 | */ 19 | 20 | .pragma library 21 | 22 | function serializeParams(object, prefix) { 23 | if (typeof object !== 'object') { 24 | return '' 25 | } 26 | 27 | if (!prefix) { 28 | prefix = '' 29 | } 30 | 31 | var output = [] 32 | var keysArray = (Array.isArray(object) ? object : Object.keys(object)) 33 | for (var i = 0; i < keysArray.length; i++) { 34 | var key = (Array.isArray(object) ? i : keysArray[i]) 35 | if (typeof object[key] === 'object') { 36 | output.push(serializeParams(object[key], (!prefix ? key : '%1[%2]'.arg(prefix).arg(key)))) 37 | } else { 38 | output.push('%1=%2'.arg(encodeURIComponent(!prefix ? key : '%1[%2]'.arg(prefix).arg(Array.isArray(object) ? '' : key))) 39 | .arg(encodeURIComponent(object[key]))) 40 | } 41 | } 42 | 43 | return output.join('&').replace('%20', '+') 44 | } 45 | 46 | function parseParams(url) { 47 | if (typeof url !== 'string') { 48 | return null 49 | } 50 | 51 | var result = {} 52 | var queries = url.replace(/[?#]/g, '&').replace(/^[^&]*?&/, '').replace(/[+]/g, '%20').split(/[&;]/) 53 | for (var i = 0; i < queries.length; i++) { 54 | var params = queries[i].split('=') 55 | result[decodeURIComponent(params[0])] = decodeURIComponent(params[1]) 56 | } 57 | 58 | return result 59 | } 60 | --------------------------------------------------------------------------------