Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
atmin committed Nov 11, 2014
1 parent 777c0e9 commit 1dceb37
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 162 deletions.
22 changes: 9 additions & 13 deletions karma.conf-ci.js → ci.karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 40,13 @@ module.exports = function(config) {
},
'SL_Chrome': {
base: 'SauceLabs',
browserName: 'chrome'
browserName: 'chrome',
platform: 'Linux'
},
'SL_Firefox': {
base: 'SauceLabs',
browserName: 'firefox'
browserName: 'firefox',
platform: 'Linux'
},
'SL_IE9': {
base: 'SauceLabs',
Expand Down Expand Up @@ -103,13 105,7 @@ module.exports = function(config) {
browserName: 'iPad',
platform: 'OSX 10.9',
version: '8.1'
}/*,
'SL_CHROME_Opera12': {
base: 'SauceLabs',
browserName: 'opera',
platform: 'Windows 7',
version: '12'
}*/
}
};

var launchersBatch = [].reduce.call(
Expand Down Expand Up @@ -142,13 138,13 @@ module.exports = function(config) {
files: [
{ pattern: 'src/**/*', included: false },
{ pattern: 'spec/*', included: false },
'src/main.js',
'spec/main.js'
'src/index.js',
'spec/index.js'
],

preprocessors: {
'src/main.js': ['browserify'],
'spec/main.js': ['browserify']
'src/index.js': ['browserify'],
'spec/index.js': ['browserify']
},


Expand Down
280 changes: 140 additions & 140 deletions jtmpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1144,143 1144,6 @@ function matchEndBlock(block, str) {
module.exports = compile;

},{"./compile-rules-attr":2,"./compile-rules-node":3}],5:[function(_dereq_,module,exports){
/*
Evaluate object from literal or CommonJS module
*/

/* jshint evil:true */
module.exports = function(target, src, model) {

model = model || {};
if (typeof model !== 'function') {
model = jtmpl(model);
}

function mixin(target, properties) {
for (var prop in properties) {
if (// Plugin
(prop.indexOf('__') === 0 &&
prop.lastIndexOf('__') === prop.length - 2) ||
// Computed property
typeof properties[prop] === 'function'
) {
if (target.values[prop] === undefined) {
target.values[prop] = properties[prop];
}
}
else {
// Target doesn't already have prop?
if (target(prop) === undefined) {
target(prop, properties[prop]);
}
}
}
}

function applyPlugins() {
var prop, arg;
for (prop in jtmpl.plugins) {
plugin = jtmpl.plugins[prop];
arg = model.values['__' prop '__'];
if (typeof plugin === 'function' && arg !== undefined) {
plugin.call(model, arg, target);
}
}
}

function evalObject(body, src) {
var result, module = { exports: {} };
src = src ?
'\n//@ sourceURL=' src
'\n//# sourceURL=' src :
'';
if (body.match(/^\s*{[\S\s]*}\s*$/)) {
// Literal
return eval('result=' body src);
}
// CommonJS module
eval(body src);
return module.exports;
}

function loadModel(src, template, doc) {
var hashIndex;
if (!src) {
// No source
jtmpl(target, template, model);
}
else if (src.match(jtmpl.RE_NODE_ID)) {
// Element in this document
var element = doc.querySelector(src);
mixin(model, evalObject(element.innerHTML, src));
applyPlugins();
jtmpl(target, template, model);
}
else {
hashIndex = src.indexOf('#');
// Get model via XHR
// Older IEs complain if URL contains hash
jtmpl('GET', hashIndex > -1 ? src.substring(0, hashIndex) : src,
function (resp) {
var match = src.match(jtmpl.RE_ENDS_WITH_NODE_ID);
var element = match && new DOMParser()
.parseFromString(resp, 'text/html')
.querySelector(match[1]);
mixin(model, evalObject(match ? element.innerHTML : resp, src));
applyPlugins();
jtmpl(target, template, model);
}
);
}
}

function loadTemplate() {
var hashIndex;

if (!src) return;

if (src.match(jtmpl.RE_NODE_ID)) {
// Template is the contents of element
// belonging to this document
var element = document.querySelector(src);
loadModel(element.getAttribute('data-model'), element.innerHTML, document);
}
else {
hashIndex = src.indexOf('#');
// Get template via XHR
jtmpl('GET', hashIndex > -1 ? src.substring(0, hashIndex) : src,
function(resp) {
var match = src.match(jtmpl.RE_ENDS_WITH_NODE_ID);
var iframe, doc;
if (match) {
iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
doc = iframe.contentDocument;
doc.writeln(resp);
document.body.removeChild(iframe);
}
else {
doc = document;
}
var element = match && doc.querySelector(match[1]);

loadModel(
match ? element.getAttribute('data-model') : '',
match ? element.innerHTML : resp,
doc
);
}
);
}
}

loadTemplate();
};

},{}],6:[function(_dereq_,module,exports){
/*
* Main function
*/
Expand Down Expand Up @@ -1427,7 1290,144 @@ jtmpl.plugins = _dereq_('./plugins');
module.exports = jtmpl;
if (typeof window !== 'undefined') window.jtmpl = jtmpl;

},{"./compile":4,"./loader":5,"./parse":7,"./plugins":8,"./polyfills/matches":12,"./utemplate":13,"./xhr":14,"freak":1}],7:[function(_dereq_,module,exports){
},{"./compile":4,"./loader":6,"./parse":7,"./plugins":8,"./polyfills/matches":12,"./utemplate":13,"./xhr":14,"freak":1}],6:[function(_dereq_,module,exports){
/*
Evaluate object from literal or CommonJS module
*/

/* jshint evil:true */
module.exports = function(target, src, model) {

model = model || {};
if (typeof model !== 'function') {
model = jtmpl(model);
}

function mixin(target, properties) {
for (var prop in properties) {
if (// Plugin
(prop.indexOf('__') === 0 &&
prop.lastIndexOf('__') === prop.length - 2) ||
// Computed property
typeof properties[prop] === 'function'
) {
if (target.values[prop] === undefined) {
target.values[prop] = properties[prop];
}
}
else {
// Target doesn't already have prop?
if (target(prop) === undefined) {
target(prop, properties[prop]);
}
}
}
}

function applyPlugins() {
var prop, arg;
for (prop in jtmpl.plugins) {
plugin = jtmpl.plugins[prop];
arg = model.values['__' prop '__'];
if (typeof plugin === 'function' && arg !== undefined) {
plugin.call(model, arg, target);
}
}
}

function evalObject(body, src) {
var result, module = { exports: {} };
src = src ?
'\n//@ sourceURL=' src
'\n//# sourceURL=' src :
'';
if (body.match(/^\s*{[\S\s]*}\s*$/)) {
// Literal
return eval('result=' body src);
}
// CommonJS module
eval(body src);
return module.exports;
}

function loadModel(src, template, doc) {
var hashIndex;
if (!src) {
// No source
jtmpl(target, template, model);
}
else if (src.match(jtmpl.RE_NODE_ID)) {
// Element in this document
var element = doc.querySelector(src);
mixin(model, evalObject(element.innerHTML, src));
applyPlugins();
jtmpl(target, template, model);
}
else {
hashIndex = src.indexOf('#');
// Get model via XHR
// Older IEs complain if URL contains hash
jtmpl('GET', hashIndex > -1 ? src.substring(0, hashIndex) : src,
function (resp) {
var match = src.match(jtmpl.RE_ENDS_WITH_NODE_ID);
var element = match && new DOMParser()
.parseFromString(resp, 'text/html')
.querySelector(match[1]);
mixin(model, evalObject(match ? element.innerHTML : resp, src));
applyPlugins();
jtmpl(target, template, model);
}
);
}
}

function loadTemplate() {
var hashIndex;

if (!src) return;

if (src.match(jtmpl.RE_NODE_ID)) {
// Template is the contents of element
// belonging to this document
var element = document.querySelector(src);
loadModel(element.getAttribute('data-model'), element.innerHTML, document);
}
else {
hashIndex = src.indexOf('#');
// Get template via XHR
jtmpl('GET', hashIndex > -1 ? src.substring(0, hashIndex) : src,
function(resp) {
var match = src.match(jtmpl.RE_ENDS_WITH_NODE_ID);
var iframe, doc;
if (match) {
iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
doc = iframe.contentDocument;
doc.writeln(resp);
document.body.removeChild(iframe);
}
else {
doc = document;
}
var element = match && doc.querySelector(match[1]);

loadModel(
match ? element.getAttribute('data-model') : '',
match ? element.innerHTML : resp,
doc
);
}
);
}
}

loadTemplate();
};

},{}],7:[function(_dereq_,module,exports){
/**
* Parse a text template to DOM structure ready for compiling
* @see compile
Expand Down Expand Up @@ -1738,6 1738,6 @@ Requests API

};

},{}]},{},[6])
(6)
},{}]},{},[5])
(5)
});
2 changes: 1 addition & 1 deletion jtmpl.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions local.karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 15,13 @@ module.exports = function(config) {
files: [
{ pattern: 'src/**/*', included: false },
{ pattern: 'spec/*', included: false },
'src/main.js',
'spec/main.js'
'src/index.js',
'spec/index.js'
],

preprocessors: {
'src/main.js': ['browserify'],
'spec/main.js': ['browserify']
'src/index.js': ['browserify'],
'spec/index.js': ['browserify']
},

//browserify: {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 36,12 @@
},
"scripts": {
"jshint": "jshint src/*.js",
"browserify": "browserify --standalone jtmpl src/main.js > jtmpl.js",
"browserify": "browserify --standalone jtmpl src > jtmpl.js",
"uglify": "uglifyjs jtmpl.js -c -m -o jtmpl.min.js",
"start": "npm run dev",
"dev": "karma start local.karma.conf.js",
"build": "npm run jshint && npm run browserify && npm run uglify",
"check": "karma start --single-run=true --browsers=Chrome,Firefox,PhantomJS local.karma.conf.js",
"test": "for i in 1 2 3 4 5; do karma start karma.conf-ci.js --batch-$i; done"
"test": "for i in 1 2 3 4 5; do karma start ci.karma.conf.js --batch-$i; done"
}
}
4 changes: 2 additions & 2 deletions spec/main.js → spec/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 19,11 @@ describe('jtmpl test suite', function() {
setTimeout(function() {
proceed = true;
done();
}, 1000);
}, 1500);
if (proceed) done();
});

describe('jtmpl', require('./base'));
describe('jtmpl', require('./jtmpl'));
describe('plugins', require('./plugins'));
describe('render', require('./render'));
});
File renamed without changes.
File renamed without changes.

0 comments on commit 1dceb37

Please sign in to comment.