Rule.vue 5.71 KB
Newer Older
1
<template>
David Foucher's avatar
David Foucher committed
2
  <div id="Rule">
3
    <div class="mb-5">
4 5
      <div class="row">
        <div  class="col-md-6 col-sm-12 col-xs-12">
6 7 8
          <h4>
            <span>{{ this.name }}</span>
          </h4>
9
        </div>
10 11 12 13
        <div class="col-md-6 col-sm-12 col-xs-12">
          <h4 v-show="isEditMode" class="pull-right"><em>Édition de la règle</em></h4>
          <input  v-show="!isEditMode" @click="edit" type="button" class="btn btn-outline-success pull-right" value="Soumettre une modification"/>
          <!-- TODO: show gitlab link of modification if exists -->
14 15 16 17 18 19 20 21 22 23 24
        </div>
      </div>
    </div>
    <div v-show="!isEditMode">
      <!-- the demo root element -->
      <ul id="demo">
        <TreeItem class="item" :item="this.ruleTree"></TreeItem>
      </ul>
    </div>
    <div v-show="isEditMode">
      <!-- rule-editor ref="editor" :rawRule="ruleToEdit"></rule-editor -->
25 26 27 28
      <div class="container">
        <div class="row mb-3">
          <label for="comment" class="mb-2"><u>Résumé de la modification</u></label>
          <textarea v-model="comment"></textarea>
29
        </div>
30 31 32 33 34 35 36 37 38 39 40
        <div class="row mb-3">
          <label for="content"><u>Contenu de la règle</u></label>
          <textarea v-model="content" class="mb-3"></textarea>
        </div>
        <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>
41 42 43
        </div>
      </div>
    </div>
44
  </div>
45

46 47
</template>
<script>
48 49 50 51 52 53 54 55
  import TreeItem from './TreeItem.vue';

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

David Foucher's avatar
David Foucher committed
56
  export default {
57
    name: 'Rule',
58 59 60
    components: {
      TreeItem,
    },
61
    props: ['name', 'data'],
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    data: function(){
      return {
        ruleData: this.data,
        content: '',
        comment: '',
        isEditMode: false,
      }
    },
    computed: {
      ruleTree: function() {
        return this.toTree(this.ruleData.split('\n'));
      },
      ruleToEdit: function() {
        return this.ruleData;
      },
    },
78
    methods: {
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
      edit: function () {
        this.content = this.ruleToEdit;
        this.isEditMode=!this.isEditMode;
      },
      closeEdit: function () {
        this.content = this.ruleToEdit;
        this.comment = "";
        this.isEditMode=!this.isEditMode;
      },
      save: function() {
        this.ruleData = this.content
        const postData = {
          title: this.name,
          comment: this.comment,
          content: this.content,
          filename: 'trefle/config/rules/' + this.data.path
        }

        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
              this.rules[this.activeRuleName]['data'] = this.data
              this.rules[this.activeRuleName]['gitlab'] = {'commit': commit}
              return this.isEditMode=!this.isEditMode
          });
      },
      toTree: function (lines) { // eslint-disable-line no-unused-vars
        var root= new Node('Règle');
        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;
145
      }
146
    },
David Foucher's avatar
David Foucher committed
147 148
  }
</script>
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
<style scoped>

  pre strong {
    font-family: 'mono';
    font-weight: bold;
  }
  pre .comment {
    font-family: 'mono';
    font-style: italic;
    color: #666;
  }
  pre .keyword {
    font-family: 'mono';
    font-weight: 600;
  }
  pre .constant {
    font-family: 'mono';
    font-weight: 600;
    text-decoration: underline dotted;
    cursor: help;
  }
  pre a,
  pre .string,
  pre .number {
    font-family: 'mono';
    font-weight: 600;
  }
  pre code {
    display: block;
    padding-left: 1em;
    font-family: mono;
  }
  a.line {
    display: none;
  }
  :scope.with-lines a.line {
    display: inline-block;
    background-color: #ddd;
    cursor: pointer;
    text-align: center;
    vertical-align: middle;
  }
  :scope.with-lines a:target, :scope.with-lines a:target + code {
    font-weight: bold;
    background-color: #ccc;
  }
  :scope.with-lines pre {
    display: grid;
    grid-template-columns: 2em auto;
  }
  .tooltip .tooltip-content:after {
    /* Why? */
    bottom: -27px;
  }
</style>