Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var App = Em.Application.create();
- //Put classes here:
- //This creates a class "ProductsController" which subclasses ArrayController -> can be used by handlebars to print each element.
- App.ProductsController = Em.ArrayController.create();
- App.UsersController = Em.ArrayController.create();
- App.CustomersController = Em.ArrayController.create();
- App.StoragesController = Em.ArrayController.create();
- App.QuantitiesController = Em.ArrayController.create();
- App.TransactionsController = Em.ArrayController.create();
- App.ReservationsController = Em.ArrayController.create();
- App.StoresController = Em.ArrayController.create();
- App.TransactionProductController = Em.ArrayController.create();
- //Product class
- //Add all functions we want to manipulate product data with here.
- App.Product = Em.Object.extend();
- App.User = Em.Object.extend();
- App.Customer = App.User.extend();
- App.Storage = Em.Object.extend();
- App.Quantity = Em.Object.extend();
- App.Transaction = Em.Object.extend();
- App.Reservation = Em.Object.extend();
- App.Store = Em.Object.extend();
- App.TransactionProduct = App.Product.extend({
- quantity: 1
- });
- //Views go here. They specify the naming types and such which is mapped directly to the html.
- App.ProductView = Em.View.extend({
- tagName: 'div',
- templateName: 'product-template',
- classNames: ["product"]
- });
- App.UserView = Em.View.extend({
- tagName: 'div',
- templateName: 'user-template',
- classNames: ["user"]
- });
- App.CustomerView = Em.View.extend({
- tagName: 'div',
- templateName: 'customer-template',
- classNames: ["customer"]
- });
- App.MainView = Em.View.extend({
- tagName: 'div',
- templateName: 'main-template',
- classNames: ["main"]
- });
- App.TransactionProductView = Em.View.extend({
- tagName: 'div',
- templateName: 'transaction-product-template',
- classNames: ["transaction-products"]
- });
- App.ReservationView = Em.View.extend({
- tagName: 'div',
- templateName: 'reserve-product-template',
- classNames: ["reserve-products"]
- });
- //This maps to the words "Add Product" in the html. Does a "POST" call to the database. If you look at app.rb, the command inserts the new product into the database.
- /*App.InsertProductView = Em.View.extend({
- tagName: 'div',
- templateName: 'product-insert-template',
- classNames: ["product-insert"],
- mouseDown: function() {
- //Hardcoding data for products.
- var newProduct = {
- name: "Fantastic Stuff",
- productPrice: 99
- }
- $.post("/products", {product: newProduct}, function(data, s, x) {
- App.ProductsController.pushObject(App.Product.create(newProduct));
- });
- }
- });*/
- $.get('/products', function(data) {
- var products = $.map(JSON.parse(data), function(o, i) {
- var p = App.Product.create();
- p.setProperties(o.product);
- return p;
- });
- App.ProductsController.set('content', products);
- });
- $.get('/customers', function(data) {
- var customers = $.map(JSON.parse(data), function(o, i) {
- var c = App.Customer.create();
- c.setProperties(o.customer);
- return c;
- });
- App.CustomersController.set('content', customers);
- });
- $.get('/users', function(data) {
- var users = $.map(JSON.parse(data), function(o, i) {
- var u = App.User.create();
- u.setProperties(o.user);
- return u;
- });
- App.UsersController.set('content', users);
- });
- $.get('/storages', function(data) {
- var storages = $.map(JSON.parse(data), function(o, i) {
- var s = App.Storage.create();
- s.setProperties(o.storage);
- return s;
- });
- App.StoragesController.set('content', storages);
- });
- $.get('/quantities', function(data) {
- var quantities = $.map(JSON.parse(data), function(o, i) {
- var q = App.Quantity.create();
- q.setProperties(o.quantity);
- return q;
- });
- App.QuantitiesController.set('content', quantities);
- });
- $.get('/transactions', function(data) {
- var transactions = $.map(JSON.parse(data), function(o, i) {
- var t = App.Transaction.create();
- t.setProperties(o.transaction);
- return t;
- });
- App.TransactionsController.set('content', transactions);
- });
- $.get('/reservations', function(data) {
- var reservations = $.map(JSON.parse(data), function(o, i) {
- var r = App.Reservation.create();
- r.setProperties(o.reservation);
- return r;
- });
- App.ReservationsController.set('content', reservations);
- });
- $.get('/stores', function(data) {
- var stores = $.map(JSON.parse(data), function(o, i) {
- var s = App.Store.create();
- s.setProperties(o.store);
- return s;
- });
- App.StoresController.set('content', stores);
- });
- App.UpdateProductView = Em.View.extend({
- tagName: 'div',
- templateName: 'product-update-template',
- classNames: ["product-update"],
- mouseDown: function() {
- var productToUpdate = this.get('element');
- console.log(productToUpdate);
- if (false) {
- var productToUpdate = {
- name: "Newer Stuff",
- productPrice: 33
- }
- $.ajax ({
- type: 'PUT',
- url: '/products/' + product.id,
- data: productToUpdate,
- success: function(data) {
- }
- })
- }
- }
- });
- //this is for users, new user
- App.InsertUserView = Em.View.extend({
- tagName: 'div',
- templateName: 'user-insert-template',
- classNames: ["user-insert"],
- mouseDown: function() {
- //Hardcoding data for user.
- var newUser = {
- firstName: "Fantastic",
- lastName: "User"
- }
- $.post("/users", {user: newUser}, function(data, s, x) {
- App.UsersController.pushObject(App.User.create(newUser));
- });
- }
- });
- App.LoginFormView = Em.View.extend({
- tagName: "div",
- templateName: "login-form-template",
- classNames: ["basic-form"],
- firstName: '',
- password: '',
- submit: function(event) {
- var loginUser = {
- firstName: this.get('firstName'),
- password: this.get('password')
- }
- $.post("/login/" + loginUser.firstName + "/" + loginUser.password, {user: loginUser}, function(data, s, x) {
- var d = JSON.parse(data);
- if (d.error) {
- //Show error "wrong password"
- alert(d.error);
- } else {
- //Show label "logged in"
- $(".not-logged-in").hide();
- $(".logged-in").show();
- }
- });
- }
- });
- /* SALES FORMS */
- /* -------------------------------------------------------------------------------------------- */
- App.TransactionView = Em.View.extend({
- tagName: "div",
- templateName: "transaction-form-template",
- classNames: ["basic-form"],
- ProductID: '',
- Quantity: '',
- CustomerID: '',
- discount: 1,
- addProduct: function(event) {
- var prodID = this.get('ProductID');
- var prodQuant = this.get('Quantity');
- var CustomerID = this.get('CustomerID');
- var numProducts = $(".num-products").val();
- var discount = this.get('discount');
- function getProduct() {
- $.get('/products/' + prodID, function(data) {
- var product = App.TransactionProduct.create();
- var tProdData = JSON.parse(data);
- tProdData.product.quantity = prodQuant;
- tProdData.product.productPrice = prodQuant*tProdData.product.productPrice*discount;
- product.setProperties(tProdData);
- App.TransactionProductController.pushObject(product.product);
- ++numProducts;
- $(".num-products").val(numProducts);
- updatePrice(numProducts);
- });
- }
- function updatePrice(numProducts) {
- var p = App.TransactionProductController.get('content').get(numProducts-1);
- var totalPrice = $(".total-price").val();
- var price = parseFloat(totalPrice) + p.productPrice;
- $(".total-price").val(price);
- }
- getProduct();
- },
- applyDiscount: function(event) {
- var discount = 1 - $(".discount-price").val()/100;
- this.set('discount', discount);
- var newTotal = parseFloat($(".total-price").val())*discount;
- $(".total-price").val(newTotal);
- },
- submit: function(event) {
- }
- });
- App.RemoveFromTransactionView =Em.View.extend({
- tagName: "div",
- templateName: "remove-product-form-template",
- classNames: ["basic-form"],
- ProductName: '',
- Quantity: '',
- CustomerID: '',
- submit: function(event) {
- var pName = this.get('ProductName');
- var quantity = this.get('Quantity');
- var customerID = this.get('CustomerID');
- }
- });
- App.RefundProductView =Em.View.extend({
- tagName: "div",
- templateName: "refund-product-form-template",
- classNames: ["basic-form"],
- ProductName: '',
- Quantity: '',
- Reason: '',
- submit: function(event) {
- var pName = this.get('ProductName');
- var quantity = this.get('Quantity');
- }
- });
- /* -------------------------------------------------------------------------------------------- */
- /* ORDERING FORMS */
- /* -------------------------------------------------------------------------------------------- */
- App.ThresholdView = Em.View.extend({
- tagName: "div",
- templateName: "threshold-form-template",
- classNames: ["basic-form"],
- ProductID: '',
- NewThreshold: '',
- submit: function(event){
- var ProductID=this.get('ProductID');
- var NewThreshold=this.get('NewThreshold');
- console.log(ProductID + '' + NewThreshold);
- }
- });
- App.AddProductView = Em.View.extend({
- tagName: "div",
- templateName: "add-product-form-template",
- classNames: ["basic-form"],
- ProductName: '',
- Price: '',
- Threshold: '',
- submit: function(event) {
- var newProduct = {
- name: this.get('ProductName'),
- productPrice: this.get('Price'),
- productThreshold: this.get('Threshold')
- }
- $.post("/products", {product: newProduct}, function(data, s, x) {
- var d = JSON.parse(data);
- if (d.error) {
- alert(d.error);
- if (d.error.name) {
- $.map(d.error.name, function(x) {
- alert(x);
- });
- }
- } else {
- App.ProductsController.pushObject(App.Product.create(JSON.parse(data).product));
- }
- });
- this.set('ProductName', '');
- this.set('Price', '');
- this.set('Threshold', '');
- }
- });
- App.ToggleOrderableView =Em.View.extend({
- tagName: "div",
- templateName: "toggle-orderable-form-template",
- classNames: ["basic-form"],
- ProductName: '',
- Quantity: '',
- Orderable: '',
- submit: function(event) {
- var pName = this.get('ProductName');
- var quantity = this.get('Quantity');
- var order = this.get('Orderable');
- }
- });
- App.OrderProductsView = Em.View.extend({
- tagName: "div",
- templateName: "order-products-form-template",
- classNames: ["basic-form"],
- ProductID: '',
- Quantity: '',
- submit: function(event){
- var ProductID=this.get('ProductID');
- var Quantity=this.get('Quantity');
- console.log(ProductID + '' + NewThreshold);
- }
- });
- /* -------------------------------------------------------------------------------------------- */
- /* CUSTOMER FORMS */
- /* -------------------------------------------------------------------------------------------- */
- App.NewUserView = Em.View.extend ({
- tagName: "div",
- templateName: "new-user-form-template",
- classNames: ["basic-form"],
- FirstName: '',
- LastName: '',
- AccessLevel: '',
- submit: function(event){
- if (this.get('AccessLevel') == '') {
- this.set('AccessLevel', 'customer')
- var typeOfUser = 'customer';
- } else {
- typeOfUser = 'user';
- }
- var nUser = {
- firstName: this.get('FirstName'),
- lastName: this.get('LastName'),
- accessLevel: this.get('AccessLevel')
- }
- if (typeOfUser == 'customer') {
- $.post("/users", {user: nUser}, function(data, s, x) {
- App.UsersController.pushObject(App.User.create(JSON.parse(data).user));
- });
- } else {
- }
- }
- });
- App.RemoveCustomerView = Em.View.extend({
- tagName: "div",
- templateName: "removeCustomer-form-template",
- classNames: ["basic-form"],
- firstName: '',
- lastName: '',
- submit: function(event) {
- var fName = this.get('firstName');
- var lName = this.get('lastName');
- console.log(fName + ' ' + lName);
- }
- });
- App.ReserveView = Em.View.extend({
- tagName: "div",
- templateName: "reserve-form-template",
- classNames: ["basic-form"],
- ProductID: '',
- Quantity: '',
- submit: function(event) {
- var newReservation = {
- product_id: this.get('ProductID'),
- //Hardcoded user atm.
- user_id: "1",
- count: this.get('Quantity')
- }
- $.post("/reservations", {reservation: newReservation}, function(data, s, x) {
- App.ReservationsController.pushObject(App.Reservation.create(JSON.parse(data).reservation));
- });
- this.set('ProductID', '');
- this.set('Quantity', '');
- }
- });
- App.CancelReservationView = Em.View.extend({
- tagName: "div",
- templateName: "cancelReservation-form-template",
- classNames: ["basic-form"],
- productToCancel: '',
- Quantity: '',
- submit: function(event) {
- var prodtoCan = this.get('productToCancel');
- var quantity = this.get('quantity');
- }
- });
- /* -------------------------------------------------------------------------------------------- */
- /* STOCK MOVEMENT FORMS */
- /* -------------------------------------------------------------------------------------------- */
- App.MoveProductView =Em.View.extend({
- tagName: "div",
- templateName: "MoveProduct-form-template",
- classNames: ["basic-form"],
- StorageOrigin: '',
- StorageDest: '',
- ProductID: '',
- Quantity: '',
- submit: function(event){
- var storageOrgin = this.get('StorageOrigin');
- var StorageDest = this.get('StorageDest');
- var ProductID = this.get('ProductID');
- var Quantity = this.get('Quantity');
- }
- });
- App.ToggleSellableView =Em.View.extend({
- tagName: "div",
- templateName: "toggle-sellable-form-template",
- classNames: ["basic-form"],
- ProductName: '',
- Quantity: '',
- Sellable: '',
- submit: function(event) {
- var pName = this.get('ProductName');
- var quantity = this.get('Quantity');
- var sell = this.get('Sellable');
- }
- });
- App.NewPriceView = Em.View.extend ({
- tagName: "div",
- templateName: "ModifyPrice-form-template",
- classNames: ["basic-form"],
- ProductID: '',
- NewPrice: '',
- submit: function(event){
- var ProductID= this.get('ProductID');
- var NewPrice = this.get('NewPrice');
- console.log(ProductID + '' + newPrice);
- }
- });
- App.ShelfProductView =Em.View.extend({
- tagName: "div",
- templateName: "shelf-product-form-template",
- classNames: ["basic-form"],
- ProductName: '',
- Quantity: '',
- LocationID: '',
- submit: function(event) {
- var pName = this.get('ProductName');
- var quantity = this.get('Quantity');
- var lID = this.get('LocationID');
- }
- });
- /* -------------------------------------------------------------------------------------------- */
- /* SETTINGS FORMS */
- /* -------------------------------------------------------------------------------------------- */
- App.ChangePasswordView =Em.View.extend({
- tagName: "div",
- templateName: "change-password-form-template",
- classNames: ["basic-form"],
- FirstName: '',
- OldPassword: '',
- NewPassword: '',
- submit: function(event) {
- var FirstName = this.get('FirstName');
- var oldPass = this.get('OldPassword');
- }
- });
- /* -------------------------------------------------------------------------------------------- */
- /* UNUSED FORMS */
- /* -------------------------------------------------------------------------------------------- */
- App.MoveToStorageView =Em.View.extend({
- tagName: "div",
- templateName: "move-to-storage-template",
- classNames: ["basic-form"],
- ProductName: '',
- Quantity: '',
- LocationID: '',
- submit: function(event) {
- var pName = this.get('ProductName');
- var quantity = this.get('Quantity');
- var lID = this.get('LocationID');
- }
- });
- /* -------------------------------------------------------------------------------------------- */
- /* Form hide and show functions: */
- /* -------------------------------------------------------------------------------------------- */
- App.HomeSidebarView = Em.View.extend ({
- templateName: "home-sidebar-template",
- welcome: function(event) {
- }
- });
- App.SalesSidebarView = Em.View.extend ({
- templateName: "sales-sidebar-template",
- refundProduct: function(event) {
- hideAll();
- $(".refund-product-form").show();
- },
- removeFromTransaction: function(event) {
- hideAll();
- $(".remove-product-form").show();
- },
- transaction: function(event) {
- hideAll();
- $(".transaction-form").show();
- initializeTransaction();
- }
- });
- App.OrderingSidebarView = Em.View.extend ({
- templateName: "ordering-sidebar-template",
- addProduct: function(event) {
- hideAll();
- $(".add-product-form").show();
- },
- changeThreshold: function(event) {
- hideAll();
- $(".threshold-form").show();
- },
- toggleOrderable: function(event) {
- hideAll();
- $(".toggle-orderable-form").show();
- },
- discontinueProduct: function(event) {
- hideAll();
- $(".discontinue-product-form").show();
- },
- orderProduct: function(event) {
- hideAll();
- $(".order-products-form").show();
- }
- });
- App.CustomerSidebarView = Em.View.extend ({
- templateName: "customer-sidebar-template",
- newUser: function(event) {
- hideAll();
- $(".new-user-form").show();
- },
- removeCustomer: function(event) {
- hideAll();
- $(".remove-customer-form").show();
- },
- reserveProduct: function(event) {
- hideAll();
- $(".reserve-form").show();
- },
- cancelReservation: function(event) {
- hideAll();
- $(".cancel-reservation-form").show();
- }
- });
- App.StockSidebarView = Em.View.extend ({
- templateName: "stock-sidebar-template",
- moveProduct: function(event) {
- hideAll();
- $(".move-product-form").show();
- },
- toggleSellabe: function(event) {
- hideAll();
- $(".toggle-sellable-form").show();
- },
- changePrice: function(event) {
- hideAll();
- $(".price-form").show();
- },
- shelfProduct: function(event) {
- hideAll();
- $(".shelf-product-form").show();
- },
- });
- App.SettingsSidebarView = Em.View.extend ({
- templateName: "settings-sidebar-template",
- changePassword: function(event) {
- hideAll();
- $(".change-password-form").show();
- },
- });
- App.TabView = Em.View.extend ({
- templateName: "tab-template",
- home: function(event) {
- hideAll();
- hideSideBars();
- $(".home-sidebar").show();
- $(".initial-form").show();
- },
- sales: function(event) {
- hideAll();
- hideSideBars();
- $(".sales-sidebar").show();
- $(".transaction-form").show();
- initializeTransaction();
- },
- ordering: function(event) {
- hideAll();
- hideSideBars();
- $(".ordering-sidebar").show();
- $(".add-product-form").show();
- },
- customer: function(event) {
- hideAll();
- hideSideBars();
- $(".customer-sidebar").show();
- $(".new-user-form").show();
- },
- stockmovement: function(event) {
- hideAll();
- hideSideBars();
- $(".stockmovement-sidebar").show();
- $(".move-product-form").show();
- },
- settings: function(event) {
- hideAll();
- hideSideBars();
- $(".settings-sidebar").show();
- $(".change-password-form").show();
- }
- });
- function hideAll() {
- $(".initial-form").hide();
- $(".reserve-form").hide();
- $(".transaction-form").hide();
- $(".threshold-form").hide();
- $(".price-form").hide();
- $(".add-product-form").hide();
- $(".cancel-reservation-form").hide();
- $(".remove-customer-form").hide();
- $(".refund-product-form").hide();
- $(".move-product-form").hide();
- $(".new-user-form").hide();
- $(".toggle-sellable-form").hide();
- $(".ModifyPrice-form").hide();
- $(".shelf-product-form").hide();
- $(".toggle-orderable-form").hide();
- $(".discontinue-product-form").hide();
- $(".change-password-form").hide();
- $(".order-products-form").hide();
- $(".remove-product-form").hide();
- }
- function hideSideBars() {
- $(".home-sidebar").hide();
- $(".sales-sidebar").hide();
- $(".ordering-sidebar").hide();
- $(".customer-sidebar").hide();
- $(".stockmovement-sidebar").hide();
- $(".settings-sidebar").hide();
- }
- function initializeTransaction() {
- var transactionProduct = {
- id: 0,
- name: 'noprod',
- quantity: 1,
- productPrice: 1
- }
- var tProducts = new Array();
- tProducts.push(transactionProduct);
- App.TransactionProductController.set('content', tProducts);
- App.TransactionProductController.clear();
- }
- /* -------------------------------------------------------------------------------------------- */
Add Comment
Please, Sign In to add comment