<% if is_in_engine? %> /* ----------------------------------------------------------------------------- Red Base - Basic website skel engine Copyright (C) 2012-2013 Yellowen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ----------------------------------------------------------------------------- */ <% end %> // <%= resource.pluralize %> Module var <%= resource.pluralize %> = angular.module("<%= resource %>", ["ListView", "Anim"]); // <%= resource.pluralize %> configuration section --------------------------- <%= resource.pluralize %>.config(["$routeProvider", function($routeProvider){ // Add any route you need here $routeProvider. when("/<%= resource_url %>", { templateUrl: template("<%= resource_path %>/index"), controller: "<%= resource %>Controller" }). when("/<%= resource_url %>/new",{ templateUrl: template("<%= resource_path %>/new"), controller: "Add<%= resource %>Controller" }). when("/<%= resource_url %>/:id/edit",{ templateUrl: template("<%= resource_path %>/new"), controller: "Add<%= resource %>Controller" }); }]); // <%= resource %> index controller ------------------------------------------------------- // This controller is responsible for list page (index) <%= resource.pluralize %>.controller("<%= resource %>Controller", ["$scope", "gettext", "Restangular", function($scope, gettext, API){ <% unless options[:no_bulk] %>// Cache object for each field name possible values $scope.cache = {}; // Change event handler for field_name combobox in bulk edit $scope.field_name_change = function(x){ var current_value = $("#field_name").val(); $scope.current_field= _.find($scope.fields, function(x){ return x.name == current_value; }); if( "to" in $scope.current_field ){ if (! ($scope.current_field.name in $scope.cache)) { $scope.current_field.to().then(function(x){ $scope.cache[$scope.current_field.name] = x; }); } } }; $scope.fields = [<% bulk_edit_fields.each do |name| %> { name: "<%= name %><% if ["belongs_to", "has_many"].include? fields_hash[name] %>_id<% end %>", title: gettext("<%= name.capitalize %>"), type: "<%= fields_hash[name] %>"<% if ["belongs_to", "has_many"].include? fields_hash[name] %>, to: API.all("<%= fields_hash[name].to %>").getList<% end %> },<% end %> ];<% end %> // details_template is the address of template which should load for // each item details section $scope.details_template = template("<%= resource_path %>/details"); // Buttons for top of the list-view $scope.buttons = [ { title: gettext("New"), icon: "fa fa-plus", classes: "btn tiny green", route: "#<%= resource_url %>/new" }<% unless options[:no_bulk] %>, { title: gettext("Bulk Edit"), icon: "fa fa-edit", classes: "btn tiny yellow", action: function(){ $scope.$apply("bulk_edit = ! bulk_edit"); } }<% end %> ];<% unless options[:no_bulk] %> /* * On bulk save event */ $scope.bulk_save = function(){ $scope.view_progressbar = true; var value = $("#field_value").val(); var field = $scope.current_field.name; var type = $scope.current_field.type; var field_name = $scope.current_field.title; if ((type == "has_many") || (type == "belongs_to")) { value = parseInt(value, 10); } var requests_count = 0; $scope.rfiller = 0; $scope.sfiller = 0; $scope.success = 0; $scope.failed = 0; $scope.total = _.where($scope.<%= resource.pluralize.underscore %>, function(x){return x.is_selected === true;}).length; _.each($scope.<%= resource.pluralize.underscore %>, function(x){ if( x.is_selected === true ){ x[field] = value; requests_count++; var rwidth = (requests_count * 100) / $scope.total; if (requests_count == $scope.total) { rwidth = 100; } $scope.rfiller = rwidth + "%"; API.one("<%= resource.pluralize.underscore %>", x.id).patch(x).then(function(data){ $scope.success++; var swidth = parseInt(($scope.success * 100) / $scope.total); if ($scope.sucess == $scope.total) { swidth = 100; } $scope.sfiller = swidth + "%"; x[field_name.toLowerCase()] = data[field_name.toLowerCase()]; }) .catch(function(data){ $scope.failed++; catch_error(data); }); } }); }; /* * On bulk cancel event */ $scope.bulk_cancel = function(){ $("#field_name").val(0); document.getElementById("bulk_form").reset(); $scope.view_progressbar = false; $scope.bulk_edit = false; }; <% end %> /* * On delete event handler - `items` is an array of objects to delete */ $scope.on_delete = function(items){ var query = []; items.forEach(function(item){ query.push(item.id); }); API.all("<%= resource.pluralize.underscore %>").customDELETE(query.join(",")) .then(function(data) { $scope.<%= resource.pluralize.underscore %> = _.filter($scope.<%= resource.pluralize.underscore %>, function(x){ return !(query.indexOf(x.id) != -1); }); success_message(data.msg); }) .catch(catch_error); }; API.all("<%= resource.pluralize.underscore %>").getList() .then(function(data){ $scope.<%= resource.pluralize.underscore %> = data; }) .catch(catch_error); }]); <%= resource.pluralize %>.controller("Add<%= resource %>Controller", ["Restangular", "$scope", "$location", "$routeParams", "gettext", function(API, $scope, $location, $routeParams, gettext){ $scope.editing = false; $scope.obj_id = null; <% fields.each do |name, type| %><% if ["belongs_to", "has_many"].include? type %><%= type.get_list %>.then(function(data){ $scope.all_<%= type.resource_name %> = data; }).catch(catch_error);<% end %><% end %> if( "id" in $routeParams ){ $scope.obj_id = $routeParams.id; $scope.editing = true; var obj = API.one("<%= resource.pluralize.underscore %>", $scope.obj_id).get() .then(function(data) { <% fields.each do |field_name, field_type| %> $scope.<%= field_name %> = <% if not ["string", "integer", "belongs_to", "text", "has_many"].include? field_type %><%= "to_#{field_type}(data.#{field_name})" %><% else %>data.<%= field_name %><% end %>;<% end %> }) .catch(catch_error); } $scope.have = function(field, obj_id) { var tmp = _.where($scope[field], {id: obj_id}); if (tmp.length > 0) { return true; } else { return false; } }; $scope.cancel = function(){ $(".form input").val(""); $location.path("<%= resource_url %>"); }; $scope.save = function(save_another){ var <%= resource.underscore %> = {<%= resource.underscore %>: {<% fields.each do |field_name, field_type| %> <% if ["belongs_to"].include? field_type %><%= field_name %>_id: parseInt($scope.<%= field_name %>),<% else %><%= field_name %>: $scope.<%= field_name %>,<% end %> <% end %> }}; if ($scope.obj_id) { API.one("<%= resource.pluralize.underscore %>", $scope.obj_id).patch(<%= resource.underscore %>) .then(function(){ success_message(gettext("<%= resource %> updated successfully.")); if (save_another) { $(".form input").val(""); } else { $location.path("<%= resource_url %>"); } }) .catch(catch_error); } else { console.dir(<%= resource.underscore %>); API.all("<%= resource.pluralize.underscore %>").customPOST(<%= resource.underscore %>, "").then(function(){ success_message(gettext("<%= resource %> created successfully.")); if (save_another) { $(".form input").val(""); } else { $location.path("<%= resource_url %>"); } }).catch(catch_error); } }; }]); <% unless options[:menu].empty? %> <%= resource.pluralize %>.controller("<%= resource %>MenuController", ["gettext", function(gettext){ this.menu_items = [<% options[:menu].split(",").each do |menu| %><% title, url = menu.split(":") %> {title: gettext("<%= title %>"), url: "<%= url %>"},<% end %> ]; }]);<% end %>