<commit_before>import Ember from 'ember';
const { getOwner } = Ember;
import { transitionTo } from '../private-api';

export default Ember.Service.extend({
  overlays: Ember.inject.service('ember-overlays'),
  resourceMetadata: Ember.inject.service(),
  cardstackRouting: Ember.inject.service(),
  marks: Ember.computed.alias('overlays.marks'),

  fields: Ember.computed('marks', function() {
    return this.get('marks').filter(m => m.group === 'cardstack-fields');
  }),

  contentItems: Ember.computed('marks', function() {
    return this.get('marks').filter(m => m.group === 'cardstack-content');
  }),

  activeContentItem: Ember.computed('contentItems', function() {
    return this.get('contentItems')[0];
  }),

  _activeItemMeta: Ember.computed('activeContentItem', function() {
    let model = this.get('activeContentItem.model')
    if (model) {
      return this.get('resourceMetadata').read(model);
    }
  }),

  branch: Ember.computed.alias('_activeItemMeta.branch'),

  activeFields: Ember.computed('activeContentItem', 'fields', function() {
    let item = this.get('activeContentItem');
    if (item) {
      return this.get('fields').filter(f => f.model.content === item.model);
    } else {
      return [];
    }
  }),

  // Can tools be enabled at all? This affects whether we will offer a
  // launcher button
  available: true,

  // Tools are active when the user has hit the launcher button and
  // revealed the toolbars.
  active: false,

  // Which tab are we showing in the toolbox?
  activePanel: 'cs-composition-panel',
  activePanelChoices: [
    {
      id: 'cs-composition-panel',
      icon: {
        name: 'write',
        width: 13,
        height: 18
      },
    },
    {
      id: 'cardstack-library',
      icon: {
        name: 'archive',
        width: 22,
        height: 24
      }
    }
  ],

  // Are we viewing the current URL as a normal page in its own right,
  // or in one of its other forms (like a preview card)?
  previewFormat: 'page',
  previewFormatChoices: [
    {
      id: 'page',
      description: 'Page',
      icon: {
        name: 'page',
        width: 18,
        height: 24
      }
    },
    {
      id: 'cards',
      description: 'Card',
      icon: {
        name: 'tiles',
        width: 20,
        height: 24
      }
    }
  ],

  requestedEditing: false,

  // This is a placeholder until I integrate the auth system here.
  mayEditLive: false,

  editing: Ember.computed('requestedEditing', 'branch', function() {
    return this.get('requestedEditing') &&
      (this.get('mayEditLive') ||
       this.get('branch') !== this.get('cardstackRouting.defaultBranch'));
  }),

  init() {
    this._super();
    let priorState;
    try {
      let item = localStorage.getItem('cardstack-tools');
      if (item) {
        priorState = JSON.parse(item);
        for (let key in priorState) {
          this.set(key, priorState[key]);
        }
      }
    } catch (err) {
      // Ignored
    }
    this.persistentState = priorState || {};


    /* --  Ephermal state -- */

    // a field is highlighted when we're drawing a blue border around it
    this.highlightedFieldId = null;

    // a field is opened when the user is actively editing it
    this.openedFieldId = null;
  },

  _updatePersistent(key, value) {
    this.persistentState[key] = value;
    localStorage.setItem('cardstack-tools', JSON.stringify(this.persistentState));
    this.set(key, value);
  },

  setPreviewFormat(mode) {
    this._updatePersistent('previewFormat', mode);
  },

  setActive(isActive) {
    this._updatePersistent('active', isActive);
  },

  setActivePanel(which) {
    this._updatePersistent('activePanel', which);
  },

  setEditing(which) {
    this._updatePersistent('requestedEditing', which);
    if (!this.get('mayEditLive') && this.get('branch') === this.get('cardstackRouting.defaultBranch')) {
      this.setBranch('draft');
    }
  },

  setBranch(which) {
    let model = this.get('activeContentItem.model')
    if (model) {
      let {
        name,
        args,
        queryParams
      } = this.get('cardstackRouting').routeFor(model.get('type'), model.get('slug'), which);
      transitionTo(getOwner(this), name, args, queryParams);
    }
  },

  openField(which) {
    this.set('openedFieldId', which ? which.id : null);
  },
<commit_msg>don't break when trying to switch branches off new-content<commit_after>import Ember from 'ember';
const { getOwner } = Ember;
import { transitionTo } from '../private-api';

export default Ember.Service.extend({
  overlays: Ember.inject.service('ember-overlays'),
  resourceMetadata: Ember.inject.service(),
  cardstackRouting: Ember.inject.service(),
  marks: Ember.computed.alias('overlays.marks'),

  fields: Ember.computed('marks', function() {
    return this.get('marks').filter(m => m.group === 'cardstack-fields');
  }),

  contentItems: Ember.computed('marks', function() {
    return this.get('marks').filter(m => m.group === 'cardstack-content');
  }),

  activeContentItem: Ember.computed('contentItems', function() {
    return this.get('contentItems')[0];
  }),

  _activeItemMeta: Ember.computed('activeContentItem', function() {
    let model = this.get('activeContentItem.model')
    if (model) {
      return this.get('resourceMetadata').read(model);
    }
  }),

  branch: Ember.computed.alias('_activeItemMeta.branch'),

  activeFields: Ember.computed('activeContentItem', 'fields', function() {
    let item = this.get('activeContentItem');
    if (item) {
      return this.get('fields').filter(f => f.model.content === item.model);
    } else {
      return [];
    }
  }),

  // Can tools be enabled at all? This affects whether we will offer a
  // launcher button
  available: true,

  // Tools are active when the user has hit the launcher button and
  // revealed the toolbars.
  active: false,

  // Which tab are we showing in the toolbox?
  activePanel: 'cs-composition-panel',
  activePanelChoices: [
    {
      id: 'cs-composition-panel',
      icon: {
        name: 'write',
        width: 13,
        height: 18
      },
    },
    {
      id: 'cardstack-library',
      icon: {
        name: 'archive',
        width: 22,
        height: 24
      }
    }
  ],

  // Are we viewing the current URL as a normal page in its own right,
  // or in one of its other forms (like a preview card)?
  previewFormat: 'page',
  previewFormatChoices: [
    {
      id: 'page',
      description: 'Page',
      icon: {
        name: 'page',
        width: 18,
        height: 24
      }
    },
    {
      id: 'cards',
      description: 'Card',
      icon: {
        name: 'tiles',
        width: 20,
        height: 24
      }
    }
  ],

  requestedEditing: false,

  // This is a placeholder until I integrate the auth system here.
  mayEditLive: false,

  editing: Ember.computed('requestedEditing', 'branch', function() {
    return this.get('requestedEditing') &&
      (this.get('mayEditLive') ||
       this.get('branch') !== this.get('cardstackRouting.defaultBranch'));
  }),

  init() {
    this._super();
    let priorState;
    try {
      let item = localStorage.getItem('cardstack-tools');
      if (item) {
        priorState = JSON.parse(item);
        for (let key in priorState) {
          this.set(key, priorState[key]);
        }
      }
    } catch (err) {
      // Ignored
    }
    this.persistentState = priorState || {};


    /* --  Ephermal state -- */

    // a field is highlighted when we're drawing a blue border around it
    this.highlightedFieldId = null;

    // a field is opened when the user is actively editing it
    this.openedFieldId = null;
  },

  _updatePersistent(key, value) {
    this.persistentState[key] = value;
    localStorage.setItem('cardstack-tools', JSON.stringify(this.persistentState));
    this.set(key, value);
  },

  setPreviewFormat(mode) {
    this._updatePersistent('previewFormat', mode);
  },

  setActive(isActive) {
    this._updatePersistent('active', isActive);
  },

  setActivePanel(which) {
    this._updatePersistent('activePanel', which);
  },

  setEditing(which) {
    this._updatePersistent('requestedEditing', which);
    if (!this.get('mayEditLive') && this.get('branch') === this.get('cardstackRouting.defaultBranch')) {
      this.setBranch('draft');
    }
  },

  setBranch(which) {
    let model = this.get('activeContentItem.model')
    if (model && model.get('slug')) {
      let {
        name,
        args,
        queryParams
      } = this.get('cardstackRouting').routeFor(model.get('type'), model.get('slug'), which);
      transitionTo(getOwner(this), name, args, queryParams);
    }
  },

  openField(which) {
    this.set('openedFieldId', which ? which.id : null);
  },

{'fraction_non_alphanumeric': 0.14805045871559633, 'fraction_numerical': 0.0038990825688073397, 'mean_word_length': 2.7639188605955978, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5443967', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2692, 'n_words': 619}