gitprep / templates / settings.html.ep /
9ec4f15 11 years ago
1 contributor
305 lines | 9.552kb
<%
  # API
  my $api = gitprep_api;
  
  # Parameters
  my $op = param('op') || '';
  my $user = param('user') || '';
  
  # Authentication
  unless ($api->logined($user)) {
    $self->redirect_to('/');
    $self->finish_rendering;
    return;
  }
  
  # Rename project
  my $git = app->git;
  if ($op eq 'rename-project') {
  
    # Validation
    my $params = $api->params;
    my $rule = [
      user => [
        'user_name'
      ],
      project => [
        'project_name'
      ],
      'renamed-project' => [
        'project_name'
      ]
    ];
    my $vresult = app->validator->validate($params, $rule);
    
    if ($vresult->is_ok) {
      # Valida parameters
      my $data = $vresult->data;
      my $user = $data->{user};
      my $project = $data->{project};
      my $renamed_project = $data->{'renamed-project'};
      
      # Rename
      my $manager = app->manager;
      my $error = $manager->rename_project($user, $project, $renamed_project);
      if (ref $error) {
        $self->render(json => {ok => 0, message => $error->{message}});
      }
      else {
        $self->render(json => {ok => 1});
      }
      return $self->res->body;
    }
    else {
      $self->render(json => {ok => 0, message => 'Invalid Parameters'});
    }
  }
  
  # Change description
  elsif ($op eq 'change_description') {
    my $description = param('description');
    $description = '' unless defined $description;
    
    $git->description($user, $project, $description);
    $self->render(json => {ok => 1});
    return $self->res->body;
  }
  
  # Delete project
  elsif ($op eq 'delete-project') {
  
    # Validation
    my $params = $api->params;
    my $rule = [
      user => [
        'user_name'
      ],
      project => [
        'project_name'
      ]
    ];
    my $vresult = app->validator->validate($params, $rule);
    
    # Delete project
    if ($vresult->is_ok) {
      my $data = $vresult->data;
      my $user = $data->{user};
      my $project = $data->{project};
      
      eval { app->manager->delete_project($user, $project) };
      if ($@) {
        app->log->fatal($@);
        $self->render(json => {ok => 0, message => 'Internal error'});
      }
      else {
        $self->render(json => {ok => 1});
      }
    }
    else {
      $self->render(json => {ok => 0, message => 'Invalid paramter'});
    }
    
    return $self->res->body;
  }
%>

