BigW Consortium Gitlab

merge_conflict_data_provider.js.es6 8.65 KB
Newer Older
1 2 3 4
const HEAD_HEADER_TEXT    = 'HEAD//our changes';
const ORIGIN_HEADER_TEXT  = 'origin//their changes';
const HEAD_BUTTON_TITLE   = 'Use ours';
const ORIGIN_BUTTON_TITLE = 'Use theirs';
5

6 7

class MergeConflictDataProvider {
8 9 10 11 12 13

  getInitialData() {
    const diffViewType = $.cookie('diff_view');

    return {
      isLoading      : true,
14
      hasError       : false,
15 16
      isParallel     : diffViewType === 'parallel',
      diffViewType   : diffViewType,
17
      isSubmitting   : false,
18 19 20 21 22 23 24
      conflictsData  : {},
      resolutionData : {}
    }
  }


  decorateData(vueInstance, data) {
25
    this.vueInstance = vueInstance;
26

27 28 29 30 31 32
    if (data.type === 'error') {
      vueInstance.hasError = true;
      data.errorMessage = data.message;
    }
    else {
      data.shortCommitSha = data.commit_sha.slice(0, 7);
33
      data.commitMessage  = data.commit_message;
34 35 36 37 38

      this.setParallelLines(data);
      this.setInlineLines(data);
      this.updateResolutionsData(data);
    }
39 40

    vueInstance.conflictsData = data;
41
    vueInstance.isSubmitting = false;
42 43 44

    const conflictsText = this.getConflictsCount() > 1 ? 'conflicts' : 'conflict';
    vueInstance.conflictsData.conflictsText = conflictsText;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  }


  updateResolutionsData(data) {
    const vi = this.vueInstance;

    data.files.forEach( (file) => {
      file.sections.forEach( (section) => {
        if (section.conflict) {
          vi.$set(`resolutionData['${section.id}']`, false);
        }
      });
    });
  }


  setParallelLines(data) {
    data.files.forEach( (file) => {
63 64 65
      file.filePath  = this.getFilePath(file);
      file.iconClass = `fa-${file.blob_icon}`;
      file.blobPath  = file.blob_path;
66 67
      file.parallelLines = [];
      const linesObj = { left: [], right: [] };
68 69 70 71 72

      file.sections.forEach( (section) => {
        const { conflict, lines, id } = section;

        if (conflict) {
73 74
          linesObj.left.push(this.getOriginHeaderLine(id));
          linesObj.right.push(this.getHeadHeaderLine(id));
75 76 77 78
        }

        lines.forEach( (line) => {
          const { type } = line;
79

80 81
          if (conflict) {
            if (type === 'old') {
82
              linesObj.left.push(this.getLineForParallelView(line, id, 'conflict'));
83 84
            }
            else if (type === 'new') {
85
              linesObj.right.push(this.getLineForParallelView(line, id, 'conflict', true));
86 87 88
            }
          }
          else {
89
            const lineType = type || 'context';
90

91 92
            linesObj.left.push (this.getLineForParallelView(line, id, lineType));
            linesObj.right.push(this.getLineForParallelView(line, id, lineType, true));
93 94
          }
        });
95

96
        this.checkLineLengths(linesObj);
97
      });
98 99 100

      for (let i = 0, len = linesObj.left.length; i < len; i++) {
        file.parallelLines.push([
101 102
          linesObj.right[i],
          linesObj.left[i]
103 104 105
        ]);
      }

106 107 108 109
    });
  }


110 111
  checkLineLengths(linesObj) {
    let { left, right } = linesObj;
112 113 114 115 116

    if (left.length !== right.length) {
      if (left.length > right.length) {
        const diff = left.length - right.length;
        for (let i = 0; i < diff; i++) {
117
          right.push({ lineType: 'emptyLine', richText: '' });
118 119 120 121 122
        }
      }
      else {
        const diff = right.length - left.length;
        for (let i = 0; i < diff; i++) {
123
          left.push({ lineType: 'emptyLine', richText: '' });
124 125 126 127 128 129
        }
      }
    }
  }


130 131
  setInlineLines(data) {
    data.files.forEach( (file) => {
132 133 134
      file.iconClass   = `fa-${file.blob_icon}`;
      file.blobPath    = file.blob_path;
      file.filePath    = this.getFilePath(file);
135 136 137 138 139 140 141 142 143 144 145 146 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
      file.inlineLines = []

      file.sections.forEach( (section) => {
        let currentLineType = 'new';
        const { conflict, lines, id } = section;

        if (conflict) {
          file.inlineLines.push(this.getHeadHeaderLine(id));
        }

        lines.forEach( (line) => {
          const { type } = line;

          if ((type === 'new' || type === 'old') && currentLineType !== type) {
            currentLineType = type;
            file.inlineLines.push({ lineType: 'emptyLine', richText: '' });
          }

          this.decorateLineForInlineView(line, id, conflict);
          file.inlineLines.push(line);
        })

        if (conflict) {
          file.inlineLines.push(this.getOriginHeaderLine(id));
        }
      });
    });
  }


