<% if is_in_engine? %> /* ----------------------------------------------------------------------------- Faalis - 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", "Filter", "Anim", "Fields",<% if options[:deps] %><% options[:deps].split(",").each do |dependency| %>, "<%= dependency.camelize %>"<% end %><% end %>]); // <%= 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", "catch_error", "$location", function($scope, gettext, API, catch_error, $location){ <% unless options[:no_filter] %>$scope.filter_config = { list: API.all("<%= resource.pluralize.underscore %>") }; $scope.<%= resource.pluralize.underscore %> = [];<% end %> <% 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.columns = [<% grid_fields.each do |name, type| %> {field:'<%= name %>', displayName: gettext('<%= name.capitalize.humanize %>')}, <% end %>]; $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.humanize %>"), 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", permission: { name: "create", model: "<%= resource.classify %>" }, route: "#<%= resource_url %>/new" },<% unless options[:no_bulk] %> { title: gettext("Bulk Edit"), icon: "fa fa-edit", classes: "btn tiny yellow", permission: { name: "update", model: "<%= resource.classify %>" }, action: function(){ $scope.$apply("bulk_edit = ! bulk_edit"); }, },<% end %> { title: gettext("Duplicate"), icon: "fa fa-files-o", classes: "btn tiny red", permission: { name: "create", model: "<%= resource.classify %>" }, action: function(){ var selected = _.find($scope.<%= resource.pluralize.underscore %>, function(x){ return x.is_selected === true; }); if (selected === undefined ) { error_message(gettext("You should only select one item to copy.")); } else { $location.path("/<%= resource.pluralize.underscore %>/-" + selected.id + "/edit"); } } } ];<% 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()]; }, 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); }, function(data){ catch_error(data); }); }; <% unless options[:no_filter] %>/*<% end %> API.all("<%= resource.pluralize.underscore %>").getList() .then(function(data){ $scope.<%= resource.pluralize.underscore %> = data; }, function(data){ catch_error(data); }); <% unless options[:no_filter] %>*/<% end %> }]); <%= resource.pluralize %>.controller("Add<%= resource %>Controller", ["Restangular", "$scope", "$location", "$routeParams", "gettext", "catch_error", function(API, $scope, $location, $routeParams, gettext, catch_error){ $scope.select2options = {}; $scope.editing = false;<% if any_tabs? %> $scope.current_tab = 1; $scope.activate_tab = function(tab, $event){ $scope.current_tab = tab; };<% end %> $scope.obj_id = null; var is_copy = false; <% fields.each do |name, type| %><% if ["belongs_to", "in", "has_many"].include? type %> $scope.<%= name %>_data = { type: '<%= type %>',<% if type == "in" %> choices: _.sortBy([<% type.to.split(",").each do |choice| %>{name: "<%= choice.underscore %>", title: "<%= choice.humanize %>"},<% end %>]),<% else %> to: '<%= type.to %>',<% end %> name: '<%= name %>' };<% end %><% end %> <% fields.each do |name, type| %><% if type == "in" %>$scope.<%= name %>_choices = _.sortBy([<% type.to.split(",").each do |choice| %> {name: "<%= choice.underscore %>", title: "<%= choice.humanize %>"},<% end %> ], "title");<% end %><% end %> if( "id" in $routeParams ){ $scope.obj_id = $routeParams.id; $scope.editing = true; if ($scope.obj_id < 0) { is_copy = true; $scope.obj_id = $scope.obj_id * -1; } 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", "in"].include? field_type %><%= "to_#{field_type}(data.#{field_name})" %><% elsif field_type == "belongs_to" %>data.<%= field_name %>.id<% else %>data.<%= field_name %><% end %>;<% end %> }, function(data){ catch_error(data); }); } $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){ $("small.error").html(""); $("small.error").removeClass("error"); 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 %> __res__: 0 }}; if (($scope.obj_id) && (is_copy === false)) { 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 %>"); } }, function(data){ catch_error(data); }); } else { 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 %>"); } }, function(data){ catch_error(data); }); } }; }]); <% unless options[:menu].empty? %> <%= resource.pluralize %>.controller("<%= resource %>MenuController", ["gettext", function(gettext){ this.menu_items = [<% options[:menu].split(",").each do |menu| %><% title, url, model = parse_menu(menu) %> {title: gettext("<%= title %>"), url: "<%= url %>"<% if model %>, permission: {action: "read", model: "<%= model %>"}<% end %>},<% end %> ]; }]);<% end %>