Skip to content

Commit

Permalink
plugin filter in development
Browse files Browse the repository at this point in the history
  • Loading branch information
Bosn committed Jan 15, 2014
1 parent 42b5114 commit 13230e9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
9 changes: 8 additions & 1 deletion WebContent/plugins/index.htm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>RAP Example</title>
<script src="http://g.tbcdn.cn/kissy/k/1.4.1/seed.js"></script>
<!-- 引入RAP开始 -->
<script type="text/javascript" src="rap.plugin.js?projectId=76"></script>
<script type="text/javascript" src="rap.plugin.js?projectId=76&mode=1"></script>
<script src="http://mockjs.com/dist/mock-min.js"></script>
<!-- 引入RAP结束 -->

Expand All @@ -14,6 +14,13 @@
<pre id="container">
</pre>
<script>

RAP.setBlackList([
'test',
/test/g
]);


KISSY.use('node, ajax',function(S, Node, IO){
KISSY.io({
url:'/testxxx',
Expand Down
59 changes: 55 additions & 4 deletions WebContent/plugins/rap.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,37 @@
*******************************************/
(function() {
var node = document.getElementById('rap');
var blackList = [];
var whiteList = [];
var mode = 0; // 0-disabled, 1-blackList, 2-whiteList, 3-combo
var modeList = [0, 1, 2];
var projectId = 0;

// console handler
if (typeof window.console === 'undefined') {
window.console = {
log : function(){},
warn : function(){}
};
}

if (!node) {
var nodes = document.getElementsByTagName('script');
node = nodes[nodes.length - 1];
}
var projectId = 0;
var ms = node.src.match(/(?:\?|&)projectId=([^&]+)(?:&|$)/);
if (ms) {
projectId = ms[1];
}
var modePattern = node.src.match(/(?:\?|&)mode=([^&]+)(?:&|$)/);
if (modePattern) {
mode = +modePattern[1];
if (!(mode in modeList)) {
mode = 0;
}
}
var enable = true;

console.log('Current RAP work mode:', mode, "(0-disabled, 1-black list, 2-white list, 3-combo)");
var ens = node.src.match(/(?:\?|&)enable=([^&]+)(?:&|$)/);
if (ens) {
enable = ens[1] == 'true';
Expand Down Expand Up @@ -82,8 +102,8 @@
url = "http://rap.alibaba-inc.com/mockjs/" + projectId + url;
oOptions.url = url;
var oldSuccess = oOptions.success;
oOptions.success = function() {
arguments[0] = Mock.mock(arguments[0]);
oOptions.success = function(data) {
data = Mock.mock(data);
oldSuccess.apply(this, arguments);
};
}
Expand All @@ -92,4 +112,35 @@
});
}
}

window.RAP = {
setBlackList : function(arr) {
if (arr && arr instanceof Array) {
blackList = arr;
}
},
setWhiteList : function(arr) {
if (arr && arr instanceof Array) {
whiteList = arr;
}
},
getBlackList : function() {
return blackList;
},
getWhiteList : function() {
return whiteList;
},
getMode : function() {
return mode;
},
setMode : function(m) {
m = +m;
if (m in modeList) {
mode = m;
console.log('RAP work mode switched to ', m);
} else {
console.warn('Illegal mode id. Please check.');
}
}
};
})();

0 comments on commit 13230e9

Please sign in to comment.