  handleSelected(sectionId, selection) {
    const vi = this.vueInstance;

    vi.resolutionData[sectionId] = selection;
    vi.conflictsData.files.forEach( (file) => {
      file.inlineLines.forEach( (line) => {
        if (line.id === sectionId && (line.hasConflict || line.isHeader)) {
          this.markLine(line, selection);
        }
      });

176 177 178 179 180 181
      file.parallelLines.forEach( (lines) => {
        const left         = lines[0];
        const right        = lines[1];
        const hasSameId    = right.id === sectionId || left.id === sectionId;
        const isLeftMatch  = left.hasConflict || left.isHeader;
        const isRightMatch = right.hasConflict || right.isHeader;
182

183 184 185 186 187
        if (hasSameId && (isLeftMatch || isRightMatch)) {
          this.markLine(left, selection);
          this.markLine(right, selection);
        }
      })
188 189 190 191 192 193 194 195 196 197 198 199 200 201
    });
  }


  updateViewType(newType) {
    const vi = this.vueInstance;

    if (newType === vi.diffView || !(newType === 'parallel' || newType === 'inline')) {
      return;
    }

    vi.diffView   = newType;
    vi.isParallel = newType === 'parallel';
    $.cookie('diff_view', newType); // TODO: Make sure that cookie path added.
202
    $('.content-wrapper .container-fluid').toggleClass('container-limited');
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  }


  markLine(line, selection) {
    if (selection === 'head' && line.isHead) {
      line.isSelected   = true;
      line.isUnselected = false;
    }
    else if (selection === 'origin' && line.isOrigin) {
      line.isSelected   = true;
      line.isUnselected = false;
    }
    else {
      line.isSelected   = false;
      line.isUnselected = true;
    }
  }


  getConflictsCount() {
    return Object.keys(this.vueInstance.resolutionData).length;
  }


  getResolvedCount() {
    let  count = 0;
    const data = this.vueInstance.resolutionData;

    for (const id in data) {
      const resolution = data[id];
      if (resolution) {
        count++;
      }
    }

    return count;
  }


242 243 244 245 246 247 248 249
  isReadyToCommit() {
    const { conflictsData, isSubmitting } = this.vueInstance
    const allResolved = this.getConflictsCount() === this.getResolvedCount();
    const hasCommitMessage = $.trim(conflictsData.commitMessage).length;

    return !isSubmitting && hasCommitMessage && allResolved;
  }

250 251 252 253 254 255 256

  getCommitButtonText() {
    const initial = 'Commit conflict resolution';
    const inProgress = 'Committing...';
    const vue = this.vueInstance;

    return vue ? vue.isSubmitting ? inProgress : initial : initial;
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  }


  decorateLineForInlineView(line, id, conflict) {
    const { type }    = line;
    line.id           = id;
    line.hasConflict  = conflict;
    line.isHead       = type === 'new';
    line.isOrigin     = type === 'old';
    line.hasMatch     = type === 'match';
    line.richText     = line.rich_text;
    line.isSelected   = false;
    line.isUnselected = false;
  }

272 273 274 275 276 277 278 279
  getLineForParallelView(line, id, lineType, isHead) {
    const { old_line, new_line, rich_text } = line;
    const hasConflict = lineType === 'conflict';

    return {
      id,
      lineType,
      hasConflict,
280 281 282 283 284 285 286 287
      isHead       : hasConflict && isHead,
      isOrigin     : hasConflict && !isHead,
      hasMatch     : lineType === 'match',
      lineNumber   : isHead ? new_line : old_line,
      section      : isHead ? 'head' : 'origin',
      richText     : rich_text,
      isSelected   : false,
      isUnselected : false
288 289 290
    }
  }

291 292 293 294 295

  getHeadHeaderLine(id) {
    return {
      id          : id,
      richText    : HEAD_HEADER_TEXT,
296
      buttonTitle : HEAD_BUTTON_TITLE,
297 298 299 300 301 302 303 304 305 306 307 308 309 310
      type        : 'new',
      section     : 'head',
      isHeader    : true,
      isHead      : true,
      isSelected  : false,
      isUnselected: false
    }
  }


  getOriginHeaderLine(id) {
    return {
      id          : id,
      richText    : ORIGIN_HEADER_TEXT,
311
      buttonTitle : ORIGIN_BUTTON_TITLE,
312 313 314 315 316 317 318 319 320
      type        : 'old',
      section     : 'origin',
      isHeader    : true,
      isOrigin    : true,
      isSelected  : false,
      isUnselected: false
    }
  }

321 322 323 324 325 326

  handleFailedRequest(vueInstance, data) {
    vueInstance.hasError = true;
    vueInstance.conflictsData.errorMessage = 'Something went wrong!';
  }

327 328 329

  getCommitData() {
    return {
330
      commit_message: this.vueInstance.conflictsData.commitMessage,
331 332 333 334
      sections: this.vueInstance.resolutionData
    }
  }

335 336 337

  getFilePath(file) {
    const { old_path, new_path } = file;
338
    return old_path === new_path ? new_path : `${old_path} → ${new_path}`;
339 340
  }

341
}