Browse Source

Update and add tests to the Dialog module

pull/36/head
Artur Arseniev 10 years ago
parent
commit
a17666ea5a
  1. 12
      src/commands/view/ExportTemplate.js
  2. 4
      src/dom_components/view/ComponentImageView.js
  3. 4
      src/editor/main.js
  4. 8
      src/modal_dialog/config/config.js
  5. 122
      src/modal_dialog/main.js
  6. 15
      src/modal_dialog/model/Modal.js
  7. 85
      src/modal_dialog/view/ModalView.js
  8. 4
      src/style_manager/view/PropertyFileView.js
  9. 1
      test/runner/main.js
  10. 69
      test/specs/modal/main.js
  11. 74
      test/specs/modal/view/ModalView.js

12
src/commands/view/ExportTemplate.js

@ -5,10 +5,10 @@ define(function() {
* */ * */
return { return {
run: function(editor, sender){ run: function(editor, sender) {
this.sender = sender; this.sender = sender;
this.components = editor.DomComponents.getComponents(); this.components = editor.DomComponents.getComponents();
this.modal = editor.Dialog || null; this.modal = editor.Modal || null;
this.cm = editor.CodeManager || null; this.cm = editor.CodeManager || null;
this.cssc = editor.CssComposer || null; this.cssc = editor.CssComposer || null;
this.protCss = editor.Config.protectedCss; this.protCss = editor.Config.protectedCss;
@ -25,8 +25,7 @@ define(function() {
* @return {Object} Editor * @return {Object} Editor
* @private * @private
* */ * */
buildEditor: function(codeName, theme, label) buildEditor: function(codeName, theme, label) {
{
if(!this.codeMirror) if(!this.codeMirror)
this.codeMirror = this.cm.getViewer('CodeMirror'); this.codeMirror = this.cm.getViewer('CodeMirror');
@ -49,8 +48,7 @@ define(function() {
return { el: editor, $el: $editor }; return { el: editor, $el: $editor };
}, },
enable: function() enable: function() {
{
if(!this.$editors){ if(!this.$editors){
var oHtmlEd = this.buildEditor('htmlmixed', 'hopscotch', 'HTML'), var oHtmlEd = this.buildEditor('htmlmixed', 'hopscotch', 'HTML'),
oCsslEd = this.buildEditor('css', 'hopscotch', 'CSS'); oCsslEd = this.buildEditor('css', 'hopscotch', 'CSS');
@ -63,7 +61,7 @@ define(function() {
if(this.modal){ if(this.modal){
this.modal.setTitle('Export template'); this.modal.setTitle('Export template');
this.modal.setContent(this.$editors); this.modal.setContent(this.$editors);
this.modal.show(); this.modal.open();
} }
var addCss = this.protCss || ''; var addCss = this.protCss || '';
this.htmlEditor.setContent( this.cm.getCode(this.components, 'html') ); this.htmlEditor.setContent( this.cm.getCode(this.components, 'html') );

4
src/dom_components/view/ComponentImageView.js

@ -46,9 +46,9 @@ define(['backbone', './ComponentView'],
this.modal.setTitle('Select image'); this.modal.setTitle('Select image');
this.modal.setContent(this.am.render(1)); this.modal.setContent(this.am.render(1));
this.am.setTarget(this.model); this.am.setTarget(this.model);
this.modal.show(); this.modal.open();
this.am.onSelect(function(){ this.am.onSelect(function(){
that.modal.hide(); that.modal.close();
that.am.setTarget(null); that.am.setTarget(null);
}); });
} }

4
src/editor/main.js

@ -124,9 +124,9 @@ define(function (require){
Commands: em.get('Commands'), Commands: em.get('Commands'),
/** /**
* @property {Dialog} * @property {Modal}
*/ */
Dialog: em.get('Modal'), Modal: em.get('Modal'),
/** /**
* @property {Panels} * @property {Panels}

8
src/modal_dialog/config/config.js

@ -1,13 +1,13 @@
define(function () { define(function () {
return { return {
stylePrefix : 'mdl-', stylePrefix: 'mdl-',
title : '', title: '',
content : '', content: '',
backdrop : true, backdrop: true,
}; };
}); });

122
src/modal_dialog/main.js

@ -1,6 +1,19 @@
/** /**
* @class Modal * * [open](#open)
* */ * * [close](#close)
* * [isOpen](#isopen)
* * [setTitle](#settitle)
* * [getTitle](#gettitle)
* * [setContent](#setcontent)
* * [getContent](#getcontent)
*
* Before using the methods you should get first the module from the editor instance, in this way:
*
* ```js
* var modal = editor.Modal;
* ```
* @module Modal
*/
define(function(require) { define(function(require) {
return function() { return function() {
var c = {}, var c = {},
@ -21,6 +34,7 @@ define(function(require) {
/** /**
* Initialize module. Automatically called with a new instance of the editor * Initialize module. Automatically called with a new instance of the editor
* @param {Object} config Configurations * @param {Object} config Configurations
* @private
*/ */
init: function(config) { init: function(config) {
c = config || {}; c = config || {};
@ -38,43 +52,107 @@ define(function(require) {
model: model, model: model,
config: c, config: c,
}); });
c.em.on('loaded', function(){
this.render().appendTo(c.em.config.el || 'body'); if(c.em)
}, this); c.em.on('loaded', function(){
this.render().appendTo(c.em.config.el || 'body');
}, this);
return this; return this;
}, },
getModel: function(){ /**
return model; * Open modal window
}, * @return {this}
*/
open: function(){
modal.show();
return this;
},
render: function(){ /**
return modal.render().$el; * Close modal window
}, * @return {this}
*/
close: function(){
modal.hide();
return this;
},
show: function(){ /**
return modal.show(); * Checks if modal window is open
}, * @return {Boolean}
*/
isOpen: function(){
return !!model.get('open');
},
hide: function(){ /**
return modal.hide(); * Set title to the modal window
* @param {string} title Title
* @return {this}
* @example
* modal.setTitle('New title');
*/
setTitle: function(title){
model.set('title', title);
return this;
}, },
setTitle: function(v){ /**
return modal.setTitle(v); * Returns title of the modal window
}, * @return {string}
*/
getTitle: function(){
return model.get('title');
},
setContent: function(v){ /**
return modal.setContent(v); * Set content of the modal window
* @param {string} content Content
* @return {this}
* @example
* modal.setContent('<div>Some HTML content</div>');
*/
setContent: function(content){
model.set('content', content);
return this;
}, },
/**
* Get content of the modal window
* @return {string}
*/
getContent: function(){
return model.get('content');
},
/** /**
* Returns content element * Returns content element
* @return {HTMLElement} * @return {HTMLElement}
* @private
*/ */
getContentEl: function(){ getContentEl: function(){
return modal.getContent(); return modal.getContent();
} },
/**
* Returns modal model
* @return {Model}
* @private
*/
getModel: function(){
return model;
},
/**
* Render the modal window
* @return {HTMLElement}
* @private
*/
render: function(){
return modal.render().$el;
}
}; };
}; };
}); });

15
src/modal_dialog/model/Modal.js

@ -1,15 +1,12 @@
define(['backbone'], define(['backbone'],
function(Backbone) { function(Backbone) {
/**
* @class Modal
* */
return Backbone.Model.extend({ return Backbone.Model.extend({
defaults: { defaults: {
title : '', title: '',
content : '', content: '',
open : false, open: false,
} }
}); });
}); });

85
src/modal_dialog/view/ModalView.js

@ -1,20 +1,17 @@
define(['backbone', 'text!./../template/modal.html'], define(['backbone', 'text!./../template/modal.html'],
function (Backbone, modalTemplate) { function (Backbone, modalTemplate) {
/**
* @class ModalView
* */
return Backbone.View.extend({ return Backbone.View.extend({
template: _.template(modalTemplate), template: _.template(modalTemplate),
events : {}, events: {},
initialize: function(o){ initialize: function(o){
this.config = o.config || {}; this.config = o.config || {};
this.pfx = this.config.stylePrefix; this.pfx = this.config.stylePrefix || '';
this.listenTo( this.model, 'change:open', this.updateOpen); this.listenTo(this.model, 'change:open', this.updateOpen);
this.listenTo( this.model, 'change:title', this.updateTitle); this.listenTo(this.model, 'change:title', this.updateTitle);
this.listenTo( this.model, 'change:content',this.updateContent); this.listenTo(this.model, 'change:content', this.updateContent);
this.events['click .'+this.pfx+'btn-close'] = 'hide'; this.events['click .'+this.pfx+'btn-close'] = 'hide';
if(this.config.backdrop) if(this.config.backdrop)
@ -26,39 +23,48 @@ define(['backbone', 'text!./../template/modal.html'],
/** /**
* Returns content element * Returns content element
* @return {HTMLElement} * @return {HTMLElement}
* @private
*/ */
getContent:function(){ getContent: function(){
if(!this.$content) if(!this.$content)
this.$content = this.$el.find('.'+this.pfx+'content #'+this.pfx+'c'); this.$content = this.$el.find('.'+this.pfx+'content #'+this.pfx+'c');
return this.$content.get(0); return this.$content.get(0);
}, },
/**
* Returns title element
* @return {HTMLElement}
* @private
*/
getTitle: function(){
if(!this.$title)
this.$title = this.$el.find('.'+this.pfx+'title');
return this.$title.get(0);
},
/** /**
* Update content * Update content
* * @private
* @return void
* */ * */
updateContent: function(){ updateContent: function(){
if(!this.$content) var content = this.getContent();
this.$content = this.$el.find('.'+this.pfx+'content #'+this.pfx+'c'); if(content)
this.$content.html(this.model.get('content')); content.innerHTML = this.model.get('content');
}, },
/** /**
* Update title * Update title
* * @private
* @return void
* */ * */
updateTitle: function(){ updateTitle: function(){
if(!this.$title) var title = this.getTitle();
this.$title = this.$el.find('.'+this.pfx+'title'); if(title)
this.$title.html(this.model.get('title')); title.innerHTML = this.model.get('title');
}, },
/** /**
* Update open * Update open
* * @private
* @return void
* */ * */
updateOpen: function(){ updateOpen: function(){
if(this.model.get('open')) if(this.model.get('open'))
@ -69,8 +75,7 @@ define(['backbone', 'text!./../template/modal.html'],
/** /**
* Hide modal * Hide modal
* * @private
* @return void
* */ * */
hide: function(){ hide: function(){
this.model.set('open', 0); this.model.set('open', 0);
@ -78,42 +83,18 @@ define(['backbone', 'text!./../template/modal.html'],
/** /**
* Show modal * Show modal
* * @private
* @return void
* */ * */
show: function(){ show: function(){
this.model.set('open', 1); this.model.set('open', 1);
}, },
/** render: function(){
* Set title var obj = this.model.toJSON();
* @param {String} v Title obj.pfx = this.pfx;
*
* @return this
* */
setTitle: function(v){
this.model.set('title',v);
return this;
},
/**
* Set content
* @param {String} v Title
*
* @return this
* */
setContent: function(v){
this.model.set('content',v);
return this;
},
render : function(){
var obj = this.model.toJSON();
obj.pfx = this.pfx;
this.$el.html( this.template(obj) ); this.$el.html( this.template(obj) );
this.$el.attr('class', this.pfx + 'container'); this.$el.attr('class', this.pfx + 'container');
this.updateOpen(); this.updateOpen();
return this; return this;
}, },

4
src/style_manager/view/PropertyFileView.js

@ -120,9 +120,9 @@ define(['backbone','./PropertyView', 'text!./../templates/propertyFile.html'],
this.modal.setTitle('Select image'); this.modal.setTitle('Select image');
this.modal.setContent(this.am.render()); this.modal.setContent(this.am.render());
this.am.setTarget(null); this.am.setTarget(null);
this.modal.show(); this.modal.open();
this.am.onSelect(function(model){ this.am.onSelect(function(model){
that.modal.hide(); that.modal.close();
that.spreadUrl(model.get('src')); that.spreadUrl(model.get('src'));
that.valueChanged(e); that.valueChanged(e);
}); });

1
test/runner/main.js

@ -5,6 +5,7 @@ require(['../src/config/require-config.js', 'config/config.js'], function() {
'specs/main.js', 'specs/main.js',
'specs/asset_manager/main.js', 'specs/asset_manager/main.js',
'specs/dom_components/main.js', 'specs/dom_components/main.js',
'specs/modal/main.js',
'specs/selector_manager/main.js', 'specs/selector_manager/main.js',
'specs/css_composer/main.js', 'specs/css_composer/main.js',
'specs/code_manager/main.js', 'specs/code_manager/main.js',

69
test/specs/modal/main.js

@ -0,0 +1,69 @@
var modulePath = './../../../test/specs/modal';
define([ 'ModalDialog',
modulePath + '/view/ModalView',
],
function(Modal, ModalView) {
describe('Modal dialog', function() {
describe('Main', function() {
var obj;
beforeEach(function () {
obj = new Modal().init();
});
afterEach(function () {
delete obj;
});
it('Object exists', function() {
obj.should.be.exist;
});
it('Is close by default', function() {
obj.isOpen().should.equal(false);
});
it('Title is empty', function() {
obj.getTitle().should.equal('');
});
it('Content is empty', function() {
obj.getContent().should.equal('');
});
it('Set title', function() {
obj.setTitle('Test');
obj.getTitle().should.equal('Test');
});
it('Set content', function() {
obj.setContent('Test');
obj.getContent().should.equal('Test');
});
it('Set HTML content', function() {
obj.setContent('<h1>Test</h1>');
obj.getContent().should.equal('<h1>Test</h1>');
});
it('Open modal', function() {
obj.open();
obj.isOpen().should.equal(true);
});
it('Close modal', function() {
obj.open();
obj.close();
obj.isOpen().should.equal(false);
});
});
ModalView.run();
});
});

74
test/specs/modal/view/ModalView.js

@ -0,0 +1,74 @@
var path = 'ModalDialog/view/';
define([path + 'ModalView', 'ModalDialog/model/Modal'],
function(ModalView, Modal) {
return {
run : function(){
describe('ModalView', function() {
var $fixtures;
var $fixture;
var model;
var view;
var editorModel;
before(function () {
$fixtures = $("#fixtures");
$fixture= $('<div class="modal-fixture"></div>');
});
beforeEach(function () {
model = new Modal();
view = new ModalView({
model: model
});
$fixture.empty().appendTo($fixtures);
$fixture.html(view.render().el);
});
afterEach(function () {
delete view;
delete model;
});
after(function () {
$fixture.remove();
});
it("The content is not empty", function (){
view.el.innerHTML.should.be.not.empty;
});
it("Get content", function (){
view.getContent().should.be.ok;
});
it("Update content", function (){
model.set('content', 'test');
view.getContent().innerHTML.should.equal('test');
});
it("Get title", function (){
view.getTitle().should.be.ok;
});
it("Update title", function (){
model.set('title', 'test');
view.getTitle().innerHTML.should.equal('test');
});
it("Close by default", function (){
view.updateOpen();
view.el.style.display.should.equal('none');
});
it("Open dialog", function (){
model.set('open', 1);
view.el.style.display.should.equal('');
});
});
}
};
});
Loading…
Cancel
Save