Rule.vue 6.43 KB
Newer Older
1
<template>
David Foucher's avatar
David Foucher committed
2
  <div id="Rule">
3
    <div class="mb-5">
4
      <div class="row mb-3">
5
        <div  class="col-md-6 col-sm-12 col-xs-12">
6 7 8
          <h5>
            <span  style="vertical-align:-30%" >{{ this.name }}</span>
          </h5>
9
        </div>
10
        <div class="col-md-6 col-sm-12 col-xs-12">
11
          <h4 v-if="isEditMode" class="pull-right"><em>Modification de la règle</em></h4>
12
          <input  v-else @click="edit" type="button" class="main-button btn pull-right" value="Soumettre une modification"/>
13
          <!-- TODO: show gitlab link of modification if exists -->
14 15
        </div>
      </div>
16 17 18 19 20
      <div v-show="!isEditMode" class="row">
        <div class="col-md-12">
          <span v-html="printRulePath"></span>
        </div>
      </div>
21 22
    </div>
    <div v-show="!isEditMode">
23 24
      <ul>
        <TreeItem class="item" :item="this.ruleTree" :rootElement="true" :rulePath="this.rulePath"></TreeItem>
25 26 27
      </ul>
    </div>
    <div v-show="isEditMode">
28 29
      <div class="container">
        <div class="row mb-3">
30
          <label for="content"><u>Contenu de la règle</u></label>
31
          <textarea id="content" v-model="content" rows="15" class="mb-3"></textarea>
32
        </div>
33
        <div class="row mb-3">
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
          <label for="comment" class="mb-2"><u>Résumé de la modification</u> * </label>
          <textarea id="comment" v-model="comment" rows="3"></textarea>
        </div>
        <div class="row mb-3">
          <div class="col-md-12 pl-0">
            <label for="contributor_email" class="pb-2"><u>Votre email</u> *</label>
          </div>
          <div class="col-md-12 pl-0">
            <input id="contributor_email" v-model="contributor_email" type="text"/>
          </div>
        </div>
        <div class="row mb-3">
          <div class="col-md-12 pl-0">
            <label for="modification_key" class="mb-2">
              <u>Clé de modification</u> * 
            </label>
          </div>
          <div class="col-md-12 pl-0">
            <input id="modification_key" v-model="user_modification_key" type="text" class="form-input" :class="{badKeyClass: badKey}"/><br>
            <span v-if="badKey" class="text-danger font-weight-light">Mauvaise clé de modification, veuillez rééssayer</span>
          </div>
55
        </div>
56 57 58 59
        <div class="row mb-3">
          <label for="auth" class="mb-2"><u>Code pour soumettre le modification</u></label>
          <input type="text" v-model="auth"  />
        </div>
60 61 62 63 64 65 66
        <div class="row mb-3">
          <div class="col-md-6 pl-0">
            <input @click="closeEdit" type="button" class="btn btn-outline-danger pull-left" value="Annuler"/>
          </div>
          <div class="col-md-6 pr-0">
            <input @click="save" type="button" class="btn btn-outline-success pull-right" value="Enregistrer"/>
          </div>
67 68 69
        </div>
      </div>
    </div>
70
  </div>
71

72 73
</template>
<script>
74 75 76 77 78 79 80 81
  import TreeItem from './TreeItem.vue';

  function Node(name) {
    this.name = name;
    this.parent = null;
    this.children = [];
  }

David Foucher's avatar
David Foucher committed
82
  export default {
83
    name: 'Rule',
84 85 86
    components: {
      TreeItem,
    },
87
    props: ['name', 'data', 'path', 'printRulePath', 'rulePath'],
88 89 90 91 92
    data: function(){
      return {
        ruleData: this.data,
        content: '',
        comment: '',
93
        auth: '',
94
        isEditMode: false,
95
        badKey: false,
96 97 98
      }
    },
    computed: {
99 100 101 102
      modification_key: function() {
        // TODO récupération de la clé niveau serveur
        return "clé";
      },
103 104 105 106 107 108 109
      ruleTree: function() {
        return this.toTree(this.ruleData.split('\n'));
      },
      ruleToEdit: function() {
        return this.ruleData;
      },
    },
110
    methods: {
111 112 113 114 115 116 117
      edit: function () {
        this.content = this.ruleToEdit;
        this.isEditMode=!this.isEditMode;
      },
      closeEdit: function () {
        this.content = this.ruleToEdit;
        this.comment = "";
118
        this.auth = "";
119 120 121
        this.isEditMode=!this.isEditMode;
      },
      save: function() {
122
        if(this.user_modification_key != this.modification_key) return this.badKey = true;
123 124
        this.ruleData = this.content
        const postData = {
125 126
          author_email:this.contributor_email,
          author_name:this.contributor_email.split("@")[0],
127 128
          title: this.name,
          comment: this.comment,
129
          auth: this.auth,
130
          content: this.content,
131
          filename: 'trefle/config/rules/' + this.path
132 133 134 135 136 137 138 139
        }

        this.$http
          .post('/source/save', postData)
          .then(response => {
              var commit = {}
              commit.url = 'https://beta.pole-emploi.fr/open-source/trefle/commit/' + response.id
              commit.title = response.title
140 141
              this.$parent.rules[this.name]['data'] = this.data
              this.$parent.rules[this.name]['gitlab'] = {'commit': commit}
142 143 144 145
              return this.isEditMode=!this.isEditMode
          });
      },
      toTree: function (lines) { // eslint-disable-line no-unused-vars
146
        var root= new Node(this.name.split('.')[0]);
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        var currentIndent=-1;
        var currentNode=root;
        // we scan the rules line by line
        for (var i=0;i<lines.length;i++) {
          var line=lines[i];
          if (line.match(/^\s*$/)) continue; // Empty line, skip
          var indent=line.search(/\S|$/); // number of indenting spaces
          var newNode= new Node(line.trim());
          if (indent>currentIndent) { // New child
            // Set the new node parent
            newNode.parent=currentNode;
            // attach the new node to its parent
            currentNode.children.push(newNode);
            // Set the new current node
            currentNode=newNode;
            currentIndent=indent;
          } else if (indent<currentIndent){
            // Move up in the tree
            var level=currentIndent-indent;
            for (var j=0;j<level/4;j++) {
              // up one level
              currentNode=currentNode.parent;
              currentIndent=currentIndent-4;
            }
            newNode.parent=currentNode.parent;
            currentNode.parent.children.push(newNode); // Add a sibbling
            currentNode=newNode;
          } else {
            // Add as sibbling
            newNode.parent=currentNode.parent;
            currentNode.parent.children.push(newNode);
            currentNode=newNode;
          }
        }
        return root;
182
      }
183
    },
David Foucher's avatar
David Foucher committed
184 185
  }
</script>
186
<style scoped>
187 188 189 190 191
.badKeyClass {
  color: red;
  border: 1px solid red;
  border-radius:3px;
}
192
</style>