Code coverage report for src/components/task-plan/plan-mixin.coffee

Statements: 41.86% (36 / 86)      Branches: 25.81% (8 / 31)      Functions: 33.33% (6 / 18)      Lines: 41.86% (36 / 86)      Ignored: none     

All files » src/components/task-plan/ » plan-mixin.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 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 176 177 178                           48× 48× 48×                             48×     48×                                                                                                                                                                                                   22× 22× 22×   22× 22× 22×   22× 22× 15×       22×     22×         22×          
var CALENDAR_DATE_FORMAT, CloseButton, PlanMixin, React, S, TaskPlanActions, TaskPlanStore, TimeStore, TutorDialog, moment, ref;
 
React = require('react');
 
ref = require('../../flux/task-plan'), TaskPlanStore = ref.TaskPlanStore, TaskPlanActions = ref.TaskPlanActions;
 
TimeStore = require('../../flux/time').TimeStore;
 
CloseButton = require('openstax-react-components').CloseButton;
 
TutorDialog = require('../tutor-dialog');
 
S = require('../../helpers/string');
 
moment = require('moment');
 
CALENDAR_DATE_FORMAT = 'YYYY-MM-DD';
 
PlanMixin = {
  contextTypes: {
    router: React.PropTypes.func
  },
  getInitialState: function() {
    var isSavedPlanVisibleToStudent;
    isSavedPlanVisibleToStudent = TaskPlanStore.isVisibleToStudents(this.props.id || this.props.planId);
    return {
      isVisibleToStudents: isSavedPlanVisibleToStudent,
      isEditable: TaskPlanStore.isEditable(this.props.id || this.props.planId)
    };
  },
  updateIsVisibleAndIsEditable: function() {
    var isEditable, isVisibleToStudents;
    isVisibleToStudents = TaskPlanStore.isVisibleToStudents(this.props.id || this.props.planId);
    isEditable = TaskPlanStore.isEditable(this.props.id || this.props.planId);
    return this.setState({
      isVisibleToStudents: isVisibleToStudents,
      isEditable: isEditable
    });
  },
  componentWillMount: function() {
    return TaskPlanStore.on('publish-queued', this.updateIsVisibleAndIsEditable);
  },
  componentWillUnmount: function() {
    return TaskPlanStore.off('publish-queued', this.updateIsVisibleAndIsEditable);
  },
  setTitle: function(title) {
    var id;
    id = this.props.id;
    return TaskPlanActions.updateTitle(id, title);
  },
  showSectionTopics: function() {
    return this.setState({
      showSectionTopics: true,
      savedTopics: TaskPlanStore.getTopics(this.props.id),
      savedExercises: TaskPlanStore.getExercises(this.props.id)
    });
  },
  cancelSelection: function() {
    TaskPlanActions.updateTopics(this.props.id, this.state.savedTopics);
    TaskPlanActions.updateExercises(this.props.id, this.state.savedExercises);
    return this.hideSectionTopics();
  },
  hideSectionTopics: function() {
    return this.setState({
      showSectionTopics: false
    });
  },
  publish: function() {
    var id, saveable;
    id = this.props.id;
    saveable = TaskPlanStore.isValid(id);
    if (saveable) {
      TaskPlanActions.publish(id);
    }
    return this.save();
  },
  save: function() {
    var id, saveable;
    id = this.props.id;
    saveable = TaskPlanStore.isValid(id);
    Iif (saveable) {
      TaskPlanActions.saved.addListener(this.saved);
      return TaskPlanActions.save(id);
    } else {
      return this.setState({
        invalid: true
      });
    }
  },
  saved: function() {
    var courseId;
    courseId = this.props.courseId;
    TaskPlanActions.saved.removeListener('change', this.saved);
    TaskPlanStore.isLoading(this.props.id);
    return this.goBackToCalendar();
  },
  cancel: function() {
    var courseId, id, ref1;
    ref1 = this.props, id = ref1.id, courseId = ref1.courseId;
    if (!TaskPlanStore.hasChanged(id)) {
      return this.reset();
    } else {
      return TutorDialog.show({
        title: 'Unsaved Changes',
        body: 'You will lose unsaved changes if you continue.'
      }).then((function(_this) {
        return function() {
          return _this.reset();
        };
      })(this));
    }
  },
  reset: function() {
    var courseId, id, ref1;
    ref1 = this.props, id = ref1.id, courseId = ref1.courseId;
    TaskPlanActions.resetPlan(id);
    return this.goBackToCalendar();
  },
  getBackToCalendarParams: function() {
    var calendarRoute, courseId, date, dueAt, id, planId, ref1;
    ref1 = this.props, id = ref1.id, courseId = ref1.courseId;
    calendarRoute = 'calendarByDate';
    dueAt = TaskPlanStore.getFirstDueDate(id) || this.context.router.getCurrentQuery().due_at;
    if (dueAt != null) {
      date = moment(dueAt).format(CALENDAR_DATE_FORMAT);
    } else {
      date = moment(TimeStore.getNow()).format(CALENDAR_DATE_FORMAT);
    }
    if (!(TaskPlanStore.isNew(id) || TaskPlanStore.isPublishing(id) || TaskPlanStore.isDeleteRequested(id))) {
      calendarRoute = 'calendarViewPlanStats';
      planId = id;
    }
    return {
      to: calendarRoute,
      params: {
        courseId: courseId,
        date: date,
        planId: planId
      }
    };
  },
  goBackToCalendar: function() {
    var params, ref1, to;
    ref1 = this.getBackToCalendarParams(), to = ref1.to, params = ref1.params;
    return this.context.router.transitionTo(to, params);
  },
  builderHeader: function(type, label) {
    var closeBtn, headerSpan, headerText, id;
    Eif (label == null) {
      label = 'Assignment';
    }
    id = this.props.id;
    Eif (label) {
      label = " " + label;
    }
    type = S.capitalize(type);
    if (TaskPlanStore.isNew(id)) {
      headerText = "Add " + type + label;
    } else Iif (TaskPlanStore.isDeleteRequested(id)) {
      headerText = type + " is being deleted";
    } else {
      headerText = "Edit " + type + label;
    }
    headerSpan = React.createElement("span", {
      "key": 'header-text'
    }, headerText);
    closeBtn = React.createElement(CloseButton, {
      "key": 'close-button',
      "className": 'pull-right',
      "onClick": this.cancel
    });
    return [headerSpan, closeBtn];
  }
};
 
module.exports = PlanMixin;