Browse Source

Start devices module

pull/36/head
Artur Arseniev 10 years ago
parent
commit
4a409cd96c
  1. 14
      src/canvas/view/FrameView.js
  2. 8
      src/commands/view/InsertCustom.js
  3. 21
      src/devices/config/config.js
  4. 87
      src/devices/main.js
  5. 14
      src/devices/model/Device.js
  6. 9
      src/devices/model/Devices.js
  7. 1
      src/editor/model/Editor.js

14
src/canvas/view/FrameView.js

@ -14,14 +14,22 @@ function(Backbone) {
initialize: function(o) { initialize: function(o) {
this.config = o.config || {}; this.config = o.config || {};
this.ppfx = this.config.pStylePrefix || ''; this.ppfx = this.config.pStylePrefix || '';
this.listenTo(this.model, 'change:width', this.updateWidth); this.em = this.config.em;
this.listenTo(this.em, 'change:device', this.updateWidth);
}, },
/** /**
* Update width of the frame * Update width of the frame
*/ */
updateWidth: function(){ updateWidth: function(model){
this.el.style.width = this.model.get('width'); // Refactor: maybe its better put it inside EditorModel
// as I will use it also inside Style Manager
// editor.getMediaWidth();
var value = model.get('device');
var media = em.get('Devices').get(value);
if(!media)
return;
this.el.style.width = media.get('width');
}, },
getBody: function(){ getBody: function(){

8
src/commands/view/InsertCustom.js

@ -8,6 +8,7 @@ define(['backbone', './CreateComponent'],
init: function(){ init: function(){
CreateComponent.init.apply(this, arguments); CreateComponent.init.apply(this, arguments);
_.bindAll(this, 'insertComponent');
this.allowDraw = 0; this.allowDraw = 0;
}, },
@ -25,7 +26,7 @@ define(['backbone', './CreateComponent'],
enable: function(){ enable: function(){
CreateComponent.enable.apply(this, arguments); CreateComponent.enable.apply(this, arguments);
this.$wr.on('click', this.insertComponent.bind(this)); this.$wr.on('click', this.insertComponent);
}, },
/** /**
@ -42,10 +43,13 @@ define(['backbone', './CreateComponent'],
var model = this.create(this.sorter.target, object, index, null, {silent: false}); var model = this.create(this.sorter.target, object, index, null, {silent: false});
if(this.opt.terminateAfterInsert && this.sender) if(this.opt.terminateAfterInsert && this.sender)
this.sender.set('active',false); this.sender.set('active', false);
else else
this.enable(); this.enable();
if(!model)
return;
if(this.em) if(this.em)
this.em.editor.initChildrenComp(model); this.em.editor.initChildrenComp(model);

21
src/devices/config/config.js

@ -0,0 +1,21 @@
define(function () {
return {
'devices': [
{
name: 'Desktop',
width: '',
},{
name: 'Tablet',
width: '991px',
},{
name: 'Mobile landscape',
width: '767px',
},{
name: 'Mobile portrait',
width: '479px',
},
],
};
});

87
src/devices/main.js

@ -0,0 +1,87 @@
/**
*
* * [getAll](#getall)
* * [get](#get)
* * [add](#add)
*
* Before using methods you should get first the module from the editor instance, in this way:
*
* ```js
* var deviceManager = editor.Devices;
* ```
*
* @module Devices
* @param {Object} config Configurations
* @param {Array<Object>} [config.devices=[]] Default devices
* @example
* ...
* devices: {
* devices: [
* {name: 'Desktop', width: ''}
* {name: 'Tablet', width: '991px'}
* ],
* }
* ...
*/
define(function(require) {
return function(config) {
var c = config || {},
defaults = require('./config/config'),
Devices = require('./model/Devices');
for (var name in defaults) {
if (!(name in c))
c[name] = defaults[name];
}
var devices = new Devices(c.devices);
return {
/**
* Add new device to the collection. URLs are supposed to be unique
* @param {string} name Device name
* @param {string} width Width of the device
* @param {Object} opts Custom options
* @return {Device} Added device
* @example
* // In case of strings, would be interpreted as images
* deviceManager.add('Tablet', '900px');
*/
add: function(name, width, opts){
var obj = opts || {};
obj.name = name;
obj.width = width;
return devices.add(obj);
},
/**
* Return device by name
* @param {string} name Name of the device
* @example
* var device = deviceManager.get('Tablet');
* console.log(JSON.stringify(device));
* // {name: 'Tablet', width: '900px'}
*/
get: function(name){
return devices.get(name);
},
/**
* Return all devices
* @return {Collection}
* @example
* var devices = deviceManager.getAll();
* console.log(JSON.stringify(devices));
* // [{name: 'Desktop', width: ''}, ...]
*/
getAll: function(){
return devices;
},
};
};
});

14
src/devices/model/Device.js

@ -0,0 +1,14 @@
define(['backbone'],
function(Backbone){
return Backbone.Model.extend({
idAttribute: 'name',
defaults :{
name: '',
width: '',
},
});
});

9
src/devices/model/Devices.js

@ -0,0 +1,9 @@
define(['backbone','./Device'],
function (Backbone, Device) {
return Backbone.Collection.extend({
model: Device,
});
});

1
src/editor/model/Editor.js

@ -42,6 +42,7 @@ define([
selectedComponent: null, selectedComponent: null,
previousModel: null, previousModel: null,
changesCount: 0, changesCount: 0,
device: '',
}, },
initialize: function(c) { initialize: function(c) {

Loading…
Cancel
Save