% layout 'common';
  
  %= javascript begin
    
    $(document).ready(function () {
    
      // Rename project name
      $('#rename').on('click', function () {
        var renamed_project = $('input[name="renamed-project"]').val();
        
        var url = "<%= url_for %>";
        var data = {
          op : "rename-project",
          user: "<%= $user %>",
          project: "<%= $project %>",
          'renamed-project': renamed_project
        };
        $.post(url, data, function (result) {
          if (result.ok) {
            location.href = "<%= url_for("/$user") %>" + '/' + renamed_project;
          }
          else {
            $('#modal-message-text').text('Rename failed:' + result.message);
            $('#modal-message').modal('show');
          }
        });
      });
      
      // Change description
      $('a[href="#description"]').on('click', function () {
        var description = $('input[name="description"]').val();
        var url = "<%= url_for %>?op=change_description&description=" + description;
        $.post(url, function (result) {
          if (result.ok) {
            $('#modal-message-text').text('Description is changed');
            $('#modal-message').modal('show');
          }
        });
      });
      
      // Check matching deleted project
      $('input[name="deleted-project"]').on('keyup', function () {
        var deleted_project = $(this).val();
        var project = "<%= $project %>";
        
        if (deleted_project == project) {
          $('#delete').attr('class', 'btn btn-danger')
            .removeAttr('disabled');
        }
        else {
          $('#delete').attr('class', 'btn btn-danger disabled')
            .attr('disabled', 'disabled');
        }
      });
      
      // Delete project
      $('#delete').on('click', function () {
        var deleted_project = $('input[name="deleted-project"]').val();
        var url = "<%= url_for %>";
        var data = {
          op : "delete-project",
          user: "<%= $user %>",
          project: "<%= $project %>",
          "deleted-project" : deleted_project,
        };
        $.post(url, data, function (result) {
          if (result.ok) {
            location.href = "<%= url_for("/$user")->query(deleted_project => $project) %>";
          }
          else {
            $('#modal-message-text').text('Error:' + result.message);
            $('#modal-message').modal('show');
          }
        });
      });
      
      // Select default branch
      var default_branch = "<%= app->manager->default_branch($user, $project) %>";
      $('select[name="default_branch"]').val(default_branch);
    });
  
  % end
  
  %= include '/include/header';
  
  <div class="container">
    %= include '/include/project_header';
    
    <div style="margin-bottom:20px">
      <div class="border-gray bk-gray-light" style="padding-left:5px">
        <h4>
          Settings
        </h4>
      </div>
      <div class="border-gray" style="padding:5px;border-top:none">
        <div >Repository Name</div>
        <div>
          %= text_field 'renamed-project' => $project, style => 'margin-top:9px';
          <a href="#rename-confirm" role="button" class="btn" data-toggle="modal">
            Rename
          </a>
        </div>
      </div>
      <div class="border-gray" style="padding:5px;border-top:none">
        <div >Description</div>
        <div>
          % my $description = $git->description($user, $project);
          % $description = '' unless defined $description;
          %= text_field 'description' => $description, class => 'span8', style => 'margin-top:9px';
          <a class="btn" href="#description">Save</a>
        </div>
        <div id="description-success" class="alert alert-success" style="width:150px;display:none">
          <button type="button" class="close" data-dismiss="alert">&times;</button>
          Savaed!
        </div>
      </div>
      <div class="border-gray" style="padding:5px;border-top:none">
        Default Branch
        % my $branches = $git->branches($user, $project);
        % my $branch_names = [map { $_->{name} } @$branches];
        % push @$branch_names, app->manager->default_branch($user, $project) unless @$branch_names;
        %= select_field 'default_branch' => $branch_names, style => 'margin-top:5px';
      </div>
    </div>
    
    <div>
      <div class="border-gray bk-gray-light" style="background:red;padding-left:5px">
        <h4 style="color:white">Danger Zone</h4>
      </div>
      <div class="border-gray" style="padding:5px;border-top:none">
        <div><b>Delete this repository</b></div>
        <span class="muted">
          Once you delete a repository, there is no going back.
        </span>
        <a style="color:red" href="#delete-confirm" role="button" class="btn" data-toggle="modal">
          Delete this repository
        </a>
    </div>
  </div>
  
  <div id="modal-message" class="modal hide">
    <div class="modal-header">
      <div id="modal-message-text" style="font-weight:bold"></div>
    </div>
    <div class="modal-body">
      <button class="btn" data-dismiss="modal" aria-hidden="true">OK</button>
    </div>
  </div>

  <div id="rename-confirm" class="modal hide" tabindex="-1" role="dialog" aria-labelledby="rename-confirm-label" aria-hidden="true">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
      <div style="font-weight:bold">Are you sure you want to rename?</div>
    </div>
    <div class="modal-body">
      <p>
        Unexpected bad things will happen if you don't read this
      </p>
      <ul>
        <li>
          We will not set up any redirects from the old location
        </li>
        <li>
          You will need to update your local repositories to point to the new location
        </li>
      </ul>
    </div>
    <div class="modal-footer">
      <button id="rename" class="btn" data-dismiss="modal" aria-hidden="true">
        I understand, rename this repository
      </button>
    </div>
  </div>

  <div id="delete-confirm" class="modal hide" tabindex="-1" role="dialog" aria-labelledby="delete-confirm-label" aria-hidden="true">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
      <div style="font-weight:bold">Are you ABSOLUTELY sure?</div>
    </div>
    <div class="modal-body">
      <p>
        Unexpected bad things will happen if you don't read this.
      </p>
      <p>
        This action <b>CANNOT</b> be undone. This will delete the <b><%= "$user/$project" %></b>
        repository, wiki, issues, and comments permanently.
      </p>
      <p>
        Please type in the name of the repository(<b><%= $project %></b>) to confirm.
      </p>
      %= text_field 'deleted-project', class => 'span5';
    </div>
    <div class="modal-footer">
      <button id="delete" class="btn btn-danger disabled" disabled data-dismiss="modal" aria-hidden="true">
        I understand the consequences, delete this repository
      </button>
    </div>
  </div>

  %= include '/include/footer';