Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Генерация событий на DOM узалх нано-блоков #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
module.exports = function (grunt) {
'use strict';

grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks("grunt-jscs-checker");
grunt.loadNpmTasks('grunt-mocha-phantomjs');
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Есть такой модуль sindresorhus/load-grunt-tasks, будет require('load-grunt-tasks')(grunt);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Согласен, не знал.


var gruntConfig = {};

Expand All @@ -13,7 +11,8 @@ module.exports = function (grunt) {
jshintrc: '.jshintrc'
},
files: [
'blocks/*/*.js'
'blocks/**/**.js',
'src/**/**.js'
]
};

Expand All @@ -29,7 +28,26 @@ module.exports = function (grunt) {
all: ['unittests/index.html']
};

gruntConfig.shell = {
rebuildNanoislands: {
command: "make"
}
};

gruntConfig.watch = {
test: {
files: [
"<%= jshint.files %>",
"unittests/**/*.js"
],
tasks: [
"shell:rebuildNanoislands",
"mocha_phantomjs"
]
}
};

grunt.initConfig(gruntConfig);

grunt.registerTask('default', ['jshint', 'jscs', 'mocha_phantomjs']);
};
};
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ unittests/tests.yate.js: $(shellshell find $(CURDIR)/unittests -name '*.yate') n
nanoislands.ie.css: $(shell find . -name '*.styl') node_modules
node build/build-styl.js ie > $@

nanoislands.js: $(CURDIR)/blocks/nanoislands.js $(shell find $(CURDIR)/blocks -name '*.js') node_modules
nanoislands.js: $(CURDIR)/blocks/nanoislands.js $(shell find $(CURDIR)/blocks -name '*.js') $(shell find $(CURDIR)/src -name '*.js') node_modules
$(NPM_BIN)/borschik --input=blocks/nanoislands.js --minimize=no --output=nanoislands.js

node_modules:
Expand Down
21 changes: 21 additions & 0 deletions blocks/dom-emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @desc Класс который генерирует события на
* корневом узле блока.
*/
(function() {
nb.define('DomEmitter', $.extend({
/**
* @desc Генерирует событие на блоке и
* корневом узле этого блока
* @param {String} eventName The name of event
* @param {Object} eventObject The object which will be pass to handlers
* of event
*/
trigger: function(eventName, eventObject) {
var $node = $(this.node);

$node.trigger.apply($node, arguments);
return this.emit.apply(this, arguments);
}
}, new nb.Emitter()));
}());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вот это наверное в blocks/common логичнее положить

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Изначально я хотел положить сюда,
но скорее всего вы просто скопипастили файл из наноблоков. Комментарий в начале файла
говорит о том что туда лучше не писать. А так как нет файлов из которых он собирается(тут), то пришлось идти на такие ухищрения. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно посмотреть сюда

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitkarpov Спасибо! Я туда смотрл, не хочется править код библиотеке nanoblocks.
Правлю тут потому, что nanoislands - зависит от jQuery.

3 changes: 3 additions & 0 deletions blocks/nanoislands.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* borschik:include:../node_modules/es5-shim/es5-shim.js */
/* borschik:include:../libs/nanoblocks.js */

/* borschik:include:../src/emitter.js */
/* borschik:include:dom-emitter.js */

/* borschik:include:button/button.js */
/* borschik:include:tooltip/tooltip.js */
/* borschik:include:checkbox/checkbox.js */
Expand Down
2 changes: 1 addition & 1 deletion blocks/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,4 @@ nb.define('select', {
this.trigger('nb-select_destroyed');
nb.destroy(this.node.getAttribute('id'));
}
});
}, 'DomEmitter');
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
"mocha-phantomjs": "*",
"phantomjs": "*",
"jscs": "1.0.11",
"expect.js": "*"
"expect.js": "*",
"grunt-contrib-watch": "~0.5.3",
"grunt-shell": "~0.6.1",
"matchdep": "~0.3.0",
"sinon": "~1.7.3"
},
"engines": {
"node": "*"
Expand Down
48 changes: 48 additions & 0 deletions src/emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @desc Класс реализованный на jQuery, который позволяет генерировать и прослушивать
* события на объекте
*/
(function() {
var Emitter = function() {};

/**
* @desc Add event handler
* @param {String} eventName The name of event
* @param {Function} eventHandler The handler of event on the object
* @return {Function} The handler of event
*/
Emitter.prototype.on = function(eventName, eventHandler) {
var $this = $(this);

$this.on.apply($this, arguments);

return eventHandler;
};

/**
* @desc Remove event handler from object
* @param {String} eventName The name of event
* @param {Function} eventHandler Then event handler function
* @return {Function} The event handler function
*/
Emitter.prototype.off = function(eventName, eventHandler) {
var $this = $(this);

$this.off.apply($this, arguments);

return eventHandler;
};

/**
* @desc Trigger event on the object
* @param {String} eventName The name of event
* @param {Object} [eventObject] The object which pass to handlers
*/
Emitter.prototype.emit = function(eventName, eventObject) {
var $this = $(this);

return $this.trigger.apply($this, arguments);
};

nb.Emitter = Emitter;
}());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вот это наверное в blocks/common логичнее положить

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Что-то мне подсказывает что это нехило ударит по производительности на страницах с большим количеством элементов.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А, что конкретно смущает?

17 changes: 12 additions & 5 deletions unittests/aftereach.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
afterEach(function() {
// clear blocks
nb.destroy();
$('.content').empty();
});
(function(globalSinon) {
beforeEach(function() {
sinon = globalSinon.sandbox.create();
});

afterEach(function() {
sinon.restore();
// clear blocks
nb.destroy();
$('.content').empty();
});
}(sinon));
3 changes: 3 additions & 0 deletions unittests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<script type="text/javascript" src="../node_modules/mocha-phantomjs/node_modules/mocha/mocha.js"></script>
<script type="text/javascript" src="../node_modules/expect.js/expect.js"></script>
<script type="text/javascript" src="../node_modules/sinon/pkg/sinon.js"></script>
<script src="../libs/jquery-ui/jquery-ui-1.10.3.nanoislands.min.js"></script>

<script src="../node_modules/yate/lib/runtime.js"></script>
Expand All @@ -40,6 +41,8 @@

<script src="spec/button.js"></script>
<script src="spec/checkbox.js"></script>
<script src="spec/emitter.js"></script>
<script src="spec/select.js"></script>

<script type="text/javascript">
if (window.mochaPhantomJS) {
Expand Down
87 changes: 87 additions & 0 deletions unittests/spec/emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*global beforeEach, describe, it, expect, sinon*/
describe("Emitter Tests", function() {
beforeEach(function() {
this.emitter = new nb.Emitter();
});

describe('#on', function() {
it('should implement interface #on', function() {
expect(this.emitter.on).to.be.ok();
});
});

describe('#off', function() {
it('should implement interface #off', function() {
expect(this.emitter.off).to.be.ok();
});

it('should remove handler of event from object', function() {
var eventHandler = this.emitter.on('test', sinon.stub());

this.emitter.off('test', eventHandler);
this.emitter.emit('test');

expect(eventHandler.called).to.be.equal(false);
});
});

describe('#emit', function() {
it('should implement interface #trigger', function() {
expect(this.emitter.emit).to.be.ok();
});

it('should trigger event on object', function() {
var eventHandler = this.emitter.on('test', sinon.stub());

this.emitter.emit('test');

expect(eventHandler.called).to.be.equal(true);
});
});

describe('How add emitter functionality to nanoblock', function() {
beforeEach(function() {
nb.define('test', {}, 'DomEmitter');
this.bTest = nb.block($('<div data-nb="test"/>').get(0));
});

it('should trigger event on nanoblock', function() {
var eventHandler = this.bTest.on('test', sinon.stub());

this.bTest.trigger('test');

expect(eventHandler.called).to.be.equal(true);
});

it('should trigger event on nanoblock node', function() {
var eventHandler = sinon.stub();
$(this.bTest.node).on('test', eventHandler);

this.bTest.trigger('test');

expect(eventHandler.called).to.be.equal(true);
});

it('should trigger event on nanoblock node without duplicate', function() {
var eventHandler = sinon.stub();
$(this.bTest.node).on('test', eventHandler);

this.bTest.trigger('test');

expect(eventHandler.calledOnce).to.be.equal(true);
});

it('should bubbling event triggered on nanoblock', function() {
var $rootNode = $('<div id="rootNode"><b data-nb="test" /></div>');
var eventHandler = sinon.stub();

this.bTest = nb.block($rootNode.find('[data-nb="test"]').get(0));

$rootNode.on('test', eventHandler);

this.bTest.trigger('test');

expect(eventHandler.called).to.be.equal(true);
});
});
});
14 changes: 14 additions & 0 deletions unittests/spec/select.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*global beforeEach, describe, it, expect, sinon*/
describe("Select Tests", function() {
it('should bubbling event nb-select_changed', function() {
var $rootNode = $('<div><div data-nb="select"/></div>');
var eventHandler = sinon.stub();

this.bSelect = nb.block($rootNode.find('[data-nb="select"]').get(0));
$rootNode.on('nb-select_changed', eventHandler);

this.bSelect.trigger('nb-select_changed');

expect(eventHandler.called).to.be.equal(true);
});
});