-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextUtils.js
118 lines (106 loc) · 3.37 KB
/
extUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*global define*/
define( [
'jquery',
'underscore',
'ng!$http',
'ng!$q',
'qlik'
], function ( $,
_,
$http,
$q,
qlik ) {
'use strict';
/**
* Add as style link to the document's header
* @param {String} linkUrl Url to the CSS file
* @param {String} id If an id is passed, the function will check if this style link has already been added or not.
* If yes, it will not be added again.
*/
function addStyleLinkToHeader ( linkUrl, id ) {
if ( id && !_.isEmpty( id ) ) {
if ( !$( '#' + id ).length ) {
var $styleLink = $( document.createElement( 'link' ) );
$styleLink.attr( 'rel', 'stylesheet' );
$styleLink.attr( 'type', 'text/css' );
$styleLink.attr( 'href', linkUrl );
if ( id && !_.isEmpty( id ) ) {
$styleLink.attr( 'id', id );
}
$( 'head' ).append( $styleLink );
}
}
}
/**
* Add a style to the document's header.
* @param cssContent {String} CSS content to be added to the header
* @param id {String} If id is passed, addStyleToHeader will check if there has already been added a style with the given id, if yes, the css content will not be added to the header again
*/
function addStyleToHeader ( cssContent, id ) {
if ( id && typeof id === 'string' ) {
if ( !$( '#' + id ).length ) {
$( "<style>" )
.attr( 'id', id )
.html( cssContent ).appendTo( "head" );
}
} else {
$( "<style>" ).html( cssContent ).appendTo( "head" );
}
}
function getBasePath () {
var prefix = window.location.pathname.substr( 0, window.location.pathname.toLowerCase().lastIndexOf( "/sense" ) + 1 );
var url = window.location.href;
url = url.split( "/" );
return url[0] + "//" + url[2] + ( ( prefix[prefix.length - 1] === "/" ) ? prefix.substr( 0, prefix.length - 1 ) : prefix );
}
function getExtensionInfo ( extensionUniqueName ) {
var defer = $q.defer();
var url = getBasePath() + '/extensions/' + extensionUniqueName + '/' + extensionUniqueName + '.qext';
$http.get( url )
.then( function ( response ) {
defer.resolve( response.data );
} ).catch( function ( err ) {
defer.reject( err );
} );
return defer.promise;
}
function getExtensionPath ( extensionUniqueName ) {
return window.location.pathname.substr( 0, window.location.pathname.toLowerCase().lastIndexOf( "/sense" ) + 1 ) + 'extensions/' + extensionUniqueName;
}
function getProductVersion () {
var defer = $q.defer();
var global = qlik.getGlobal( {} );
global.getProductVersion( function ( reply ) {
var v = reply.qReturn;
var lastDot = xIndexOf( v, '.', 2 );
var rest = v.substr( lastDot + 1 );
var chars = rest.split();
var numDigitsAfterRest = 0;
for ( var i = 0; i < chars.length; i++ ) {
if ( !_.isNumber( chars[i] ) ) {
numDigitsAfterRest = i + 1;
break;
}
}
defer.resolve( v.substr( 0, lastDot + 1 + numDigitsAfterRest ) );
} );
return defer.promise;
}
if(typeof String.prototype.startsWith != 'function'){
String.prototype.startsWith = function(str){
if(str == null) return false;
var i = str.length;
if(this.length < i) return false;
for(--i; (i >= 0) && (this[i] === str[i]); --i) continue;
return i < 0;
}
}
return {
addStyleToHeader: addStyleToHeader,
addStyleLinkToHeader: addStyleLinkToHeader,
getExtensionInfo: getExtensionInfo,
getExtensionPath: getExtensionPath,
getProductVersion: getProductVersion,
getBasePath: getBasePath
}
} );