spacedeck_sections.js 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015
  1. /*
  2. SpacedeckSections
  3. This module contains functions dealing with Space Sections UI.
  4. */
  5. var SpacedeckSections = {
  6. data: {
  7. MAX_COLUMNS: 20,
  8. redo_stack: [],
  9. undo_stack: [],
  10. opened_dialog: "none", // which property panel to display
  11. color_options_picker: false,
  12. advanced_properties: false,
  13. embed_code_html: "",
  14. active_tool: "pointer",
  15. lightbox_artifact: {},
  16. snap_ruler_y: -1000,
  17. snap_ruler_x: -1000,
  18. minimap_width: 100,
  19. minimap_height: 200,
  20. minimap_scale: 10,
  21. scroll_left: 0,
  22. scroll_top: 0,
  23. window_width: 800,
  24. window_height: 600,
  25. bounds_margin_horiz: 0,
  26. bounds_margin_vert: 0,
  27. editing_artifact_id: null,
  28. selected_artifacts_dict: {},
  29. first_selected_artifact: null,
  30. selection_metrics: {
  31. contains_text: false,
  32. contains_images: false,
  33. contains_audio: false,
  34. contains_vectors: false,
  35. contains_shapes: false,
  36. borders_stylable: true,
  37. count: 0,
  38. x: 0,
  39. y: 0,
  40. w: 0,
  41. h: 0,
  42. x1: 0,
  43. y1: 0,
  44. x2: 0,
  45. y2: 0,
  46. style: "display:none",
  47. vector_points: [{},{}],
  48. vector_selection: false
  49. },
  50. selected_artifacts_json: "",
  51. zones: [],
  52. user_cursors: [],
  53. default_style: {}, // will be copied from active_style on startup
  54. active_style: {
  55. border_radius: 0,
  56. stroke: 0,
  57. font_family: "Avenir W01",
  58. font_size: 18,
  59. line_height: 1.5,
  60. letter_spacing: 0,
  61. stroke_color: "#000000",
  62. fill_color: "#00000000",
  63. text_color: "#000000",
  64. background_color: "#ffffff",
  65. padding: 0,
  66. padding_horz: 0,
  67. padding_vert: 0,
  68. padding_top: 0,
  69. padding_left: 0,
  70. padding_right: 0,
  71. padding_bottom: 0,
  72. margin: 0,
  73. margin_horz: 0,
  74. margin_vert: 0,
  75. margin_top: 0,
  76. margin_left: 0,
  77. margin_right: 0,
  78. margin_bottom: 0,
  79. brightness: 100,
  80. contrast: 100,
  81. opacity: 100,
  82. saturation: 100,
  83. blur: 0,
  84. hue: 0,
  85. columns: 1,
  86. column_width: 900,
  87. row_height: 0,
  88. gutter: 0,
  89. },
  90. color_picker_target: "fill_color",
  91. color_picker_saturation: 255,
  92. color_picker_value: 255,
  93. color_picker_hue: 127,
  94. color_picker_opacity: 255,
  95. swatches: [
  96. {id:0, hex:"#4a2f7e"},
  97. {id:1, hex:"#9b59b6"},
  98. {id:2, hex:"#3498db"},
  99. {id:3, hex:"#2ecc71"},
  100. {id:4, hex:"#f1c40f"},
  101. {id:5, hex:"#e67e22"},
  102. {id:6, hex:"#d55c4b"},
  103. {id:7, hex:"#6f4021"},
  104. {id:8, hex:"#ffffff"},
  105. {id:9, hex:"#95a5a6"},
  106. {id:10, hex:"#252525"},
  107. {id:11, hex:"rgba(0,0,0,0)"},
  108. ],
  109. swatches_text: [
  110. {id:1, hex:"#9b59b6"},
  111. {id:2, hex:"#3498db"},
  112. {id:3, hex:"#2ecc71"},
  113. {id:4, hex:"#f1c40f"},
  114. {id:5, hex:"#e67e22"},
  115. {id:6, hex:"#d55c4b"},
  116. {id:8, hex:"#ffffff"},
  117. {id:10, hex:"#252525"},
  118. ],
  119. fonts: [
  120. "Arial",
  121. "Courier",
  122. "Georgia",
  123. "Verdana",
  124. "Comic Sans MS",
  125. "Montserrat",
  126. "Lato",
  127. "Roboto",
  128. "Crimson Text",
  129. "EB Garamond",
  130. "Vollkorn",
  131. "Avenir W01"
  132. ],
  133. detected_text_formats: {},
  134. active_text_format_name: "Paragraph",
  135. image_search_results: [],
  136. video_search_results: [],
  137. audio_search_results: [],
  138. generic_search_query: "",
  139. media_search_target: "google",
  140. search_loading: false,
  141. viewport_zoom: 1,
  142. viewport_zoom_percent: 100,
  143. bounds_zoom: 1,
  144. current_zone_idx: -1,
  145. margin_mode: "global",
  146. padding_mode: "global",
  147. delete_artifact: "unconfirmed",
  148. color_mode: "palette",
  149. background_mode: "image",
  150. layout_mode: "layout",
  151. follow_mode: true,
  152. space_background_uploading: false,
  153. toolbar_props_x: 0,
  154. toolbar_props_y: 0,
  155. toolbar_props_in: false,
  156. toolbar_artifacts_x: "-1000px",
  157. toolbar_artifacts_y: "-1000px",
  158. toolbar_artifacts_in: false
  159. },
  160. methods: {
  161. setup_section_module: function() {
  162. // defaults -----------------------------------------------------------------------------
  163. this.default_style = _.clone(this.active_style);
  164. // keybindings --------------------------------------------------------------------------
  165. Mousetrap.bind('del', function(evt) { this.if_editable(function() {this.delete_selected_artifacts(evt);}) }.bind(this));
  166. Mousetrap.bind('backspace', function(evt) { this.if_editable(function() {this.delete_selected_artifacts(evt);}) }.bind(this));
  167. Mousetrap.bind(['command+d', 'ctrl+d' ], function(evt) { evt.preventDefault(); evt.stopPropagation(); this.if_editable(function() {this.duplicate_selected_artifacts();}) }.bind(this));
  168. Mousetrap.bind(['command+z', 'ctrl+z' ], function(evt) { this.if_editable(function() {this.undo();}) }.bind(this));
  169. Mousetrap.bind(['command+shift+z','ctrl+shift+z'], function(evt) { this.if_editable(function() {this.redo();}) }.bind(this));
  170. Mousetrap.bind(['command+a', 'ctrl+a' ], function(evt) { this.if_editable(function() {this.select_all_artifacts();}) }.bind(this));
  171. Mousetrap.bind(['command+e', 'ctrl+e' ], function(evt) { this.if_editable(function() {this.toggle_full_width();}) }.bind(this));
  172. Mousetrap.bind(['command+=', 'ctrl+=' ], function(evt) { evt.preventDefault(); evt.stopPropagation(); this.zoom_in(); }.bind(this));
  173. Mousetrap.bind(['command+-', 'ctrl+-' ], function(evt) { evt.preventDefault(); evt.stopPropagation(); this.zoom_out(); }.bind(this));
  174. Mousetrap.bind('+', function(evt) { evt.preventDefault(); evt.stopPropagation(); this.zoom_in(); }.bind(this));
  175. Mousetrap.bind('-', function(evt) { evt.preventDefault(); evt.stopPropagation(); this.zoom_out(); }.bind(this));
  176. Mousetrap.bind('up', function(evt) { this.nudge_selected_artifacts(0,-1,evt);}.bind(this));
  177. Mousetrap.bind('down', function(evt) { this.nudge_selected_artifacts(0,1,evt);}.bind(this));
  178. Mousetrap.bind('left', function(evt) { this.nudge_selected_artifacts(-1,0,evt);}.bind(this));
  179. Mousetrap.bind('right', function(evt) { this.nudge_selected_artifacts(1,0,evt);}.bind(this));
  180. Mousetrap.bind('shift+up', function(evt) { this.if_editable(function() {this.nudge_selected_artifacts(0,-10,evt);}) }.bind(this));
  181. Mousetrap.bind('shift+down', function(evt) { this.if_editable(function() {this.nudge_selected_artifacts(0,10,evt);}) }.bind(this));
  182. Mousetrap.bind('shift+left', function(evt) { this.if_editable(function() {this.nudge_selected_artifacts(-10,0,evt);}) }.bind(this));
  183. Mousetrap.bind('shift+right', function(evt) { this.if_editable(function() {this.nudge_selected_artifacts(10,0,evt);}) }.bind(this));
  184. Mousetrap.bind('space', function(evt) { this.activate_pan_tool(evt); }.bind(this));
  185. $(document).bind("beforecopy", this.handle_onbeforecopy.bind(this));
  186. $(window).bind("beforeunload", this.handle_onunload.bind(this));
  187. $(window).bind("resize", this.handle_window_resize.bind(this));
  188. },
  189. setup_watches: function() {
  190. this.$watch('active_style.stroke', function (value, mutation) {
  191. this.set_artifact_style_prop("stroke",parseInt(this.active_style.stroke));
  192. }.bind(this));
  193. this.$watch('active_style.border_radius', function (value, mutation) {
  194. this.set_artifact_style_prop("border_radius",parseInt(this.active_style.border_radius));
  195. }.bind(this));
  196. this.$watch('active_style.padding', function (value, mutation) {
  197. this.active_style.padding_horz = this.active_style.padding;
  198. this.active_style.padding_vert = this.active_style.padding;
  199. }.bind(this));
  200. this.$watch('active_style.padding_horz', function (value, mutation) {
  201. this.active_style.padding_left = this.active_style.padding_horz;
  202. this.active_style.padding_right = this.active_style.padding_horz;
  203. }.bind(this));
  204. this.$watch('active_style.padding_vert', function (value, mutation) {
  205. this.active_style.padding_top = this.active_style.padding_vert;
  206. this.active_style.padding_bottom = this.active_style.padding_vert;
  207. }.bind(this));
  208. this.$watch('active_style.padding_top', function (value, mutation) {
  209. this.set_artifact_style_prop("padding_top",parseInt(this.active_style.padding_top));
  210. }.bind(this));
  211. this.$watch('active_style.padding_bottom', function (value, mutation) {
  212. this.set_artifact_style_prop("padding_bottom",parseInt(this.active_style.padding_bottom));
  213. }.bind(this));
  214. this.$watch('active_style.padding_left', function (value, mutation) {
  215. this.set_artifact_style_prop("padding_left",parseInt(this.active_style.padding_left));
  216. }.bind(this));
  217. this.$watch('active_style.padding_right', function (value, mutation) {
  218. this.set_artifact_style_prop("padding_right",parseInt(this.active_style.padding_right));
  219. }.bind(this));
  220. this.$watch('active_style.margin', function (value, mutation) {
  221. this.active_style.margin_horz = this.active_style.margin;
  222. this.active_style.margin_vert = this.active_style.margin;
  223. }.bind(this));
  224. this.$watch('active_style.margin_horz', function (value, mutation) {
  225. this.active_style.margin_left = this.active_style.margin_horz;
  226. this.active_style.margin_right = this.active_style.margin_horz;
  227. }.bind(this));
  228. this.$watch('active_style.margin_vert', function (value, mutation) {
  229. this.active_style.margin_top = this.active_style.margin_vert;
  230. this.active_style.margin_bottom = this.active_style.margin_vert;
  231. }.bind(this));
  232. this.$watch('active_style.margin_top', function (value, mutation) {
  233. this.set_artifact_style_prop("margin_top",parseInt(this.active_style.margin_top));
  234. }.bind(this));
  235. this.$watch('active_style.margin_bottom', function (value, mutation) {
  236. this.set_artifact_style_prop("margin_bottom",parseInt(this.active_style.margin_bottom));
  237. }.bind(this));
  238. this.$watch('active_style.margin_left', function (value, mutation) {
  239. this.set_artifact_style_prop("margin_left",parseInt(this.active_style.margin_left));
  240. }.bind(this));
  241. this.$watch('active_style.margin_right', function (value, mutation) {
  242. this.set_artifact_style_prop("margin_right",parseInt(this.active_style.margin_right));
  243. }.bind(this));
  244. this.$watch('active_style.stroke_color', function (value, mutation) {
  245. this.set_artifact_style_prop("stroke_color",this.active_style.stroke_color);
  246. var rgba = hex_to_rgba(this.active_style.stroke_color);
  247. var hsv = rgb_to_hsv(rgba.r, rgba.g, rgba.b);
  248. this.active_style.stroke_color_hsv = hsv;
  249. }.bind(this));
  250. this.$watch('active_style.fill_color', function (value, mutation) {
  251. this.set_artifact_style_prop("fill_color",this.active_style.fill_color);
  252. var rgba = hex_to_rgba(this.active_style.fill_color);
  253. var hsv = rgb_to_hsv(rgba.r, rgba.g, rgba.b);
  254. this.active_style.fill_color_hsv = hsv;
  255. }.bind(this));
  256. this.$watch('active_style.text_color', function (value, mutation) {
  257. this.set_artifact_style_prop("text_color",this.active_style.text_color);
  258. this.apply_formatting(null,"forecolor",this.active_style.text_color);
  259. var rgba = hex_to_rgba(this.active_style.text_color);
  260. var hsv = rgb_to_hsv(rgba.r, rgba.g, rgba.b);
  261. this.active_style.text_color_hsv = hsv;
  262. }.bind(this));
  263. this.$watch('active_style.font_size', function (value, mutation) {
  264. this.apply_formatting(null,"preciseFontSize",this.active_style.font_size+"px");
  265. }.bind(this));
  266. this.$watch('active_style.line_height', function (value, mutation) {
  267. this.apply_formatting(null,"lineHeight",this.active_style.line_height+"em");
  268. }.bind(this));
  269. this.$watch('active_style.letter_spacing', function (value, mutation) {
  270. this.apply_formatting(null,"letterSpacing",this.active_style.letter_spacing+"px");
  271. }.bind(this));
  272. // color picker
  273. this.$watch('color_picker_hue', function (value, mutation) {
  274. this.apply_color_picker();
  275. }.bind(this));
  276. this.$watch('color_picker_value', function (value, mutation) {
  277. this.apply_color_picker();
  278. }.bind(this));
  279. this.$watch('color_picker_saturation', function (value, mutation) {
  280. this.apply_color_picker();
  281. }.bind(this));
  282. this.$watch('color_picker_opacity', function (value, mutation) {
  283. this.apply_color_picker();
  284. }.bind(this));
  285. // filters
  286. this.$watch('active_style.brightness', function (value, mutation) {
  287. this.set_artifact_style_prop("brightness",parseInt(this.active_style.brightness));
  288. }.bind(this));
  289. this.$watch('active_style.blur', function (value, mutation) {
  290. this.set_artifact_style_prop("blur",parseInt(this.active_style.blur));
  291. }.bind(this));
  292. this.$watch('active_style.contrast', function (value, mutation) {
  293. this.set_artifact_style_prop("contrast",parseInt(this.active_style.contrast));
  294. }.bind(this));
  295. this.$watch('active_style.saturation', function (value, mutation) {
  296. this.set_artifact_style_prop("saturation",parseInt(this.active_style.saturation));
  297. }.bind(this));
  298. this.$watch('active_style.hue', function (value, mutation) {
  299. this.set_artifact_style_prop("hue",parseInt(this.active_style.hue));
  300. }.bind(this));
  301. this.$watch('active_style.opacity', function (value, mutation) {
  302. this.set_artifact_style_prop("opacity",parseInt(this.active_style.opacity));
  303. }.bind(this));
  304. this.throttled_save_active_space = _.throttle(function(){
  305. save_space(this.active_space);
  306. }.bind(this), 2000);
  307. // canvas
  308. this.$watch('active_style.background_color', function (value, mutation) {
  309. if (this.active_style.background_color != this.active_space.background_color) {
  310. this.$set("active_space.background_color",this.active_style.background_color);
  311. this.throttled_save_active_space();
  312. }
  313. var rgba = hex_to_rgba(this.active_style.background_color);
  314. var hsv = rgb_to_hsv(rgba.r, rgba.g, rgba.b);
  315. this.active_style.background_color_hsv = hsv;
  316. }.bind(this));
  317. },
  318. if_editable: function(fn) {
  319. // call given closure if space is editable
  320. // used by key bindings
  321. if (this.active_space_role!="viewer") fn.bind(this)();
  322. },
  323. background_image_style: function(images) {
  324. if (!images) return null;
  325. if (isNaN(images.length)) images = [images];
  326. for (var i=0; i<images.length; i++) {
  327. if (images[i] && images[i].length>0) {
  328. return "background-image: url("+images[i]+")";
  329. }
  330. }
  331. },
  332. space_thumbnail_style: function(space) {
  333. if (space.avatar_thumb_uri && space.avatar_thumb_uri.length>0) {
  334. return "background-image:url('"+space.avatar_thumb_uri+"')";
  335. }
  336. if (space.space_type == "folder") return "";
  337. return "background-image:url('/api/spaces/"+space._id+"/png')";
  338. },
  339. reset_artifact_filters: function() {
  340. this.active_style.brightness = this.default_style.brightness;
  341. this.active_style.contrast = this.default_style.contrast;
  342. this.active_style.opacity = this.default_style.opacity;
  343. this.active_style.saturation = this.default_style.saturation;
  344. this.active_style.blur = this.default_style.blur;
  345. this.active_style.hue = this.default_style.hue;
  346. },
  347. increase_columns: function() {
  348. if (this.active_style.columns<this.MAX_COLUMNS) this.active_style.columns++;
  349. },
  350. decrease_columns: function() {
  351. if (this.active_style.columns>1) this.active_style.columns--;
  352. },
  353. extract_properties_from_selection: function() {
  354. // stop extract->apply feedback loop
  355. this.skip_formatting = true;
  356. var arts = this.selected_artifacts();
  357. window.setTimeout(function() {
  358. this.skip_formatting = false;
  359. }.bind(this),10);
  360. if (!arts.length) return;
  361. if (arts.length == 1) {
  362. var a = arts[0];
  363. var props = [
  364. "stroke",
  365. "border_radius",
  366. "letter_spacing",
  367. "stroke_color",
  368. "fill_color",
  369. "text_color"
  370. ];
  371. for (var i=0; i<props.length; i++) {
  372. var prop = props[i];
  373. this.active_style[prop]=a[prop];
  374. }
  375. // defaults
  376. //this.active_style.padding = this.default_style.padding;
  377. this.active_style.font_size = this.default_style.font_size;
  378. this.active_style.line_height = this.default_style.line_height;
  379. this.active_style.letter_spacing = this.default_style.letter_spacing;
  380. this.active_style.padding_top = a.padding_top || 0;
  381. this.active_style.padding_bottom = a.padding_bottom || 0;
  382. this.active_style.padding_left = a.padding_left || 0;
  383. this.active_style.padding_right = a.padding_right || 0;
  384. if (this.active_style.padding_top == this.active_style.padding_bottom) {
  385. this.active_style.padding_vert = this.active_style.padding_top;
  386. }
  387. if (this.active_style.padding_left == this.active_style.padding_right) {
  388. this.active_style.padding_horz = this.active_style.padding_left;
  389. }
  390. if (this.active_style.padding_top == this.active_style.padding_bottom &&
  391. this.active_style.padding_left == this.active_style.padding_right &&
  392. this.active_style.padding_left == this.active_style.padding_top) {
  393. this.active_style.padding = this.active_style.padding_top;
  394. }
  395. this.active_style.margin_top = a.margin_top || 0;
  396. this.active_style.margin_bottom = a.margin_bottom || 0;
  397. this.active_style.margin_left = a.margin_left || 0;
  398. this.active_style.margin_right = a.margin_right || 0;
  399. if (this.active_style.margin_top == this.active_style.margin_bottom) {
  400. this.active_style.margin_vert = this.active_style.margin_top;
  401. }
  402. if (this.active_style.margin_left == this.active_style.margin_right) {
  403. this.active_style.margin_horz = this.active_style.margin_left;
  404. }
  405. if (this.active_style.margin_top == this.active_style.margin_bottom &&
  406. this.active_style.margin_left == this.active_style.margin_right &&
  407. this.active_style.margin_left == this.active_style.margin_top) {
  408. this.active_style.margin = this.active_style.margin_top;
  409. }
  410. }
  411. this.update_selection_metrics();
  412. // extract text styles
  413. this.selection_metrics.contains_text = false;
  414. this.selection_metrics.contains_images = false;
  415. this.selection_metrics.contains_audio = false;
  416. this.selection_metrics.contains_embeds = false;
  417. this.selection_metrics.contains_vectors = false;
  418. this.selection_metrics.contains_shapes = false;
  419. this.selection_metrics.borders_stylable = false;
  420. var notes = _.filter(arts, function(a) { return (a.mime=="text/html" || a.mime=="x-spacedeck/shape") });
  421. if (notes.length>=1) {
  422. this.selection_metrics.contains_text = true;
  423. if (notes.length==1) {
  424. var a = notes[0];
  425. var dom = $("<div>"+a.description+"</div>")[0];
  426. var el = dom.firstChild;
  427. do {
  428. if (el && el.style) {
  429. if (el.style.fontSize) this.active_style.font_size = parseInt(el.style.fontSize);
  430. if (el.style.fontFamily) this.active_style.font_family = el.style.fontFamily;
  431. if (el.style.letterSpacing) this.active_style.letter_spacing = parseInt(el.style.letterSpacing);
  432. if (el.style.lineHeight) this.active_style.line_height = parseFloat(el.style.lineHeight);
  433. if (el.style.color) this.active_style.text_color = el.style.color;
  434. }
  435. } while (el && (el = dom.nextSibling));
  436. }
  437. }
  438. if (arts.length == 1) {
  439. this.extract_color_picker_from_selection();
  440. }
  441. var images = _.filter(arts, function(a) { return a.mime.match("image") });
  442. if (images.length>=1) {
  443. this.selection_metrics.contains_images = true;
  444. }
  445. var audio = _.filter(arts, function(a) { return a.mime.match("audio") });
  446. if (audio.length>=1) {
  447. this.selection_metrics.contains_audio = true;
  448. }
  449. var embeds = _.filter(arts, function(a) { return a.mime.match("embed") });
  450. if (embeds.length>=1) {
  451. this.selection_metrics.contains_embeds = true;
  452. }
  453. var embeds = _.filter(arts, function(a) { return a.mime=="x-spacedeck/vector" });
  454. if (embeds.length>=1) {
  455. this.selection_metrics.contains_vectors = true;
  456. }
  457. var embeds = _.filter(arts, function(a) { return a.mime=="x-spacedeck/shape"; });
  458. if (embeds.length>=1) {
  459. this.selection_metrics.contains_shapes = true;
  460. }
  461. var sm = this.selection_metrics;
  462. this.selection_metrics.borders_stylable = !(sm.contains_vectors||sm.contains_shapes);
  463. },
  464. increase_letter_spacing: function(evt) {
  465. this.active_style.letter_spacing++;
  466. },
  467. decrease_letter_spacing: function(evt) {
  468. this.active_style.letter_spacing--;
  469. },
  470. apply_font: function(evt, font) {
  471. this.apply_formatting(evt,'fontName',font);
  472. this.active_style.font_family = font;
  473. },
  474. toggle_advanced_properties: function() {
  475. this.advanced_properties = !this.advanced_properties;
  476. },
  477. open_dialog: function(id, evt) {
  478. if (evt) {
  479. evt.stopPropagation();
  480. evt.preventDefault();
  481. }
  482. this.active_tool = "pointer";
  483. if (this.opened_dialog == id) {
  484. this.opened_dialog = "none";
  485. return;
  486. }
  487. if (_.contains(["mobile","shapes","zones"],id)) {
  488. this.deselect();
  489. }
  490. this.opened_dialog=id;
  491. if (id.match("color") || id.match("background")) {
  492. this.color_picker_target = id.replace("color-","")+"_color";
  493. this.color_mode = "palette";
  494. this.extract_color_picker_from_selection();
  495. }
  496. if (_.contains(["audio","video","image","search"],id)) {
  497. if ($("#"+id+" input")[0]) {
  498. $("#"+id+" input")[0].focus();
  499. }
  500. }
  501. if (this.opened_dialog == "background") {
  502. this.color_picker_target = "background_color";
  503. this.background_mode='color';
  504. }
  505. if (this.opened_dialog == "info") {
  506. this.access_settings_space = this.active_space;
  507. this.access_settings_memberships = this.active_space_memberships;
  508. this.editors_section = "list";
  509. if (this.active_space_is_readonly || this.embedded) {
  510. this.space_info_section = "info";
  511. } else if (this.active_space_role == "admin") {
  512. this.space_info_section = "access";
  513. }
  514. }
  515. },
  516. toggle_color_options: function() {
  517. this.color_options_picker = !this.color_options_picker;
  518. },
  519. close_lightbox: function() {
  520. this.lightbox_artifact = {};
  521. this.close_modal();
  522. },
  523. /* --------------- artifact manipulation ------------------------- */
  524. prepare_clipboard: function() {
  525. if ('ontouchstart' in window) return; // don't do this on touch devices
  526. this.selected_artifacts_json = JSON.stringify(this.selected_artifacts());
  527. //$("#space-clipboard > textarea")[0].blur();
  528. this.prepare_clipboard_step2();
  529. },
  530. prepare_clipboard_step2: function() {
  531. if ('ontouchstart' in window) return; // don't do this on touch devices
  532. setTimeout(function() {
  533. if (!$("#space-clipboard > textarea").length) return; // not ready yet
  534. $("#space-clipboard > textarea")[0].focus();
  535. $("#space-clipboard > textarea")[0].select();
  536. },100);
  537. },
  538. handle_section_keydown: function(evt) {
  539. if (evt.keyCode == 67 && (evt.ctrlKey || evt.metaKey)) { // c key
  540. this.prepare_clipboard();
  541. this.prepare_clipboard_step2();
  542. }
  543. return true;
  544. },
  545. handle_onbeforecopy: function(evt) {
  546. if (this.editing_artifact_id) return;
  547. var focused_tag = evt.target.nodeName.toLowerCase();
  548. if (focused_tag != "body") return;
  549. this.prepare_clipboard_step2();
  550. window.setTimeout(function() {
  551. if (!$("#space-clipboard > textarea").length) return; // not ready yet
  552. $("#space-clipboard > textarea")[0].blur();
  553. },10);
  554. },
  555. handle_onunload: function(evt) {
  556. if (!window.artifact_save_queue) return;
  557. var changes = Object.keys(window.artifact_save_queue).length;
  558. if (changes>0) {
  559. var message = "There are "+changes+" changes that are still being saved. Discard them?";
  560. evt.returnValue = message;
  561. return message;
  562. }
  563. window._spacedeck_location_change = true;
  564. },
  565. handle_window_resize: function(evt) {
  566. this.adjust_bounds_zoom();
  567. },
  568. handle_scroll: function(evt) {
  569. if (this.active_view!="space") return;
  570. if (!$("#space").length) return;
  571. el = $("#space")[0];
  572. this.scroll_left = el.scrollLeft/this.viewport_zoom;
  573. this.scroll_top = el.scrollTop/this.viewport_zoom;
  574. this.window_width = window.innerWidth/this.viewport_zoom;
  575. this.window_height = window.innerHeight/this.viewport_zoom;
  576. this.resize_minimap();
  577. // follow presenter mode: send viewport rectangle to viewers
  578. if (this.logged_in && this.present_mode) {
  579. if (this.active_space_role!="viewer") {
  580. this.presenter_send_viewport();
  581. }
  582. }
  583. },
  584. presenter_send_viewport: function() {
  585. name = this.user.nickname || this.user.email;
  586. var msg = {
  587. action: "viewport",
  588. x: this.scroll_left,
  589. y: this.scroll_top,
  590. w: this.window_width,
  591. h: this.window_height,
  592. zoom: this.viewport_zoom,
  593. name: name,
  594. id: this.user._id
  595. };
  596. var packed = JSON.stringify(msg);
  597. if (packed==this._old_viewport_msg) return;
  598. this._old_viewport_msg = packed;
  599. if (this.present_mode && this.active_space_role!="viewer")
  600. this.websocket_send(msg);
  601. },
  602. presenter_send_media_action: function(artifact_id,type,cmd,time) {
  603. name = this.user.nickname || this.user.email;
  604. var msg = {
  605. action: "media",
  606. artifact_id: artifact_id,
  607. type: type,
  608. command: cmd,
  609. time: time,
  610. name: name,
  611. id: this.user._id
  612. };
  613. if (this.present_mode && this.active_space_role!="viewer")
  614. this.websocket_send(msg);
  615. },
  616. resize_minimap: function() {
  617. if (!this.active_space) return;
  618. this.minimap_scale = this.active_space.width/100.0;
  619. },
  620. handle_minimap_mouseup: function(evt) {
  621. this.minimap_mouse_state = "idle";
  622. },
  623. handle_minimap_mousemove: function(evt) {
  624. if (this.minimap_mouse_state=="pressed") {
  625. this.handle_minimap_mousedown(evt);
  626. }
  627. },
  628. handle_minimap_mousedown: function(evt) {
  629. if (!$("#space").length) return;
  630. this.minimap_mouse_state = "pressed";
  631. el = $("#space")[0];
  632. evt = fixup_touches(evt);
  633. var ofs = $(evt.target).offset();
  634. var x = evt.pageX - ofs.left;
  635. var y = evt.pageY - ofs.top;
  636. el.scrollLeft = (x-this.window_width/(this.minimap_scale*2))*this.minimap_scale*this.viewport_zoom;
  637. el.scrollTop = (y-this.window_height/(this.minimap_scale*2))*this.minimap_scale*this.viewport_zoom;
  638. this.handle_scroll();
  639. },
  640. handle_user_cursor_update: function(msg) {
  641. // console.log("handle cursor", msg);
  642. var now = new Date().getTime();
  643. msg.t = now;
  644. var existing = false;
  645. for (var i=0; i<this.user_cursors.length; i++) {
  646. var u = this.user_cursors[i];
  647. if (u.id == msg.id) {
  648. u.x = msg.x;
  649. u.y = msg.y;
  650. u.t = now;
  651. u.name = msg.name;
  652. // console.log("updated cursor "+i);
  653. existing = true;
  654. } else {
  655. // hide if no updates since 2sec
  656. if ((now-u.t)>5000) {
  657. u.x=-10000;
  658. }
  659. }
  660. }
  661. if (!existing) {
  662. this.user_cursors.push(_.clone(msg));
  663. }
  664. },
  665. handle_presenter_viewport_update: function(msg) {
  666. this.zoom_to_rect({
  667. x1: msg.x,
  668. y1: msg.y,
  669. x2: msg.x+msg.w,
  670. y2: msg.y+msg.h
  671. });
  672. },
  673. handle_presenter_media_update: function(msg) {
  674. if(this.follow_mode) {
  675. if (msg.type=="audio") {
  676. var sel="#artifact-"+msg.artifact_id+" .audio";
  677. try {
  678. $(sel)[0].dispatchEvent(new Event("remote_"+msg.command));
  679. console.log("event dispatched");
  680. } catch (e) {
  681. }
  682. }
  683. if (msg.type=="video") {
  684. var sel="#artifact-"+msg.artifact_id+" .video";
  685. try {
  686. $(sel)[0].dispatchEvent(new Event("remote_"+msg.command));
  687. console.log("event dispatched");
  688. } catch (e) {
  689. }
  690. }
  691. } else {
  692. console.log("ignore media update, muted");
  693. }
  694. },
  695. may_select: function(a) {
  696. if (!a) return false;
  697. if (!this.active_space) return false;
  698. if (this.active_space_role=="viewer" || (a.locked && this.active_space_role!="admin")) {
  699. return false;
  700. }
  701. if (this.active_space.editors_locking && !this.logged_in && this.guest_nickname!=a.editor_name) {
  702. return false;
  703. }
  704. return true;
  705. },
  706. select: function(evt, a) {
  707. if (!this.may_select(a)) return;
  708. if (evt && !evt.shiftKey && this.is_selected(a)) return; // already selected
  709. if (!evt || !evt.shiftKey) {
  710. this.deselect();
  711. }
  712. if (evt && evt.shiftKey) {
  713. if (this.selected_artifacts_dict[a._id]) {
  714. delete this.selected_artifacts_dict[a._id];
  715. } else {
  716. this.selected_artifacts_dict[a._id] = true;
  717. }
  718. } else {
  719. this.selected_artifacts_dict[a._id] = true;
  720. }
  721. this.update_board_artifact_viewmodel(a);
  722. this.extract_properties_from_selection();
  723. this.update_selection_metrics();
  724. this.prepare_clipboard();
  725. this.show_toolbar_props();
  726. },
  727. select_all_artifacts: function(evt) {
  728. this.deselect();
  729. for (var i=0; i<this.active_space_artifacts.length; i++) {
  730. var a = this.active_space_artifacts[i];
  731. if (this.may_select(a)) {
  732. this.selected_artifacts_dict[a._id] = true;
  733. this.update_board_artifact_viewmodel(a);
  734. }
  735. }
  736. this.update_selection_metrics();
  737. this.extract_properties_from_selection();
  738. this.prepare_clipboard();
  739. this.show_toolbar_props();
  740. },
  741. multi_select: function(arts) {
  742. for (var i=0; i<arts.length; i++) {
  743. var a = arts[i];
  744. if (this.may_select(a)) {
  745. this.selected_artifacts_dict[a._id] = true;
  746. this.update_board_artifact_viewmodel(a);
  747. }
  748. }
  749. this.extract_properties_from_selection();
  750. this.update_selection_metrics();
  751. this.prepare_clipboard();
  752. this.show_toolbar_props();
  753. },
  754. discover_zones: function() {
  755. this.zones = _.sortBy(_.filter(this.active_space_artifacts, function(a) { return (a.mime=="x-spacedeck/zone") }),
  756. function(z){return z.order});
  757. },
  758. artifact_plaintext: function(a) {
  759. if (!a) return "";
  760. var txt = $("<div>"+a.description+"</div>").text();
  761. return txt || "";
  762. },
  763. deselect: function(hard) {
  764. if (window._sd_fader_moving) {
  765. window._sd_fader_moving = false; // signal from fader directive
  766. return;
  767. }
  768. this.hide_toolbar_props();
  769. document.getSelection().removeAllRanges();
  770. blur();
  771. //this.prepare_clipboard();
  772. this.prepare_clipboard_step2();
  773. this.discover_zones();
  774. var prev_selected = this.selected_artifacts();
  775. this.selected_artifacts_dict = {};
  776. // nuke empty notes
  777. for (var i=0; i<prev_selected.length; i++) {
  778. var a = prev_selected[i];
  779. var keep = true;
  780. if (a && a.mime == "text/html") {
  781. var txt = this.artifact_plaintext(a);
  782. if (txt.length == 0) {
  783. keep = true; //Quick Fix For yassin, complains about buggy behaviour
  784. }
  785. }
  786. if (!keep) {
  787. this.selected_artifacts_dict[a._id] = a;
  788. }
  789. }
  790. this.delete_selected_artifacts(null, true); // delete all selected in loop
  791. this.selected_artifacts_dict = {};
  792. this.editing_artifact_id = null;
  793. this.opened_dialog = "none";
  794. for (var i=0; i<prev_selected.length; i++) {
  795. this.update_board_artifact_viewmodel(prev_selected[i]);
  796. }
  797. /*this.selection_metrics.x=0;
  798. this.selection_metrics.y=0;
  799. this.selection_metrics.w=0;
  800. this.selection_metrics.h=0;
  801. this.selection_metrics.style="display:none";*/
  802. this.selection_metrics.contains_text=false;
  803. this.selection_metrics.count=0;
  804. if (hard) {
  805. this.active_tool = "pointer";
  806. this.mouse_state = "idle";
  807. }
  808. this.update_selection_metrics();
  809. },
  810. is_selected: function(a) {
  811. if (!a) return;
  812. return (!!this.selected_artifacts_dict[a._id]);
  813. },
  814. unselected_artifacts: function() {
  815. return this.active_space_artifacts.filter(function(a){return !this.is_selected(a)}.bind(this));
  816. },
  817. selection_rect_style: function() {
  818. var r = this.selection_rect();
  819. if (r==null) return "display:none";
  820. return "left:"+r.x1+"px;top:"+r.y1+"px;width:"+(r.x2-r.x1)+"px;height:"+(r.y2-r.y1)+"px;";
  821. },
  822. selection_rect: function() {
  823. return this.enclosing_rect(this.selected_artifacts());
  824. },
  825. enclosing_rect: function(arts) {
  826. if (arts.length==0) return null;
  827. arts = _.filter(arts); // remove any nulls
  828. return {
  829. x1: parseInt(_.min(arts.map(function(a){return ((!a || !a.x)?0:a.x)}))),
  830. y1: parseInt(_.min(arts.map(function(a){return ((!a || !a.y)?0:a.y)}))),
  831. x2: parseInt(_.max(arts.map(function(a){return (!a?0:a.x+a.w)}))),
  832. y2: parseInt(_.max(arts.map(function(a){return (!a?0:a.y+a.h)})))
  833. };
  834. },
  835. update_selection_metrics: function(arts) {
  836. if (this.active_tool == "scribble") {
  837. this.selection_metrics.count = 1;
  838. return;
  839. }
  840. var sr = this.selection_rect() || {x:0,y:0,w:0,h:0,style:"display:none"};
  841. if (sr.x1 || sr.x2) {
  842. sr.w = sr.x2-sr.x1;
  843. sr.h = sr.y2-sr.y1;
  844. sr.style = this.selection_rect_style();
  845. var pp = this.space_point_to_window(sr.x1+sr.w/2,sr.y2);
  846. var pp2 = this.space_point_to_window(sr.x1+sr.w/2,sr.y1);
  847. pp.x-=260;
  848. pp.y-=10;
  849. if (pp.y>=window.innerHeight-300) {
  850. pp.y = pp2.y-100;
  851. }
  852. if (pp.x<0) {
  853. pp.x=0;
  854. }
  855. if (pp.y<0) {
  856. pp.y=0;
  857. }
  858. // FIXME make sure that menus fit in window
  859. this.toolbar_props_x = pp.x+"px";
  860. this.toolbar_props_y = pp.y+"px";
  861. this.hide_toolbar_artifacts();
  862. }
  863. this.selection_metrics.x1 = sr.x1;
  864. this.selection_metrics.x2 = sr.x2;
  865. this.selection_metrics.y1 = sr.y1;
  866. this.selection_metrics.y2 = sr.y2;
  867. this.selection_metrics.x = sr.x;
  868. this.selection_metrics.y = sr.y;
  869. this.selection_metrics.w = sr.w;
  870. this.selection_metrics.h = sr.h;
  871. this.selection_metrics.style = sr.style;
  872. if (!arts) arts = this.selected_artifacts();
  873. this.first_selected_artifact = arts[0];
  874. this.selection_metrics.count=arts.length;
  875. this.selection_metrics.scribble_selection = false;
  876. if (arts.length == 1 && arts[0].mime == "x-spacedeck/vector") {
  877. if (arts[0].shape == "scribble") {
  878. this.selection_metrics.scribble_selection = true;
  879. }
  880. this.selection_metrics.vector_points = arts[0].control_points;
  881. this.selection_metrics.vector_selection = true;
  882. } else {
  883. this.selection_metrics.vector_points = [{},{}];
  884. this.selection_metrics.vector_selection = false;
  885. }
  886. this.selection_metrics.has_link=false;
  887. this.insert_link_url="";
  888. if (arts.length == 1 && arts[0].meta && arts[0].meta.link_uri && arts[0].meta.link_uri.length>0) {
  889. this.selection_metrics.has_link=true;
  890. this.insert_link_url = arts[0].meta.link_uri;
  891. }
  892. },
  893. begin_transaction: function() {
  894. this.transaction_running = true;
  895. if (!this.undo_stack.length || this.undo_stack[this.undo_stack.length-1].action!="empty") {
  896. this.undo_stack.push({action:"empty"});
  897. } else {
  898. //console.log("undo slot is already empty.");
  899. }
  900. this.redo_stack = [];
  901. this.artifacts_before_transaction = this.active_space_artifacts.map(function(a) {
  902. return _.cloneDeep(a);
  903. });
  904. },
  905. fixup_space_size: function() {
  906. if (!this.active_space) return;
  907. this.active_space.width =Math.max(this.active_space.width, window.innerWidth);
  908. this.active_space.height=Math.max(this.active_space.height, window.innerHeight);
  909. },
  910. end_transaction: function() {
  911. this.transaction_running = false;
  912. this.throttled_process_artifact_save_queue();
  913. if (!this.active_space) return;
  914. var er = this.enclosing_rect(this.active_space_artifacts);
  915. if (!er) return;
  916. this.active_space.width =Math.max(er.x2+100, window.innerWidth);
  917. this.active_space.height=Math.max(er.y2+100, window.innerHeight);
  918. if (this._last_bounds_width != this.active_space.width ||
  919. this._last_bounds_height != this.active_space.height) {
  920. this._last_bounds_width = this.active_space.width;
  921. this._last_bounds_height = this.active_space.height;
  922. save_space(this.active_space);
  923. }
  924. this.resize_minimap();
  925. this.discover_zones();
  926. },
  927. find_artifact_before_transaction: function(needle) {
  928. return this.find_artifact_in_array(this.artifacts_before_transaction, needle);
  929. },
  930. find_artifact_in_array: function(haystack, needle) {
  931. var res = _.find(haystack, function(a) {
  932. return (needle._id && (a._id == needle._id));
  933. });
  934. return res;
  935. },
  936. unsaved_transactions: function() {
  937. if (!window.artifact_save_queue) return 0;
  938. return Object.keys(window.artifact_save_queue).length;
  939. },
  940. process_artifact_save_queue: function() {
  941. if (!window.artifact_save_queue) {
  942. return;
  943. }
  944. if (this.transaction_running) {
  945. console.log("not saving, transaction still in progress.");
  946. return;
  947. }
  948. var ids = Object.keys(window.artifact_save_queue);
  949. for (var i=0; i<ids.length; i++) {
  950. var id = ids[i];
  951. var a = window.artifact_save_queue[id];
  952. if (this.guest_nickname) {
  953. a.editor_name = this.guest_nickname;
  954. }
  955. save_artifact(a, function() {
  956. delete window.artifact_save_queue[id];
  957. }.bind(this), function(req) {
  958. if (req && req.status == 404) {
  959. // artifact was already deleted, ignore
  960. delete window.artifact_save_queue[id];
  961. } else {
  962. console.log("could not save artifact, will try again:",a,req);
  963. }
  964. });
  965. }
  966. // mark version dirty locally
  967. if (this.active_space) {
  968. this.active_space.updated_at = (new Date()).getTime();
  969. }
  970. //window.artifact_save_queue = {};
  971. },
  972. throttled_process_artifact_save_queue: function() {
  973. if (!this._throttled_process_artifact_save_queue) {
  974. this._throttled_process_artifact_save_queue = _.throttle(this.process_artifact_save_queue, 500);
  975. }
  976. this._throttled_process_artifact_save_queue();
  977. },
  978. queue_artifact_for_save: function(a) {
  979. if (!window.artifact_save_queue) {
  980. window.artifact_save_queue = {};
  981. }
  982. if (!a._id) {
  983. console.log("warning: illegal artifact queued for save");
  984. }
  985. // this is a bit hacky, but might be the smartest place to do it
  986. if (a.view && a.view.vector_svg) {
  987. a.shape_svg = a.view.vector_svg;
  988. }
  989. window.artifact_save_queue[a._id] = a;
  990. },
  991. update_properties: function(artifact_ids, changes) {
  992. for (var i=0; i<artifact_ids.length; i++) {
  993. var id = artifact_ids[i];
  994. var a = this.find_artifact_by_id(id);
  995. if (a) {
  996. var changed = false;
  997. for (k in changes[i]) {
  998. //console.log("change: ",k,": ",changes[i][k],"<-",a[k])
  999. a[k] = changes[i][k];
  1000. changed = true;
  1001. }
  1002. this.update_board_artifact_viewmodel(a);
  1003. // TODO: throttle, bundle etc
  1004. if (changed) {
  1005. this.queue_artifact_for_save(a);
  1006. }
  1007. }
  1008. }
  1009. },
  1010. update_artifacts: function(artifacts, change_func) {
  1011. var artifact_ids = [];
  1012. var changes = [];
  1013. for (var i=0; i<artifacts.length; i++) {
  1014. var a = artifacts[i];
  1015. var c = change_func(a);
  1016. if (c) {
  1017. artifact_ids.push(a._id);
  1018. changes.push(c);
  1019. }
  1020. }
  1021. if (changes.length) {
  1022. this.push_to_undo({
  1023. action: "update",
  1024. artifact_ids: artifact_ids,
  1025. changes: changes,
  1026. snapshot: this.artifacts_before_transaction
  1027. });
  1028. }
  1029. if (changes.length) {
  1030. this.update_properties(artifact_ids, changes);
  1031. }
  1032. },
  1033. push_to_undo: function(transaction) {
  1034. // begin_transaction opens new undo slot
  1035. // push_to_undo replaces the current undo slot
  1036. this.undo_stack[this.undo_stack.length-1] = transaction;
  1037. //console.log(transaction.action+"; undo_stack len: ",this.undo_stack.length);
  1038. },
  1039. undo: function() {
  1040. if (!this.undo_stack.length || this.undo_stack[this.undo_stack.length-1].action=="empty") {
  1041. console.log("nothing to undo!");
  1042. return;
  1043. }
  1044. var step = this.undo_stack.pop();
  1045. console.log("undo popped: ",step);
  1046. this.redo_stack.push(step);
  1047. for (var i=0; i<step.artifact_ids.length; i++) {
  1048. var id = step.artifact_ids[i];
  1049. var old_version = this.find_artifact_in_array(step.snapshot, {_id:id});
  1050. if (step.action == "update") {
  1051. if (old_version) {
  1052. this.update_properties([id], [old_version]);
  1053. }
  1054. } else {
  1055. delete old_version._id;
  1056. save_artifact(old_version, function(restored_a) {
  1057. this.update_board_artifact_viewmodel(restored_a);
  1058. this.active_space_artifacts.push(restored_a);
  1059. // TODO: rewrite undo history's artifact ids
  1060. }.bind(this));
  1061. }
  1062. }
  1063. this.update_selection_metrics();
  1064. },
  1065. redo: function() {
  1066. if (!this.redo_stack.length) {
  1067. console.log("nothing to redo!");
  1068. return;
  1069. }
  1070. var step = this.redo_stack.pop();
  1071. console.log("redo popped: ",step);
  1072. this.undo_stack.push(step);
  1073. this.update_properties(step.artifact_ids, step.changes);
  1074. this.update_selection_metrics();
  1075. },
  1076. set_artifact_prop: function(prop, val) {
  1077. this.begin_transaction();
  1078. this.update_selected_artifacts(function(a) {
  1079. var c = {};
  1080. if (a[prop] != val) {
  1081. //console.log("set_artifact_prop: ",c,val);
  1082. c[prop]=val;
  1083. return c;
  1084. }
  1085. return null;
  1086. });
  1087. },
  1088. set_artifact_style_prop: function(prop, val) {
  1089. this.begin_transaction();
  1090. this.update_selected_artifacts(function(a) {
  1091. var c = {};
  1092. if (a[prop] != val) {
  1093. //console.log("set_artifact_style_prop: ",c,val);
  1094. c[prop]=val;
  1095. return c;
  1096. }
  1097. return null;
  1098. });
  1099. },
  1100. activate_color_mode: function(mode) {
  1101. this.color_mode = mode;
  1102. if (mode == 'picker') {
  1103. // default to full alpha if color is 0,0,0,0
  1104. if (this.color_picker_hue == 0 &&
  1105. this.color_picker_saturation == 0 &&
  1106. this.color_picker_value == 0 &&
  1107. this.color_picker_opacity == 0) {
  1108. this.color_picker_opacity = 255;
  1109. this.color_picker_value = 255;
  1110. }
  1111. }
  1112. },
  1113. reset_stroke: function() {
  1114. this.active_style.stroke = 0;
  1115. this.active_style.border_radius = 0;
  1116. this.active_style.stroke_style = "solid";
  1117. },
  1118. apply_font_size: function(px) {
  1119. this.apply_formatting(null,"preciseFontSize",px+"px");
  1120. },
  1121. apply_swatch_color: function(swatch) {
  1122. var rgba = hex_to_rgba(swatch.hex);
  1123. var hsv = rgb_to_hsv(rgba.r, rgba.g, rgba.b);
  1124. this.color_picker_hue = parseFloat(hsv.h*255);
  1125. this.color_picker_saturation = parseFloat(hsv.s*255);
  1126. this.color_picker_value = parseFloat(hsv.v*255);
  1127. this.color_picker_opacity = rgba.a*255;
  1128. this.color_picker_rgb = rgb_to_hex(rgba.r,rgba.g,rgba.b);
  1129. //console.log("swatch hex: ",swatch.hex);
  1130. this.active_style[this.color_picker_target] = swatch.hex;
  1131. if (this.color_picker_target == "stroke_color") {
  1132. if (!this.active_style.stroke) {
  1133. // set a default stroke for convenience
  1134. this.active_style.stroke = 2;
  1135. }
  1136. }
  1137. },
  1138. apply_color_picker: function() {
  1139. var rgb = hsv_to_rgb(this.color_picker_hue/255,
  1140. this.color_picker_saturation/255,
  1141. this.color_picker_value/255);
  1142. var alpha = this.color_picker_opacity/255;
  1143. //console.log("apply_color_picker: ",rgb,alpha);
  1144. this.active_style[this.color_picker_target] = "rgba("+[rgb.r,rgb.g,rgb.b,alpha].join(",")+")";
  1145. },
  1146. extract_color_picker_from_selection: function() {
  1147. if (this.selected_artifacts().length!=1 && this.opened_dialog!="background") return;
  1148. if (this.opened_dialog=="background") {
  1149. this.active_style[this.color_picker_target] = this.active_space.background_color;
  1150. } else {
  1151. if (!this.active_style[this.color_picker_target]) {
  1152. this.active_style[this.color_picker_target] = this.default_style[this.color_picker_target];
  1153. }
  1154. }
  1155. //console.log("active_style for "+this.color_picker_target+": ",this.active_style[this.color_picker_target]);
  1156. var rgba = hex_to_rgba(this.active_style[this.color_picker_target]);
  1157. var hsv = rgb_to_hsv(rgba.r, rgba.g, rgba.b);
  1158. this.color_picker_hue = parseFloat(hsv.h*255);
  1159. this.color_picker_saturation = parseFloat(hsv.s*255);
  1160. this.color_picker_value = parseFloat(hsv.v*255);
  1161. this.color_picker_opacity = parseInt(rgba.a);
  1162. this.color_picker_rgb = rgb_to_hex(rgba.r,rgba.g,rgba.b);
  1163. },
  1164. update_selected_artifacts: function(change_func, override_locked) {
  1165. var artifacts = this.selected_artifacts(!override_locked);
  1166. if (!artifacts.length) return;
  1167. this.update_artifacts(artifacts, change_func);
  1168. this.update_selection_metrics();
  1169. },
  1170. nudge_selected_artifacts: function(dx, dy, event) {
  1171. if (this.present_mode) {
  1172. if (dx>0 || dy>0) {
  1173. this.go_to_next_zone();
  1174. return;
  1175. }
  1176. if (dx<0 || dy<0) {
  1177. this.go_to_previous_zone();
  1178. return;
  1179. }
  1180. }
  1181. if (!this.selected_artifacts().length) {
  1182. if (!$("#space").length) return;
  1183. var el = $("#space")[0];
  1184. el.scrollLeft+=dx*100;
  1185. el.scrollTop +=dy*100;
  1186. return;
  1187. }
  1188. if (this.active_space_is_readonly) return;
  1189. if (event) {
  1190. event.preventDefault();
  1191. event.stopPropagation();
  1192. }
  1193. this.begin_transaction();
  1194. this.update_selected_artifacts(function(a) {
  1195. return {
  1196. x: a.x+dx,
  1197. y: a.y+dy
  1198. };
  1199. });
  1200. },
  1201. /* -------------------------------------------------------------------- */
  1202. highest_z: function() {
  1203. var z = _.max(this.active_space_artifacts.map(function(a){return a.z||0}));
  1204. if (z<0) z=0;
  1205. if (z>999) z=999;
  1206. return z;
  1207. },
  1208. find_place_for_item: function(width, height) {
  1209. var arts = this.active_space_artifacts;
  1210. var tw = window.innerWidth;
  1211. var th = window.innerHeight;
  1212. var el = $("#space")[0];
  1213. if (!el) return {x:0,y:0,z:1}; // FIXME
  1214. var wrap = $(".wrapper");
  1215. var wx = parseInt(wrap.css("margin-left"));
  1216. var wy = parseInt(wrap.css("margin-top"));
  1217. var x = parseInt((el.scrollLeft + tw/2)/this.viewport_zoom - width/2 - wx/this.viewport_zoom);
  1218. var y = parseInt((el.scrollTop + th/2)/this.viewport_zoom - height/2 - wy/this.viewport_zoom);
  1219. /*
  1220. if (this.opened_dialog!="none") {
  1221. // we have less visible space if a dialog is obscuring sight
  1222. y/=2;
  1223. }
  1224. */
  1225. var z = this.highest_z()+1;
  1226. if (arts.length==0) return {x:x,y:y};
  1227. x += parseInt(Math.random()*20)-10;
  1228. y += parseInt(Math.random()*20)-10;
  1229. return {x:x,y:y,z:z};
  1230. },
  1231. save_audio_edit: function(a) { // just a helper to be called from view model
  1232. this.opened_dialog = "none";
  1233. this.update_board_artifact_viewmodel(a);
  1234. save_artifact(a);
  1235. },
  1236. save_artifact: function(a, on_success) { // helper to be called from view model
  1237. if (this.guest_nickname) {
  1238. a.editor_name = this.guest_nickname;
  1239. }
  1240. this.update_board_artifact_viewmodel(a);
  1241. save_artifact(a, on_success);
  1242. },
  1243. add_artifact: function (space, item_type, url, evt) {
  1244. this.active_tool = "pointer";
  1245. this.mouse_state = "idle";
  1246. this.hide_toolbar_artifacts();
  1247. if (!url && (item_type == 'image' || item_type == 'video' || item_type == 'embed')) {
  1248. url = prompt("URL?");
  1249. if (!url || !url.length) return;
  1250. }
  1251. //this.opened_dialog = "none";
  1252. var w=300,h=200;
  1253. var z=this.highest_z()+1;
  1254. // TODO: find solution for legacy types
  1255. mimes = {
  1256. "text": "text/html",
  1257. "note": "text/html",
  1258. "image": "image/jpg",
  1259. "video": "video/mp4"
  1260. };
  1261. var new_item = {
  1262. mime: mimes[item_type],
  1263. description: "",
  1264. payload_uri: url,
  1265. payload_thumbnail_medium_uri: url || null,
  1266. payload_thumbnail_web_uri: url || null,
  1267. space_id: space._id,
  1268. order: this.active_space_artifacts.length+1,
  1269. valign: "middle",
  1270. align: "center"
  1271. //fill_color: "#f8f8f8"
  1272. };
  1273. if (mimes[item_type] == "text/html") {
  1274. new_item.padding_left = 10;
  1275. new_item.padding_top = 10;
  1276. new_item.padding_right = 10;
  1277. new_item.padding_bottom = 10;
  1278. new_item.fill_color = "rgba(255,255,255,1)";
  1279. new_item.description = "<p>Text</p>";
  1280. }
  1281. if (evt) {
  1282. var point = this.cursor_point_to_space(evt);
  1283. point.x-=100;
  1284. point.y-=100;
  1285. } else {
  1286. var point = this.find_place_for_item(w,h);
  1287. z = point.z;
  1288. }
  1289. new_item.x = parseInt(point.x);
  1290. new_item.y = parseInt(point.y);
  1291. new_item.z = z;
  1292. new_item.w = w;
  1293. new_item.h = h;
  1294. if (this.guest_nickname) {
  1295. new_item.editor_name = this.guest_nickname;
  1296. }
  1297. // console.log("new artifact", new_item);
  1298. save_artifact(new_item, function(saved_item) {
  1299. // console.log("saved artifact", saved_item);
  1300. this.update_board_artifact_viewmodel(saved_item);
  1301. this.active_space_artifacts.push(saved_item);
  1302. if (!url) {
  1303. this.select(null, saved_item);
  1304. }
  1305. if (item_type.match("text")) {
  1306. this.editing_artifact_id = saved_item._id;
  1307. window.setTimeout(function() {
  1308. // FIXME: replace hack
  1309. var el = $("#artifact-"+saved_item._id+" .text-editing");
  1310. focus_contenteditable(el[0], false);
  1311. },400);
  1312. }
  1313. }.bind(this));
  1314. },
  1315. go_to_first_zone: function() {
  1316. this.discover_zones();
  1317. if (!this.zones.length) return;
  1318. this.zoom_to_zone(this.zones[0]);
  1319. },
  1320. go_to_previous_zone: function() {
  1321. this.discover_zones();
  1322. if (!this.zones.length) return;
  1323. var prev_idx = (this.current_zone_idx-1);
  1324. if (prev_idx<0) prev_idx = this.zones.length-1;
  1325. this.current_zone_idx = prev_idx;
  1326. this.zoom_to_zone(this.zones[this.current_zone_idx]);
  1327. },
  1328. go_to_next_zone: function() {
  1329. this.discover_zones();
  1330. if (!this.zones.length) return;
  1331. var next_idx = ((this.current_zone_idx+1) % this.zones.length);
  1332. this.current_zone_idx = next_idx;
  1333. this.zoom_to_zone(this.zones[this.current_zone_idx]);
  1334. },
  1335. sort_zone_up: function(z) {
  1336. var idx = this.zones.indexOf(z);
  1337. if (idx<1) return;
  1338. var new_zones = _.flatten([this.zones.slice(0,idx-1),[z],this.zones[idx-1],this.zones.slice(idx+1,this.zones.length)]);
  1339. for (var i=0; i<new_zones.length; i++) {
  1340. if (new_zones[i]) {
  1341. if (!new_zones[i].style) new_zones[i].style = {};
  1342. new_zones[i].order = i;
  1343. save_artifact(new_zones[i]);
  1344. }
  1345. }
  1346. this.discover_zones();
  1347. },
  1348. sort_zone_down: function(z) {
  1349. var idx = this.zones.indexOf(z);
  1350. if (idx>=this.zones.length) return;
  1351. var new_zones = _.flatten([this.zones.slice(0,idx),this.zones[idx+1],[z],this.zones.slice(idx+2,this.zones.length)]);
  1352. for (var i=0; i<new_zones.length; i++) {
  1353. if (new_zones[i]) {
  1354. if (!new_zones[i].style) new_zones[i].style = {};
  1355. new_zones[i].order = i;
  1356. save_artifact(new_zones[i]);
  1357. }
  1358. }
  1359. this.discover_zones();
  1360. },
  1361. add_zone: function() {
  1362. var w = 600;
  1363. var h = 600;
  1364. var point = this.find_place_for_item(w,h);
  1365. var a = {
  1366. space_id: this.active_space._id,
  1367. mime: "x-spacedeck/zone",
  1368. description: "Zone "+(this.zones.length+1),
  1369. x: point.x,
  1370. y: point.y,
  1371. w: w,
  1372. h: h,
  1373. z: 0,
  1374. valign: "middle",
  1375. align: "center"
  1376. };
  1377. if (this.guest_nickname) {
  1378. a.editor_name = this.guest_nickname;
  1379. }
  1380. save_artifact(a, function(saved_item) {
  1381. this.update_board_artifact_viewmodel(saved_item);
  1382. this.active_space_artifacts.push(saved_item);
  1383. this.select(null, saved_item);
  1384. }.bind(this));
  1385. },
  1386. add_shape: function(shape_type, evt) {
  1387. var w = 200;
  1388. var h = 200;
  1389. if (shape_type=="cloud") {
  1390. w = 400;
  1391. }
  1392. //var point = this.find_place_for_item(w,h);
  1393. var point = this.cursor_point_to_space(evt);
  1394. var a = {
  1395. space_id: this.active_space._id,
  1396. mime: "x-spacedeck/shape",
  1397. description: "Text",
  1398. x: point.x,
  1399. y: point.y,
  1400. z: point.z,
  1401. w: w,
  1402. h: h,
  1403. stroke_color: "#ffffff",
  1404. text_color: "#ffffff",
  1405. stroke: 0,
  1406. fill_color: "#000000",
  1407. shape: shape_type,
  1408. valign: "middle",
  1409. align: "center"
  1410. };
  1411. if (this.guest_nickname) {
  1412. a.editor_name = this.guest_nickname;
  1413. }
  1414. save_artifact(a, function(saved_item) {
  1415. this.update_board_artifact_viewmodel(saved_item);
  1416. this.active_space_artifacts.push(saved_item);
  1417. this.select(null, saved_item);
  1418. }.bind(this));
  1419. },
  1420. cursor_point_to_space: function(evt) {
  1421. if (!evt) return {x:0,y:0};
  1422. if (!$("#space").length) return {x:0,y:0};
  1423. var el = $("#space")[0];
  1424. var pt = parseInt($("#space").css("padding-top"));
  1425. var ml = 0;
  1426. var mt = 0;
  1427. var px = evt.pageX;
  1428. var py = evt.pageY;
  1429. if (!("pageX" in evt) && ("originalEvent" in evt)) {
  1430. px = evt.originalEvent.pageX;
  1431. py = evt.originalEvent.pageY;
  1432. }
  1433. var ox = ((px + el.scrollLeft) - this.bounds_margin_horiz)/this.viewport_zoom;
  1434. var oy = ((py + el.scrollTop) - pt - this.bounds_margin_vert)/this.viewport_zoom;
  1435. return {x: ox, y: oy};
  1436. },
  1437. space_point_to_window: function(x,y) {
  1438. var rx = 0;
  1439. var ry = 0;
  1440. var el = $("#space")[0];
  1441. rx = x*this.viewport_zoom-el.scrollLeft+this.bounds_margin_horiz;
  1442. ry = y*this.viewport_zoom-el.scrollTop+this.bounds_margin_vert;
  1443. return {x:rx,y:ry};
  1444. },
  1445. create_artifact_via_upload: function(evt, file, multiple) {
  1446. if (this.active_space_role=="viewer") {
  1447. return false;
  1448. }
  1449. this.hide_toolbar_artifacts();
  1450. // 1. create placeholder artifact
  1451. var w=300,h=150;
  1452. var fill="transparent";
  1453. if (file.type.match("audio")) {
  1454. w=600;
  1455. h=150;
  1456. fill="#ffffff";
  1457. }
  1458. var point = this.cursor_point_to_space(evt);
  1459. point.x-=w/2;
  1460. point.y-=h/2;
  1461. if (multiple) {
  1462. point = this.find_place_for_item(w,h);
  1463. }
  1464. var a = {
  1465. space_id: this.active_space._id,
  1466. mime: file.type,
  1467. description: "Uploading…",
  1468. state: "uploading",
  1469. payload_thumbnail_medium_uri: null,
  1470. payload_thumbnail_web_uri: null,
  1471. x: point.x,
  1472. y: point.y,
  1473. w: w,
  1474. h: h,
  1475. z: point.z,
  1476. order: this.active_space_artifacts.length+1,
  1477. fill_color: fill
  1478. }
  1479. this.update_board_artifact_viewmodel(a);
  1480. // 2. post file
  1481. if (this.guest_nickname) {
  1482. a.editor_name = this.guest_nickname;
  1483. }
  1484. save_artifact(a, function(updated_a) {
  1485. a = updated_a;
  1486. this.update_board_artifact_viewmodel(a);
  1487. this.active_space_artifacts.push(a);
  1488. save_artifact_file(a, file, file.name, function(updated_a) {
  1489. console.log("file saved. result: ",updated_a);
  1490. // TODO: generify
  1491. a.payload_uri = updated_a.payload_uri;
  1492. a.payload_thumbnail_web_uri = updated_a.payload_thumbnail_web_uri;
  1493. a.payload_thumbnail_medium_uri = updated_a.payload_thumbnail_medium_uri;
  1494. a.payload_thumbnail_big_uri = updated_a.payload_thumbnail_big_uri;
  1495. a.payload_alternatives = updated_a.payload_alternatives;
  1496. a.mime = updated_a.mime;
  1497. a.x = updated_a.x;
  1498. a.y = updated_a.y;
  1499. a.w = updated_a.w;
  1500. a.h = updated_a.h;
  1501. a.z = updated_a.z;
  1502. a.state = updated_a.state;
  1503. this.update_board_artifact_viewmodel(a);
  1504. }.bind(this), null, function(e) {
  1505. // upload progress
  1506. var progress = e.loaded/e.total;
  1507. if (progress=1) {
  1508. a.description = "Converting Media…";
  1509. } else {
  1510. a.description = "Upload "+parseInt(progress*100)+"%";
  1511. }
  1512. this.update_board_artifact_viewmodel(a);
  1513. a.view.progress = parseInt(progress*100);
  1514. }.bind(this));
  1515. }.bind(this), this.display_saving_error);
  1516. },
  1517. delete_selected_artifacts: function(evt, skip_deselect) {
  1518. if (!this.active_space) return;
  1519. if (evt) {
  1520. evt.preventDefault();
  1521. evt.stopPropagation();
  1522. }
  1523. this.begin_transaction();
  1524. var ids = this.selected_artifacts().map(function(a){return a._id});
  1525. var backup = [];
  1526. var backup_ids = [];
  1527. if (ids.length>1 && !skip_deselect) {
  1528. if (!confirm("Delete "+ids.length+" items?")) return;
  1529. }
  1530. for (var i=0; i<ids.length; i++) {
  1531. if (this.selected_artifacts_dict[ids[i]]) {
  1532. var id = ids[i];
  1533. var a = this.find_artifact_by_id(id);
  1534. if (a) {
  1535. backup.push(a);
  1536. backup_ids.push(id);
  1537. delete_artifact(a);
  1538. }
  1539. var idx = this.active_space_artifacts.indexOf(a);
  1540. this.active_space_artifacts.splice(idx, 1);
  1541. }
  1542. }
  1543. this.push_to_undo({
  1544. action: "delete",
  1545. artifact_ids: backup_ids,
  1546. snapshot: backup
  1547. });
  1548. if (!skip_deselect) {
  1549. this.deselect();
  1550. }
  1551. },
  1552. find_artifact_by_id: function(id) {
  1553. var ars = this.active_space_artifacts;
  1554. for (var i=0; i<ars.length; i++) {
  1555. var a = ars[i];
  1556. if (a._id==id) return a;
  1557. }
  1558. return null;
  1559. },
  1560. selected_artifacts: function(exclude_locked) {
  1561. if (!this.active_space || !this.active_space_artifacts) return [];
  1562. return this.active_space_artifacts.filter(function(a) {
  1563. var sel = this.artifact_is_selected(a);
  1564. if (sel && a.locked) return !exclude_locked;
  1565. return sel;
  1566. }.bind(this));
  1567. },
  1568. delayed_edit_artifact: function(evt) {
  1569. evt.stopPropagation();
  1570. evt.preventDefault();
  1571. var a = this.selected_artifacts()[0];
  1572. var el = $("#ios-focuser-"+a._id);
  1573. el.focus();
  1574. el.select();
  1575. this.toggle_selected_artifact_editing(true, true);
  1576. },
  1577. toggle_selected_artifact_editing: function(force_on, delayed) {
  1578. var a = this.selected_artifacts()[0];
  1579. if (!a) {
  1580. this.editing_artifact_id = null;
  1581. return;
  1582. }
  1583. if (this.editing_artifact_id == a._id && !force_on) {
  1584. this.editing_artifact_id = null;
  1585. return;
  1586. }
  1587. if (a.locked) return;
  1588. if (!_.include(["text/html","x-spacedeck/shape","x-spacedeck/zone"],a.mime)) return;
  1589. if (this.editing_artifact_id == a._id) return;
  1590. this.editing_artifact_id = a._id;
  1591. var ms = 100;
  1592. if (delayed) ms = 500; // wait for slow devices
  1593. window.setTimeout(function(){
  1594. var el = $("#artifact-"+a._id+" .text-editing");
  1595. if (el[0]) {
  1596. // tuned weirdness for firefox, chrome + ios safari
  1597. //el.select();
  1598. focus_contenteditable(el[0], true);
  1599. } else {
  1600. // one more try (slow device)
  1601. window.setTimeout(function(){
  1602. var el = $("#artifact-"+a._id+" .text-editing");
  1603. //el.select();
  1604. focus_contenteditable(el[0], true);
  1605. },ms);
  1606. }
  1607. },ms);
  1608. },
  1609. clear_formatting_walk: function(el,cmd,arg1,arg2) {
  1610. if (el && el.style) {
  1611. if (cmd == "preciseFontSize") {
  1612. el.fontSize = null;
  1613. } else if (cmd == "letterSpacing") {
  1614. el.letterSpacing = null;
  1615. } else if (cmd == "lineHeight") {
  1616. el.lineHeight = null;
  1617. } else if (cmd == "fontName") {
  1618. el.fontFamily = null;
  1619. } else if (cmd == "fontWeight") {
  1620. el.fontWeight = null;
  1621. el.fontStyle = null;
  1622. } else if (cmd == "bold") {
  1623. el.fontWeight = null;
  1624. } else if (cmd == "italic") {
  1625. el.fontStyle = null;
  1626. } else if (cmd == "underline") {
  1627. el.textDecoration = null;
  1628. } else if (cmd == "strikeThrough") {
  1629. el.textDecoration = null;
  1630. } else if (cmd == "forecolor") {
  1631. el.color = null;
  1632. }
  1633. }
  1634. if (el && el.childNodes) {
  1635. for (var i=0; i<el.childNodes.length; i++) {
  1636. this.clear_formatting_walk(el.childNodes[i],cmd,arg1,arg2);
  1637. }
  1638. }
  1639. },
  1640. apply_formatting: function(evt,cmd,arg1,arg2) {
  1641. if (evt) {
  1642. evt.preventDefault();
  1643. evt.stopPropagation();
  1644. }
  1645. if (this.skip_formatting) return;
  1646. if (cmd=='createlink') {
  1647. arg1 = prompt("Link URL?");
  1648. if (!arg1) return;
  1649. }
  1650. var selected = this.selected_artifacts();
  1651. var collapsed = false;
  1652. if (!window.selection || window.selection.type == "Caret" || window.selection.type == "None") {
  1653. collapsed = true;
  1654. }
  1655. // (selected.length==1 && (!this.editing_artifact_id))
  1656. if (!this.editing_artifact_id || cmd == "preciseFontSize" || cmd == "forecolor") {
  1657. for (var i=0; i<selected.length; i++) {
  1658. var a = selected[i];
  1659. var dom = $("<div>"+a.description+"</div>")[0];
  1660. var el = dom.firstChild;
  1661. do {
  1662. // clear nested styles first
  1663. if (el && el.childNodes) {
  1664. for (var j=0; j<el.childNodes.length; j++) {
  1665. this.clear_formatting_walk(el.childNodes[j],cmd,arg1,arg2);
  1666. }
  1667. }
  1668. // toggle new style
  1669. if (el && el.style) {
  1670. if (cmd == "preciseFontSize") {
  1671. if (arg1 == this.default_style.font_size+"px") {
  1672. el.style.fontSize = null;
  1673. } else {
  1674. el.style.fontSize = arg1;
  1675. }
  1676. } else if (cmd == "letterSpacing") {
  1677. if (arg1 == this.default_style.letter_spacing+"px") {
  1678. el.style.letterSpacing = null;
  1679. } else {
  1680. el.style.letterSpacing = arg1;
  1681. }
  1682. } else if (cmd == "lineHeight") {
  1683. if (arg1 == this.default_style.line_height+"em") {
  1684. el.style.lineHeight = null;
  1685. } else {
  1686. el.style.lineHeight = arg1;
  1687. }
  1688. } else if (cmd == "fontName") {
  1689. el.style.fontFamily = arg1;
  1690. } else if (cmd == "fontWeight") {
  1691. el.style.fontWeight = arg1;
  1692. el.style.fontStyle = arg2;
  1693. } else if (cmd == "bold") {
  1694. el.style.fontWeight = (el.style.fontWeight!="bold"?"bold":"normal");
  1695. } else if (cmd == "italic") {
  1696. el.style.fontStyle = (el.style.fontStyle!="italic"?"italic":"normal");
  1697. } else if (cmd == "underline") {
  1698. el.style.textDecoration = (el.style.textDecoration!="underline"?"underline":"none");
  1699. } else if (cmd == "strikeThrough") {
  1700. el.style.textDecoration = (el.style.textDecoration!="line-through"?"line-through":"none");
  1701. } else if (cmd == "forecolor") {
  1702. el.style.color = arg1;
  1703. }
  1704. }
  1705. } while (el && (el = el.nextSibling));
  1706. if (a.description!=dom.innerHTML) {
  1707. a.description = dom.innerHTML;
  1708. console.log("new DOM:",dom.innerHTML);
  1709. this.update_board_artifact_viewmodel(a);
  1710. this.queue_artifact_for_save(a);
  1711. if (this.editing_artifact_id) {
  1712. var a = this.find_artifact_by_id(this.editing_artifact_id);
  1713. var medium = this.medium_for_object[a._id];
  1714. if (medium && a) {
  1715. medium.value(a.description);
  1716. }
  1717. }
  1718. }
  1719. }
  1720. } else if (this.editing_artifact_id) {
  1721. // text level selection
  1722. var a = this.find_artifact_by_id(this.editing_artifact_id);
  1723. var medium = this.medium_for_object[a._id];
  1724. if (medium && a) {
  1725. medium.focus();
  1726. medium.element.focus();
  1727. medium.invokeElement(cmd);
  1728. a.description = medium.value();
  1729. this.queue_artifact_for_save(a);
  1730. }
  1731. }
  1732. this.extract_text_format_from_selection();
  1733. },
  1734. remove_link_from_selected_artifacts: function() {
  1735. this.update_selected_artifacts(function(a) {
  1736. return {link_uri: ""};
  1737. });
  1738. },
  1739. create_link_on_selected_artifacts: function() {
  1740. var def = "";
  1741. var sa = this.selected_artifacts();
  1742. if (sa.length>=1) {
  1743. if (sa[0].meta && sa[0].meta.link_uri) {
  1744. def = sa[0].meta.link_uri;
  1745. }
  1746. }
  1747. var insert_link_url = prompt("URL:",def);
  1748. this.update_selected_artifacts(function(a) {
  1749. var update = {link_uri: insert_link_url};
  1750. if (a.payload_uri && a.payload_uri.match("webgrabber")) {
  1751. var enc_uri = encodeURIComponent(btoa(insert_link_url));
  1752. var thumb_uri = ENV.apiEndpoint + "/api/webgrabber/"+enc_uri;
  1753. update.payload_uri = thumb_uri;
  1754. update.payload_thumbnail_web_uri = thumb_uri;
  1755. update.payload_thumbnail_medium_uri = thumb_uri;
  1756. update.payload_thumbnail_big_uri = thumb_uri;
  1757. }
  1758. return update;
  1759. });
  1760. this.opened_dialog = "none";
  1761. },
  1762. clone_artifact: function(a,dx,dy,on_success) {
  1763. var copy = _.cloneDeep(a);
  1764. delete copy["$index"];
  1765. delete copy["_id"];
  1766. if (dx) copy.x += dx;
  1767. if (dy) copy.y += dy;
  1768. copy.order = this.active_space_artifacts.length+1;
  1769. if (this.guest_nickname) {
  1770. copy.editor_name = this.guest_nickname;
  1771. }
  1772. copy.space_id = this.active_space._id;
  1773. save_artifact(copy, function(saved) {
  1774. this.update_board_artifact_viewmodel(saved);
  1775. this.active_space_artifacts.push(saved);
  1776. if (on_success) {
  1777. on_success(saved);
  1778. } else {
  1779. this.select(null,saved);
  1780. }
  1781. }.bind(this));
  1782. return copy;
  1783. },
  1784. toggle_lock_of_selected_artifacts: function() {
  1785. this.update_selected_artifacts(function(a) {
  1786. return {locked: !a.locked};
  1787. }, true);
  1788. },
  1789. duplicate_selected_artifacts: function() {
  1790. var arts = this.selected_artifacts();
  1791. for (var i=0; i<arts.length; i++) {
  1792. var a = arts[i];
  1793. var cloned = this.clone_artifact(a,50,50);
  1794. }
  1795. },
  1796. copy_selected_artifacts_to_clipboard: function() {
  1797. if ('ontouchstart' in window) return; // don't do this on touch devices
  1798. $("#space-clipboard").focus();
  1799. $("#space-clipboard").select();
  1800. },
  1801. handle_section_click: function(evt) {
  1802. if (evt.target == evt.currentTarget) {
  1803. this.deselect();
  1804. }
  1805. },
  1806. handle_space_doubleclick: function(evt) {
  1807. if (this.selected_artifacts().length) return;
  1808. if (this.guest_nickname) return;
  1809. if (this.active_space && this.active_space.access_mode == "public") return;
  1810. //this.add_artifact(this.active_space, "text", null, evt);
  1811. },
  1812. handle_body_click: function(evt) {
  1813. if (this.active_view == "space") {
  1814. this.handle_section_click(evt);
  1815. }
  1816. this.close_dropdown(evt);
  1817. },
  1818. extract_text_format_from_selection: function() {
  1819. if (!window.selection) return;
  1820. var parents = $(window.selection.baseNode).parents().toArray();
  1821. this.detected_text_formats = {};
  1822. var formats = {
  1823. "p":"Paragraph",
  1824. "h1":"Headline 1",
  1825. "h2":"Headline 2",
  1826. "h3":"Headline 3",
  1827. "h4":"Headline 4",
  1828. "h5":"Headline 5",
  1829. "h6":"Headline 6",
  1830. "ul":"Bullet List",
  1831. "ol":"Numbered List",
  1832. "blockquote":"Blockquote"
  1833. };
  1834. for (var i=0; i<parents.length; i++) {
  1835. var p = parents[i];
  1836. if (p.contentEditable=="true") break;
  1837. var nn = p.nodeName.toLowerCase();
  1838. if (formats[nn]) {
  1839. this.detected_text_formats[nn] = true;
  1840. this.active_text_format_name = formats[nn];
  1841. }
  1842. }
  1843. },
  1844. // called on artifact keyup
  1845. save_edited_artifact_text: function(evt) {
  1846. // FIXME
  1847. if (this.editing_artifact_id) {
  1848. //var scribe = _scribe_handle_for_object[this.editing_artifact_id];
  1849. var artifact = this.find_artifact_by_id(this.editing_artifact_id);
  1850. if (artifact) {
  1851. //artifact.description = scribe.getHTML();
  1852. this.queue_artifact_for_save(artifact);
  1853. }
  1854. this.extract_text_format_from_selection();
  1855. }
  1856. },
  1857. handle_section_paste: function(evt) {
  1858. if (this.editing_artifact_id) return;
  1859. var pastedText = null;
  1860. try {
  1861. pastedText = evt.clipboardData.getData('text/plain');
  1862. } catch (e) {
  1863. }
  1864. if (!pastedText) return;
  1865. if (!pastedText.match(/<[a-zA-Z]+>/g)) {
  1866. // crappy heuristic if this is actually HTML
  1867. pastedText = pastedText.replace(/\n/g,"<br>");
  1868. }
  1869. this.insert_embedded_artifact(pastedText);
  1870. },
  1871. insert_embedded_artifact: function(text) {
  1872. var space = this.active_space;
  1873. if (!space) return;
  1874. if (text[0]=='[' || text[0]=='{') {
  1875. // might be json
  1876. try {
  1877. parsed = JSON.parse(text);
  1878. if (text[0]=='{') parsed = [parsed];
  1879. this.deselect();
  1880. for (var i=0; i<parsed.length; i++) {
  1881. if (parsed[i].mime) {
  1882. var z = this.highest_z()+1;
  1883. if (parsed.length==1) {
  1884. var w = parsed[i].w;
  1885. var h = parsed[i].h;
  1886. var point = this.find_place_for_item(w,h);
  1887. parsed[i].x = point.x;
  1888. parsed[i].y = point.y;
  1889. parsed[i].z = point.z;
  1890. } else {
  1891. parsed[i].x = parsed[i].x+50;
  1892. parsed[i].y = parsed[i].y+50;
  1893. parsed[i].y = parsed[i].z+z;
  1894. }
  1895. this.clone_artifact(parsed[i], 0,0, function(a) {
  1896. this.multi_select([a]);
  1897. }.bind(this));
  1898. }
  1899. }
  1900. return;
  1901. } catch(e) {
  1902. // not json
  1903. }
  1904. }
  1905. if (text.match(/^http[s]*\:\/\//)) {
  1906. // crude url matching
  1907. this.create_artifact_via_embed_url(text);
  1908. return;
  1909. }
  1910. var new_item = {
  1911. mime: "text/html",
  1912. description: text.replace("\n", "<br />"),
  1913. title: "",
  1914. space_id: space._id
  1915. };
  1916. var w = 400;
  1917. var h = 300;
  1918. var point = this.find_place_for_item(w,h);
  1919. new_item.x = point.x;
  1920. new_item.y = point.y;
  1921. new_item.w = w;
  1922. new_item.h = h;
  1923. new_item.z = point.z;
  1924. if (this.guest_nickname) {
  1925. new_item.editor_name = this.guest_nickname;
  1926. }
  1927. save_artifact(new_item, function(saved_item) {
  1928. this.update_board_artifact_viewmodel(saved_item);
  1929. this.active_space_artifacts.push(saved_item);
  1930. }.bind(this));
  1931. },
  1932. create_artifact_via_embed_url: function(url) {
  1933. this.close_modal();
  1934. var point = this.find_place_for_item(200,200);
  1935. var z = this.highest_z()+1;
  1936. var a = {
  1937. space_id: this.active_space._id,
  1938. mime: "image/png",
  1939. description: url,
  1940. state: "uploading",
  1941. x: point.x,
  1942. y: point.y,
  1943. w: 200,
  1944. h: 200,
  1945. z: z,
  1946. order: this.active_space_artifacts.length
  1947. }
  1948. var metadata = parse_link(url)
  1949. if (!metadata) {
  1950. return;
  1951. }
  1952. if (metadata.type == "unknown") {
  1953. var enc_uri = encodeURIComponent(btoa(url));
  1954. a.meta = {
  1955. link_uri: url
  1956. }
  1957. if (this.guest_nickname) {
  1958. a.editor_name = this.guest_nickname;
  1959. }
  1960. // step 1: create placeholder
  1961. save_artifact(a, function(saved_a) {
  1962. this.update_board_artifact_viewmodel(saved_a);
  1963. this.active_space_artifacts.push(saved_a);
  1964. var thumb_uri = ENV.apiEndpoint + "/api/webgrabber/"+enc_uri;
  1965. // step 2: push payload_uri for processing
  1966. saved_a.state = "idle";
  1967. saved_a.payload_uri = thumb_uri;
  1968. saved_a.payload_thumbnail_web_uri = thumb_uri;
  1969. saved_a.payload_thumbnail_medium_uri = thumb_uri;
  1970. saved_a.payload_thumbnail_big_uri = thumb_uri;
  1971. save_artifact(saved_a, function(saved_a2) {
  1972. this.update_board_artifact_viewmodel(saved_a);
  1973. }.bind(this));
  1974. }.bind(this));
  1975. return;
  1976. }
  1977. var w = metadata.thumbnail_width || 200;
  1978. var h = metadata.thumbnail_height || 200;
  1979. if (w<200) w = 200;
  1980. if (h<200) h = 200;
  1981. if (metadata.provider_name == "soundcloud") {
  1982. w = 500;
  1983. h = 150;
  1984. }
  1985. a = _.extend(a, {
  1986. mime: "oembed/"+metadata.type+"-"+metadata.provider_name,
  1987. description: metadata.url || url,
  1988. //payload_uri: metadata.url || url,
  1989. payload_thumbnail_medium_uri: metadata.thumbnail_url,
  1990. payload_thumbnail_web_uri: metadata.thumbnail_url,
  1991. state: "idle",
  1992. title: metadata.title,
  1993. link_uri: metadata.url || url,
  1994. x: point.x - w/2,
  1995. y: point.y - h/2,
  1996. w: w,
  1997. h: h
  1998. });
  1999. if (this.guest_nickname) {
  2000. a.editor_name = this.guest_nickname;
  2001. }
  2002. save_artifact(a, function(saved_a) {
  2003. this.update_board_artifact_viewmodel(saved_a);
  2004. this.active_space_artifacts.push(saved_a);
  2005. }.bind(this));
  2006. },
  2007. create_artifact_via_payload_url: function(type, url) {
  2008. this.add_artifact(this.active_space, type, url, null);
  2009. },
  2010. handle_touch_select_background_image: function() {
  2011. $('#background-uploader').click();
  2012. },
  2013. handle_insert_image_url: function(url) {
  2014. if (!url || !url.length) {
  2015. $("#image_file_upload").click(); // redirect to file upload
  2016. return;
  2017. }
  2018. this.create_artifact_via_payload_url("image", url);
  2019. this.insert_image_url = "";
  2020. // this.opened_dialog = "none";
  2021. },
  2022. handle_insert_video_url: function(url) {
  2023. if (!url.length) {
  2024. $("#video_file_upload").click(); // redirect to file upload
  2025. return;
  2026. }
  2027. var object = parse_link(url);
  2028. if (object) {
  2029. this.create_artifact_via_embed_url(url);
  2030. } else {
  2031. this.create_artifact_via_payload_url("video", url);
  2032. }
  2033. this.insert_video_url = "";
  2034. //this.opened_dialog = "none";
  2035. },
  2036. handle_insert_audio_url: function(url) {
  2037. if (!url.length) {
  2038. $("#audio_file_upload").click(); // redirect to file upload
  2039. return;
  2040. }
  2041. var object = parse_link(url);
  2042. if (object) {
  2043. this.create_artifact_via_embed_url(url);
  2044. } else {
  2045. this.create_artifact_via_payload_url("audio", url);
  2046. }
  2047. this.insert_audio_url = "";
  2048. },
  2049. handle_generic_file_upload: function(evt) {
  2050. var files = evt.target.files;
  2051. this.opened_dialog = "none";
  2052. if (files && files.length) {
  2053. console.log("file: ",files[0]);
  2054. for (var i=0; i<files.length; i++) {
  2055. var file = files[i];
  2056. if (file.type === "application/pdf") {
  2057. var point = {x: 100, y: 100}; //fixme, center upload?
  2058. this.dropped_point = point;
  2059. this.pending_pdf_file = file;
  2060. this.activate_modal('pdfoptions');
  2061. } else {
  2062. this.create_artifact_via_upload(null, file, true);
  2063. }
  2064. }
  2065. }
  2066. },
  2067. handle_image_file_upload: function(evt) {
  2068. this.handle_generic_file_upload(evt);
  2069. },
  2070. handle_video_file_upload: function(evt) {
  2071. this.handle_generic_file_upload(evt);
  2072. },
  2073. handle_audio_file_upload: function(evt) {
  2074. this.handle_generic_file_upload(evt);
  2075. },
  2076. handle_section_background_upload: function(evt) {
  2077. var f = evt.target.files[0];
  2078. this.space_background_uploading = true;
  2079. save_space_background_file(this.active_space, f, function(space) {
  2080. this.active_space = space;
  2081. this.space_background_uploading = false;
  2082. }.bind(this));
  2083. },
  2084. remove_section_background: function() {
  2085. this.active_space.background_uri = null;
  2086. save_space(this.active_space);
  2087. },
  2088. show_toolbar_props: function() {
  2089. if (this.selection_metrics.count==0) return;
  2090. arts = this.selected_artifacts();
  2091. for (var i=0;i<arts.length; i++) {
  2092. if (arts[i].mime=="x-spacedeck/zone") return;
  2093. }
  2094. this.toolbar_props_in = true;
  2095. },
  2096. hide_toolbar_props: function() {
  2097. this.toolbar_props_in = false;
  2098. },
  2099. show_toolbar_artifacts: function(x,y) {
  2100. this.toolbar_artifacts_x = (x-175)+"px";
  2101. this.toolbar_artifacts_y = y+"px";
  2102. this.toolbar_artifacts_in = true;
  2103. },
  2104. hide_toolbar_artifacts: function() {
  2105. this.toolbar_artifacts_in = false;
  2106. },
  2107. start_adding_artifact: function(evt) {
  2108. evt = fixup_touches(evt);
  2109. // toggle
  2110. if (this.toolbar_artifacts_in) {
  2111. this.hide_toolbar_artifacts();
  2112. return;
  2113. }
  2114. this.show_toolbar_artifacts(evt.pageX,evt.pageY);
  2115. },
  2116. start_drawing_scribble: function(evt) {
  2117. this.hide_toolbar_artifacts();
  2118. this.active_tool = "scribble";
  2119. this.opened_dialog = "none";
  2120. },
  2121. start_drawing_arrow: function(evt) {
  2122. this.hide_toolbar_artifacts();
  2123. this.active_tool = "arrow";
  2124. this.opened_dialog = "none";
  2125. },
  2126. start_drawing_line: function(evt) {
  2127. this.hide_toolbar_artifacts();
  2128. this.active_tool = "line";
  2129. this.opened_dialog = "none";
  2130. },
  2131. adjust_bounds_zoom: function() {
  2132. if (!this.active_space) return;
  2133. this.bounds_zoom = this.viewport_zoom;
  2134. var eff_w = this.active_space.width*this.viewport_zoom;
  2135. var eff_h = this.active_space.height*this.viewport_zoom;
  2136. if (window.innerWidth>eff_w) {
  2137. // horizontal centering
  2138. this.bounds_margin_horiz = (window.innerWidth-eff_w)/2;
  2139. } else {
  2140. this.bounds_margin_horiz = 0;
  2141. }
  2142. if (window.innerHeight-80>eff_h) {
  2143. // horizontal centering
  2144. this.bounds_margin_vert = (window.innerHeight-eff_h)/2-80;
  2145. } else {
  2146. this.bounds_margin_vert = 0;
  2147. }
  2148. },
  2149. zoom_to_original: function() {
  2150. var old_zoom = this.viewport_zoom;
  2151. this.viewport_zoom = 1;
  2152. this.viewport_zoom_percent = parseInt(this.viewport_zoom*100);
  2153. this.adjust_bounds_zoom();
  2154. this.zoom_adjust_scroll(this.viewport_zoom/old_zoom);
  2155. },
  2156. zoom_to_fit: function() {
  2157. var er = this.enclosing_rect(this.active_space_artifacts);
  2158. if (!er) return;
  2159. var pad = 200;
  2160. er.x1-=pad;
  2161. er.y1-=pad-100;
  2162. er.x2+=pad;
  2163. er.y2+=pad+100;
  2164. this.zoom_to_rect(er, 1);
  2165. },
  2166. zoom_to_zone: function(z) {
  2167. if (!$("#space").length) return;
  2168. var er = this.enclosing_rect([z]);
  2169. var el = $("#space")[0];
  2170. var cur_r = {
  2171. x1: el.scrollLeft/this.viewport_zoom,
  2172. y1: el.scrollTop/this.viewport_zoom,
  2173. x2: (el.scrollLeft+window.innerWidth)/this.viewport_zoom,
  2174. y2: (el.scrollTop+window.innerHeight)/this.viewport_zoom
  2175. };
  2176. var pad = 10;
  2177. er.x1-=pad;
  2178. er.y1-=pad;
  2179. er.x2+=pad;
  2180. er.y2+=pad;
  2181. if (!this.animation_running) {
  2182. this.animation_running = true;
  2183. this.animate_zoom_to_rect(er, 200, cur_r);
  2184. this.current_zone_idx = this.zones.indexOf(z);
  2185. }
  2186. },
  2187. zoom_to_rect: function(er, max_zoom) {
  2188. if (!$("#space").length) return;
  2189. var el = $("#space")[0];
  2190. var w = er.x2-er.x1;
  2191. var h = er.y2-er.y1;
  2192. if (w>h) {
  2193. this.viewport_zoom = window.innerWidth/w;
  2194. if (window.innerHeight < h*this.viewport_zoom) {
  2195. this.viewport_zoom = window.innerHeight/h;
  2196. }
  2197. } else {
  2198. this.viewport_zoom = window.innerHeight/h;
  2199. if (window.innerWidth < w*this.viewport_zoom) {
  2200. this.viewport_zoom = window.innerWidth/w;
  2201. }
  2202. }
  2203. if (max_zoom) {
  2204. if (this.viewport_zoom>max_zoom) this.viewport_zoom = max_zoom;
  2205. }
  2206. if (this.viewport_zoom<0.05) this.viewport_zoom = 0.05;
  2207. this.viewport_zoom_percent = parseInt(this.viewport_zoom*100);
  2208. this.adjust_bounds_zoom();
  2209. if (!el) return;
  2210. var animate = function() {
  2211. el.scrollTop = (er.y1+(h/2))*this.viewport_zoom-window.innerHeight/2;
  2212. el.scrollLeft = (er.x1+(w/2))*this.viewport_zoom-window.innerWidth/2;
  2213. this.handle_scroll();
  2214. }.bind(this);
  2215. if ("requestAnimationFrame" in window) {
  2216. window.requestAnimationFrame(animate);
  2217. } else {
  2218. animate();
  2219. }
  2220. },
  2221. animate_zoom_to_rect: function(target_r, duration, cur_r, elapsed) {
  2222. if (!$("#space").length) return;
  2223. var el = $("#space")[0];
  2224. var anim_res = 20;
  2225. if (!elapsed) elapsed = 0;
  2226. if (duration>elapsed) {
  2227. window.setTimeout(function() {
  2228. this.animate_zoom_to_rect(target_r, duration, cur_r, elapsed+anim_res);
  2229. }.bind(this), anim_res);
  2230. /*var dx = (el.scrollLeft-ncx)/anim_res;
  2231. var dy = (el.scrollLeft-ncy)/anim_res;
  2232. el.scrollLeft += dx;
  2233. el.scrollTop += dy;*/
  2234. // interpolate
  2235. var dx1 = ((target_r.x1-cur_r.x1)/duration)*elapsed;
  2236. var dx2 = ((target_r.x2-cur_r.x2)/duration)*elapsed;
  2237. var dy1 = ((target_r.y1-cur_r.y1)/duration)*elapsed;
  2238. var dy2 = ((target_r.y2-cur_r.y2)/duration)*elapsed;
  2239. var step_r = {
  2240. x1: cur_r.x1+dx1,
  2241. x2: cur_r.x2+dx2,
  2242. y1: cur_r.y1+dy1,
  2243. y2: cur_r.y2+dy2
  2244. };
  2245. /*console.log("cur_r: ",cur_r);
  2246. console.log("target_r: ",target_r);
  2247. console.log("step_r: ",step_r);*/
  2248. this.zoom_to_rect(step_r);
  2249. } else {
  2250. // done
  2251. this.zoom_to_rect(target_r);
  2252. this.animation_running = false;
  2253. }
  2254. },
  2255. zoom_to_point: function(p,amount) {
  2256. var el = $("#space")[0];
  2257. var sx = el.scrollLeft/this.viewport_zoom;
  2258. var sy = el.scrollTop/this.viewport_zoom;
  2259. var ww = window.innerWidth/(this.viewport_zoom);
  2260. var wh = window.innerHeight/(this.viewport_zoom);
  2261. var oxx = (p.x-(sx+ww/2))*amount;
  2262. var oyy = (p.y-(sy+wh/2))*amount;
  2263. var ox = -oxx;
  2264. var oy = -oyy;
  2265. var r = {
  2266. x1: p.x-(ww/2)*amount + ox,
  2267. y1: p.y-(wh/2)*amount + oy,
  2268. x2: p.x+(ww/2)*amount + ox,
  2269. y2: p.y+(wh/2)*amount + oy
  2270. };
  2271. this.zoom_to_rect(r,2);
  2272. },
  2273. throttled_zoom_to_point: _.throttle(function(p,a){
  2274. this.zoom_to_point(p,a);
  2275. }, 50),
  2276. zoom_to_cursor: function(evt,amount) {
  2277. var point = this.cursor_point_to_space(evt);
  2278. this.throttled_zoom_to_point.bind(this)(point,amount);
  2279. },
  2280. zoom_adjust_scroll: function(f) {
  2281. var adjust_scroll = function() {
  2282. if (!$("#space").length) return;
  2283. if (!this.active_space || !this.active_space_loaded) return;
  2284. var el = $("#space")[0];
  2285. var eff_w = this.active_space.width*this.viewport_zoom;
  2286. var eff_h = this.active_space.height*this.viewport_zoom;
  2287. var sx = el.scrollLeft;
  2288. var sy = el.scrollTop;
  2289. var cx = window.innerWidth/2;
  2290. var cy = window.innerHeight/2;
  2291. var ncx = f*(sx+cx)-cx;
  2292. var ncy = f*(sy+cy)-cy;
  2293. if (eff_w<window.innerWidth) ncx = 0;
  2294. if (eff_h<window.innerHeight) ncy = 0;
  2295. el.scrollLeft = ncx;
  2296. el.scrollTop = ncy;
  2297. this.handle_scroll();
  2298. };
  2299. if ("requestAnimationFrame" in window) {
  2300. window.requestAnimationFrame(adjust_scroll.bind(this));
  2301. } else {
  2302. adjust_scroll();
  2303. }
  2304. },
  2305. zoom_in: function() {
  2306. if (!this.viewport_zoom) this.viewport_zoom = 1;
  2307. var old_zoom = this.viewport_zoom;
  2308. this.viewport_zoom *= 1.5;
  2309. if (this.viewport_zoom>=2) {
  2310. this.viewport_zoom = 2;
  2311. }
  2312. this.viewport_zoom_percent = parseInt(this.viewport_zoom*100);
  2313. this.adjust_bounds_zoom();
  2314. this.zoom_adjust_scroll(this.viewport_zoom/old_zoom);
  2315. },
  2316. zoom_out: function() {
  2317. if (!this.viewport_zoom) this.viewport_zoom = 1;
  2318. var old_zoom = this.viewport_zoom;
  2319. this.viewport_zoom /= 1.5;
  2320. if (this.viewport_zoom<0.05) {
  2321. this.viewport_zoom = 0.05;
  2322. }
  2323. this.viewport_zoom_percent = parseInt(this.viewport_zoom*100);
  2324. this.adjust_bounds_zoom();
  2325. this.zoom_adjust_scroll(this.viewport_zoom/old_zoom);
  2326. },
  2327. activate_pan_tool: function(evt) {
  2328. if (evt) {
  2329. evt.stopPropagation();
  2330. evt.preventDefault();
  2331. }
  2332. this.active_tool = "pan";
  2333. if (this.stop_pan_timeout) {
  2334. window.clearTimeout(this.stop_pan_timeout);
  2335. }
  2336. // pan exit hack
  2337. this.stop_pan_timeout = window.setTimeout(function () {
  2338. if (this.active_tool == "pan") {
  2339. this.active_tool = "pointer";
  2340. }
  2341. }.bind(this),500);
  2342. },
  2343. approve_pdf_upload: function(evt,approve_pdf_upload, mode){
  2344. this.close_modal();
  2345. if(mode == "classic"){
  2346. this.create_artifact_via_upload(evt, this.pending_pdf_file, false);
  2347. }
  2348. if(mode == "grid") {
  2349. this.global_spinner = true;
  2350. save_pdf_file(this.active_space, this.dropped_point, this.pending_pdf_file, approve_pdf_upload, function(createdArtifacts){
  2351. this.global_spinner = false;
  2352. _.each(createdArtifacts, function(new_artifact){
  2353. this.update_board_artifact_viewmodel(new_artifact);
  2354. this.active_space_artifacts.push(new_artifact)
  2355. }.bind(this));
  2356. }.bind(this), function(xhr) {
  2357. this.global_spinner = false;
  2358. alert("Error PDF ("+xhr.status+")");
  2359. }.bind(this));
  2360. }
  2361. },
  2362. handle_data_drop: function(evt) {
  2363. if (this.active_space_role=="viewer") {
  2364. return false;
  2365. }
  2366. var json = evt.dataTransfer.getData('application/json');
  2367. var dest_section = this.active_space;
  2368. var files = evt.dataTransfer.files;
  2369. if (files && files.length) {
  2370. for (var i=0; i<files.length; i++) {
  2371. var file = files[i];
  2372. if (file.type === "application/pdf") {
  2373. var point = this.cursor_point_to_space(evt);
  2374. this.dropped_point = point;
  2375. this.pending_pdf_file = file;
  2376. this.activate_modal('pdfoptions');
  2377. } else {
  2378. this.create_artifact_via_upload(evt, file, (files.length>1));
  2379. }
  2380. }
  2381. } else {
  2382. var json = evt.dataTransfer.getData('application/json');
  2383. if (json) {
  2384. var parsed = JSON.parse(json);
  2385. delete parsed._id;
  2386. parsed.space_id = this.active_space._id;
  2387. var w = 300;
  2388. var h = 200;
  2389. if (parsed.board && parsed.w && parsed.h) {
  2390. w = parsed.w;
  2391. h = parsed.h;
  2392. }
  2393. var point = this.cursor_point_to_space(evt);
  2394. point.x-=w/2;
  2395. point.y-=h/2;
  2396. parsed.board = {
  2397. x: point.x,
  2398. y: point.y,
  2399. w: w,
  2400. h: h,
  2401. z: 20
  2402. };
  2403. if (this.guest_nickname) {
  2404. parsed.editor_name = this.guest_nickname;
  2405. }
  2406. save_artifact(parsed, function(saved_a) {
  2407. this.update_board_artifact_viewmodel(saved_a);
  2408. this.active_space_artifacts.push(saved_a);
  2409. }.bind(this));
  2410. return;
  2411. }
  2412. var html = evt.dataTransfer.getData('text/html');
  2413. if (html) {
  2414. var rx = /src="([^"]+)"/g;
  2415. var m = rx.exec(html);
  2416. if (m) {
  2417. this.add_artifact(this.active_space, "image", m[1], evt);
  2418. }
  2419. }
  2420. }
  2421. },
  2422. clear_search_results: function() {
  2423. this.image_search_results = [];
  2424. this.audio_search_results = [];
  2425. this.video_search_results = [];
  2426. },
  2427. download_selected_artifacts: function() {
  2428. var arts = this.selected_artifacts();
  2429. if (arts.length!=1) return;
  2430. if (arts[0].payload_uri) {
  2431. try {
  2432. window.open(arts[0].payload_uri);
  2433. } catch (e) {
  2434. // could not open window
  2435. }
  2436. }
  2437. }
  2438. }
  2439. }