-
Notifications
You must be signed in to change notification settings - Fork 0
/
oj.JSFiddle.js
129 lines (101 loc) · 3.44 KB
/
oj.JSFiddle.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
119
120
121
122
123
124
125
126
127
// oj.JSFiddle.js
;(function(root, factory){
// Export to Node, Require.JS, or globally
if (typeof module === 'object' && module.exports) module.exports = factory(root)
else if (typeof define === 'function' && define.amd) define(function(){return factory(root)})
else factory(root, root.oj)
}(this, function(root, oj){
// Create plugin
var plugin = function(oj, settings){
if (typeof settings !== 'object')
settings = {}
var JSFiddle = oj.createType('JSFiddle', {
base: oj.View,
constructor: function(){
var this_ = this;
var union = oj.unionArguments(arguments);
var options = union.options;
var args = union.args;
// Shift properties
var props = [
'url',
'tabs',
'style',
'width',
'height'
];
for (var i = 0; i < props.length; i++) {
var prop = props[i];
if (options[prop] != null)
this[prop] = oj.argumentShift(options, prop);
}
// Accept url as first arg
if (!this._url && args.length >= 1)
this.url = args[0];
// Create el
this.el = oj(function(){
if (!this_.url)
throw new Error('oj.JSFiddle: url is not specified');
var url = this_.url;
var tabs = this_.tabs ? this_.tabs + '/' : '';
var style = this_.style ? this_.style + '/' : '';
// Calculate src to iframe
var src = "http://jsfiddle.net/" + url + "/embedded/" + tabs + style;
oj.iframe({
src:src,
width:this_.width,
height:this_.height
});
});
JSFiddle.base.constructor.apply(this, [options]);
},
properties: {
url: {
get:function(){
return this._url || 'evanmoran/vhNcD';
},
set:function(v){
// Strip off unecesary parts of url
v = unprepend(v, 'http://');
v = unprepend(v, 'jsfiddle.net')
v = unappend(v, '/')
this._url = v;
}
},
tabs: 'result,js,html,css,resources',
style: '', // '' or 'presentation'
width:{ // pixals if specified. Otherwise is calculate from settings
get:function(){return this._width || 300},
set:function(v){this._width = v;}
},
height:{ // pixals if specified. Otherwise is calculate from settings
get:function(){return this._height || 200},
set:function(v){this._height = v;}
}
},
});
return {JSFiddle:JSFiddle};
};
// Helper methods
// ---------------------------------------------------------------------------
function startsWith (strInput, strStart) {
return strInput.length >= strStart.length && strInput.lastIndexOf(strStart, 0) == 0;
}
function endsWith (strInput, strEnd) {
return strInput.length >= strEnd.length && strInput.lastIndexOf(strEnd, strInput.length - strEnd.length) == strInput.length - strEnd.length;
}
function unprepend (strInput, strStart) {
if(startsWith(strInput, strStart))
return strInput.slice(strStart.length);
return strInput;
}
function unappend (strInput, strEnd) {
if (endsWith(strInput, strEnd))
return strInput.slice(0, strInput.length - strEnd.length);
return strInput;
}
// Export to OJ
if (typeof oj != 'undefined')
oj.use(plugin);
return plugin;
}));