-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshowSource.js
90 lines (89 loc) · 3.57 KB
/
showSource.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
'use strict';
app.directive('showSource', ['Languages', '$rootScope', 'FioiEditor2Tabsets', function(Languages, $rootScope, tabsets) {
// TODO: move this function a service, all this is more or less temporary anyway
function getSource(groupName, sLangProg, sLocale) {
var sourceCodes = ModelsManager.curData.tm_source_codes;
var res = null;
_.forEach(sourceCodes, function(sourceCode) {
if (sourceCode.sName == groupName && (sourceCode.sType == 'Task' || sourceCode.sType == 'Solution')) {
if (sourceCode.params && sourceCode.params.sLangProg == sLangProg) {
res = sourceCode.sSource;
return false;
}
}
});
return res;
}
function getAceOptions(sLangProg) {
var aceOptions = {
showGutter: false,
rendererOptions: {
maxLines: 'Infinity',
printMarginColumn: false,
},
advanced: {
highlightActiveLine: false,
readOnly: true,
}
};
var languages = Languages.sourceLanguages;
_.forOwn(languages, function(language) {
if (language.id == sLangProg) {
_.merge(aceOptions, language.ace);
return false;
}
});
return aceOptions;
}
return {
restrict: 'EA',
scope: true,
template: function(elem, attrs) {
if (attrs.lang == 'all' && attrs.sourceId) {
var groupName = attrs.sourceId;
var source_codes = ModelsManager.getRecords('tm_source_codes');
var tabsetConfig = {
languages: Languages.sourceLanguages,
defaultLanguage: 'cpp',
titlePrefix: ''
};
tabsets.add().update({name: groupName}).update(tabsetConfig);
var groupTabset = tabsets.find(groupName);
// sorted non-submission source codes
var editorCodeTabs = _.sortBy(_.filter(source_codes, {bSubmission: false, sName: groupName}), 'iRank');
console.error(editorCodeTabs);
var activeTabRank = null;
var i = 0;
_.forEach(editorCodeTabs, function(sourceCode) {
if (sourceCode.sName == groupName && (sourceCode.sType == 'Task' || sourceCode.sType == 'Solution')) {
var code = groupTabset.addTab().update({title: sourceCode.params.sLangProg});
code.getBuffer().update({text: sourceCode.sSource, language: sourceCode.params.sLangProg});
if (sourceCode.params.sLangProg == Languages.currentLanguage) {
activeTabRank = i;
}
i = i+1;
}
});
if (activeTabRank !== null) {
var tab = groupTabset.getTabs()[activeTabRank-1];
if (tab) {
groupTabset.update({activeTabId: tab.id});
}
}
return '<div fioi-editor2="{tabset:\''+groupName+'\'}"></div>';
} else {
var aceOptions = getAceOptions(Languages.currentLanguage);
// yeark...
var aceOptionsString = JSON.stringify(aceOptions).replace(/"/g, "'");
return '<div ui-ace="'+aceOptionsString+'" ng-model="sSource" class="readOnlySource" readonly></div>';
}
},
link: function(scope, element, attrs) {
if (attrs.code) {
scope.sSource = attrs.code;
} else if (attrs.sourceId) {
scope.sSource = getSource(attrs.sourceId, Languages.currentLanguage, $rootScope.sLocaleLang);
}
}
};
}]);