Skip to content

Commit

Permalink
Basic function done!
Browse files Browse the repository at this point in the history
1. Add depandence: underscore( Remove alot of function which include in
underscore).
2. Jumper function: add, go ,back, action,once, onceInAll,jump.
3. Add a easy example page.
  • Loading branch information
yaoazhen committed Jul 14, 2012
1 parent 9b411c5 commit 48ffa7e
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 52 deletions.
43 changes: 43 additions & 0 deletions example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Example</title>
<script type="text/javascript" src="underscore.js"></script>
<script type="text/javascript" src="jumper.js"></script>
</head>
<body>
<h1>Results in the console</h1>
<script type="text/javascript">
var processExemple = new Jumper(
function(a,b,c){
// If you want to using the once function, you have to give a name for the function
// like once and onceInAll

this.onceInAll('t11', function(){
console.log('ALL 1-1'); // Just run once even you back to this step
});
console.log(1);
this.once('t12', function(){
console.log('Step 1-2' +a +b+c); // Run once every time go to this step
});
}
,function(e,f,g){
console.log(2);
this.once('t22',function(){
console.log('Step 2-1'+e+f+g);
});
}
,function(){
console.log(3);
}
,function(){
console.log(4);
}
,function(){
console.log(5);
}).go(1,2,3).go(4,5,6).go().go().go().back().back().back(4,5,6).action().back(1,2,3).action();
// Here you can pass the args with go(),back(),action() for functions
</script>
</body>
</html>
208 changes: 156 additions & 52 deletions jumper.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,182 @@
// ## Multiple environment support
;(function(name, definition){
var Jumper = definition(this),
;(function(name, factory){

hasDefine = typeof define === 'function' && define.amd,
hasExports = typeof moudule !== 'undefined' && moudule.exports;

if(hasDefine){/*AMD Module*/
define(Jumper);
define(['underscore'], factory);
}
else if(hasExports){/*Node.js Module*/
moudule.exports = Jumper;
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
moudule.exports = factory(require('underscore'));
}
else{
/*Assign to common namespaces or simply the global object (window)*/
this.Jumper = Jumper;
(this._ && this)[name] = factory(this._);
}
})('Jumper', function(global, undef){
})('Jumper', function(_, undef){
"use strict";

var ver = '0.0.1'
, slice = Array.prototype.slice
, nativeBind = Function.prototype.bind
, noop
, bind
, isFunction;

, slice = Array.prototype.slice;

if(global.jQuery){
noop = global.jQuery.noop;
}
else{
noop = function(){};
}
var _createTask = function(){
return {
disable:false
, went:false
};
};

if(global._){
bind = _.bind;
isFunction = _.isFunction;
}
else if(global.jQuery){
bind = jQuery.proxy;
isFunction = global.jQuery.isFunction;
}
else{
isFunction = function(obj) {
return toString.call(obj) == '[object Function]';
var _createProcess = function(func){
return {
func : func
, running: false
, tasks : undef
};
bind = function(func, context){
var bound, args;
};

if(func.bind === nativeBind && nativeBind){
return nativeBind.apply(func, slice.call(arguments, 1))
}
var _reviveProcess =function(process){
_.each(process.tasks, function(task){
task.went = false;
});
};

var Jumper = function() {
this.index = -1;
this.steps=[];

this.add.apply(this, arguments);
this.go = _.bind(this.go, this);
this.back = _.bind(this.back, this);
this.action = _.bind(this.action, this);
this.jump = _.bind(this.jump, this);
this.onceInAll = _.bind(this.onceInAll, this);
this.once = _.bind(this.once, this);

};

var _createTaskInProcess = function(process, taskName, func){
if(process && !process.tasks){
process.tasks=[];
}

// var task = _createTask();
// task.func = func;
// process.tasks.push(task);

var task = _.find(process.tasks, function(item){
return item.taskName === taskName;
});

if(!isFunction(func)){
throw new TypeError;
if(!task){
task = _createTask();
task.taskName = taskName;
task.func = func;
process.tasks.push(task);
}
return task;
};

var _onceAction = function(taskName, func){
// TODO: What about arguments is a Jumper instance
if(_.isFunction(func)){
var process = this.current();
if(!process){
new Error('Once only using in a process');
}

var task = _createTaskInProcess.call(this, process, taskName, func);

args = slice.call(arguments, 2);
return bound = function() {
if(this instanceof bound) {
noop.prototype = func.prototype;
var self = new noop();
var result = func.apply(self, args.concat(slice.call(arguments)));

if (Object(result) === result){
return result;
}
return self;
}
return func.apply(context, args.concat(slice.call(arguments)));
if(!(task.disable || task.went)){
func();
}
};
}
return task;
}
else{
throw new TypeError;
}
};

var p = Jumper.prototype;

p.current = function(){
return this.steps[this.index];
};

// Just run once even you back to this step
p.onceInAll = function(taskName, func) {
var task = _onceAction.apply(this, arguments);
task.disable = true;
return this;
};

// Run once every time go to this step
// it will not run when using action
p.once = function(taskName, func){

var Jumper = function(){
var task = _onceAction.apply(this, arguments);
task.went = true;
return this;
};

// Run this step and keep current process
p.action =function(){
var process = this.current();
if(process){
process.func.apply(this, arguments);
}
return this;
};
// Goto next process
p.go = function() {
if(this.index < this.steps.length-1){
this.index++;
var process = this.current();
_reviveProcess(process);
this.action.apply(this, arguments);
}

return this;
};
// Back to last process
p.back = function() {
if(this.index > 0){
this.index--;
var process = this.current();
_reviveProcess(process);
this.action.apply(this, arguments);
}

return this;
};
// Jump to special process with index
p.jump = function(index){
if(index >-1 && index < this.steps.length){
this.index = index;
this.action.apply(this, slice.call(arguments, 1));
}
return this;
};
// Add process
p.add = function(func) {
if(arguments.length >1){
_.each(arguments, function(func){
if(_.isFunction(func)){
this.add(func);
}
else{
throw new TypeError;
}
}, this);
}
else{
var process = _createProcess(func);
this.steps.push(process);
}
};

return Jumper;
});
32 changes: 32 additions & 0 deletions underscore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 48ffa7e

Please sign in to comment.