Commit 03aa0d64 authored by Vassilis Kanellopoulos's avatar Vassilis Kanellopoulos
Browse files

schools module; assets manager

parent 89d81d23
{% extends "school/index.twig" %}
{% extends "schools/index.twig" %}
{% import "school/index.twig" as macros %}
{% import "schools/index.twig" as macros %}
{% block schoolTitle %}
{{ parent() }} <small>Εκπαιδευτικοί</small>
......@@ -100,5 +100,5 @@
{% block inlinejs %}
{{ parent() }}
<script src="{{ base_url }}/js/school/staff.js"></script>
<script src="{{ base_url }}/js/schools/staff.js"></script>
{% endblock %}
\ No newline at end of file
(function () {
'use strict';
var Asset,
Assets,
AssetView,
AssetsView,
AssetModalView,
assetTemplate;
Asset = Backbone.Model.extend({ idAttribute: 'id' });
Assets = Backbone.Collection.extend({
model: Asset,
comparator: 'name',
});
AssetView = Backbone.View.extend({
tagName: 'tr',
template: (function () {
if (typeof assetTemplate === 'undefined') {
assetTemplate = _.template($('#asset-row-template').html());
}
return assetTemplate;
}()),
initialize: function () {
this.model.on('change', this.render, this);
},
render: function () {
var html = this.template({ asset: this.model.attributes });
this.$el.attr('data-id', this.model.get('id'));
this.$el.html(html);
return this;
}
});
AssetsView = Backbone.View.extend({
el: '#school',
modal: null,
events: {
'click .btn-add-asset': 'addAsset',
'click tbody tr': 'editAsset'
},
initialize: function () {
var that = this;
this.modal = new AssetModalView();
_.each(this.$el.find('tbody tr[data-asset]'), function (tr) {
var data,
asset;
tr = $(tr);
data = tr.data('asset');
asset = new Asset(data);
that.model.add(asset);
new AssetView({ model: asset });
tr.attr('data-asset', null);
});
this.model.on('add', this.renderAsset, this);
},
renderAsset: function (asset) {
this.$el.find('tbody').append(new AssetView({
model: asset
}).render().el);
return this;
},
addAsset: function (evt) {
var that = this;
evt.preventDefault();
this.modal.render(function (data) {
that.model.add(data);
});
},
editAsset: function (evt) {
var asset = this.model.get($(evt.target).closest('tr').data('id'));
this.modal.render(function (data) {
asset.set(data);
}, asset.attributes);
}
});
AssetModalView = Backbone.View.extend({
el: '#asset-form-modal',
form: null,
events: {
'submit': 'submit',
},
initialize: function () {
this.form = this.$el.find('form');
},
show: function () {
this.$el.modal('show');
return this;
},
hide: function () {
this.$el.modal('hide');
return this;
},
render: function (done, asset) {
var template,
that = this;
asset = asset || {};
this.form[0].reset();
this.form.find('input[type="hidden"]').val('');
this.form.data('done', done);
_.each(this.form[0].elements, function (element) {
var element = $(element),
name = element.attr('name');
if (typeof asset[name] !== undefined) {
element.val(asset[name]);
}
});
this.show();
return this;
},
submit: function (evt) {
var data;
evt.preventDefault();
data = _.reduce(this.form.serializeArray(), function (hash, pair) {
hash[pair.name] = pair.value;
return hash;
}, {});
evt.preventDefault();
if (!data.id) {
data.id = (100 * Math.random()).toFixed(0);
}
this.form.data('done')(data);
this.form.data('done', undefined);
this.hide();
}
});
new AssetsView({ model: new Assets() });
}());
\ No newline at end of file
(function () {
'use strict';
$.each($('#school .nav-tabs li'), function (index, tab) {
tab = $(tab);
if (window.location.pathname === tab.find('a').attr('href')) {
tab.addClass('active');
} else {
tab.removeClass('active');
}
});
$('[required]').parents('.form-group').find('label').append('<span>*</span>');
}());
\ No newline at end of file
(function () {
'use strict';
var Lab,
Labs,
LabView,
LabsView,
LabModalView,
labTemplate,
utils = window.EDULABS.utils;
Lab = Backbone.Model.extend({ idAttribute: 'id' });
Labs = Backbone.Collection.extend({
model: Lab,
comparator: 'name'
});
LabView = Backbone.View.extend({
tagName: 'tr',
template: (function () {
if (typeof labTemplate === 'undefined') {
labTemplate = _.template($('#lab-row-template').html());
}
return labTemplate;
}()),
initialize: function () {
this.model.on('change', this.render, this);
},
render: function () {
var html = this.template({ lab: this.model.attributes });
this.$el.html(html);
this.$el.attr('data-id', this.model.get('id'));
return this;
}
});
LabsView = Backbone.View.extend({
el: '#school',
modal: null,
events: {
'click .btn-add-lab': 'addLab',
'click tbody tr': 'editLab'
},
initialize: function () {
var that = this;
this.modal = new LabModalView();
_.each(this.$el.find('tbody tr[data-lab]'), function (tr) {
var data,
lab;
tr = $(tr);
data = tr.data('lab');
lab = new Lab(data);
that.model.add(lab);
new LabView({ model: lab });
tr.attr('data-lab', null);
});
this.model.on('add', this.renderLab, this);
},
renderLab: function (lab) {
this.$el.find('tbody').append(new LabView({
model: lab
}).render().el);
return this;
},
addLab: function (evt) {
var that = this;
evt.preventDefault();
this.modal.render(function (data) {
that.model.add(data);
});
},
editLab: function (evt) {
var lab = this.model.get(utils.parseInt($(evt.target).closest('tr').data('id')));
if (!lab) return;
this.modal.render(function (data) {
lab.set(data);
}, lab.attributes);
}
});
LabModalView = Backbone.View.extend({
el: '#lab-form-modal',
form: null,
events: {
'submit': 'submit',
},
initialize: function () {
this.form = this.$el.find('form');
},
show: function () {
this.$el.modal('show');
return this;
},
hide: function () {
this.$el.modal('hide');
return this;
},
render: function (done, lab) {
var template,
that = this;
lab = lab || {};
this.form[0].reset();
this.form.find('input[type="hidden"]').val('');
this.form.data('done', done);
_.each(this.form[0].elements, function (element) {
var element = $(element),
name = element.attr('name');
element.val(lab[name] || '');
});
this.show();
return this;
},
submit: function (evt) {
var data;
evt.preventDefault();
data = _.reduce(this.form.serializeArray(), function (hash, pair) {
hash[pair.name] = pair.value;
return hash;
}, {});
evt.preventDefault();
if (!data.id) {
data.id = (100 * Math.random()).toFixed(0);
}
this.form.data('done')(data);
this.form.data('done', undefined);
this.hide();
}
});
new LabsView({ model: new Labs() });
}());
\ No newline at end of file
(function () {
'use strict';
var Staff,
Employee,
EmployeeView,
StaffView,
ModalView,
employeeTemplate;
Employee = Backbone.Model.extend({ idAttribute: 'id' });
Staff = Backbone.Collection.extend({
model: Employee,
comparator: 'surname'
});
EmployeeView = Backbone.View.extend({
tagName: 'tr',
template: (function () {
if (typeof employeeTemplate === 'undefined') {
employeeTemplate = _.template($('#employee-row').html());
}
return employeeTemplate;
}()),
initialize: function () {
this.model.on('change', this.render, this);
},
render: function () {
this.$el.html(this.template({ employee: this.model.attributes }));
this.$el.attr('data-id', this.model.get('id'));
return this;
}
});
StaffView = Backbone.View.extend({
el: '#school',
modal: null,
events: {
'click .btn-add-employee': 'addEmployee',
'click tbody tr': 'editEmployee'
},
initialize: function () {
var that = this;
this.modal = new ModalView();
_.each(this.$el.find('tbody tr'), function (tr) {
var data = $(tr).data('employee'),
employee = new Employee(data);
that.model.add(employee);
new EmployeeView({ model: employee, el: tr });
$(tr).attr('data-employee', null);
});
this.model.on('add', this.renderEmployee, this);
},
addEmployee: function (evt) {
var that = this;
evt.preventDefault();
this.modal.render(function (data) {
that.model.add(data);
});
return this;
},
editEmployee: function (evt) {
var employee = this.model.get($(evt.target).closest('tr').data('id'));
if ($(evt.target).is('a')) return;
if (!employee) return;
this.modal.render(function (data) {
employee.set(data);
}, employee.attributes);
return this;
},
renderEmployee: function (employee) {
this.$el.find('tbody').append(new EmployeeView({
model: employee
}).render().el);
return this;
}
});
ModalView = Backbone.View.extend({
el: '#employee-form-modal',
form: null,
events: {
'submit form': 'submit',
},
initialize: function () {
this.form = this.$el.find('form');
},
render: function (done, employee) {
this.form.data('done', done);
this.form[0].reset();
this.form.find('input[type="hidden"]').val('');
employee = employee || {};
_.each(this.form[0].elements, function (element) {
var element = $(element),
name = element.attr('name');
element.val(employee[name] || '');
});
this.show();
},
show: function () {
this.$el.modal('show');
},
hide: function () {
this.$el.modal('hide');
},
submit: function (evt) {
var data = _.reduce(this.form.serializeArray(), function (hash, pair) {
hash[pair.name] = pair.value;
return hash;
}, {});
evt.preventDefault();
if (!data.id) {
data.id = (100 * Math.random()).toFixed(0);
}
this.form.data('done')(data);
this.form.data('done', undefined);
this.hide();
}
});
new StaffView({ model: new Staff() });
}());
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment